Skip to content

Commit b278ca7

Browse files
committed
test: migrate clean to snapbox
1 parent d899ce7 commit b278ca7

File tree

1 file changed

+103
-94
lines changed

1 file changed

+103
-94
lines changed

tests/testsuite/clean.rs

Lines changed: 103 additions & 94 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,7 @@ fn different_dir() {
3838

3939
p.cargo("clean")
4040
.cwd("src")
41-
.with_stderr("[REMOVED] [..]")
41+
.with_stderr_data("[REMOVED] 11 files, [FILE_SIZE]B total\n")
4242
.run();
4343
assert!(!p.build_dir().is_dir());
4444
}
@@ -88,7 +88,7 @@ fn clean_multiple_packages() {
8888

8989
p.cargo("clean -p d1 -p d2")
9090
.cwd("src")
91-
.with_stderr("[REMOVED] [..]")
91+
.with_stderr_data("[REMOVED] 16 files, [FILE_SIZE]B total\n")
9292
.run();
9393
assert!(p.bin("foo").is_file());
9494
assert!(!d1_path.is_file());
@@ -235,17 +235,16 @@ fn clean_release() {
235235

236236
p.cargo("clean -p foo").run();
237237
p.cargo("build --release")
238-
.with_stderr("[FINISHED] [..]")
238+
.with_stderr_data("[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s\n")
239239
.run();
240240

241241
p.cargo("clean -p foo --release").run();
242242
p.cargo("build --release")
243-
.with_stderr(
244-
"\
245-
[COMPILING] foo v0.0.1 ([..])
246-
[FINISHED] `release` profile [optimized] target(s) in [..]
247-
",
248-
)
243+
.with_stderr_data(str![[r#"
244+
[COMPILING] foo v0.0.1 ([ROOT]/foo)
245+
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
246+
247+
"#]])
249248
.run();
250249

251250
p.cargo("build").run();
@@ -283,7 +282,9 @@ fn clean_doc() {
283282

284283
assert!(doc_path.is_dir());
285284

286-
p.cargo("clean --doc").with_stderr("[REMOVED] [..]").run();
285+
p.cargo("clean --doc")
286+
.with_stderr_data("[REMOVED] 47 files, [FILE_SIZE]B total\n")
287+
.run();
287288

288289
assert!(!doc_path.is_dir());
289290
assert!(p.build_dir().is_dir());
@@ -326,15 +327,14 @@ fn build_script() {
326327
p.cargo("build").env("FIRST", "1").run();
327328
p.cargo("clean -p foo").run();
328329
p.cargo("build -v")
329-
.with_stderr(
330-
"\
331-
[COMPILING] foo v0.0.1 ([..])
330+
.with_stderr_data(str![[r#"
331+
[COMPILING] foo v0.0.1 ([ROOT]/foo)
332332
[RUNNING] `rustc [..] build.rs [..]`
333-
[RUNNING] `[..]build-script-build`
333+
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build`
334334
[RUNNING] `rustc [..] src/main.rs [..]`
335-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]
336-
",
337-
)
335+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
336+
337+
"#]])
338338
.run();
339339
}
340340

@@ -367,7 +367,9 @@ fn clean_git() {
367367
.build();
368368

369369
p.cargo("build").run();
370-
p.cargo("clean -p dep").with_stderr("[REMOVED] [..]").run();
370+
p.cargo("clean -p dep")
371+
.with_stderr_data("[REMOVED] 7 files, [FILE_SIZE]B total\n")
372+
.run();
371373
p.cargo("build").run();
372374
}
373375

@@ -393,7 +395,9 @@ fn registry() {
393395
Package::new("bar", "0.1.0").publish();
394396

395397
p.cargo("build").run();
396-
p.cargo("clean -p bar").with_stderr("[REMOVED] [..]").run();
398+
p.cargo("clean -p bar")
399+
.with_stderr_data("[REMOVED] 7 files, [FILE_SIZE]B total\n")
400+
.run();
397401
p.cargo("build").run();
398402
}
399403

@@ -420,21 +424,24 @@ fn clean_verbose() {
420424
p.cargo("build").run();
421425
let mut expected = String::from(
422426
"\
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
427+
[REMOVING] [ROOT]/foo/target/debug/.fingerprint/bar-[HASH]
428+
[REMOVING] [ROOT]/foo/target/debug/deps/libbar-[HASH].rlib
429+
[REMOVING] [ROOT]/foo/target/debug/deps/bar-[HASH].d
430+
[REMOVING] [ROOT]/foo/target/debug/deps/libbar-[HASH].rmeta
427431
",
428432
);
429433
if cfg!(target_os = "macos") {
430434
// Rust 1.69 has changed so that split-debuginfo=unpacked includes unpacked for rlibs.
431435
for obj in p.glob("target/debug/deps/bar-*.o") {
432-
expected.push_str(&format!("[REMOVING] [..]{}\n", obj.unwrap().display()));
436+
expected.push_str(&format!(
437+
"[REMOVING] [ROOT]/foo/{}\n",
438+
obj.unwrap().display()
439+
));
433440
}
434441
}
435-
expected.push_str("[REMOVED] [..] files, [..] total\n");
442+
expected.push_str("[REMOVED] 7 files, [FILE_SIZE]B total\n");
436443
p.cargo("clean -p bar --verbose")
437-
.with_stderr_unordered(&expected)
444+
.with_stderr_data(&expected.unordered())
438445
.run();
439446
p.cargo("build").run();
440447
}
@@ -614,21 +621,20 @@ fn clean_spec_version() {
614621
// Check suggestion for bad pkgid.
615622
p.cargo("clean -p baz")
616623
.with_status(101)
617-
.with_stderr(
618-
"\
619-
error: package ID specification `baz` did not match any packages
624+
.with_stderr_data(str![[r#"
625+
[ERROR] package ID specification `baz` did not match any packages
620626
621-
<tab>Did you mean `bar`?
622-
",
623-
)
627+
Did you mean `bar`?
628+
629+
"#]])
624630
.run();
625631

626632
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-
)
633+
.with_stderr_data(str![[r#"
634+
[WARNING] version qualifier in `-p bar:0.1.0` is ignored, cleaning all versions of `bar` found
635+
[REMOVED] 14 files, [FILE_SIZE]B total
636+
637+
"#]])
632638
.run();
633639
let mut walker = walkdir::WalkDir::new(p.build_dir())
634640
.into_iter()
@@ -670,21 +676,20 @@ fn clean_spec_partial_version() {
670676
// Check suggestion for bad pkgid.
671677
p.cargo("clean -p baz")
672678
.with_status(101)
673-
.with_stderr(
674-
"\
675-
error: package ID specification `baz` did not match any packages
679+
.with_stderr_data(str![[r#"
680+
[ERROR] package ID specification `baz` did not match any packages
676681
677-
<tab>Did you mean `bar`?
678-
",
679-
)
682+
Did you mean `bar`?
683+
684+
"#]])
680685
.run();
681686

682687
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-
)
688+
.with_stderr_data(str![[r#"
689+
[WARNING] version qualifier in `-p bar:0.1` is ignored, cleaning all versions of `bar` found
690+
[REMOVED] 14 files, [FILE_SIZE]B total
691+
692+
"#]])
688693
.run();
689694
let mut walker = walkdir::WalkDir::new(p.build_dir())
690695
.into_iter()
@@ -726,21 +731,20 @@ fn clean_spec_partial_version_ambiguous() {
726731
// Check suggestion for bad pkgid.
727732
p.cargo("clean -p baz")
728733
.with_status(101)
729-
.with_stderr(
730-
"\
731-
error: package ID specification `baz` did not match any packages
734+
.with_stderr_data(str![[r#"
735+
[ERROR] package ID specification `baz` did not match any packages
732736
733-
<tab>Did you mean `bar`?
734-
",
735-
)
737+
Did you mean `bar`?
738+
739+
"#]])
736740
.run();
737741

738742
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-
)
743+
.with_stderr_data(str![[r#"
744+
[WARNING] version qualifier in `-p bar:0` is ignored, cleaning all versions of `bar` found
745+
[REMOVED] 14 files, [FILE_SIZE]B total
746+
747+
"#]])
744748
.run();
745749
let mut walker = walkdir::WalkDir::new(p.build_dir())
746750
.into_iter()
@@ -794,16 +798,15 @@ fn clean_spec_reserved() {
794798

795799
// This should not rebuild bar.
796800
p.cargo("build -v --all-targets")
797-
.with_stderr(
798-
"\
801+
.with_stderr_data(str![[r#"
799802
[FRESH] bar v1.0.0
800-
[COMPILING] foo v0.1.0 [..]
801-
[RUNNING] `rustc [..]
802-
[RUNNING] `rustc [..]
803-
[RUNNING] `rustc [..]
804-
[FINISHED] [..]
805-
",
806-
)
803+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
804+
[RUNNING] `rustc [..]`
805+
[RUNNING] `rustc [..]`
806+
[RUNNING] `rustc [..]`
807+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
808+
809+
"#]])
807810
.run();
808811
}
809812

@@ -829,32 +832,39 @@ fn clean_dry_run() {
829832

830833
// Start with no files.
831834
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-
)
835+
.with_stdout_data("")
836+
.with_stderr_data(str![[r#"
837+
[SUMMARY] 0 files
838+
[WARNING] no files deleted due to --dry-run
839+
840+
"#]])
837841
.run();
838842
p.cargo("check").run();
839843
let before = p.build_dir().ls_r();
840844
p.cargo("clean --dry-run")
841-
.with_stderr(
842-
"[SUMMARY] [..] files, [..] total\n\
843-
[WARNING] no files deleted due to --dry-run",
844-
)
845+
.with_stderr_data(str![[r#"
846+
[SUMMARY] 15 files, [FILE_SIZE]B total
847+
[WARNING] no files deleted due to --dry-run
848+
849+
"#]])
845850
.run();
846851
// Verify it didn't delete anything.
847852
let after = p.build_dir().ls_r();
848853
assert_eq!(before, after);
849-
let expected = itertools::join(before.iter().map(|p| p.to_str().unwrap()), "\n");
854+
let path_stringify = |p: &PathBuf| format!("{}\n", p.to_str().unwrap());
855+
let files = itertools::join(before.iter().map(path_stringify), "");
856+
let re = regex::Regex::new("/(?<head>[a-z0-9\\-_]+)-([0-9a-f]{16})(?<tail>.*)").unwrap();
857+
let expected = re.replace_all(&files, "/$head-[HASH]$tail");
858+
let expected = expected.replace(p.build_dir().to_str().unwrap(), "[ROOT]/foo/target");
850859
eprintln!("{expected}");
851860
// Verify the verbose output.
852861
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-
)
862+
.with_stdout_data(expected.unordered())
863+
.with_stderr_data(str![[r#"
864+
[SUMMARY] 15 files, [FILE_SIZE]B total
865+
[WARNING] no files deleted due to --dry-run
866+
867+
"#]])
858868
.run();
859869
}
860870

@@ -864,7 +874,7 @@ fn doc_with_package_selection() {
864874
let p = project().file("src/lib.rs", "").build();
865875
p.cargo("clean --doc -p foo")
866876
.with_status(101)
867-
.with_stderr("error: --doc cannot be used with -p")
877+
.with_stderr_data("[ERROR] --doc cannot be used with -p\n")
868878
.run();
869879
}
870880

@@ -879,17 +889,16 @@ fn quiet_does_not_show_summary() {
879889

880890
p.cargo("check").run();
881891
p.cargo("clean --quiet --dry-run")
882-
.with_stdout("")
883-
.with_stderr("")
892+
.with_stdout_data("")
893+
.with_stderr_data("")
884894
.run();
885895
// Verify exact same command without -q would actually display something.
886896
p.cargo("clean --dry-run")
887-
.with_stdout("")
888-
.with_stderr(
889-
"\
890-
[SUMMARY] [..] files, [..] total
897+
.with_stdout_data("")
898+
.with_stderr_data(str![[r#"
899+
[SUMMARY] 9 files, [FILE_SIZE]B total
891900
[WARNING] no files deleted due to --dry-run
892-
",
893-
)
901+
902+
"#]])
894903
.run();
895904
}

0 commit comments

Comments
 (0)