Skip to content

Commit e2bb094

Browse files
committed
Win: Add test cases for renaming a directory while the target file is opened and for renaming over a non-empty directory
1 parent 1e414f1 commit e2bb094

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

library/std/src/fs/tests.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,3 +1766,44 @@ fn test_hidden_file_truncation() {
17661766
let metadata = file.metadata().unwrap();
17671767
assert_eq!(metadata.len(), 0);
17681768
}
1769+
1770+
#[cfg(windows)]
1771+
#[test]
1772+
fn test_rename_file_over_open_file() {
1773+
// Make sure that std::fs::rename works if the target file is already opened with FILE_SHARE_DELETE. See #123985.
1774+
let tmpdir = tmpdir();
1775+
1776+
// Create source with test data to read.
1777+
let source_path = tmpdir.join("source_file.txt");
1778+
fs::write(&source_path, b"source hello world").unwrap();
1779+
1780+
// Create target file with test data to read;
1781+
let target_path = tmpdir.join("target_file.txt");
1782+
fs::write(&target_path, b"target hello world").unwrap();
1783+
1784+
// Open target file
1785+
let target_file = fs::File::open(&target_path).unwrap();
1786+
1787+
// Rename source
1788+
fs::rename(source_path, &target_path).unwrap();
1789+
1790+
core::mem::drop(target_file);
1791+
assert_eq!(fs::read(target_path).unwrap(), b"source hello world");
1792+
}
1793+
1794+
#[test]
1795+
#[cfg(windows)]
1796+
fn test_rename_directory_to_non_empty_directory() {
1797+
// Renaming a directory over a non-empty existing directory should fail on Windows.
1798+
let tmpdir: TempDir = tmpdir();
1799+
1800+
let source_path = tmpdir.join("source_directory");
1801+
let target_path = tmpdir.join("target_directory");
1802+
1803+
fs::create_dir(&source_path).unwrap();
1804+
fs::create_dir(&target_path).unwrap();
1805+
1806+
fs::write(target_path.join("target_file.txt"), b"target hello world").unwrap();
1807+
1808+
error!(fs::rename(source_path, target_path), 145); // ERROR_DIR_NOT_EMPTY
1809+
}

0 commit comments

Comments
 (0)