cargo/tests/testsuite/rustc.rs

442 lines
11 KiB
Rust
Raw Normal View History

2018-12-06 19:17:36 +00:00
use crate::support::{basic_bin_manifest, basic_lib_manifest, basic_manifest, project};
const CARGO_RUSTC_ERROR: &str =
2018-03-14 15:17:44 +00:00
"[ERROR] extra arguments to `rustc` can only be passed to one target, consider filtering
2019-02-03 04:01:23 +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()
.file("src/main.rs", "fn main() {}")
.file("src/lib.rs", r#" "#)
.build();
p.cargo("rustc --lib -v")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[COMPILING] foo v0.0.1 ([CWD])
[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \
--emit=[..]link -C debuginfo=2 \
-C metadata=[..] \
--out-dir [..] \
-L dependency=[CWD]/target/debug/deps`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[test]
fn lib() {
let p = project()
.file("src/main.rs", "fn main() {}")
.file("src/lib.rs", r#" "#)
.build();
p.cargo("rustc --lib -v -- -C debug-assertions=off")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[COMPILING] foo v0.0.1 ([CWD])
[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib \
--emit=[..]link -C debuginfo=2 \
-C debug-assertions=off \
-C metadata=[..] \
--out-dir [..] \
-L dependency=[CWD]/target/debug/deps`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[test]
fn build_main_and_allow_unstable_options() {
let p = project()
.file("src/main.rs", "fn main() {}")
.file("src/lib.rs", r#" "#)
.build();
p.cargo("rustc -v --bin foo -- -C debug-assertions")
.with_stderr(format!(
2018-03-14 15:17:44 +00:00
"\
[COMPILING] {name} v{version} ([CWD])
[RUNNING] `rustc --crate-name {name} src/lib.rs --color never --crate-type lib \
--emit=[..]link -C debuginfo=2 \
-C metadata=[..] \
--out-dir [..] \
-L dependency=[CWD]/target/debug/deps`
[RUNNING] `rustc --crate-name {name} src/main.rs --color never --crate-type bin \
--emit=[..]link -C debuginfo=2 \
-C debug-assertions \
-C metadata=[..] \
--out-dir [..] \
-L dependency=[CWD]/target/debug/deps \
--extern {name}=[CWD]/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
name = "foo",
version = "0.0.1"
2018-12-08 11:19:47 +00:00
))
.run();
}
#[test]
fn fails_when_trying_to_build_main_and_lib_with_args() {
let p = project()
.file("src/main.rs", "fn main() {}")
.file("src/lib.rs", r#" "#)
.build();
p.cargo("rustc -v -- -C debug-assertions")
.with_status(101)
.with_stderr(CARGO_RUSTC_ERROR)
.run();
}
#[test]
fn build_with_args_to_one_of_multiple_binaries() {
let p = project()
.file("src/bin/foo.rs", "fn main() {}")
.file("src/bin/bar.rs", "fn main() {}")
.file("src/bin/baz.rs", "fn main() {}")
.file("src/lib.rs", r#" "#)
.build();
p.cargo("rustc -v --bin bar -- -C debug-assertions")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[COMPILING] foo v0.0.1 ([CWD])
[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib --emit=[..]link \
-C debuginfo=2 -C metadata=[..] \
--out-dir [..]`
[RUNNING] `rustc --crate-name bar src/bin/bar.rs --color never --crate-type bin --emit=[..]link \
-C debuginfo=2 -C debug-assertions [..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
).run();
}
#[test]
fn fails_with_args_to_all_binaries() {
let p = project()
.file("src/bin/foo.rs", "fn main() {}")
.file("src/bin/bar.rs", "fn main() {}")
.file("src/bin/baz.rs", "fn main() {}")
.file("src/lib.rs", r#" "#)
.build();
p.cargo("rustc -v -- -C debug-assertions")
.with_status(101)
.with_stderr(CARGO_RUSTC_ERROR)
.run();
}
#[test]
fn build_with_args_to_one_of_multiple_tests() {
let p = project()
.file("tests/foo.rs", r#" "#)
.file("tests/bar.rs", r#" "#)
.file("tests/baz.rs", r#" "#)
.file("src/lib.rs", r#" "#)
.build();
p.cargo("rustc -v --test bar -- -C debug-assertions")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[COMPILING] foo v0.0.1 ([CWD])
[RUNNING] `rustc --crate-name foo src/lib.rs --color never --crate-type lib --emit=[..]link \
-C debuginfo=2 -C metadata=[..] \
--out-dir [..]`
[RUNNING] `rustc --crate-name bar tests/bar.rs --color never --emit=[..]link -C debuginfo=2 \
-C debug-assertions [..]--test[..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[test]
fn build_foo_with_bar_dependency() {
let foo = project()
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
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "extern crate bar; fn main() { bar::baz() }")
.build();
let _bar = project()
.at("bar")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
.file("src/lib.rs", "pub fn baz() {}")
.build();
foo.cargo("rustc -v -- -C debug-assertions")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[COMPILING] bar v0.1.0 ([..])
[RUNNING] `[..] -C debuginfo=2 [..]`
[COMPILING] foo v0.0.1 ([CWD])
[RUNNING] `[..] -C debuginfo=2 -C debug-assertions [..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[test]
fn build_only_bar_dependency() {
let foo = project()
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
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "extern crate bar; fn main() { bar::baz() }")
.build();
let _bar = project()
.at("bar")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
.file("src/lib.rs", "pub fn baz() {}")
.build();
foo.cargo("rustc -v -p bar -- -C debug-assertions")
.with_stderr(
2018-03-14 15:17:44 +00:00
"\
[COMPILING] bar v0.1.0 ([..])
[RUNNING] `rustc --crate-name bar [..] --color never --crate-type lib [..] -C debug-assertions [..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
2018-03-15 15:20:15 +00:00
#[test]
fn targets_selected_default() {
let p = project().file("src/main.rs", "fn main() {}").build();
p.cargo("rustc -v")
2018-03-15 15:20:15 +00:00
// bin
2018-12-08 11:19:47 +00:00
.with_stderr_contains(
"\
[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
--emit=[..]link[..]",
2018-12-08 11:19:47 +00:00
)
2018-03-15 15:20:15 +00:00
// bench
2018-12-08 11:19:47 +00:00
.with_stderr_does_not_contain(
"\
[RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=[..]link \
2018-12-08 11:19:47 +00:00
-C opt-level=3 --test [..]",
)
2018-03-15 15:20:15 +00:00
// unit test
2018-12-08 11:19:47 +00:00
.with_stderr_does_not_contain(
"\
[RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=[..]link \
2018-12-08 11:19:47 +00:00
-C debuginfo=2 --test [..]",
)
.run();
}
#[test]
fn targets_selected_all() {
let p = project().file("src/main.rs", "fn main() {}").build();
p.cargo("rustc -v --all-targets")
2018-03-15 15:20:15 +00:00
// bin
2018-12-08 11:19:47 +00:00
.with_stderr_contains(
"\
[RUNNING] `rustc --crate-name foo src/main.rs --color never --crate-type bin \
--emit=[..]link[..]",
2018-12-08 11:19:47 +00:00
)
2018-03-15 15:20:15 +00:00
// unit test
2018-12-08 11:19:47 +00:00
.with_stderr_contains(
"\
[RUNNING] `rustc --crate-name foo src/main.rs --color never --emit=[..]link \
2018-12-08 11:19:47 +00:00
-C debuginfo=2 --test [..]",
)
.run();
2018-03-15 15:20:15 +00:00
}
#[test]
fn fail_with_multiple_packages() {
let foo = project()
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
"#,
2018-12-08 11:19:47 +00:00
)
.file("src/main.rs", "fn main() {}")
.build();
let _bar = project()
.at("bar")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
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
"#,
2018-12-08 11:19:47 +00:00
)
.build();
let _baz = project()
.at("baz")
2018-07-24 22:35:01 +00:00
.file("Cargo.toml", &basic_manifest("baz", "0.1.0"))
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
"#,
2018-12-08 11:19:47 +00:00
)
.build();
foo.cargo("rustc -v -p bar -p baz")
.with_status(1)
.with_stderr_contains(
2018-03-14 15:17:44 +00:00
"\
error: The argument '--package <SPEC>' was provided more than once, \
but cannot be used multiple times
2018-03-14 15:17:44 +00:00
",
2018-12-08 11:19:47 +00:00
)
.run();
}
#[test]
fn rustc_with_other_profile() {
let p = project()
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
"#,
2018-12-08 11:19:47 +00:00
)
.file(
2018-03-14 15:17:44 +00:00
"src/main.rs",
r#"
#[cfg(test)] extern crate a;
#[test]
fn foo() {}
2018-03-14 15:17:44 +00:00
"#,
2018-12-08 11:19:47 +00:00
)
.file("a/Cargo.toml", &basic_manifest("a", "0.1.0"))
.file("a/src/lib.rs", "")
.build();
p.cargo("rustc --profile test").run();
}
2018-04-22 00:21:42 +00:00
#[test]
fn rustc_fingerprint() {
// Verify that the fingerprint includes the rustc args.
let p = project()
2018-04-22 00:21:42 +00:00
.file("Cargo.toml", &basic_lib_manifest("foo"))
.file("src/lib.rs", "")
.build();
p.cargo("rustc -v -- -C debug-assertions")
.with_stderr(
2018-04-22 00:21:42 +00:00
"\
[COMPILING] foo [..]
[RUNNING] `rustc [..]-C debug-assertions [..]
[FINISHED] [..]
",
2018-12-08 11:19:47 +00:00
)
.run();
2018-04-22 00:21:42 +00:00
p.cargo("rustc -v -- -C debug-assertions")
.with_stderr(
2018-04-22 00:21:42 +00:00
"\
[FRESH] foo [..]
[FINISHED] [..]
",
2018-12-08 11:19:47 +00:00
)
.run();
p.cargo("rustc -v")
.with_stderr_does_not_contain("-C debug-assertions")
.with_stderr(
"\
2018-04-22 00:21:42 +00:00
[COMPILING] foo [..]
[RUNNING] `rustc [..]
[FINISHED] [..]
",
2018-12-08 11:19:47 +00:00
)
.run();
2018-04-22 00:21:42 +00:00
p.cargo("rustc -v")
.with_stderr(
2018-04-22 00:21:42 +00:00
"\
[FRESH] foo [..]
[FINISHED] [..]
",
2018-12-08 11:19:47 +00:00
)
.run();
2018-04-22 00:21:42 +00:00
}
#[test]
fn rustc_test_with_implicit_bin() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file(
"src/main.rs",
r#"
#[cfg(foo)]
fn f() { compile_fail!("Foo shouldn't be set."); }
fn main() {}
"#,
2018-12-08 11:19:47 +00:00
)
.file(
"tests/test1.rs",
r#"
#[cfg(not(foo))]
fn f() { compile_fail!("Foo should be set."); } "#,
2018-12-08 11:19:47 +00:00
)
.build();
p.cargo("rustc --test test1 -v -- --cfg foo")
.with_stderr_contains(
"\
2018-08-02 09:18:48 +00:00
[RUNNING] `rustc --crate-name test1 tests/test1.rs [..] --cfg foo [..]
",
2018-12-08 11:19:47 +00:00
)
.with_stderr_contains(
"\
2018-08-02 09:18:48 +00:00
[RUNNING] `rustc --crate-name foo src/main.rs [..]
",
2018-12-08 11:19:47 +00:00
)
.run();
}