@@ -627,6 +627,203 @@ fn cargo_metadata_with_deps_and_version() {
627627 . run ( ) ;
628628}
629629
630+ /// The `public` field should not show up in `cargo metadata` output if `-Zpublic-dependency`
631+ /// is not enabled
632+ #[ cargo_test]
633+ fn cargo_metadata_public_private_dependencies_disabled ( ) {
634+ let p = project ( )
635+ . file ( "src/foo.rs" , "" )
636+ . file (
637+ "Cargo.toml" ,
638+ r#"
639+ [package]
640+ name = "foo"
641+ version = "0.5.0"
642+ authors = []
643+ license = "MIT"
644+ description = "foo"
645+
646+ [[bin]]
647+ name = "foo"
648+
649+ [dependencies]
650+ bar = { version = "*", public = false }
651+ foobar = { version = "*", public = true }
652+ baz = "*"
653+ "# ,
654+ )
655+ . build ( ) ;
656+ Package :: new ( "bar" , "0.0.1" ) . publish ( ) ;
657+ Package :: new ( "foobar" , "0.0.2" ) . publish ( ) ;
658+ Package :: new ( "baz" , "0.0.3" ) . publish ( ) ;
659+
660+ p. cargo ( "metadata -q --format-version 1" )
661+ . with_stdout_data (
662+ str![ [ r#"
663+ {
664+ "metadata": null,
665+ "packages": [
666+ {
667+ "name": "bar",
668+ "...": "{...}"
669+ },
670+ {
671+ "name": "baz",
672+ "...": "{...}"
673+ },
674+ {
675+ "name": "foo",
676+ "dependencies": [
677+ {
678+ "features": [],
679+ "kind": null,
680+ "name": "bar",
681+ "optional": false,
682+ "registry": null,
683+ "rename": null,
684+ "req": "*",
685+ "source": "registry+https://github.com/rust-lang/crates.io-index",
686+ "target": null,
687+ "uses_default_features": true
688+ },
689+ {
690+ "features": [],
691+ "kind": null,
692+ "name": "baz",
693+ "optional": false,
694+ "registry": null,
695+ "rename": null,
696+ "req": "*",
697+ "source": "registry+https://github.com/rust-lang/crates.io-index",
698+ "target": null,
699+ "uses_default_features": true
700+ },
701+ {
702+ "features": [],
703+ "kind": null,
704+ "name": "foobar",
705+ "optional": false,
706+ "registry": null,
707+ "rename": null,
708+ "req": "*",
709+ "source": "registry+https://github.com/rust-lang/crates.io-index",
710+ "target": null,
711+ "uses_default_features": true
712+ }
713+ ],
714+ "...": "{...}"
715+ },
716+ {
717+ "name": "foobar",
718+ "...": "{...}"
719+ }
720+ ],
721+ "...": "{...}"
722+ }
723+ "# ] ]
724+ . is_json ( ) ,
725+ )
726+ . run ( ) ;
727+ }
728+
729+ #[ cargo_test]
730+ fn cargo_metadata_public_private_dependencies_enabled ( ) {
731+ let p = project ( )
732+ . file ( "src/foo.rs" , "" )
733+ . file (
734+ "Cargo.toml" ,
735+ r#"
736+ [package]
737+ name = "foo"
738+ version = "0.5.0"
739+ authors = []
740+ license = "MIT"
741+ description = "foo"
742+
743+ [[bin]]
744+ name = "foo"
745+
746+ [dependencies]
747+ bar = { version = "*", public = false }
748+ foobar = { version = "*", public = true }
749+ baz = "*"
750+ "# ,
751+ )
752+ . build ( ) ;
753+ Package :: new ( "bar" , "0.0.1" ) . publish ( ) ;
754+ Package :: new ( "foobar" , "0.0.2" ) . publish ( ) ;
755+ Package :: new ( "baz" , "0.0.3" ) . publish ( ) ;
756+
757+ p. cargo ( "metadata -q --format-version 1 -Zpublic-dependency" )
758+ . masquerade_as_nightly_cargo ( & [ "public-dependency" ] )
759+ . with_stdout_data (
760+ str![ [ r#"
761+ {
762+ "metadata": null,
763+ "packages": [
764+ {
765+ "name": "bar",
766+ "...": "{...}"
767+ },
768+ {
769+ "name": "baz",
770+ "...": "{...}"
771+ },
772+ {
773+ "name": "foo",
774+ "dependencies": [
775+ {
776+ "features": [],
777+ "kind": null,
778+ "name": "bar",
779+ "optional": false,
780+ "registry": null,
781+ "rename": null,
782+ "req": "*",
783+ "source": "registry+https://github.com/rust-lang/crates.io-index",
784+ "target": null,
785+ "uses_default_features": true
786+ },
787+ {
788+ "features": [],
789+ "kind": null,
790+ "name": "baz",
791+ "optional": false,
792+ "registry": null,
793+ "rename": null,
794+ "req": "*",
795+ "source": "registry+https://github.com/rust-lang/crates.io-index",
796+ "target": null,
797+ "uses_default_features": true
798+ },
799+ {
800+ "features": [],
801+ "kind": null,
802+ "name": "foobar",
803+ "optional": false,
804+ "registry": null,
805+ "rename": null,
806+ "req": "*",
807+ "source": "registry+https://github.com/rust-lang/crates.io-index",
808+ "target": null,
809+ "uses_default_features": true
810+ }
811+ ],
812+ "...": "{...}"
813+ },
814+ {
815+ "name": "foobar",
816+ "...": "{...}"
817+ }
818+ ],
819+ "...": "{...}"
820+ }
821+ "# ] ]
822+ . is_json ( ) ,
823+ )
824+ . run ( ) ;
825+ }
826+
630827#[ cargo_test]
631828fn example ( ) {
632829 let p = project ( )
0 commit comments