Skip to content

Commit 823f6fb

Browse files
authored
Merge pull request #126 from eugenesvk/fr-test-file-new
Fail in tests on truncating pre-existing user files
2 parents 1a0fc59 + 243b00d commit 823f6fb

File tree

8 files changed

+35
-35
lines changed

8 files changed

+35
-35
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ use trash;
2626

2727
fn main() {
2828
// Let's create and remove a single file
29-
File::create("remove-me").unwrap();
29+
File::create_new("remove-me").unwrap();
3030
trash::delete("remove-me").unwrap();
3131
assert!(File::open("remove-me").is_err());
3232

3333
// Now let's remove multiple files at once
3434
let the_others = ["remove-me-too", "dont-forget-about-me-either"];
3535
for name in the_others.iter() {
36-
File::create(name).unwrap();
36+
File::create_new(name).unwrap();
3737
}
3838
trash::delete_all(&the_others).unwrap();
3939
for name in the_others.iter() {

examples/delete_method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn main() {
1717
trash_ctx.set_delete_method(DeleteMethod::NsFileManager);
1818

1919
let path = "this_file_was_deleted_using_the_ns_file_manager";
20-
File::create(path).unwrap();
20+
File::create_new(path).unwrap();
2121
trash_ctx.delete(path).unwrap();
2222
assert!(File::open(path).is_err());
2323
}

examples/hello.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use std::fs::File;
22

33
fn main() {
44
// Let's create and remove a single file
5-
File::create("remove-me").unwrap();
5+
File::create_new("remove-me").unwrap();
66
trash::delete("remove-me").unwrap();
77
assert!(File::open("remove-me").is_err());
88

99
// Now let's remove multiple files at once
1010
let the_others = ["remove-me-too", "dont-forget-about-me-either"];
1111
for name in the_others.iter() {
12-
File::create(name).unwrap();
12+
File::create_new(name).unwrap();
1313
}
1414
trash::delete_all(&the_others).unwrap();
1515
for name in the_others.iter() {

src/freedesktop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ mod tests {
896896
let names: Vec<OsString> = (0..files_per_batch).map(|i| format!("{}#{}", file_name_prefix, i).into()).collect();
897897
for _ in 0..batches {
898898
for path in &names {
899-
File::create(path).unwrap();
899+
File::create_new(path).unwrap();
900900
}
901901
// eprintln!("Deleting {:?}", names);
902902
let result = delete_all_using_system_program(&names);
@@ -946,7 +946,7 @@ mod tests {
946946
let symlink_names: Vec<OsString> = names.iter().map(|name| format!("{:?}-symlink", name).into()).collect();
947947

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

952952
for (i, (name, symlink)) in names.iter().zip(&symlink_names).enumerate() {

src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl TrashContext {
7777
/// ```
7878
/// use std::fs::File;
7979
/// use trash::delete;
80-
/// File::create("delete_me").unwrap();
80+
/// File::create_new("delete_me").unwrap();
8181
/// trash::delete("delete_me").unwrap();
8282
/// assert!(File::open("delete_me").is_err());
8383
/// ```
@@ -95,8 +95,8 @@ impl TrashContext {
9595
/// ```
9696
/// use std::fs::File;
9797
/// use trash::delete_all;
98-
/// File::create("delete_me_1").unwrap();
99-
/// File::create("delete_me_2").unwrap();
98+
/// File::create_new("delete_me_1").unwrap();
99+
/// File::create_new("delete_me_2").unwrap();
100100
/// delete_all(&["delete_me_1", "delete_me_2"]).unwrap();
101101
/// assert!(File::open("delete_me_1").is_err());
102102
/// assert!(File::open("delete_me_2").is_err());
@@ -440,7 +440,7 @@ pub mod os_limited {
440440
/// use trash::{delete, os_limited::{list, purge_all}};
441441
///
442442
/// let filename = "trash-purge_all-example-ownership";
443-
/// File::create(filename).unwrap();
443+
/// File::create_new(filename).unwrap();
444444
/// delete(filename).unwrap();
445445
/// // Collect the filtered list just so that we can make sure there's exactly one element.
446446
/// // There's no need to `collect` it otherwise.
@@ -456,7 +456,7 @@ pub mod os_limited {
456456
/// use trash::{delete, os_limited::{list, purge_all}};
457457
///
458458
/// let filename = "trash-purge_all-example-reference";
459-
/// File::create(filename).unwrap();
459+
/// File::create_new(filename).unwrap();
460460
/// delete(filename).unwrap();
461461
/// let mut selected = list().unwrap();
462462
/// selected.retain(|x| x.name == filename);
@@ -495,7 +495,7 @@ pub mod os_limited {
495495
/// use trash::os_limited::{list, restore_all};
496496
///
497497
/// let filename = "trash-restore_all-example";
498-
/// File::create(filename).unwrap();
498+
/// File::create_new(filename).unwrap();
499499
/// restore_all(list().unwrap().into_iter().filter(|x| x.name == filename)).unwrap();
500500
/// std::fs::remove_file(filename).unwrap();
501501
/// ```
@@ -522,13 +522,13 @@ pub mod os_limited {
522522
{
523523
// Check for twins here cause that's pretty platform independent.
524524
struct ItemWrapper<'a>(&'a TrashItem);
525-
impl<'a> PartialEq for ItemWrapper<'a> {
525+
impl PartialEq for ItemWrapper<'_> {
526526
fn eq(&self, other: &Self) -> bool {
527527
self.0.original_path() == other.0.original_path()
528528
}
529529
}
530-
impl<'a> Eq for ItemWrapper<'a> {}
531-
impl<'a> Hash for ItemWrapper<'a> {
530+
impl Eq for ItemWrapper<'_> {}
531+
impl Hash for ItemWrapper<'_> {
532532
fn hash<H: Hasher>(&self, state: &mut H) {
533533
self.0.original_path().hash(state);
534534
}

src/macos.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ mod tests {
153153
trash_ctx.set_delete_method(DeleteMethod::NsFileManager);
154154

155155
let path = get_unique_name();
156-
File::create(&path).unwrap();
156+
File::create_new(&path).unwrap();
157157
trash_ctx.delete(&path).unwrap();
158158
assert!(File::open(&path).is_err());
159159
}

src/tests.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ mod os_limited {
5252
let names: Vec<OsString> = (0..files_per_batch).map(|i| format!("{}#{}", file_name_prefix, i).into()).collect();
5353
for _ in 0..batches {
5454
for path in names.iter() {
55-
File::create(path).unwrap();
55+
File::create_new(path).unwrap();
5656
}
5757
trash::delete_all(&names).unwrap();
5858
}
@@ -104,7 +104,7 @@ mod os_limited {
104104
let mut name = OsStr::new(&get_unique_name()).to_os_string().into_encoded_bytes();
105105
name.push(168);
106106
let name = OsString::from_vec(name);
107-
File::create(&name).unwrap();
107+
File::create_new(&name).unwrap();
108108

109109
// Delete, list, and remove file with an invalid UTF8 name
110110
// Listing items is already exhaustively checked above, so this test is mainly concerned
@@ -136,7 +136,7 @@ mod os_limited {
136136
let names: Vec<_> = (0..files_per_batch).map(|i| format!("{}#{}", file_name_prefix, i)).collect();
137137
for _ in 0..batches {
138138
for path in names.iter() {
139-
File::create(path).unwrap();
139+
File::create_new(path).unwrap();
140140
}
141141
trash::delete_all(&names).unwrap();
142142
}
@@ -165,7 +165,7 @@ mod os_limited {
165165
let file_count: usize = 3;
166166
let names: Vec<_> = (0..file_count).map(|i| format!("{}#{}", file_name_prefix, i)).collect();
167167
for path in names.iter() {
168-
File::create(path).unwrap();
168+
File::create_new(path).unwrap();
169169
}
170170
trash::delete_all(&names).unwrap();
171171

@@ -207,11 +207,11 @@ mod os_limited {
207207
let collision_remaining = file_count - 1;
208208
let names: Vec<_> = (0..file_count).map(|i| format!("{}#{}", file_name_prefix, i)).collect();
209209
for path in names.iter() {
210-
File::create(path).unwrap();
210+
File::create_new(path).unwrap();
211211
}
212212
trash::delete_all(&names).unwrap();
213213
for path in names.iter().skip(file_count - collision_remaining) {
214-
File::create(path).unwrap();
214+
File::create_new(path).unwrap();
215215
}
216216
let mut targets: Vec<_> = trash::os_limited::list()
217217
.unwrap()
@@ -264,12 +264,12 @@ mod os_limited {
264264
let file_count: usize = 4;
265265
let names: Vec<_> = (0..file_count).map(|i| format!("{}#{}", file_name_prefix, i)).collect();
266266
for path in names.iter() {
267-
File::create(path).unwrap();
267+
File::create_new(path).unwrap();
268268
}
269269
trash::delete_all(&names).unwrap();
270270

271271
let twin_name = &names[1];
272-
File::create(twin_name).unwrap();
272+
File::create_new(twin_name).unwrap();
273273
trash::delete(twin_name).unwrap();
274274

275275
let mut targets: Vec<_> = trash::os_limited::list()

tests/trash.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn test_delete_file() {
3434
trace!("Started test_delete_file");
3535

3636
let path = get_unique_name();
37-
File::create(&path).unwrap();
37+
File::create_new(&path).unwrap();
3838

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

5050
let path = PathBuf::from(get_unique_name());
5151
create_dir(&path).unwrap();
52-
File::create(path.join("file_in_folder")).unwrap();
52+
File::create_new(path.join("file_in_folder")).unwrap();
5353

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

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

7272
delete_all(&paths).unwrap();
@@ -94,7 +94,7 @@ mod unix {
9494
init_logging();
9595
trace!("Started test_delete_symlink");
9696
let target_path = get_unique_name();
97-
File::create(&target_path).unwrap();
97+
File::create_new(&target_path).unwrap();
9898

9999
let link_path = "test_link_to_delete";
100100
symlink(&target_path, link_path).unwrap();
@@ -112,7 +112,7 @@ mod unix {
112112
init_logging();
113113
trace!("Started test_delete_symlink_in_folder");
114114
let target_path = "test_link_target_for_delete_from_folder";
115-
File::create(target_path).unwrap();
115+
File::create_new(target_path).unwrap();
116116

117117
let folder = Path::new("test_parent_folder_for_link_to_delete");
118118
create_dir(folder).unwrap();
@@ -134,7 +134,7 @@ mod unix {
134134
fn create_remove_single_file() {
135135
// Let's create and remove a single file
136136
let name = get_unique_name();
137-
File::create(&name).unwrap();
137+
File::create_new(&name).unwrap();
138138
trash::delete(&name).unwrap();
139139
assert!(File::open(&name).is_err());
140140
}
@@ -144,7 +144,7 @@ fn create_remove_single_file() {
144144
#[serial]
145145
fn create_remove_single_file_invalid_utf8() {
146146
let name = unsafe { OsStr::from_encoded_bytes_unchecked(&[168]) };
147-
File::create(name).unwrap();
147+
File::create_new(name).unwrap();
148148
trash::delete(name).unwrap();
149149
}
150150

@@ -155,8 +155,8 @@ fn recursive_file_deletion() {
155155
let dir2 = parent_dir.join("dir2");
156156
std::fs::create_dir_all(&dir1).unwrap();
157157
std::fs::create_dir_all(&dir2).unwrap();
158-
File::create(dir1.join("same-name")).unwrap();
159-
File::create(dir2.join("same-name")).unwrap();
158+
File::create_new(dir1.join("same-name")).unwrap();
159+
File::create_new(dir2.join("same-name")).unwrap();
160160

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

175175
trash::delete(parent_dir).unwrap();

0 commit comments

Comments
 (0)