cargo/tests/test_cargo_compile.rs

491 lines
13 KiB
Rust
Raw Normal View History

2014-06-12 22:51:16 +00:00
use support::{ResultTest,project,execs,main_file};
2014-03-20 22:17:19 +00:00
use hamcrest::{assert_that,existing_file};
use cargo;
2014-06-11 21:50:54 +00:00
use cargo::util::{process,realpath};
fn setup() {
}
2014-05-27 23:14:34 +00:00
fn basic_bin_manifest(name: &str) -> String {
format!(r#"
[project]
name = "{}"
version = "0.5.0"
authors = ["wycats@example.com"]
[[bin]]
name = "{}"
"#, name, name)
}
2014-06-11 22:59:18 +00:00
test!(cargo_compile_simple {
let p = project("foo")
2014-05-27 23:14:34 +00:00
.file("Cargo.toml", basic_bin_manifest("foo").as_slice())
.file("src/foo.rs", main_file(r#""i am foo""#, []).as_slice());
2014-05-09 00:50:28 +00:00
assert_that(p.cargo_process("cargo-compile"), execs());
2014-03-20 22:17:19 +00:00
assert_that(&p.root().join("target/foo"), existing_file());
2014-05-09 00:50:28 +00:00
let target = p.root().join("target");
2014-04-02 23:34:19 +00:00
assert_that(
2014-05-09 00:50:28 +00:00
process("foo").extra_path(target),
2014-04-02 23:34:19 +00:00
execs().with_stdout("i am foo\n"));
})
test!(cargo_compile_with_invalid_manifest {
let p = project("foo")
.file("Cargo.toml", "");
2014-05-09 00:50:28 +00:00
assert_that(p.cargo_process("cargo-compile"),
execs()
.with_status(101)
.with_stderr("Cargo.toml is not a valid manifest"));
})
2014-05-09 00:50:28 +00:00
test!(cargo_compile_without_manifest {
let p = project("foo");
2014-05-09 00:50:28 +00:00
assert_that(p.cargo_process("cargo-compile"),
execs()
.with_status(102)
.with_stderr("Could not find Cargo.toml in this directory or any \
parent directory"));
})
test!(cargo_compile_with_invalid_code {
let p = project("foo")
2014-05-27 23:14:34 +00:00
.file("Cargo.toml", basic_bin_manifest("foo").as_slice())
.file("src/foo.rs", "invalid rust code!");
2014-06-09 20:08:09 +00:00
let target = realpath(&p.root().join("target")).assert();
assert_that(p.cargo_process("cargo-compile"),
execs()
.with_status(101)
.with_stderr(format!("\
src/foo.rs:1:1: 1:8 error: expected item but found `invalid`
src/foo.rs:1 invalid rust code!
^~~~~~~
Could not execute process \
`rustc src/foo.rs --crate-type bin --out-dir {} -L {} -L {}` (status=101)",
target.display(),
target.display(),
target.join("deps").display()).as_slice()));
})
test!(cargo_compile_with_warnings_in_the_root_package {
let p = project("foo")
2014-05-27 23:14:34 +00:00
.file("Cargo.toml", basic_bin_manifest("foo").as_slice())
.file("src/foo.rs", "fn main() {} fn dead() {}");
assert_that(p.cargo_process("cargo-compile"),
execs()
.with_stderr("\
src/foo.rs:1:14: 1:26 warning: code is never used: `dead`, #[warn(dead_code)] \
on by default
src/foo.rs:1 fn main() {} fn dead() {}
^~~~~~~~~~~~
"));
})
test!(cargo_compile_with_warnings_in_a_dep_package {
let mut p = project("foo");
let bar = p.root().join("bar");
p = p
.file(".cargo/config", format!(r#"
paths = ["{}"]
2014-05-27 23:14:34 +00:00
"#, bar.display()).as_slice())
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies]
bar = "0.5.0"
[[bin]]
name = "foo"
"#)
.file("src/foo.rs",
main_file(r#""{}", bar::gimme()"#, ["bar"]).as_slice())
.file("bar/Cargo.toml", r#"
[project]
name = "bar"
version = "0.5.0"
authors = ["wycats@example.com"]
[[lib]]
name = "bar"
"#)
.file("bar/src/bar.rs", r#"
2014-05-27 23:14:34 +00:00
pub fn gimme() -> String {
2014-05-29 20:57:31 +00:00
"test passed".to_str()
}
fn dead() {}
"#);
2014-06-11 21:50:54 +00:00
let bar = realpath(&p.root().join("bar")).assert();
let main = realpath(&p.root()).assert();
assert_that(p.cargo_process("cargo-compile"),
execs()
.with_stdout(format!("Compiling bar v0.5.0 (file:{})\n\
Compiling foo v0.5.0 (file:{})\n",
2014-06-11 21:50:54 +00:00
bar.display(), main.display()))
.with_stderr(""));
assert_that(&p.root().join("target/foo"), existing_file());
assert_that(
cargo::util::process("foo").extra_path(p.root().join("target")),
execs().with_stdout("test passed\n"));
})
2014-05-09 00:50:28 +00:00
2014-06-11 21:50:54 +00:00
test!(cargo_compile_with_nested_deps_shorthand {
2014-05-08 23:49:58 +00:00
let mut p = project("foo");
let bar = p.root().join("bar");
let baz = p.root().join("baz");
p = p
.file(".cargo/config", format!(r#"
paths = ["{}", "{}"]
2014-05-27 23:14:34 +00:00
"#, bar.display(), baz.display()).as_slice())
2014-05-08 23:49:58 +00:00
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies]
bar = "0.5.0"
[[bin]]
name = "foo"
"#)
.file("src/foo.rs",
main_file(r#""{}", bar::gimme()"#, ["bar"]).as_slice())
2014-05-08 23:49:58 +00:00
.file("bar/Cargo.toml", r#"
[project]
name = "bar"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies]
baz = "0.5.0"
[[lib]]
name = "bar"
"#)
.file("bar/src/bar.rs", r#"
extern crate baz;
2014-05-27 23:14:34 +00:00
pub fn gimme() -> String {
2014-05-08 23:49:58 +00:00
baz::gimme()
}
"#)
.file("baz/Cargo.toml", r#"
[project]
name = "baz"
version = "0.5.0"
authors = ["wycats@example.com"]
[[lib]]
name = "baz"
"#)
.file("baz/src/baz.rs", r#"
2014-05-27 23:14:34 +00:00
pub fn gimme() -> String {
2014-05-29 21:06:43 +00:00
"test passed".to_str()
2014-05-08 23:49:58 +00:00
}
2014-05-09 00:50:28 +00:00
"#);
2014-05-08 23:49:58 +00:00
p.cargo_process("cargo-compile")
.exec_with_output()
.assert();
assert_that(&p.root().join("target/foo"), existing_file());
assert_that(
cargo::util::process("foo").extra_path(p.root().join("target")),
execs().with_stdout("test passed\n"));
})
test!(cargo_compile_with_nested_deps_longhand {
let mut p = project("foo");
let bar = p.root().join("bar");
let baz = p.root().join("baz");
p = p
.file(".cargo/config", format!(r#"
paths = ["{}", "{}"]
"#, bar.display(), baz.display()).as_slice())
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.bar]
version = "0.5.0"
[[bin]]
name = "foo"
"#)
.file("src/foo.rs",
main_file(r#""{}", bar::gimme()"#, ["bar"]).as_slice())
.file("bar/Cargo.toml", r#"
[project]
name = "bar"
version = "0.5.0"
authors = ["wycats@example.com"]
[dependencies.baz]
version = "0.5.0"
[[lib]]
name = "bar"
"#)
.file("bar/src/bar.rs", r#"
extern crate baz;
pub fn gimme() -> String {
baz::gimme()
}
"#)
.file("baz/Cargo.toml", r#"
[project]
name = "baz"
version = "0.5.0"
authors = ["wycats@example.com"]
[[lib]]
name = "baz"
"#)
.file("baz/src/baz.rs", r#"
pub fn gimme() -> String {
"test passed".to_str()
}
"#);
assert_that(p.cargo_process("cargo-compile"), execs());
assert_that(&p.root().join("target/foo"), existing_file());
assert_that(
cargo::util::process("foo").extra_path(p.root().join("target")),
execs().with_stdout("test passed\n"));
})
// test!(compiling_project_with_invalid_manifest)
test!(custom_build {
let mut build = project("builder");
build = build
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[[bin]] name = "foo"
"#)
.file("src/foo.rs", r#"
fn main() { println!("Hello!"); }
"#);
assert_that(build.cargo_process("cargo-compile"),
execs().with_status(0));
let mut p = project("foo");
p = p
.file("Cargo.toml", format!(r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
build = "{}"
[[bin]] name = "foo"
"#, build.root().join("target/foo").display()))
.file("src/foo.rs", r#"
fn main() {}
"#);
assert_that(p.cargo_process("cargo-compile"),
execs().with_status(0)
.with_stdout(format!("Compiling foo v0.5.0 (file:{})\n",
p.root().display()))
.with_stderr(""));
})
test!(custom_build_failure {
let mut build = project("builder");
build = build
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[[bin]] name = "foo"
"#)
.file("src/foo.rs", r#"
fn main() { fail!("nope") }
"#);
assert_that(build.cargo_process("cargo-compile"), execs().with_status(0));
let mut p = project("foo");
p = p
.file("Cargo.toml", format!(r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
build = "{}"
[[bin]] name = "foo"
"#, build.root().join("target/foo").display()))
.file("src/foo.rs", r#"
fn main() {}
"#);
assert_that(p.cargo_process("cargo-compile"),
execs().with_status(101).with_stderr(format!("\
Could not execute process `{}` (status=101)
--- stderr
task '<main>' failed at 'nope', src/foo.rs:2
", build.root().join("target/foo").display())));
})
test!(custom_build_env_vars {
let mut p = project("foo");
let mut build = project("builder");
build = build
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[[bin]] name = "foo"
"#)
.file("src/foo.rs", format!(r#"
use std::os;
fn main() {{
assert_eq!(os::getenv("OUT_DIR").unwrap(), "{}".to_str());
assert_eq!(os::getenv("DEPS_DIR").unwrap(), "{}".to_str());
}}
"#,
p.root().join("target").display(),
p.root().join("target/deps").display()));
assert_that(build.cargo_process("cargo-compile"), execs().with_status(0));
p = p
.file("Cargo.toml", format!(r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
build = "{}"
[[bin]] name = "foo"
"#, build.root().join("target/foo").display()))
.file("src/foo.rs", r#"
fn main() {}
"#);
assert_that(p.cargo_process("cargo-compile"), execs().with_status(0));
})
test!(custom_build_in_dependency {
let mut p = project("foo");
let bar = p.root().join("bar");
let mut build = project("builder");
build = build
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[[bin]] name = "foo"
"#)
.file("src/foo.rs", format!(r#"
use std::os;
fn main() {{
assert_eq!(os::getenv("OUT_DIR").unwrap(), "{}".to_str());
assert_eq!(os::getenv("DEPS_DIR").unwrap(), "{}".to_str());
}}
"#,
p.root().join("target/deps").display(),
p.root().join("target/deps").display()));
assert_that(build.cargo_process("cargo-compile"), execs().with_status(0));
p = p
.file(".cargo/config", format!(r#"
paths = ["{}"]
"#, bar.display()).as_slice())
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[[bin]] name = "foo"
[dependencies.bar] version = "0.5.0"
"#)
.file("src/foo.rs", r#"
extern crate bar;
fn main() { bar::bar() }
"#)
.file("bar/Cargo.toml", format!(r#"
[project]
name = "bar"
version = "0.5.0"
authors = ["wycats@example.com"]
build = "{}"
[[lib]] name = "bar"
"#, build.root().join("target/foo").display()))
.file("bar/src/bar.rs", r#"
pub fn bar() {}
"#);
assert_that(p.cargo_process("cargo-compile"),
execs().with_status(0));
})