forked from nushell/nushell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
umv.rs
631 lines (536 loc) · 18.2 KB
/
umv.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
use nu_test_support::fs::{files_exist_at, Stub::EmptyFile, Stub::FileWithContent};
use nu_test_support::nu;
use nu_test_support::playground::Playground;
use rstest::rstest;
#[test]
fn moves_a_file() {
Playground::setup("umv_test_1", |dirs, sandbox| {
sandbox
.with_files(vec![EmptyFile("andres.txt")])
.mkdir("expected");
let original = dirs.test().join("andres.txt");
let expected = dirs.test().join("expected/yehuda.txt");
nu!(
cwd: dirs.test(),
"umv andres.txt expected/yehuda.txt"
);
assert!(!original.exists());
assert!(expected.exists());
})
}
#[test]
fn overwrites_if_moving_to_existing_file_and_force_provided() {
Playground::setup("umv_test_2", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("andres.txt"), EmptyFile("jttxt")]);
let original = dirs.test().join("andres.txt");
let expected = dirs.test().join("jttxt");
nu!(
cwd: dirs.test(),
"umv andres.txt -f jttxt"
);
assert!(!original.exists());
assert!(expected.exists());
})
}
#[test]
fn moves_a_directory() {
Playground::setup("umv_test_3", |dirs, sandbox| {
sandbox.mkdir("empty_dir");
let original_dir = dirs.test().join("empty_dir");
let expected = dirs.test().join("renamed_dir");
nu!(
cwd: dirs.test(),
"umv empty_dir renamed_dir"
);
assert!(!original_dir.exists());
assert!(expected.exists());
})
}
#[test]
fn moves_the_file_inside_directory_if_path_to_move_is_existing_directory() {
Playground::setup("umv_test_4", |dirs, sandbox| {
sandbox
.with_files(vec![EmptyFile("jttxt")])
.mkdir("expected");
let original_dir = dirs.test().join("jttxt");
let expected = dirs.test().join("expected/jttxt");
nu!(
cwd: dirs.test(),
"umv jttxt expected"
);
assert!(!original_dir.exists());
assert!(expected.exists());
})
}
#[test]
fn moves_the_directory_inside_directory_if_path_to_move_is_existing_directory() {
Playground::setup("umv_test_5", |dirs, sandbox| {
sandbox
.within("contributors")
.with_files(vec![EmptyFile("jttxt")])
.mkdir("expected");
let original_dir = dirs.test().join("contributors");
let expected = dirs.test().join("expected/contributors");
nu!(
cwd: dirs.test(),
"umv contributors expected"
);
assert!(!original_dir.exists());
assert!(expected.exists());
assert!(files_exist_at(vec!["jttxt"], expected))
})
}
#[test]
fn moves_using_path_with_wildcard() {
Playground::setup("umv_test_7", |dirs, sandbox| {
sandbox
.within("originals")
.with_files(vec![
EmptyFile("andres.ini"),
EmptyFile("caco3_plastics.csv"),
EmptyFile("cargo_sample.toml"),
EmptyFile("jt.ini"),
EmptyFile("jt.xml"),
EmptyFile("sgml_description.json"),
EmptyFile("sample.ini"),
EmptyFile("utf16.ini"),
EmptyFile("yehuda.ini"),
])
.mkdir("work_dir")
.mkdir("expected");
let work_dir = dirs.test().join("work_dir");
let expected = dirs.test().join("expected");
nu!(cwd: work_dir, "umv ../originals/*.ini ../expected");
assert!(files_exist_at(
vec!["yehuda.ini", "jt.ini", "sample.ini", "andres.ini",],
expected
));
})
}
#[test]
fn moves_using_a_glob() {
Playground::setup("umv_test_8", |dirs, sandbox| {
sandbox
.within("meals")
.with_files(vec![
EmptyFile("arepa.txt"),
EmptyFile("empanada.txt"),
EmptyFile("taquiza.txt"),
])
.mkdir("work_dir")
.mkdir("expected");
let meal_dir = dirs.test().join("meals");
let work_dir = dirs.test().join("work_dir");
let expected = dirs.test().join("expected");
nu!(cwd: work_dir, "umv ../meals/* ../expected");
assert!(meal_dir.exists());
assert!(files_exist_at(
vec!["arepa.txt", "empanada.txt", "taquiza.txt",],
expected
));
})
}
#[test]
fn moves_a_directory_with_files() {
Playground::setup("umv_test_9", |dirs, sandbox| {
sandbox
.mkdir("vehicles/car")
.mkdir("vehicles/bicycle")
.with_files(vec![
EmptyFile("vehicles/car/car1.txt"),
EmptyFile("vehicles/car/car2.txt"),
])
.with_files(vec![
EmptyFile("vehicles/bicycle/bicycle1.txt"),
EmptyFile("vehicles/bicycle/bicycle2.txt"),
]);
let original_dir = dirs.test().join("vehicles");
let expected_dir = dirs.test().join("expected");
nu!(
cwd: dirs.test(),
"umv vehicles expected"
);
assert!(!original_dir.exists());
assert!(expected_dir.exists());
assert!(files_exist_at(
vec![
"car/car1.txt",
"car/car2.txt",
"bicycle/bicycle1.txt",
"bicycle/bicycle2.txt"
],
expected_dir
));
})
}
#[test]
fn errors_if_source_doesnt_exist() {
Playground::setup("umv_test_10", |dirs, sandbox| {
sandbox.mkdir("test_folder");
let actual = nu!(
cwd: dirs.test(),
"umv non-existing-file test_folder/"
);
assert!(actual.err.contains("Directory not found"));
})
}
#[test]
#[ignore = "GNU/uutils overwrites rather than error out"]
fn error_if_moving_to_existing_file_without_force() {
Playground::setup("umv_test_10_0", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("andres.txt"), EmptyFile("jttxt")]);
let actual = nu!(
cwd: dirs.test(),
"umv andres.txt jttxt"
);
assert!(actual.err.contains("file already exists"))
})
}
#[test]
fn errors_if_destination_doesnt_exist() {
Playground::setup("umv_test_10_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("empty.txt")]);
let actual = nu!(
cwd: dirs.test(),
"umv empty.txt does/not/exist/"
);
assert!(actual.err.contains("failed to access"));
})
}
#[test]
#[ignore = "GNU/uutils doesnt expand, rather cannot stat 'file?.txt'"]
fn errors_if_multiple_sources_but_destination_not_a_directory() {
Playground::setup("umv_test_10_2", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("file1.txt"),
EmptyFile("file2.txt"),
EmptyFile("file3.txt"),
]);
let actual = nu!(
cwd: dirs.test(),
"umv file?.txt not_a_dir"
);
assert!(actual
.err
.contains("Can only move multiple sources if destination is a directory"));
})
}
#[test]
fn errors_if_renaming_directory_to_an_existing_file() {
Playground::setup("umv_test_10_3", |dirs, sandbox| {
sandbox
.mkdir("mydir")
.with_files(vec![EmptyFile("empty.txt")]);
let actual = nu!(
cwd: dirs.test(),
"umv mydir empty.txt"
);
assert!(actual.err.contains("cannot overwrite non-directory"),);
assert!(actual.err.contains("with directory"),);
})
}
#[test]
fn errors_if_moving_to_itself() {
Playground::setup("umv_test_10_4", |dirs, sandbox| {
sandbox.mkdir("mydir").mkdir("mydir/mydir_2");
let actual = nu!(
cwd: dirs.test(),
"umv mydir mydir/mydir_2/"
);
assert!(actual.err.contains("cannot move"));
assert!(actual.err.contains("to a subdirectory"));
});
}
#[test]
fn does_not_error_on_relative_parent_path() {
Playground::setup("umv_test_11", |dirs, sandbox| {
sandbox
.mkdir("first")
.with_files(vec![EmptyFile("first/william_hartnell.txt")]);
let original = dirs.test().join("first/william_hartnell.txt");
let expected = dirs.test().join("william_hartnell.txt");
nu!(
cwd: dirs.test().join("first"),
"umv william_hartnell.txt ./.."
);
assert!(!original.exists());
assert!(expected.exists());
})
}
#[test]
fn move_files_using_glob_two_parents_up_using_multiple_dots() {
Playground::setup("umv_test_12", |dirs, sandbox| {
sandbox.within("foo").within("bar").with_files(vec![
EmptyFile("jtjson"),
EmptyFile("andres.xml"),
EmptyFile("yehuda.yaml"),
EmptyFile("kevin.txt"),
EmptyFile("many_more.ppl"),
]);
nu!(
cwd: dirs.test().join("foo/bar"),
r#"
umv * ...
"#
);
let files = vec![
"yehuda.yaml",
"jtjson",
"andres.xml",
"kevin.txt",
"many_more.ppl",
];
let original_dir = dirs.test().join("foo/bar");
let destination_dir = dirs.test();
assert!(files_exist_at(files.clone(), destination_dir));
assert!(!files_exist_at(files, original_dir))
})
}
#[test]
fn move_file_from_two_parents_up_using_multiple_dots_to_current_dir() {
Playground::setup("cp_test_10", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("hello_there")]);
sandbox.within("foo").mkdir("bar");
nu!(
cwd: dirs.test().join("foo/bar"),
r#"
umv .../hello_there .
"#
);
let expected = dirs.test().join("foo/bar/hello_there");
let original = dirs.test().join("hello_there");
assert!(expected.exists());
assert!(!original.exists());
})
}
#[test]
fn does_not_error_when_some_file_is_moving_into_itself() {
Playground::setup("umv_test_13", |dirs, sandbox| {
sandbox.mkdir("11").mkdir("12");
let original_dir = dirs.test().join("11");
let expected = dirs.test().join("12/11");
nu!(cwd: dirs.test(), "umv 1* 12");
assert!(!original_dir.exists());
assert!(expected.exists());
})
}
#[test]
fn mv_ignores_ansi() {
Playground::setup("umv_test_ansi", |_dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("test.txt")]);
let actual = nu!(
cwd: sandbox.cwd(),
r#"
ls | find test | umv $in.0.name success.txt; ls | $in.0.name
"#
);
assert_eq!(actual.out, "success.txt");
})
}
#[test]
fn mv_directory_with_same_name() {
Playground::setup("umv_test_directory_with_same_name", |_dirs, sandbox| {
sandbox.mkdir("testdir");
sandbox.mkdir("testdir/testdir");
let cwd = sandbox.cwd().join("testdir");
let actual = nu!(
cwd: cwd,
r#"
umv testdir ..
"#
);
assert!(actual.err.contains("Directory not empty"));
})
}
#[test]
// Test that changing the case of a file/directory name works;
// this is an important edge case on Windows (and any other case-insensitive file systems).
// We were bitten badly by this once: https://github.com/nushell/nushell/issues/6583
// Currently as we are using `uutils` and have no say in the behavior, this should succeed on Linux,
// but fail on both macOS and Windows.
fn mv_change_case_of_directory() {
Playground::setup("mv_change_case_of_directory", |dirs, sandbox| {
sandbox
.mkdir("somedir")
.with_files(vec![EmptyFile("somedir/somefile.txt")]);
let original_dir = String::from("somedir");
let new_dir = String::from("SomeDir");
let _actual = nu!(
cwd: dirs.test(),
format!("umv {original_dir} {new_dir}")
);
// Doing this instead of `Path::exists()` because we need to check file existence in
// a case-sensitive way. `Path::exists()` is understandably case-insensitive on NTFS
let _files_in_test_directory: Vec<String> = std::fs::read_dir(dirs.test())
.unwrap()
.map(|de| de.unwrap().file_name().to_string_lossy().into_owned())
.collect();
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
assert!(
!_files_in_test_directory.contains(&original_dir)
&& _files_in_test_directory.contains(&new_dir)
);
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
assert!(files_exist_at(
vec!["somefile.txt",],
dirs.test().join(new_dir)
));
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
_actual.err.contains("to a subdirectory of itself");
})
}
#[test]
// Currently as we are using `uutils` and have no say in the behavior, this should succeed on Linux,
// but fail on both macOS and Windows.
fn mv_change_case_of_file() {
Playground::setup("mv_change_case_of_file", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("somefile.txt")]);
let original_file_name = String::from("somefile.txt");
let new_file_name = String::from("SomeFile.txt");
let _actual = nu!(
cwd: dirs.test(),
format!("umv {original_file_name} -f {new_file_name}")
);
// Doing this instead of `Path::exists()` because we need to check file existence in
// a case-sensitive way. `Path::exists()` is understandably case-insensitive on NTFS
let _files_in_test_directory: Vec<String> = std::fs::read_dir(dirs.test())
.unwrap()
.map(|de| de.unwrap().file_name().to_string_lossy().into_owned())
.collect();
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
assert!(
!_files_in_test_directory.contains(&original_file_name)
&& _files_in_test_directory.contains(&new_file_name)
);
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
_actual.err.contains("are the same file");
})
}
#[test]
#[ignore = "Update not supported..remove later"]
fn mv_with_update_flag() {
Playground::setup("mv_with_update_flag", |_dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("valid.txt"),
FileWithContent("newer_valid.txt", "body"),
]);
let actual = nu!(
cwd: sandbox.cwd(),
"umv -uf valid.txt newer_valid.txt; open newer_valid.txt",
);
assert_eq!(actual.out, "body");
// create a file after assert to make sure that newest_valid.txt is newest
std::thread::sleep(std::time::Duration::from_secs(1));
sandbox.with_files(vec![FileWithContent("newest_valid.txt", "newest_body")]);
let actual = nu!(cwd: sandbox.cwd(), "umv -uf newest_valid.txt valid.txt; open valid.txt");
assert_eq!(actual.out, "newest_body");
// when destination doesn't exist
sandbox.with_files(vec![FileWithContent("newest_valid.txt", "newest_body")]);
let actual = nu!(cwd: sandbox.cwd(), "umv -uf newest_valid.txt des_missing.txt; open des_missing.txt");
assert_eq!(actual.out, "newest_body");
});
}
#[test]
fn test_mv_no_clobber() {
Playground::setup("umv_test_13", |dirs, sandbox| {
let file_a = "test_mv_no_clobber_file_a";
let file_b = "test_mv_no_clobber_file_b";
sandbox.with_files(vec![EmptyFile(file_a)]);
sandbox.with_files(vec![EmptyFile(file_b)]);
let actual = nu!(
cwd: dirs.test(),
"umv -n {} {}",
file_a,
file_b,
);
assert!(actual.err.contains("not replacing"));
})
}
#[test]
fn mv_with_no_arguments() {
Playground::setup("umv_test_14", |dirs, _| {
let actual = nu!(
cwd: dirs.test(),
"umv",
);
assert!(actual.err.contains("Missing file operand"));
})
}
#[test]
fn mv_with_no_target() {
Playground::setup("umv_test_15", |dirs, _| {
let actual = nu!(
cwd: dirs.test(),
"umv a",
);
assert!(actual.err.contains(
format!(
"Missing destination path operand after {}",
dirs.test().join("a").display()
)
.as_str()
));
})
}
#[rstest]
#[case("a]c")]
#[case("a[c")]
#[case("a[bc]d")]
#[case("a][c")]
fn mv_files_with_glob_metachars(#[case] src_name: &str) {
Playground::setup("umv_test_16", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
src_name,
"What is the sound of one hand clapping?",
)]);
let src = dirs.test().join(src_name);
let actual = nu!(
cwd: dirs.test(),
"umv '{}' {}",
src.display(),
"hello_world_dest"
);
assert!(actual.err.is_empty());
assert!(dirs.test().join("hello_world_dest").exists());
});
}
#[cfg(not(windows))]
#[rstest]
#[case("a]?c")]
#[case("a*.?c")]
// windows doesn't allow filename with `*`.
fn mv_files_with_glob_metachars_nw(#[case] src_name: &str) {
mv_files_with_glob_metachars(src_name);
}
#[test]
fn mv_with_cd() {
Playground::setup("umv_test_17", |_dirs, sandbox| {
sandbox
.mkdir("tmp_dir")
.with_files(vec![FileWithContent("tmp_dir/file.txt", "body")]);
let actual = nu!(
cwd: sandbox.cwd(),
r#"do { cd tmp_dir; let f = 'file.txt'; umv $f .. }; open file.txt"#,
);
assert!(actual.out.contains("body"));
});
}
#[test]
fn test_cp_inside_glob_metachars_dir() {
Playground::setup("open_files_inside_glob_metachars_dir", |dirs, sandbox| {
let sub_dir = "test[]";
sandbox
.within(sub_dir)
.with_files(vec![FileWithContent("test_file.txt", "hello")]);
let actual = nu!(
cwd: dirs.test().join(sub_dir),
"mv test_file.txt ../",
);
assert!(actual.err.is_empty());
assert!(!files_exist_at(
vec!["test_file.txt"],
dirs.test().join(sub_dir)
));
assert!(files_exist_at(vec!["test_file.txt"], dirs.test()));
});
}