Skip to content

Commit dac398b

Browse files
committed
test: migrate clean to snapbox
1 parent e11d172 commit dac398b

File tree

1 file changed

+122
-95
lines changed

1 file changed

+122
-95
lines changed

tests/testsuite/clean.rs

Lines changed: 122 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Tests for the `cargo clean` command.
22
3-
#![allow(deprecated)]
4-
53
use cargo_test_support::paths::CargoPathExt;
4+
use cargo_test_support::prelude::*;
65
use cargo_test_support::registry::Package;
6+
use cargo_test_support::str;
77
use cargo_test_support::{
88
basic_bin_manifest, basic_manifest, git, main_file, project, project_in, rustc_host,
99
};
@@ -38,7 +38,10 @@ fn different_dir() {
3838

3939
p.cargo("clean")
4040
.cwd("src")
41-
.with_stderr("[REMOVED] [..]")
41+
.with_stderr_data(str![[r#"
42+
[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total
43+
44+
"#]])
4245
.run();
4346
assert!(!p.build_dir().is_dir());
4447
}
@@ -88,7 +91,10 @@ fn clean_multiple_packages() {
8891

8992
p.cargo("clean -p d1 -p d2")
9093
.cwd("src")
91-
.with_stderr("[REMOVED] [..]")
94+
.with_stderr_data(str![[r#"
95+
[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total
96+
97+
"#]])
9298
.run();
9399
assert!(p.bin("foo").is_file());
94100
assert!(!d1_path.is_file());
@@ -235,17 +241,19 @@ fn clean_release() {
235241

236242
p.cargo("clean -p foo").run();
237243
p.cargo("build --release")
238-
.with_stderr("[FINISHED] [..]")
244+
.with_stderr_data(str![[r#"
245+
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
246+
247+
"#]])
239248
.run();
240249

241250
p.cargo("clean -p foo --release").run();
242251
p.cargo("build --release")
243-
.with_stderr(
244-
"\
245-
[COMPILING] foo v0.0.1 ([..])
246-
[FINISHED] `release` profile [optimized] target(s) in [..]
247-
",
248-
)
252+
.with_stderr_data(str![[r#"
253+
[COMPILING] foo v0.0.1 ([ROOT]/foo)
254+
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
255+
256+
"#]])
249257
.run();
250258

251259
p.cargo("build").run();
@@ -283,7 +291,12 @@ fn clean_doc() {
283291

284292
assert!(doc_path.is_dir());
285293

286-
p.cargo("clean --doc").with_stderr("[REMOVED] [..]").run();
294+
p.cargo("clean --doc")
295+
.with_stderr_data(str![[r#"
296+
[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total
297+
298+
"#]])
299+
.run();
287300

288301
assert!(!doc_path.is_dir());
289302
assert!(p.build_dir().is_dir());
@@ -326,15 +339,14 @@ fn build_script() {
326339
p.cargo("build").env("FIRST", "1").run();
327340
p.cargo("clean -p foo").run();
328341
p.cargo("build -v")
329-
.with_stderr(
330-
"\
331-
[COMPILING] foo v0.0.1 ([..])
342+
.with_stderr_data(str![[r#"
343+
[COMPILING] foo v0.0.1 ([ROOT]/foo)
332344
[RUNNING] `rustc [..] build.rs [..]`
333-
[RUNNING] `[..]build-script-build`
345+
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build`
334346
[RUNNING] `rustc [..] src/main.rs [..]`
335-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]
336-
",
337-
)
347+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
348+
349+
"#]])
338350
.run();
339351
}
340352

@@ -367,7 +379,12 @@ fn clean_git() {
367379
.build();
368380

369381
p.cargo("build").run();
370-
p.cargo("clean -p dep").with_stderr("[REMOVED] [..]").run();
382+
p.cargo("clean -p dep")
383+
.with_stderr_data(str![[r#"
384+
[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total
385+
386+
"#]])
387+
.run();
371388
p.cargo("build").run();
372389
}
373390

@@ -393,7 +410,12 @@ fn registry() {
393410
Package::new("bar", "0.1.0").publish();
394411

395412
p.cargo("build").run();
396-
p.cargo("clean -p bar").with_stderr("[REMOVED] [..]").run();
413+
p.cargo("clean -p bar")
414+
.with_stderr_data(str![[r#"
415+
[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total
416+
417+
"#]])
418+
.run();
397419
p.cargo("build").run();
398420
}
399421

@@ -420,21 +442,21 @@ fn clean_verbose() {
420442
p.cargo("build").run();
421443
let mut expected = String::from(
422444
"\
423-
[REMOVING] [..]target/debug/.fingerprint/bar[..]
424-
[REMOVING] [..]target/debug/deps/libbar[..].rlib
425-
[REMOVING] [..]target/debug/deps/bar-[..].d
426-
[REMOVING] [..]target/debug/deps/libbar[..].rmeta
445+
[REMOVING] [ROOT]/foo/target/debug/.fingerprint/bar-[HASH]
446+
[REMOVING] [ROOT]/foo/target/debug/deps/libbar-[HASH].rlib
447+
[REMOVING] [ROOT]/foo/target/debug/deps/bar-[HASH].d
448+
[REMOVING] [ROOT]/foo/target/debug/deps/libbar-[HASH].rmeta
427449
",
428450
);
429451
if cfg!(target_os = "macos") {
430452
// Rust 1.69 has changed so that split-debuginfo=unpacked includes unpacked for rlibs.
431-
for obj in p.glob("target/debug/deps/bar-*.o") {
432-
expected.push_str(&format!("[REMOVING] [..]{}\n", obj.unwrap().display()));
453+
for _ in p.glob("target/debug/deps/bar-*.o") {
454+
expected.push_str("[REMOVING] [ROOT]/foo/target/debug/deps/bar-[HASH][..].o");
433455
}
434456
}
435-
expected.push_str("[REMOVED] [..] files, [..] total\n");
457+
expected.push_str("[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total\n");
436458
p.cargo("clean -p bar --verbose")
437-
.with_stderr_unordered(&expected)
459+
.with_stderr_data(&expected.unordered())
438460
.run();
439461
p.cargo("build").run();
440462
}
@@ -614,21 +636,20 @@ fn clean_spec_version() {
614636
// Check suggestion for bad pkgid.
615637
p.cargo("clean -p baz")
616638
.with_status(101)
617-
.with_stderr(
618-
"\
619-
error: package ID specification `baz` did not match any packages
639+
.with_stderr_data(str![[r#"
640+
[ERROR] package ID specification `baz` did not match any packages
620641
621-
<tab>Did you mean `bar`?
622-
",
623-
)
642+
Did you mean `bar`?
643+
644+
"#]])
624645
.run();
625646

626647
p.cargo("clean -p bar:0.1.0")
627-
.with_stderr(
628-
"warning: version qualifier in `-p bar:0.1.0` is ignored, \
629-
cleaning all versions of `bar` found\n\
630-
[REMOVED] [..] files, [..] total",
631-
)
648+
.with_stderr_data(str![[r#"
649+
[WARNING] version qualifier in `-p bar:0.1.0` is ignored, cleaning all versions of `bar` found
650+
[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total
651+
652+
"#]])
632653
.run();
633654
let mut walker = walkdir::WalkDir::new(p.build_dir())
634655
.into_iter()
@@ -670,21 +691,20 @@ fn clean_spec_partial_version() {
670691
// Check suggestion for bad pkgid.
671692
p.cargo("clean -p baz")
672693
.with_status(101)
673-
.with_stderr(
674-
"\
675-
error: package ID specification `baz` did not match any packages
694+
.with_stderr_data(str![[r#"
695+
[ERROR] package ID specification `baz` did not match any packages
676696
677-
<tab>Did you mean `bar`?
678-
",
679-
)
697+
Did you mean `bar`?
698+
699+
"#]])
680700
.run();
681701

682702
p.cargo("clean -p bar:0.1")
683-
.with_stderr(
684-
"warning: version qualifier in `-p bar:0.1` is ignored, \
685-
cleaning all versions of `bar` found\n\
686-
[REMOVED] [..] files, [..] total",
687-
)
703+
.with_stderr_data(str![[r#"
704+
[WARNING] version qualifier in `-p bar:0.1` is ignored, cleaning all versions of `bar` found
705+
[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total
706+
707+
"#]])
688708
.run();
689709
let mut walker = walkdir::WalkDir::new(p.build_dir())
690710
.into_iter()
@@ -726,21 +746,20 @@ fn clean_spec_partial_version_ambiguous() {
726746
// Check suggestion for bad pkgid.
727747
p.cargo("clean -p baz")
728748
.with_status(101)
729-
.with_stderr(
730-
"\
731-
error: package ID specification `baz` did not match any packages
749+
.with_stderr_data(str![[r#"
750+
[ERROR] package ID specification `baz` did not match any packages
732751
733-
<tab>Did you mean `bar`?
734-
",
735-
)
752+
Did you mean `bar`?
753+
754+
"#]])
736755
.run();
737756

738757
p.cargo("clean -p bar:0")
739-
.with_stderr(
740-
"warning: version qualifier in `-p bar:0` is ignored, \
741-
cleaning all versions of `bar` found\n\
742-
[REMOVED] [..] files, [..] total",
743-
)
758+
.with_stderr_data(str![[r#"
759+
[WARNING] version qualifier in `-p bar:0` is ignored, cleaning all versions of `bar` found
760+
[REMOVED] [FILE_NUM] files, [FILE_SIZE]B total
761+
762+
"#]])
744763
.run();
745764
let mut walker = walkdir::WalkDir::new(p.build_dir())
746765
.into_iter()
@@ -794,16 +813,15 @@ fn clean_spec_reserved() {
794813

795814
// This should not rebuild bar.
796815
p.cargo("build -v --all-targets")
797-
.with_stderr(
798-
"\
816+
.with_stderr_data(str![[r#"
799817
[FRESH] bar v1.0.0
800-
[COMPILING] foo v0.1.0 [..]
801-
[RUNNING] `rustc [..]
802-
[RUNNING] `rustc [..]
803-
[RUNNING] `rustc [..]
804-
[FINISHED] [..]
805-
",
806-
)
818+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
819+
[RUNNING] `rustc [..]`
820+
[RUNNING] `rustc [..]`
821+
[RUNNING] `rustc [..]`
822+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
823+
824+
"#]])
807825
.run();
808826
}
809827

@@ -829,32 +847,39 @@ fn clean_dry_run() {
829847

830848
// Start with no files.
831849
p.cargo("clean --dry-run")
832-
.with_stdout("")
833-
.with_stderr(
834-
"[SUMMARY] 0 files\n\
835-
[WARNING] no files deleted due to --dry-run",
836-
)
850+
.with_stdout_data("")
851+
.with_stderr_data(str![[r#"
852+
[SUMMARY] [FILE_NUM] files
853+
[WARNING] no files deleted due to --dry-run
854+
855+
"#]])
837856
.run();
838857
p.cargo("check").run();
839858
let before = p.build_dir().ls_r();
840859
p.cargo("clean --dry-run")
841-
.with_stderr(
842-
"[SUMMARY] [..] files, [..] total\n\
843-
[WARNING] no files deleted due to --dry-run",
844-
)
860+
.with_stderr_data(str![[r#"
861+
[SUMMARY] [FILE_NUM] files, [FILE_SIZE]B total
862+
[WARNING] no files deleted due to --dry-run
863+
864+
"#]])
845865
.run();
846866
// Verify it didn't delete anything.
847867
let after = p.build_dir().ls_r();
848868
assert_eq!(before, after);
849-
let expected = itertools::join(before.iter().map(|p| p.to_str().unwrap()), "\n");
869+
let path_stringify = |p: &PathBuf| format!("{}\n", p.to_str().unwrap());
870+
let files = itertools::join(before.iter().map(path_stringify), "");
871+
let re = regex::Regex::new("/(?<head>[a-z0-9\\-_]+)-([0-9a-f]{16})(?<tail>.*)").unwrap();
872+
let expected = re.replace_all(&files, "/$head-[HASH]$tail");
873+
let expected = expected.replace(p.build_dir().to_str().unwrap(), "[ROOT]/foo/target");
850874
eprintln!("{expected}");
851875
// Verify the verbose output.
852876
p.cargo("clean --dry-run -v")
853-
.with_stdout_unordered(expected)
854-
.with_stderr(
855-
"[SUMMARY] [..] files, [..] total\n\
856-
[WARNING] no files deleted due to --dry-run",
857-
)
877+
.with_stdout_data(expected.unordered())
878+
.with_stderr_data(str![[r#"
879+
[SUMMARY] [FILE_NUM] files, [FILE_SIZE]B total
880+
[WARNING] no files deleted due to --dry-run
881+
882+
"#]])
858883
.run();
859884
}
860885

@@ -864,7 +889,10 @@ fn doc_with_package_selection() {
864889
let p = project().file("src/lib.rs", "").build();
865890
p.cargo("clean --doc -p foo")
866891
.with_status(101)
867-
.with_stderr("error: --doc cannot be used with -p")
892+
.with_stderr_data(str![[r#"
893+
[ERROR] --doc cannot be used with -p
894+
895+
"#]])
868896
.run();
869897
}
870898

@@ -879,17 +907,16 @@ fn quiet_does_not_show_summary() {
879907

880908
p.cargo("check").run();
881909
p.cargo("clean --quiet --dry-run")
882-
.with_stdout("")
883-
.with_stderr("")
910+
.with_stdout_data("")
911+
.with_stderr_data("")
884912
.run();
885913
// Verify exact same command without -q would actually display something.
886914
p.cargo("clean --dry-run")
887-
.with_stdout("")
888-
.with_stderr(
889-
"\
890-
[SUMMARY] [..] files, [..] total
915+
.with_stdout_data("")
916+
.with_stderr_data(str![[r#"
917+
[SUMMARY] [FILE_NUM] files, [FILE_SIZE]B total
891918
[WARNING] no files deleted due to --dry-run
892-
",
893-
)
919+
920+
"#]])
894921
.run();
895922
}

0 commit comments

Comments
 (0)