Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,31 @@ pub enum TargetKind {
CustomBuild,
}

impl Encodable for TargetKind {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
impl TargetKind {
pub fn name(&self) -> &'static str {
use self::TargetKind::*;
match *self {
TargetKind::Lib(ref kinds) |
TargetKind::ExampleLib(ref kinds) => {
Lib(_) => "lib",
Bin => "bin",
ExampleBin | ExampleLib(_) => "example",
Test => "test",
CustomBuild => "custom-build",
Bench => "bench"
}
}

pub fn crate_types(&self) -> Vec<&str> {
use self::TargetKind::*;
match *self {
Lib(ref kinds) | ExampleLib(ref kinds) => {
kinds.iter().map(LibKind::crate_type).collect()
}
TargetKind::Bin => vec!["bin"],
TargetKind::ExampleBin => vec!["example"],
TargetKind::Test => vec!["test"],
TargetKind::CustomBuild => vec!["custom-build"],
TargetKind::Bench => vec!["bench"],
}.encode(s)
Bin => vec!["bin"],
ExampleBin => vec!["example"],
Test => vec!["test"],
CustomBuild => vec!["custom-build"],
Bench => vec!["bench"]
}
}
}

Expand Down Expand Up @@ -195,15 +207,17 @@ pub struct Target {

#[derive(RustcEncodable)]
struct SerializedTarget<'a> {
kind: &'a TargetKind,
kind: Vec<&'a str>,
crate_types: Vec<&'a str>,
name: &'a str,
src_path: &'a str,
}

impl Encodable for Target {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
SerializedTarget {
kind: &self.kind,
kind: vec![self.kind.name()],
crate_types: self.kind.crate_types(),
name: &self.name,
src_path: &self.src_path.display().to_string(),
}.encode(s)
Expand Down
28 changes: 24 additions & 4 deletions tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2510,7 +2510,12 @@ fn compiler_json_error_format() {
{
"reason":"compiler-message",
"package_id":"bar 0.5.0 ([..])",
"target":{"kind":["lib"],"name":"bar","src_path":"[..]lib.rs"},
"target":{
"kind":["lib"],
"crate_types":["lib"],
"name":"bar",
"src_path":"[..]lib.rs"
},
"message":"{...}"
}

Expand All @@ -2524,21 +2529,36 @@ fn compiler_json_error_format() {
},
"features": [],
"package_id":"bar 0.5.0 ([..])",
"target":{"kind":["lib"],"name":"bar","src_path":"[..]lib.rs"},
"target":{
"kind":["lib"],
"crate_types":["lib"],
"name":"bar",
"src_path":"[..]lib.rs"
},
"filenames":["[..].rlib"]
}

{
"reason":"compiler-message",
"package_id":"foo 0.5.0 ([..])",
"target":{"kind":["bin"],"name":"foo","src_path":"[..]main.rs"},
"target":{
"kind":["bin"],
"crate_types":["bin"],
"name":"foo",
"src_path":"[..]main.rs"
},
"message":"{...}"
}

{
"reason":"compiler-artifact",
"package_id":"foo 0.5.0 ([..])",
"target":{"kind":["bin"],"name":"foo","src_path":"[..]main.rs"},
"target":{
"kind":["bin"],
"crate_types":["bin"],
"name":"foo",
"src_path":"[..]main.rs"
},
"profile": {
"debug_assertions": true,
"debuginfo": 2,
Expand Down
138 changes: 138 additions & 0 deletions tests/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ fn cargo_metadata_simple() {
"kind": [
"bin"
],
"crate_types": [
"bin"
],
"name": "foo",
"src_path": "[..][/]foo[/]src[/]foo.rs"
}
Expand Down Expand Up @@ -93,6 +96,9 @@ fn cargo_metadata_with_deps_and_version() {
"kind": [
"lib"
],
"crate_types": [
"lib"
],
"name": "baz",
"src_path": "[..]lib.rs"
}
Expand Down Expand Up @@ -125,6 +131,9 @@ fn cargo_metadata_with_deps_and_version() {
"kind": [
"lib"
],
"crate_types": [
"lib"
],
"name": "bar",
"src_path": "[..]lib.rs"
}
Expand Down Expand Up @@ -157,6 +166,9 @@ fn cargo_metadata_with_deps_and_version() {
"kind": [
"bin"
],
"crate_types": [
"bin"
],
"name": "foo",
"src_path": "[..]foo.rs"
}
Expand Down Expand Up @@ -190,6 +202,127 @@ fn cargo_metadata_with_deps_and_version() {
}"#));
}

#[test]
fn example() {
let p = project("foo")
.file("src/lib.rs", "")
.file("examples/ex.rs", "")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.1.0"

[[example]]
name = "ex"
"#);

assert_that(p.cargo_process("metadata"), execs().with_json(r#"
{
"packages": [
{
"name": "foo",
"version": "0.1.0",
"id": "foo[..]",
"license": null,
"license_file": null,
"description": null,
"source": null,
"dependencies": [],
"targets": [
{
"kind": [ "lib" ],
"crate_types": [ "lib" ],
"name": "foo",
"src_path": "[..][/]foo[/]src[/]lib.rs"
},
{
"kind": [ "example" ],
"crate_types": [ "example" ],
"name": "ex",
"src_path": "[..][/]foo[/]examples[/]ex.rs"
}
],
"features": {},
"manifest_path": "[..]Cargo.toml"
}
],
"workspace_members": [
"foo 0.1.0 (path+file:[..]foo)"
],
"resolve": {
"root": "foo 0.1.0 (path+file://[..]foo)",
"nodes": [
{
"id": "foo 0.1.0 (path+file:[..]foo)",
"dependencies": []
}
]
},
"version": 1
}"#));
}

#[test]
fn example_lib() {
let p = project("foo")
.file("src/lib.rs", "")
.file("examples/ex.rs", "")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.1.0"

[[example]]
name = "ex"
crate-type = ["rlib", "dylib"]
"#);

assert_that(p.cargo_process("metadata"), execs().with_json(r#"
{
"packages": [
{
"name": "foo",
"version": "0.1.0",
"id": "foo[..]",
"license": null,
"license_file": null,
"description": null,
"source": null,
"dependencies": [],
"targets": [
{
"kind": [ "lib" ],
"crate_types": [ "lib" ],
"name": "foo",
"src_path": "[..][/]foo[/]src[/]lib.rs"
},
{
"kind": [ "example" ],
"crate_types": [ "rlib", "dylib" ],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexcrichton,
The test checks what you asked.

"name": "ex",
"src_path": "[..][/]foo[/]examples[/]ex.rs"
}
],
"features": {},
"manifest_path": "[..]Cargo.toml"
}
],
"workspace_members": [
"foo 0.1.0 (path+file:[..]foo)"
],
"resolve": {
"root": "foo 0.1.0 (path+file://[..]foo)",
"nodes": [
{
"id": "foo 0.1.0 (path+file:[..]foo)",
"dependencies": []
}
]
},
"version": 1
}"#));
}

#[test]
fn workspace_metadata() {
let p = project("foo")
Expand Down Expand Up @@ -218,6 +351,7 @@ fn workspace_metadata() {
"targets": [
{
"kind": [ "lib" ],
"crate_types": [ "lib" ],
"name": "bar",
"src_path": "[..]bar[/]src[/]lib.rs"
}
Expand All @@ -237,6 +371,7 @@ fn workspace_metadata() {
"targets": [
{
"kind": [ "lib" ],
"crate_types": [ "lib" ],
"name": "baz",
"src_path": "[..]baz[/]src[/]lib.rs"
}
Expand Down Expand Up @@ -291,6 +426,7 @@ fn workspace_metadata_no_deps() {
"targets": [
{
"kind": [ "lib" ],
"crate_types": [ "lib" ],
"name": "bar",
"src_path": "[..]bar[/]src[/]lib.rs"
}
Expand All @@ -310,6 +446,7 @@ fn workspace_metadata_no_deps() {
"targets": [
{
"kind": [ "lib" ],
"crate_types": ["lib"],
"name": "baz",
"src_path": "[..]baz[/]src[/]lib.rs"
}
Expand Down Expand Up @@ -351,6 +488,7 @@ const MANIFEST_OUTPUT: &'static str=
"description": null,
"targets":[{
"kind":["bin"],
"crate_types":["bin"],
"name":"foo",
"src_path":"[..][/]foo[/]src[/]foo.rs"
}],
Expand Down
1 change: 1 addition & 0 deletions tests/read-manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ static MANIFEST_OUTPUT: &'static str = r#"
"dependencies":[],
"targets":[{
"kind":["bin"],
"crate_types":["bin"],
"name":"foo",
"src_path":"[..][/]foo[/]src[/]foo.rs"
}],
Expand Down