cargo/tests/testsuite/rustc.rs

563 lines
13 KiB
Rust
Raw Normal View History

use cargotest::support::{execs, project};
use hamcrest::assert_that;
2016-05-12 17:06:36 +00:00
const CARGO_RUSTC_ERROR: &'static str =
2018-03-14 15:17:44 +00:00
"[ERROR] extra arguments to `rustc` can only be passed to one target, consider filtering
2016-05-12 17:06:36 +00:00
the package by passing e.g. `--lib` or `--bin NAME` to specify a single target";
#[test]
fn build_lib_for_foo() {
let p = project("foo")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/main.rs",
r#"
fn main() {}
2018-03-14 15:17:44 +00:00
"#,
)
.file("src/lib.rs", r#" "#)
.build();
2018-03-14 15:17:44 +00:00
assert_that(
p.cargo("rustc").arg("--lib").arg("-v"),
execs().with_status(0).with_stderr(format!(
"\
[COMPILING] foo v0.0.1 ({url})
2017-01-03 21:22:58 +00:00
[RUNNING] `rustc --crate-name foo src[/]lib.rs --crate-type lib \
--emit=dep-info,link -C debuginfo=2 \
-C metadata=[..] \
--out-dir [..] \
2016-11-06 04:53:21 +00:00
-L dependency={dir}[/]target[/]debug[/]deps`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
dir = p.root().display(),
url = p.url()
)),
);
}
#[test]
fn lib() {
let p = project("foo")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/main.rs",
r#"
fn main() {}
2018-03-14 15:17:44 +00:00
"#,
)
.file("src/lib.rs", r#" "#)
.build();
2018-03-14 15:17:44 +00:00
assert_that(
p.cargo("rustc")
.arg("--lib")
.arg("-v")
.arg("--")
.arg("-C")
.arg("debug-assertions=off"),
execs().with_status(0).with_stderr(format!(
"\
[COMPILING] foo v0.0.1 ({url})
2017-01-03 21:22:58 +00:00
[RUNNING] `rustc --crate-name foo src[/]lib.rs --crate-type lib \
--emit=dep-info,link -C debuginfo=2 \
-C debug-assertions=off \
-C metadata=[..] \
--out-dir [..] \
2016-11-06 04:53:21 +00:00
-L dependency={dir}[/]target[/]debug[/]deps`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
dir = p.root().display(),
url = p.url()
)),
)
}
#[test]
fn build_main_and_allow_unstable_options() {
let p = project("foo")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/main.rs",
r#"
fn main() {}
2018-03-14 15:17:44 +00:00
"#,
)
.file("src/lib.rs", r#" "#)
.build();
2018-03-14 15:17:44 +00:00
assert_that(
p.cargo("rustc")
.arg("-v")
.arg("--bin")
.arg("foo")
.arg("--")
.arg("-C")
.arg("debug-assertions"),
execs().with_status(0).with_stderr(&format!(
"\
[COMPILING] {name} v{version} ({url})
2017-01-03 21:22:58 +00:00
[RUNNING] `rustc --crate-name {name} src[/]lib.rs --crate-type lib \
--emit=dep-info,link -C debuginfo=2 \
-C metadata=[..] \
--out-dir [..] \
2016-11-06 04:53:21 +00:00
-L dependency={dir}[/]target[/]debug[/]deps`
2017-01-03 21:22:58 +00:00
[RUNNING] `rustc --crate-name {name} src[/]main.rs --crate-type bin \
--emit=dep-info,link -C debuginfo=2 \
-C debug-assertions \
-C metadata=[..] \
--out-dir [..] \
2016-11-06 04:53:21 +00:00
-L dependency={dir}[/]target[/]debug[/]deps \
--extern {name}={dir}[/]target[/]debug[/]deps[/]lib{name}-[..].rlib`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2016-11-06 04:53:21 +00:00
",
2018-03-14 15:17:44 +00:00
dir = p.root().display(),
url = p.url(),
name = "foo",
version = "0.0.1"
)),
);
}
#[test]
fn fails_when_trying_to_build_main_and_lib_with_args() {
let p = project("foo")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/main.rs",
r#"
fn main() {}
2018-03-14 15:17:44 +00:00
"#,
)
.file("src/lib.rs", r#" "#)
.build();
2018-03-14 15:17:44 +00:00
assert_that(
p.cargo("rustc")
.arg("-v")
.arg("--")
.arg("-C")
.arg("debug-assertions"),
execs().with_status(101).with_stderr(CARGO_RUSTC_ERROR),
);
}
#[test]
fn build_with_args_to_one_of_multiple_binaries() {
let p = project("foo")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/bin/foo.rs",
r#"
fn main() {}
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/bin/bar.rs",
r#"
fn main() {}
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/bin/baz.rs",
r#"
fn main() {}
2018-03-14 15:17:44 +00:00
"#,
)
.file("src/lib.rs", r#" "#)
.build();
2018-03-14 15:17:44 +00:00
assert_that(
p.cargo("rustc")
.arg("-v")
.arg("--bin")
.arg("bar")
.arg("--")
.arg("-C")
.arg("debug-assertions"),
execs().with_status(0).with_stderr(format!(
"\
[COMPILING] foo v0.0.1 ({url})
[RUNNING] `rustc --crate-name foo src[/]lib.rs --crate-type lib --emit=dep-info,link \
-C debuginfo=2 -C metadata=[..] \
--out-dir [..]`
[RUNNING] `rustc --crate-name bar src[/]bin[/]bar.rs --crate-type bin --emit=dep-info,link \
-C debuginfo=2 -C debug-assertions [..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
url = p.url()
)),
);
}
#[test]
fn fails_with_args_to_all_binaries() {
let p = project("foo")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/bin/foo.rs",
r#"
fn main() {}
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/bin/bar.rs",
r#"
fn main() {}
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/bin/baz.rs",
r#"
fn main() {}
2018-03-14 15:17:44 +00:00
"#,
)
.file("src/lib.rs", r#" "#)
.build();
2018-03-14 15:17:44 +00:00
assert_that(
p.cargo("rustc")
.arg("-v")
.arg("--")
.arg("-C")
.arg("debug-assertions"),
execs().with_status(101).with_stderr(CARGO_RUSTC_ERROR),
);
}
#[test]
fn build_with_args_to_one_of_multiple_tests() {
let p = project("foo")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
2018-03-14 15:17:44 +00:00
"#,
)
.file("tests/foo.rs", r#" "#)
.file("tests/bar.rs", r#" "#)
.file("tests/baz.rs", r#" "#)
.file("src/lib.rs", r#" "#)
.build();
2018-03-14 15:17:44 +00:00
assert_that(
p.cargo("rustc")
.arg("-v")
.arg("--test")
.arg("bar")
.arg("--")
.arg("-C")
.arg("debug-assertions"),
execs().with_status(0).with_stderr(format!(
"\
[COMPILING] foo v0.0.1 ({url})
[RUNNING] `rustc --crate-name foo src[/]lib.rs --crate-type lib --emit=dep-info,link \
-C debuginfo=2 -C metadata=[..] \
--out-dir [..]`
[RUNNING] `rustc --crate-name bar tests[/]bar.rs --emit=dep-info,link -C debuginfo=2 \
-C debug-assertions [..]--test[..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
url = p.url()
)),
);
}
#[test]
fn build_foo_with_bar_dependency() {
let foo = project("foo")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
[dependencies.bar]
path = "../bar"
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/main.rs",
r#"
extern crate bar;
fn main() {
bar::baz()
}
2018-03-14 15:17:44 +00:00
"#,
)
.build();
let _bar = project("bar")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "bar"
version = "0.1.0"
authors = []
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/lib.rs",
r#"
pub fn baz() {}
2018-03-14 15:17:44 +00:00
"#,
)
.build();
2018-03-14 15:17:44 +00:00
assert_that(
foo.cargo("rustc")
.arg("-v")
.arg("--")
.arg("-C")
.arg("debug-assertions"),
execs().with_status(0).with_stderr(format!(
"\
[COMPILING] bar v0.1.0 ([..])
[RUNNING] `[..] -C debuginfo=2 [..]`
[COMPILING] foo v0.0.1 ({url})
[RUNNING] `[..] -C debuginfo=2 -C debug-assertions [..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
url = foo.url()
)),
);
}
#[test]
fn build_only_bar_dependency() {
let foo = project("foo")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
[dependencies.bar]
path = "../bar"
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/main.rs",
r#"
extern crate bar;
fn main() {
bar::baz()
}
2018-03-14 15:17:44 +00:00
"#,
)
.build();
let _bar = project("bar")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "bar"
version = "0.1.0"
authors = []
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/lib.rs",
r#"
pub fn baz() {}
2018-03-14 15:17:44 +00:00
"#,
)
.build();
2018-03-14 15:17:44 +00:00
assert_that(
foo.cargo("rustc")
.arg("-v")
.arg("-p")
.arg("bar")
.arg("--")
.arg("-C")
.arg("debug-assertions"),
execs().with_status(0).with_stderr(
"\
[COMPILING] bar v0.1.0 ([..])
2016-11-30 20:05:23 +00:00
[RUNNING] `rustc --crate-name bar [..] --crate-type lib [..] -C debug-assertions [..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
),
);
}
#[test]
fn fail_with_multiple_packages() {
let foo = project("foo")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
[dependencies.bar]
path = "../bar"
[dependencies.baz]
path = "../baz"
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/main.rs",
r#"
fn main() {}
2018-03-14 15:17:44 +00:00
"#,
)
.build();
let _bar = project("bar")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "bar"
version = "0.1.0"
authors = []
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/main.rs",
r#"
fn main() {
if cfg!(flag = "1") { println!("Yeah from bar!"); }
}
2018-03-14 15:17:44 +00:00
"#,
)
.build();
let _baz = project("baz")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "baz"
version = "0.1.0"
authors = []
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/main.rs",
r#"
fn main() {
if cfg!(flag = "1") { println!("Yeah from baz!"); }
}
2018-03-14 15:17:44 +00:00
"#,
)
.build();
2018-03-14 15:17:44 +00:00
assert_that(
foo.cargo("rustc")
.arg("-v")
.arg("-p")
.arg("bar")
.arg("-p")
.arg("baz"),
execs().with_status(1).with_stderr_contains(
"\
error: The argument '--package <SPEC>' was provided more than once, \
but cannot be used multiple times
2018-03-14 15:17:44 +00:00
",
),
);
}
#[test]
fn rustc_with_other_profile() {
let p = project("foo")
2018-03-14 15:17:44 +00:00
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
[dev-dependencies]
a = { path = "a" }
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"src/main.rs",
r#"
#[cfg(test)] extern crate a;
#[test]
fn foo() {}
2018-03-14 15:17:44 +00:00
"#,
)
.file(
"a/Cargo.toml",
r#"
[package]
name = "a"
version = "0.1.0"
authors = []
2018-03-14 15:17:44 +00:00
"#,
)
.file("a/src/lib.rs", "")
.build();
2018-03-14 15:17:44 +00:00
assert_that(
p.cargo("rustc").arg("--profile").arg("test"),
execs().with_status(0),
);
}