Skip to content

Commit

Permalink
basic tests for metadata command
Browse files Browse the repository at this point in the history
  • Loading branch information
winger committed Apr 22, 2015
1 parent 7bbbc75 commit 2261fa3
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/cargo/ops/cargo_output_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ impl Decodable for OutputTo {
fn decode<D: Decoder>(d: &mut D) -> Result<OutputTo, D::Error> {
d.read_option(|d, b| {
if b {
Ok(OutputTo::Path(try!(Decodable::decode(d))))
let path = PathBuf::from(try!(d.read_str()));
Ok(OutputTo::Path(path))
} else {
Ok(OutputTo::StdOut)
}
Expand Down
85 changes: 85 additions & 0 deletions tests/test_cargo_metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::fs::File;
use std::io::prelude::*;

use support::{project, execs, basic_bin_manifest};
use hamcrest::{assert_that, existing_file, is, equal_to};


fn setup() {
}

test!(cargo_metadata_simple {
let p = project("foo")
.file("Cargo.toml", &basic_bin_manifest("foo"));

assert_that(p.cargo_process("metadata"), execs().with_stdout(format!(r#"
[[packages]]
dependencies = []
manifest_path = "{}/Cargo.toml"
name = "foo"
version = "0.5.0"
[[packages.targets]]
kind = ["bin"]
name = "foo"
src_path = "src/foo.rs"
[root]
name = "foo"
version = "0.5.0"
"#, p.root().to_str().unwrap())));
});

test!(cargo_metadata_simple_json {
let p = project("foo")
.file("Cargo.toml", &basic_bin_manifest("foo"));

assert_that(p.cargo_process("metadata").arg("-f").arg("json"), execs().with_stdout(format!(r#"{{"root":{{"name":"foo","version":"0.5.0","features":null}},"packages":[{{"name":"foo","version":"0.5.0","dependencies":[],"targets":[{{"kind":["bin"],"name":"foo","src_path":"src/foo.rs","metadata":null}}],"manifest_path":"{}/Cargo.toml"}}]}}
"#, p.root().to_str().unwrap())));
});

test!(cargo_metadata_with_invalid_manifest {
let p = project("foo")
.file("Cargo.toml", "");

assert_that(p.cargo_process("metadata"),
execs()
.with_status(101)
.with_stderr("\
failed to parse manifest at `[..]`
Caused by:
No `package` or `project` section found.
"))
});

test!(cargo_metadata_simple_file {
let p = project("foo")
.file("Cargo.toml", &basic_bin_manifest("foo"));

assert_that(p.cargo_process("metadata").arg("--output-path").arg("metadata.toml"), execs().with_status(0));

let outputfile = p.root().join("metadata.toml");
assert_that(&outputfile, existing_file());

let mut output = String::new();
File::open(&outputfile).unwrap().read_to_string(&mut output).unwrap();

assert_that(output, is(equal_to(format!(r#"
[[packages]]
dependencies = []
manifest_path = "{}/Cargo.toml"
name = "foo"
version = "0.5.0"
[[packages.targets]]
kind = ["bin"]
name = "foo"
src_path = "src/foo.rs"
[root]
name = "foo"
version = "0.5.0"
"#, p.root().to_str().unwrap()))));
});
1 change: 1 addition & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod test_cargo_fetch;
mod test_cargo_freshness;
mod test_cargo_generate_lockfile;
mod test_cargo_new;
mod test_cargo_metadata;
mod test_cargo_package;
mod test_cargo_profiles;
mod test_cargo_publish;
Expand Down

0 comments on commit 2261fa3

Please sign in to comment.