Skip to content

Commit

Permalink
Rework tests to avoid using -vv
Browse files Browse the repository at this point in the history
Environment variables are represented differently in differents OSes in
the output.

Add tests checking if the variable is set instead.
  • Loading branch information
ebroto committed Oct 14, 2020
1 parent c12aed2 commit fb02ade
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 92 deletions.
99 changes: 42 additions & 57 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5182,80 +5182,65 @@ fn build_script_o0_default_even_with_release() {

#[cargo_test]
fn primary_package_env_var() {
// "foo" depends on "bar" (local) which depends on "baz" (git) which depends on "qux" (registry)
// Test that CARGO_PRIMARY_PACKAGE is enabled only for "foo".
// Test that CARGO_PRIMARY_PACKAGE is enabled only for "foo" and not for any dependency.

Package::new("qux", "1.0.0")
.file("src/lib.rs", "")
let is_primary_package = r#"
pub fn is_primary_package() -> bool {{
option_env!("CARGO_PRIMARY_PACKAGE").is_some()
}}
"#;

Package::new("qux", "0.1.0")
.file("src/lib.rs", is_primary_package)
.publish();

let baz = git::new("baz", |project| {
project
.file(
"Cargo.toml",
r#"
[package]
name = "baz"
version = "1.0.0"
[dependencies]
qux = "1.0"
"#,
)
.file("src/lib.rs", "extern crate qux;")
.file("Cargo.toml", &basic_manifest("baz", "0.1.0"))
.file("src/lib.rs", is_primary_package)
});

let foo = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
&format!(
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
bar = {path = "bar"}
"#,
[dependencies]
bar = {{ path = "bar" }}
baz = {{ git = '{}' }}
qux = "0.1"
"#,
baz.url()
),
)
.file("src/lib.rs", "extern crate bar;")
.file(
"bar/Cargo.toml",
"src/lib.rs",
&format!(
r#"
[package]
name = "bar"
version = "1.0.0"
[dependencies]
baz = {{ git = '{}' }}
"#,
baz.url()
extern crate bar;
extern crate baz;
extern crate qux;
{}
#[test]
fn verify_primary_package() {{
assert!(!bar::is_primary_package());
assert!(!baz::is_primary_package());
assert!(!qux::is_primary_package());
assert!(is_primary_package());
}}
"#,
is_primary_package
),
)
.file("bar/src/lib.rs", "extern crate baz;")
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
.file("bar/src/lib.rs", is_primary_package)
.build();

foo.cargo("build -vv")
.with_stderr_contains("[COMPILING] qux[..]")
.with_stderr_line_without(
&["[RUNNING]", "CARGO_CRATE_NAME=qux"],
&["CARGO_PRIMARY_PACKAGE=1"],
)
.with_stderr_contains("[COMPILING] baz[..]")
.with_stderr_line_without(
&["[RUNNING]", "CARGO_CRATE_NAME=baz"],
&["CARGO_PRIMARY_PACKAGE=1"],
)
.with_stderr_contains("[COMPILING] bar[..]")
.with_stderr_line_without(
&["[RUNNING]", "CARGO_CRATE_NAME=bar"],
&["CARGO_PRIMARY_PACKAGE=1"],
)
.with_stderr_contains(
"\
[COMPILING] foo[..]
[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
)
.run();
foo.cargo("test").run();
}
58 changes: 23 additions & 35 deletions tests/testsuite/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2322,6 +2322,13 @@ Caused by:

#[cargo_test]
fn simple_primary_package_env_var() {
let is_primary_package = r#"
#[test]
fn verify_primary_package() {{
assert!(option_env!("CARGO_PRIMARY_PACKAGE").is_some());
}}
"#;

let p = project()
.file(
"Cargo.toml",
Expand All @@ -2335,7 +2342,7 @@ fn simple_primary_package_env_var() {
members = ["bar"]
"#,
)
.file("src/main.rs", "fn main() {}")
.file("src/lib.rs", is_primary_package)
.file(
"bar/Cargo.toml",
r#"
Expand All @@ -2346,37 +2353,29 @@ fn simple_primary_package_env_var() {
workspace = ".."
"#,
)
.file("bar/src/main.rs", "fn main() {}");
.file("bar/src/lib.rs", is_primary_package);
let p = p.build();

p.cargo("build -vv")
.with_stderr_contains(
"[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]",
)
.run();
p.cargo("test").run();

// Again, this time selecting a specific crate
p.cargo("clean").run();
p.cargo("build -vv -p bar")
.with_stderr_contains(
"[RUNNING] [..] CARGO_CRATE_NAME=bar [..] CARGO_PRIMARY_PACKAGE=1 [..]",
)
.run();
p.cargo("test -p bar").run();

// Again, this time selecting all crates
p.cargo("clean").run();
p.cargo("build -vv --all")
.with_stderr_contains(
"[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]",
)
.with_stderr_contains(
"[RUNNING] [..] CARGO_CRATE_NAME=bar [..] CARGO_PRIMARY_PACKAGE=1 [..]",
)
.run();
p.cargo("test --all").run();
}

#[cargo_test]
fn virtual_primary_package_env_var() {
let is_primary_package = r#"
#[test]
fn verify_primary_package() {{
assert!(option_env!("CARGO_PRIMARY_PACKAGE").is_some());
}}
"#;

let p = project()
.file(
"Cargo.toml",
Expand All @@ -2386,25 +2385,14 @@ fn virtual_primary_package_env_var() {
"#,
)
.file("foo/Cargo.toml", &basic_manifest("foo", "0.1.0"))
.file("foo/src/main.rs", "fn main() {}")
.file("foo/src/lib.rs", is_primary_package)
.file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
.file("bar/src/main.rs", "fn main() {}");
.file("bar/src/lib.rs", is_primary_package);
let p = p.build();

p.cargo("build -vv")
.with_stderr_contains(
"[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]",
)
.with_stderr_contains(
"[RUNNING] [..] CARGO_CRATE_NAME=bar [..] CARGO_PRIMARY_PACKAGE=1 [..]",
)
.run();
p.cargo("test").run();

// Again, this time selecting a specific crate
p.cargo("clean").run();
p.cargo("build -vv -p foo")
.with_stderr_contains(
"[RUNNING] [..] CARGO_CRATE_NAME=foo [..] CARGO_PRIMARY_PACKAGE=1 [..]",
)
.run();
p.cargo("test -p foo").run();
}

0 comments on commit fb02ade

Please sign in to comment.