@@ -17,6 +17,10 @@ use std::str;
1717
1818use structopt:: StructOpt ;
1919
20+ #[ path = "test/mod.rs" ]
21+ #[ cfg( test) ]
22+ mod cargo_fmt_tests;
23+
2024#[ derive( StructOpt , Debug ) ]
2125#[ structopt(
2226 bin_name = "cargo fmt" ,
@@ -356,7 +360,7 @@ fn get_targets_root_only(
356360 manifest_path : Option < & Path > ,
357361 targets : & mut BTreeSet < Target > ,
358362) -> Result < ( ) , io:: Error > {
359- let metadata = get_cargo_metadata ( manifest_path, false ) ?;
363+ let metadata = get_cargo_metadata ( manifest_path) ?;
360364 let workspace_root_path = PathBuf :: from ( & metadata. workspace_root ) . canonicalize ( ) ?;
361365 let ( in_workspace_root, current_dir_manifest) = if let Some ( target_manifest) = manifest_path {
362366 (
@@ -400,34 +404,29 @@ fn get_targets_recursive(
400404 mut targets : & mut BTreeSet < Target > ,
401405 visited : & mut BTreeSet < String > ,
402406) -> Result < ( ) , io:: Error > {
403- let metadata = get_cargo_metadata ( manifest_path, false ) ?;
404- let metadata_with_deps = get_cargo_metadata ( manifest_path, true ) ?;
405-
406- for package in metadata. packages {
407+ let metadata = get_cargo_metadata ( manifest_path) ?;
408+ for package in & metadata. packages {
407409 add_targets ( & package. targets , & mut targets) ;
408410
409- // Look for local dependencies.
410- for dependency in package. dependencies {
411- if dependency. source . is_some ( ) || visited. contains ( & dependency. name ) {
411+ // Look for local dependencies using information available since cargo v1.51
412+ // It's theoretically possible someone could use a newer version of rustfmt with
413+ // a much older version of `cargo`, but we don't try to explicitly support that scenario.
414+ // If someone reports an issue with path-based deps not being formatted, be sure to
415+ // confirm their version of `cargo` (not `cargo-fmt`) is >= v1.51
416+ // https://github.com/rust-lang/cargo/pull/8994
417+ for dependency in & package. dependencies {
418+ if dependency. path . is_none ( ) || visited. contains ( & dependency. name ) {
412419 continue ;
413420 }
414421
415- let dependency_package = metadata_with_deps
416- . packages
417- . iter ( )
418- . find ( |p| p. name == dependency. name && p. source . is_none ( ) ) ;
419- let manifest_path = if let Some ( dep_pkg) = dependency_package {
420- PathBuf :: from ( & dep_pkg. manifest_path )
421- } else {
422- let mut package_manifest_path = PathBuf :: from ( & package. manifest_path ) ;
423- package_manifest_path. pop ( ) ;
424- package_manifest_path. push ( & dependency. name ) ;
425- package_manifest_path. push ( "Cargo.toml" ) ;
426- package_manifest_path
427- } ;
428-
429- if manifest_path. exists ( ) {
430- visited. insert ( dependency. name ) ;
422+ let manifest_path = PathBuf :: from ( dependency. path . as_ref ( ) . unwrap ( ) ) . join ( "Cargo.toml" ) ;
423+ if manifest_path. exists ( )
424+ && !metadata
425+ . packages
426+ . iter ( )
427+ . any ( |p| p. manifest_path . eq ( & manifest_path) )
428+ {
429+ visited. insert ( dependency. name . to_owned ( ) ) ;
431430 get_targets_recursive ( Some ( & manifest_path) , & mut targets, visited) ?;
432431 }
433432 }
@@ -441,8 +440,7 @@ fn get_targets_with_hitlist(
441440 hitlist : & [ String ] ,
442441 targets : & mut BTreeSet < Target > ,
443442) -> Result < ( ) , io:: Error > {
444- let metadata = get_cargo_metadata ( manifest_path, false ) ?;
445-
443+ let metadata = get_cargo_metadata ( manifest_path) ?;
446444 let mut workspace_hitlist: BTreeSet < & String > = BTreeSet :: from_iter ( hitlist) ;
447445
448446 for package in metadata. packages {
@@ -527,14 +525,9 @@ fn run_rustfmt(
527525 . unwrap_or ( SUCCESS ) )
528526}
529527
530- fn get_cargo_metadata (
531- manifest_path : Option < & Path > ,
532- include_deps : bool ,
533- ) -> Result < cargo_metadata:: Metadata , io:: Error > {
528+ fn get_cargo_metadata ( manifest_path : Option < & Path > ) -> Result < cargo_metadata:: Metadata , io:: Error > {
534529 let mut cmd = cargo_metadata:: MetadataCommand :: new ( ) ;
535- if !include_deps {
536- cmd. no_deps ( ) ;
537- }
530+ cmd. no_deps ( ) ;
538531 if let Some ( manifest_path) = manifest_path {
539532 cmd. manifest_path ( manifest_path) ;
540533 }
@@ -551,224 +544,3 @@ fn get_cargo_metadata(
551544 }
552545 }
553546}
554-
555- #[ cfg( test) ]
556- mod cargo_fmt_tests {
557- use super :: * ;
558-
559- #[ test]
560- fn default_options ( ) {
561- let empty: Vec < String > = vec ! [ ] ;
562- let o = Opts :: from_iter ( & empty) ;
563- assert_eq ! ( false , o. quiet) ;
564- assert_eq ! ( false , o. verbose) ;
565- assert_eq ! ( false , o. version) ;
566- assert_eq ! ( false , o. check) ;
567- assert_eq ! ( empty, o. packages) ;
568- assert_eq ! ( empty, o. rustfmt_options) ;
569- assert_eq ! ( false , o. format_all) ;
570- assert_eq ! ( None , o. manifest_path) ;
571- assert_eq ! ( None , o. message_format) ;
572- }
573-
574- #[ test]
575- fn good_options ( ) {
576- let o = Opts :: from_iter ( & [
577- "test" ,
578- "-q" ,
579- "-p" ,
580- "p1" ,
581- "-p" ,
582- "p2" ,
583- "--message-format" ,
584- "short" ,
585- "--check" ,
586- "--" ,
587- "--edition" ,
588- "2018" ,
589- ] ) ;
590- assert_eq ! ( true , o. quiet) ;
591- assert_eq ! ( false , o. verbose) ;
592- assert_eq ! ( false , o. version) ;
593- assert_eq ! ( true , o. check) ;
594- assert_eq ! ( vec![ "p1" , "p2" ] , o. packages) ;
595- assert_eq ! ( vec![ "--edition" , "2018" ] , o. rustfmt_options) ;
596- assert_eq ! ( false , o. format_all) ;
597- assert_eq ! ( Some ( String :: from( "short" ) ) , o. message_format) ;
598- }
599-
600- #[ test]
601- fn unexpected_option ( ) {
602- assert ! (
603- Opts :: clap( )
604- . get_matches_from_safe( & [ "test" , "unexpected" ] )
605- . is_err( )
606- ) ;
607- }
608-
609- #[ test]
610- fn unexpected_flag ( ) {
611- assert ! (
612- Opts :: clap( )
613- . get_matches_from_safe( & [ "test" , "--flag" ] )
614- . is_err( )
615- ) ;
616- }
617-
618- #[ test]
619- fn mandatory_separator ( ) {
620- assert ! (
621- Opts :: clap( )
622- . get_matches_from_safe( & [ "test" , "--emit" ] )
623- . is_err( )
624- ) ;
625- assert ! (
626- !Opts :: clap( )
627- . get_matches_from_safe( & [ "test" , "--" , "--emit" ] )
628- . is_err( )
629- ) ;
630- }
631-
632- #[ test]
633- fn multiple_packages_one_by_one ( ) {
634- let o = Opts :: from_iter ( & [
635- "test" ,
636- "-p" ,
637- "package1" ,
638- "--package" ,
639- "package2" ,
640- "-p" ,
641- "package3" ,
642- ] ) ;
643- assert_eq ! ( 3 , o. packages. len( ) ) ;
644- }
645-
646- #[ test]
647- fn multiple_packages_grouped ( ) {
648- let o = Opts :: from_iter ( & [
649- "test" ,
650- "--package" ,
651- "package1" ,
652- "package2" ,
653- "-p" ,
654- "package3" ,
655- "package4" ,
656- ] ) ;
657- assert_eq ! ( 4 , o. packages. len( ) ) ;
658- }
659-
660- #[ test]
661- fn empty_packages_1 ( ) {
662- assert ! ( Opts :: clap( ) . get_matches_from_safe( & [ "test" , "-p" ] ) . is_err( ) ) ;
663- }
664-
665- #[ test]
666- fn empty_packages_2 ( ) {
667- assert ! (
668- Opts :: clap( )
669- . get_matches_from_safe( & [ "test" , "-p" , "--" , "--check" ] )
670- . is_err( )
671- ) ;
672- }
673-
674- #[ test]
675- fn empty_packages_3 ( ) {
676- assert ! (
677- Opts :: clap( )
678- . get_matches_from_safe( & [ "test" , "-p" , "--verbose" ] )
679- . is_err( )
680- ) ;
681- }
682-
683- #[ test]
684- fn empty_packages_4 ( ) {
685- assert ! (
686- Opts :: clap( )
687- . get_matches_from_safe( & [ "test" , "-p" , "--check" ] )
688- . is_err( )
689- ) ;
690- }
691-
692- mod convert_message_format_to_rustfmt_args_tests {
693- use super :: * ;
694-
695- #[ test]
696- fn invalid_message_format ( ) {
697- assert_eq ! (
698- convert_message_format_to_rustfmt_args( "awesome" , & mut vec![ ] ) ,
699- Err ( String :: from(
700- "invalid --message-format value: awesome. Allowed values are: short|json|human"
701- ) ) ,
702- ) ;
703- }
704-
705- #[ test]
706- fn json_message_format_and_check_arg ( ) {
707- let mut args = vec ! [ String :: from( "--check" ) ] ;
708- assert_eq ! (
709- convert_message_format_to_rustfmt_args( "json" , & mut args) ,
710- Err ( String :: from(
711- "cannot include --check arg when --message-format is set to json"
712- ) ) ,
713- ) ;
714- }
715-
716- #[ test]
717- fn json_message_format_and_emit_arg ( ) {
718- let mut args = vec ! [ String :: from( "--emit" ) , String :: from( "checkstyle" ) ] ;
719- assert_eq ! (
720- convert_message_format_to_rustfmt_args( "json" , & mut args) ,
721- Err ( String :: from(
722- "cannot include --emit arg when --message-format is set to json"
723- ) ) ,
724- ) ;
725- }
726-
727- #[ test]
728- fn json_message_format ( ) {
729- let mut args = vec ! [ String :: from( "--edition" ) , String :: from( "2018" ) ] ;
730- assert ! ( convert_message_format_to_rustfmt_args( "json" , & mut args) . is_ok( ) ) ;
731- assert_eq ! (
732- args,
733- vec![
734- String :: from( "--edition" ) ,
735- String :: from( "2018" ) ,
736- String :: from( "--emit" ) ,
737- String :: from( "json" )
738- ]
739- ) ;
740- }
741-
742- #[ test]
743- fn human_message_format ( ) {
744- let exp_args = vec ! [ String :: from( "--emit" ) , String :: from( "json" ) ] ;
745- let mut act_args = exp_args. clone ( ) ;
746- assert ! ( convert_message_format_to_rustfmt_args( "human" , & mut act_args) . is_ok( ) ) ;
747- assert_eq ! ( act_args, exp_args) ;
748- }
749-
750- #[ test]
751- fn short_message_format ( ) {
752- let mut args = vec ! [ String :: from( "--check" ) ] ;
753- assert ! ( convert_message_format_to_rustfmt_args( "short" , & mut args) . is_ok( ) ) ;
754- assert_eq ! ( args, vec![ String :: from( "--check" ) , String :: from( "-l" ) ] ) ;
755- }
756-
757- #[ test]
758- fn short_message_format_included_short_list_files_flag ( ) {
759- let mut args = vec ! [ String :: from( "--check" ) , String :: from( "-l" ) ] ;
760- assert ! ( convert_message_format_to_rustfmt_args( "short" , & mut args) . is_ok( ) ) ;
761- assert_eq ! ( args, vec![ String :: from( "--check" ) , String :: from( "-l" ) ] ) ;
762- }
763-
764- #[ test]
765- fn short_message_format_included_long_list_files_flag ( ) {
766- let mut args = vec ! [ String :: from( "--check" ) , String :: from( "--files-with-diff" ) ] ;
767- assert ! ( convert_message_format_to_rustfmt_args( "short" , & mut args) . is_ok( ) ) ;
768- assert_eq ! (
769- args,
770- vec![ String :: from( "--check" ) , String :: from( "--files-with-diff" ) ]
771- ) ;
772- }
773- }
774- }
0 commit comments