11//! Tests for the `cargo clean` command.
22
3- #![ allow( deprecated) ]
4-
53use cargo_test_support:: paths:: CargoPathExt ;
4+ use cargo_test_support:: prelude:: * ;
65use cargo_test_support:: registry:: Package ;
6+ use cargo_test_support:: str;
77use 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,25 @@ 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.
431453 for obj in p. glob ( "target/debug/deps/bar-*.o" ) {
432- expected. push_str ( & format ! ( "[REMOVING] [..]{}\n " , obj. unwrap( ) . display( ) ) ) ;
454+ let obj = obj. unwrap ( ) . display ( ) . to_string ( ) ;
455+ expected. push_str ( & format ! (
456+ "[REMOVING] {}\n " ,
457+ obj. replace( p. root( ) . to_str( ) . unwrap( ) , "[ROOT]/foo" )
458+ ) ) ;
433459 }
434460 }
435- expected. push_str ( "[REMOVED] [.. ] files, [..] total\n " ) ;
461+ expected. push_str ( "[REMOVED] [FILE_NUM ] files, [FILE_SIZE]B total\n " ) ;
436462 p. cargo ( "clean -p bar --verbose" )
437- . with_stderr_unordered ( & expected)
463+ . with_stderr_data ( & expected. unordered ( ) )
438464 . run ( ) ;
439465 p. cargo ( "build" ) . run ( ) ;
440466}
@@ -614,21 +640,20 @@ fn clean_spec_version() {
614640 // Check suggestion for bad pkgid.
615641 p. cargo ( "clean -p baz" )
616642 . with_status ( 101 )
617- . with_stderr (
618- "\
619- error: package ID specification `baz` did not match any packages
643+ . with_stderr_data ( str![ [ r#"
644+ [ERROR] package ID specification `baz` did not match any packages
620645
621- <tab> Did you mean `bar`?
622- " ,
623- )
646+ Did you mean `bar`?
647+
648+ "# ] ] )
624649 . run ( ) ;
625650
626651 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- )
652+ . with_stderr_data ( str! [ [ r#"
653+ [WARNING] version qualifier in `-p bar:0.1.0` is ignored, cleaning all versions of `bar` found
654+ [REMOVED] [FILE_NUM] files, [FILE_SIZE]B total
655+
656+ "# ] ] )
632657 . run ( ) ;
633658 let mut walker = walkdir:: WalkDir :: new ( p. build_dir ( ) )
634659 . into_iter ( )
@@ -670,21 +695,20 @@ fn clean_spec_partial_version() {
670695 // Check suggestion for bad pkgid.
671696 p. cargo ( "clean -p baz" )
672697 . with_status ( 101 )
673- . with_stderr (
674- "\
675- error: package ID specification `baz` did not match any packages
698+ . with_stderr_data ( str![ [ r#"
699+ [ERROR] package ID specification `baz` did not match any packages
676700
677- <tab> Did you mean `bar`?
678- " ,
679- )
701+ Did you mean `bar`?
702+
703+ "# ] ] )
680704 . run ( ) ;
681705
682706 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- )
707+ . with_stderr_data ( str! [ [ r#"
708+ [WARNING] version qualifier in `-p bar:0.1` is ignored, cleaning all versions of `bar` found
709+ [REMOVED] [FILE_NUM] files, [FILE_SIZE]B total
710+
711+ "# ] ] )
688712 . run ( ) ;
689713 let mut walker = walkdir:: WalkDir :: new ( p. build_dir ( ) )
690714 . into_iter ( )
@@ -726,21 +750,20 @@ fn clean_spec_partial_version_ambiguous() {
726750 // Check suggestion for bad pkgid.
727751 p. cargo ( "clean -p baz" )
728752 . with_status ( 101 )
729- . with_stderr (
730- "\
731- error: package ID specification `baz` did not match any packages
753+ . with_stderr_data ( str![ [ r#"
754+ [ERROR] package ID specification `baz` did not match any packages
732755
733- <tab> Did you mean `bar`?
734- " ,
735- )
756+ Did you mean `bar`?
757+
758+ "# ] ] )
736759 . run ( ) ;
737760
738761 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- )
762+ . with_stderr_data ( str! [ [ r#"
763+ [WARNING] version qualifier in `-p bar:0` is ignored, cleaning all versions of `bar` found
764+ [REMOVED] [FILE_NUM] files, [FILE_SIZE]B total
765+
766+ "# ] ] )
744767 . run ( ) ;
745768 let mut walker = walkdir:: WalkDir :: new ( p. build_dir ( ) )
746769 . into_iter ( )
@@ -794,16 +817,15 @@ fn clean_spec_reserved() {
794817
795818 // This should not rebuild bar.
796819 p. cargo ( "build -v --all-targets" )
797- . with_stderr (
798- "\
820+ . with_stderr_data ( str![ [ r#"
799821[FRESH] bar v1.0.0
800- [COMPILING] foo v0.1.0 [..]
801- [RUNNING] `rustc [..]
802- [RUNNING] `rustc [..]
803- [RUNNING] `rustc [..]
804- [FINISHED] [..]
805- " ,
806- )
822+ [COMPILING] foo v0.1.0 ([ROOT]/foo)
823+ [RUNNING] `rustc [..]`
824+ [RUNNING] `rustc [..]`
825+ [RUNNING] `rustc [..]`
826+ [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
827+
828+ "# ] ] )
807829 . run ( ) ;
808830}
809831
@@ -829,32 +851,39 @@ fn clean_dry_run() {
829851
830852 // Start with no files.
831853 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- )
854+ . with_stdout_data ( "" )
855+ . with_stderr_data ( str![ [ r#"
856+ [SUMMARY] [FILE_NUM] files
857+ [WARNING] no files deleted due to --dry-run
858+
859+ "# ] ] )
837860 . run ( ) ;
838861 p. cargo ( "check" ) . run ( ) ;
839862 let before = p. build_dir ( ) . ls_r ( ) ;
840863 p. cargo ( "clean --dry-run" )
841- . with_stderr (
842- "[SUMMARY] [..] files, [..] total\n \
843- [WARNING] no files deleted due to --dry-run",
844- )
864+ . with_stderr_data ( str![ [ r#"
865+ [SUMMARY] [FILE_NUM] files, [FILE_SIZE]B total
866+ [WARNING] no files deleted due to --dry-run
867+
868+ "# ] ] )
845869 . run ( ) ;
846870 // Verify it didn't delete anything.
847871 let after = p. build_dir ( ) . ls_r ( ) ;
848872 assert_eq ! ( before, after) ;
849- let expected = itertools:: join ( before. iter ( ) . map ( |p| p. to_str ( ) . unwrap ( ) ) , "\n " ) ;
873+ let path_stringify = |p : & PathBuf | format ! ( "{}\n " , p. to_str( ) . unwrap( ) ) ;
874+ let files = itertools:: join ( before. iter ( ) . map ( path_stringify) , "" ) ;
875+ let re = regex:: Regex :: new ( "/(?<head>[a-z0-9\\ -_]+)-([0-9a-f]{16})(?<tail>.*)" ) . unwrap ( ) ;
876+ let expected = re. replace_all ( & files, "/$head-[HASH]$tail" ) ;
877+ let expected = expected. replace ( p. build_dir ( ) . to_str ( ) . unwrap ( ) , "[ROOT]/foo/target" ) ;
850878 eprintln ! ( "{expected}" ) ;
851879 // Verify the verbose output.
852880 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- )
881+ . with_stdout_data ( expected. unordered ( ) )
882+ . with_stderr_data ( str![ [ r#"
883+ [SUMMARY] [FILE_NUM] files, [FILE_SIZE]B total
884+ [WARNING] no files deleted due to --dry-run
885+
886+ "# ] ] )
858887 . run ( ) ;
859888}
860889
@@ -864,7 +893,10 @@ fn doc_with_package_selection() {
864893 let p = project ( ) . file ( "src/lib.rs" , "" ) . build ( ) ;
865894 p. cargo ( "clean --doc -p foo" )
866895 . with_status ( 101 )
867- . with_stderr ( "error: --doc cannot be used with -p" )
896+ . with_stderr_data ( str![ [ r#"
897+ [ERROR] --doc cannot be used with -p
898+
899+ "# ] ] )
868900 . run ( ) ;
869901}
870902
@@ -879,17 +911,16 @@ fn quiet_does_not_show_summary() {
879911
880912 p. cargo ( "check" ) . run ( ) ;
881913 p. cargo ( "clean --quiet --dry-run" )
882- . with_stdout ( "" )
883- . with_stderr ( "" )
914+ . with_stdout_data ( "" )
915+ . with_stderr_data ( "" )
884916 . run ( ) ;
885917 // Verify exact same command without -q would actually display something.
886918 p. cargo ( "clean --dry-run" )
887- . with_stdout ( "" )
888- . with_stderr (
889- "\
890- [SUMMARY] [..] files, [..] total
919+ . with_stdout_data ( "" )
920+ . with_stderr_data ( str![ [ r#"
921+ [SUMMARY] [FILE_NUM] files, [FILE_SIZE]B total
891922[WARNING] no files deleted due to --dry-run
892- " ,
893- )
923+
924+ "# ] ] )
894925 . run ( ) ;
895926}
0 commit comments