Skip to content

Commit

Permalink
test: replace create with create_new to avoid potentially nulling exi…
Browse files Browse the repository at this point in the history
…sting files
  • Loading branch information
eugenesvk committed Dec 4, 2024
1 parent 9ed83e7 commit 9c213c9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/freedesktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ mod tests {
let names: Vec<OsString> = (0..files_per_batch).map(|i| format!("{}#{}", file_name_prefix, i).into()).collect();
for _ in 0..batches {
for path in &names {
File::create(path).unwrap();
File::create_new(path).unwrap();
}
// eprintln!("Deleting {:?}", names);
let result = delete_all_using_system_program(&names);
Expand Down Expand Up @@ -946,7 +946,7 @@ mod tests {
let symlink_names: Vec<OsString> = names.iter().map(|name| format!("{:?}-symlink", name).into()).collect();

// Test file symbolic link and directory symbolic link
File::create(&names[0]).unwrap();
File::create_new(&names[0]).unwrap();
std::fs::create_dir(&names[1]).unwrap();

for (i, (name, symlink)) in names.iter().zip(&symlink_names).enumerate() {
Expand Down
2 changes: 1 addition & 1 deletion src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ mod tests {
trash_ctx.set_delete_method(DeleteMethod::NsFileManager);

let path = get_unique_name();
File::create(&path).unwrap();
File::create_new(&path).unwrap();
trash_ctx.delete(&path).unwrap();
assert!(File::open(&path).is_err());
}
Expand Down
16 changes: 8 additions & 8 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mod os_limited {
let names: Vec<OsString> = (0..files_per_batch).map(|i| format!("{}#{}", file_name_prefix, i).into()).collect();
for _ in 0..batches {
for path in names.iter() {
File::create(path).unwrap();
File::create_new(path).unwrap();
}
trash::delete_all(&names).unwrap();
}
Expand Down Expand Up @@ -104,7 +104,7 @@ mod os_limited {
let mut name = OsStr::new(&get_unique_name()).to_os_string().into_encoded_bytes();
name.push(168);
let name = OsString::from_vec(name);
File::create(&name).unwrap();
File::create_new(&name).unwrap();

// Delete, list, and remove file with an invalid UTF8 name
// Listing items is already exhaustively checked above, so this test is mainly concerned
Expand Down Expand Up @@ -136,7 +136,7 @@ mod os_limited {
let names: Vec<_> = (0..files_per_batch).map(|i| format!("{}#{}", file_name_prefix, i)).collect();
for _ in 0..batches {
for path in names.iter() {
File::create(path).unwrap();
File::create_new(path).unwrap();
}
trash::delete_all(&names).unwrap();
}
Expand Down Expand Up @@ -165,7 +165,7 @@ mod os_limited {
let file_count: usize = 3;
let names: Vec<_> = (0..file_count).map(|i| format!("{}#{}", file_name_prefix, i)).collect();
for path in names.iter() {
File::create(path).unwrap();
File::create_new(path).unwrap();
}
trash::delete_all(&names).unwrap();

Expand Down Expand Up @@ -207,11 +207,11 @@ mod os_limited {
let collision_remaining = file_count - 1;
let names: Vec<_> = (0..file_count).map(|i| format!("{}#{}", file_name_prefix, i)).collect();
for path in names.iter() {
File::create(path).unwrap();
File::create_new(path).unwrap();
}
trash::delete_all(&names).unwrap();
for path in names.iter().skip(file_count - collision_remaining) {
File::create(path).unwrap();
File::create_new(path).unwrap();
}
let mut targets: Vec<_> = trash::os_limited::list()
.unwrap()
Expand Down Expand Up @@ -264,12 +264,12 @@ mod os_limited {
let file_count: usize = 4;
let names: Vec<_> = (0..file_count).map(|i| format!("{}#{}", file_name_prefix, i)).collect();
for path in names.iter() {
File::create(path).unwrap();
File::create_new(path).unwrap();
}
trash::delete_all(&names).unwrap();

let twin_name = &names[1];
File::create(twin_name).unwrap();
File::create_new(twin_name).unwrap();
trash::delete(twin_name).unwrap();

let mut targets: Vec<_> = trash::os_limited::list()
Expand Down
20 changes: 10 additions & 10 deletions tests/trash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn test_delete_file() {
trace!("Started test_delete_file");

let path = get_unique_name();
File::create(&path).unwrap();
File::create_new(&path).unwrap();

delete(&path).unwrap();
assert!(File::open(&path).is_err());
Expand All @@ -49,7 +49,7 @@ fn test_delete_folder() {

let path = PathBuf::from(get_unique_name());
create_dir(&path).unwrap();
File::create(path.join("file_in_folder")).unwrap();
File::create_new(path.join("file_in_folder")).unwrap();

assert!(path.exists());
delete(&path).unwrap();
Expand All @@ -66,7 +66,7 @@ fn test_delete_all() {

let paths: Vec<_> = (0..count).map(|i| format!("test_file_to_delete_{i}")).collect();
for path in paths.iter() {
File::create(path).unwrap();
File::create_new(path).unwrap();
}

delete_all(&paths).unwrap();
Expand Down Expand Up @@ -94,7 +94,7 @@ mod unix {
init_logging();
trace!("Started test_delete_symlink");
let target_path = get_unique_name();
File::create(&target_path).unwrap();
File::create_new(&target_path).unwrap();

let link_path = "test_link_to_delete";
symlink(&target_path, link_path).unwrap();
Expand All @@ -112,7 +112,7 @@ mod unix {
init_logging();
trace!("Started test_delete_symlink_in_folder");
let target_path = "test_link_target_for_delete_from_folder";
File::create(target_path).unwrap();
File::create_new(target_path).unwrap();

let folder = Path::new("test_parent_folder_for_link_to_delete");
create_dir(folder).unwrap();
Expand All @@ -134,7 +134,7 @@ mod unix {
fn create_remove_single_file() {
// Let's create and remove a single file
let name = get_unique_name();
File::create(&name).unwrap();
File::create_new(&name).unwrap();
trash::delete(&name).unwrap();
assert!(File::open(&name).is_err());
}
Expand All @@ -144,7 +144,7 @@ fn create_remove_single_file() {
#[serial]
fn create_remove_single_file_invalid_utf8() {
let name = unsafe { OsStr::from_encoded_bytes_unchecked(&[168]) };
File::create(name).unwrap();
File::create_new(name).unwrap();
trash::delete(name).unwrap();
}

Expand All @@ -155,8 +155,8 @@ fn recursive_file_deletion() {
let dir2 = parent_dir.join("dir2");
std::fs::create_dir_all(&dir1).unwrap();
std::fs::create_dir_all(&dir2).unwrap();
File::create(dir1.join("same-name")).unwrap();
File::create(dir2.join("same-name")).unwrap();
File::create_new(dir1.join("same-name")).unwrap();
File::create_new(dir2.join("same-name")).unwrap();

trash::delete(parent_dir).unwrap();
assert!(!parent_dir.exists());
Expand All @@ -169,7 +169,7 @@ fn recursive_file_with_content_deletion() {
let dir2 = parent_dir.join("dir2");
std::fs::create_dir_all(&dir1).unwrap();
std::fs::create_dir_all(&dir2).unwrap();
File::create(dir1.join("same-name")).unwrap();
File::create_new(dir1.join("same-name")).unwrap();
std::fs::write(dir2.join("same-name"), b"some content").unwrap();

trash::delete(parent_dir).unwrap();
Expand Down

0 comments on commit 9c213c9

Please sign in to comment.