include package metadata in `cargo metadata`
authorAndy Russell <arussell123@gmail.com>
Sat, 14 Apr 2018 19:07:23 +0000 (15:07 -0400)
committerAndy Russell <arussell123@gmail.com>
Sat, 14 Apr 2018 19:07:23 +0000 (15:07 -0400)
Fixes #4819.

src/cargo/core/manifest.rs
src/cargo/core/package.rs
src/cargo/util/toml/mod.rs
tests/testsuite/metadata.rs
tests/testsuite/read_manifest.rs

index 6784cee7f13b12562204983e9a225130b3a14d74..b4b50fc90a97cd87fb1254c36e0d2abd8381b3c6 100644 (file)
@@ -6,6 +6,7 @@ use std::hash::{Hash, Hasher};
 
 use semver::Version;
 use serde::ser;
+use toml;
 use url::Url;
 
 use core::{Dependency, PackageId, PackageIdSpec, SourceId, Summary};
@@ -30,6 +31,7 @@ pub struct Manifest {
     exclude: Vec<String>,
     include: Vec<String>,
     metadata: ManifestMetadata,
+    custom_metadata: Option<toml::Value>,
     profiles: Profiles,
     publish: Option<Vec<String>>,
     publish_lockfile: bool,
@@ -265,6 +267,7 @@ impl Manifest {
         include: Vec<String>,
         links: Option<String>,
         metadata: ManifestMetadata,
+        custom_metadata: Option<toml::Value>,
         profiles: Profiles,
         publish: Option<Vec<String>>,
         publish_lockfile: bool,
@@ -284,6 +287,7 @@ impl Manifest {
             include,
             links,
             metadata,
+            custom_metadata,
             profiles,
             publish,
             replace,
@@ -409,6 +413,10 @@ impl Manifest {
     pub fn edition(&self) -> Edition {
         self.edition
     }
+
+    pub fn custom_metadata(&self) -> Option<&toml::Value> {
+        self.custom_metadata.as_ref()
+    }
 }
 
 impl VirtualManifest {
index 990144254da043a8e6a4a92c7e624b3bb1828ab5..cc75a702aa8438cd3871de912bdec747c86e87a2 100644 (file)
@@ -41,6 +41,7 @@ struct SerializedPackage<'a> {
     targets: &'a [Target],
     features: &'a FeatureMap,
     manifest_path: &'a str,
+    metadata: Option<&'a toml::Value>,
 }
 
 impl ser::Serialize for Package {
@@ -67,6 +68,7 @@ impl ser::Serialize for Package {
             targets: self.manifest.targets(),
             features: summary.features(),
             manifest_path: &self.manifest_path.display().to_string(),
+            metadata: self.manifest.custom_metadata(),
         }.serialize(s)
     }
 }
index 651928efdb430ce9463cd10d7bbf168e07684245..7f20e979b4f3fc977279d9a108592353096095d0 100644 (file)
@@ -810,6 +810,7 @@ impl TomlManifest {
         } else {
             Edition::Edition2015
         };
+        let custom_metadata = project.metadata.clone();
         let mut manifest = Manifest::new(
             summary,
             targets,
@@ -817,6 +818,7 @@ impl TomlManifest {
             include,
             project.links.clone(),
             metadata,
+            custom_metadata,
             profiles,
             publish,
             publish_lockfile,
index 074d5499d4ac640918579d4db8cc91969a80e83a..1eb1dd2e43a2b1fc754858035f9abbe1c322deed 100644 (file)
@@ -1,6 +1,6 @@
-use hamcrest::assert_that;
 use cargotest::support::registry::Package;
 use cargotest::support::{basic_bin_manifest, basic_lib_manifest, execs, main_file, project};
+use hamcrest::assert_that;
 
 #[test]
 fn cargo_metadata_simple() {
@@ -37,7 +37,8 @@ fn cargo_metadata_simple() {
                     }
                 ],
                 "features": {},
-                "manifest_path": "[..]Cargo.toml"
+                "manifest_path": "[..]Cargo.toml",
+                "metadata": null
             }
         ],
         "workspace_members": ["foo 0.5.0 (path+file:[..]foo)"],
@@ -122,7 +123,8 @@ crate-type = ["lib", "staticlib"]
                     }
                 ],
                 "features": {},
-                "manifest_path": "[..]Cargo.toml"
+                "manifest_path": "[..]Cargo.toml",
+                "metadata": null
             }
         ],
         "workspace_members": ["foo 0.5.0 (path+file:[..]foo)"],
@@ -197,7 +199,8 @@ optional_feat = []
                   "default_feat": [],
                   "optional_feat": []
                 },
-                "manifest_path": "[..]Cargo.toml"
+                "manifest_path": "[..]Cargo.toml",
+                "metadata": null
             }
         ],
         "workspace_members": ["foo 0.5.0 (path+file:[..]foo)"],
@@ -278,7 +281,8 @@ fn cargo_metadata_with_deps_and_version() {
                         "src_path": "[..]lib.rs"
                     }
                 ],
-                "version": "0.0.1"
+                "version": "0.0.1",
+                "metadata": null
             },
             {
                 "dependencies": [
@@ -314,7 +318,8 @@ fn cargo_metadata_with_deps_and_version() {
                         "src_path": "[..]lib.rs"
                     }
                 ],
-                "version": "0.0.1"
+                "version": "0.0.1",
+                "metadata": null
             },
             {
                 "dependencies": [
@@ -350,7 +355,8 @@ fn cargo_metadata_with_deps_and_version() {
                         "src_path": "[..]foo.rs"
                     }
                 ],
-                "version": "0.5.0"
+                "version": "0.5.0",
+                "metadata": null
             }
         ],
         "workspace_members": ["foo 0.5.0 (path+file:[..]foo)"],
@@ -434,7 +440,8 @@ name = "ex"
                     }
                 ],
                 "features": {},
-                "manifest_path": "[..]Cargo.toml"
+                "manifest_path": "[..]Cargo.toml",
+                "metadata": null
             }
         ],
         "workspace_members": [
@@ -507,7 +514,8 @@ crate-type = ["rlib", "dylib"]
                     }
                 ],
                 "features": {},
-                "manifest_path": "[..]Cargo.toml"
+                "manifest_path": "[..]Cargo.toml",
+                "metadata": null
             }
         ],
         "workspace_members": [
@@ -571,7 +579,8 @@ fn workspace_metadata() {
                     }
                 ],
                 "features": {},
-                "manifest_path": "[..]bar[/]Cargo.toml"
+                "manifest_path": "[..]bar[/]Cargo.toml",
+                "metadata": null
             },
             {
                 "name": "baz",
@@ -591,7 +600,8 @@ fn workspace_metadata() {
                     }
                 ],
                 "features": {},
-                "manifest_path": "[..]baz[/]Cargo.toml"
+                "manifest_path": "[..]baz[/]Cargo.toml",
+                "metadata": null
             }
         ],
         "workspace_members": ["baz 0.5.0 (path+file:[..]baz)", "bar 0.5.0 (path+file:[..]bar)"],
@@ -658,7 +668,8 @@ fn workspace_metadata_no_deps() {
                     }
                 ],
                 "features": {},
-                "manifest_path": "[..]bar[/]Cargo.toml"
+                "manifest_path": "[..]bar[/]Cargo.toml",
+                "metadata": null
             },
             {
                 "name": "baz",
@@ -678,7 +689,8 @@ fn workspace_metadata_no_deps() {
                     }
                 ],
                 "features": {},
-                "manifest_path": "[..]baz[/]Cargo.toml"
+                "manifest_path": "[..]baz[/]Cargo.toml",
+                "metadata": null
             }
         ],
         "workspace_members": ["baz 0.5.0 (path+file:[..]baz)", "bar 0.5.0 (path+file:[..]bar)"],
@@ -707,7 +719,7 @@ Caused by:
     )
 }
 
-const MANIFEST_OUTPUT: &'static str = r#"
+const MANIFEST_OUTPUT: &str = r#"
 {
     "packages": [{
         "name":"foo",
@@ -725,7 +737,8 @@ const MANIFEST_OUTPUT: &'static str = r#"
             "src_path":"[..][/]foo[/]src[/]foo.rs"
         }],
         "features":{},
-        "manifest_path":"[..]Cargo.toml"
+        "manifest_path":"[..]Cargo.toml",
+        "metadata": null
     }],
     "workspace_members": [ "foo 0.5.0 (path+file:[..]foo)" ],
     "resolve": null,
@@ -867,3 +880,63 @@ fn multiple_features() {
         execs().with_status(0),
     );
 }
+
+#[test]
+fn package_metadata() {
+    let p = project("foo")
+        .file(
+            "Cargo.toml",
+            r#"
+            [package]
+            name = "foo"
+            version = "0.1.0"
+            authors = []
+
+            [package.metadata.bar]
+            baz = "quux"
+        "#,
+        )
+        .file("src/lib.rs", "")
+        .build();
+
+    assert_that(
+        p.cargo("metadata").arg("--no-deps"),
+        execs().with_json(
+            r#"
+    {
+        "packages": [
+            {
+                "name": "foo",
+                "version": "0.1.0",
+                "id": "foo[..]",
+                "source": null,
+                "dependencies": [],
+                "license": null,
+                "license_file": null,
+                "description": null,
+                "targets": [
+                    {
+                        "kind": [ "lib" ],
+                        "crate_types": [ "lib" ],
+                        "name": "foo",
+                        "src_path": "[..]foo[/]src[/]lib.rs"
+                    }
+                ],
+                "features": {},
+                "manifest_path": "[..]foo[/]Cargo.toml",
+                "metadata": {
+                    "bar": {
+                        "baz": "quux"
+                    }
+                }
+            }
+        ],
+        "workspace_members": ["foo[..]"],
+        "resolve": null,
+        "target_directory": "[..]foo[/]target",
+        "version": 1,
+        "workspace_root": "[..][/]foo"
+    }"#,
+        ),
+    );
+}
index fff5a2f1893c7a10d3b5ca274b7bffd352407d1f..b033380416457ea27bf7a04bc125c05afe633b1e 100644 (file)
@@ -18,7 +18,8 @@ static MANIFEST_OUTPUT: &'static str = r#"
         "src_path":"[..][/]foo[/]src[/]foo.rs"
     }],
     "features":{},
-    "manifest_path":"[..]Cargo.toml"
+    "manifest_path":"[..]Cargo.toml",
+    "metadata": null
 }"#;
 
 #[test]