Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: migrate build_script_env to snapbox #14044

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 125 additions & 68 deletions tests/testsuite/build_script_env.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! Tests for build.rs rerun-if-env-changed and rustc-env

#![allow(deprecated)]

use cargo_test_support::basic_manifest;
use cargo_test_support::project;
use cargo_test_support::sleep_ms;
use cargo_test_support::str;

#[cargo_test]
fn rerun_if_env_changes() {
Expand All @@ -21,42 +20,42 @@ fn rerun_if_env_changes() {
.build();

p.cargo("check")
.with_stderr(
"\
[COMPILING] foo v0.0.1 ([..])
[FINISHED] [..]
",
)
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
p.cargo("check")
.env("FOO", "bar")
.with_stderr(
"\
[COMPILING] foo v0.0.1 ([..])
[FINISHED] [..]
",
)
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
p.cargo("check")
.env("FOO", "baz")
.with_stderr(
"\
[COMPILING] foo v0.0.1 ([..])
[FINISHED] [..]
",
)
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
p.cargo("check")
.env("FOO", "baz")
.with_stderr("[FINISHED] [..]")
.with_stderr_data(str![[r#"
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();

p.cargo("check")
.with_stderr(
"\
[COMPILING] foo v0.0.1 ([..])
[FINISHED] [..]
",
)
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
}

Expand All @@ -77,36 +76,36 @@ fn rerun_if_env_or_file_changes() {
.build();

p.cargo("check")
.with_stderr(
"\
[COMPILING] foo v0.0.1 ([..])
[FINISHED] [..]
",
)
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
p.cargo("check")
.env("FOO", "bar")
.with_stderr(
"\
[COMPILING] foo v0.0.1 ([..])
[FINISHED] [..]
",
)
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
p.cargo("check")
.env("FOO", "bar")
.with_stderr("[FINISHED] [..]")
.with_stderr_data(str![[r#"
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
sleep_ms(1000);
p.change_file("foo", "// modified");
p.cargo("check")
.env("FOO", "bar")
.with_stderr(
"\
[COMPILING] foo v0.0.1 ([..])
[FINISHED] [..]
",
)
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
}

Expand All @@ -127,10 +126,13 @@ fn rustc_bootstrap() {
.build();
// RUSTC_BOOTSTRAP unset on stable should error
p.cargo("check")
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
.with_stderr_contains(
"help: [..] set the environment variable `RUSTC_BOOTSTRAP=has_dashes` [..]",
)
.with_stderr_data(str![[r#"
[COMPILING] has-dashes v0.0.1 ([ROOT]/foo)
[ERROR] Cannot set `RUSTC_BOOTSTRAP=1` from build script of `has-dashes v0.0.1 ([ROOT]/foo)`.
[NOTE] Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.
[HELP] If you're sure you want to do this in your project, set the environment variable `RUSTC_BOOTSTRAP=has_dashes` before running cargo instead.
"#]])
.with_status(101)
.run();
// nightly should warn whether or not RUSTC_BOOTSTRAP is set
Expand All @@ -139,20 +141,33 @@ fn rustc_bootstrap() {
// NOTE: uses RUSTC_BOOTSTRAP so it will be propagated to rustc
// (this matters when tests are being run with a beta or stable cargo)
.env("RUSTC_BOOTSTRAP", "1")
.with_stderr_contains("warning: has-dashes@0.0.1: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
.with_stderr_data(str![[r#"
[COMPILING] has-dashes v0.0.1 ([ROOT]/foo)
[WARNING] has-dashes@0.0.1: Cannot set `RUSTC_BOOTSTRAP=1` from build script of `has-dashes v0.0.1 ([ROOT]/foo)`.
[NOTE] Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
// RUSTC_BOOTSTRAP set to the name of the library should warn
p.cargo("check")
.env("RUSTC_BOOTSTRAP", "has_dashes")
.with_stderr_contains("warning: has-dashes@0.0.1: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
.with_stderr_data(str![[r#"
[WARNING] has-dashes@0.0.1: Cannot set `RUSTC_BOOTSTRAP=1` from build script of `has-dashes v0.0.1 ([ROOT]/foo)`.
[NOTE] Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
// RUSTC_BOOTSTRAP set to some random value should error
p.cargo("check")
.env("RUSTC_BOOTSTRAP", "bar")
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
.with_stderr_contains(
"help: [..] set the environment variable `RUSTC_BOOTSTRAP=has_dashes` [..]",
)
.with_stderr_data(str![[r#"
[ERROR] Cannot set `RUSTC_BOOTSTRAP=1` from build script of `has-dashes v0.0.1 ([ROOT]/foo)`.
[NOTE] Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.
[HELP] If you're sure you want to do this in your project, set the environment variable `RUSTC_BOOTSTRAP=has_dashes` before running cargo instead.
"#]])
.with_status(101)
.run();

Expand All @@ -171,13 +186,23 @@ fn rustc_bootstrap() {
// NOTE: uses RUSTC_BOOTSTRAP so it will be propagated to rustc
// (this matters when tests are being run with a beta or stable cargo)
.env("RUSTC_BOOTSTRAP", "1")
.with_stderr_contains("warning: foo@0.0.1: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[WARNING] foo@0.0.1: Cannot set `RUSTC_BOOTSTRAP=1` from build script of `foo v0.0.1 ([ROOT]/foo)`.
[NOTE] Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
// RUSTC_BOOTSTRAP conditionally set when there's no library should error (regardless of the value)
p.cargo("check")
.env("RUSTC_BOOTSTRAP", "foo")
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
.with_stderr_contains("help: [..] set the environment variable `RUSTC_BOOTSTRAP=1` [..]")
.with_stderr_data(str![[r#"
[ERROR] Cannot set `RUSTC_BOOTSTRAP=1` from build script of `foo v0.0.1 ([ROOT]/foo)`.
[NOTE] Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.
[HELP] If you're sure you want to do this in your project, set the environment variable `RUSTC_BOOTSTRAP=1` before running cargo instead.
"#]])
.with_status(101)
.run();
}
Expand All @@ -194,7 +219,12 @@ fn build_script_env_verbose() {
.build();

p.cargo("check -vv")
.with_stderr_contains("[RUNNING] `[..]CARGO=[..]build-script-build`")
.with_stderr_data(
"\
...
[RUNNING] `[..]CARGO=[..]build-script-build`
...",
)
.run();
}

Expand Down Expand Up @@ -227,8 +257,14 @@ fn build_script_sees_cfg_target_feature() {
.build();

p.cargo("check -vv")
.with_stderr_contains("[foo 0.0.1] CARGO_CFG_TARGET_FEATURE=[..]sse4.2[..]")
.with_stderr_contains("[..]-Ctarget-feature=[..]+sse4.2[..]")
.with_stderr_data(
"\
...
[foo 0.0.1] CARGO_CFG_TARGET_FEATURE=[..]sse4.2[..]
...
[..]-Ctarget-feature=[..]+sse4.2[..]
...",
)
.run();
}
}
Expand Down Expand Up @@ -258,9 +294,15 @@ fn cfg_paradox() {
.build();

p.cargo("check -vv")
.with_stderr_contains("[WARNING] non-trivial mutual dependency between target-specific configuration and RUSTFLAGS")
.with_stderr_contains("[foo 0.0.1] cfg!(bertrand)=true")
.with_stderr_contains("[..]--cfg=bertrand[..]")
.with_stderr_data(
"\
[WARNING] non-trivial mutual dependency between target-specific configuration and RUSTFLAGS
...
[foo 0.0.1] cfg!(bertrand)=true
...
[..]--cfg=bertrand[..]
...",
)
.run();
}

Expand Down Expand Up @@ -318,10 +360,25 @@ fn rustc_cfg_with_and_without_value() {

let mut check = p.cargo("check -vv");
#[cfg(target_has_atomic = "64")]
check.with_stderr_contains("[foo 0.0.1] CARGO_CFG_TARGET_HAS_ATOMIC=Ok(\"[..]64[..]\")");
check.with_stderr_data(
"\
...
[foo 0.0.1] CARGO_CFG_TARGET_HAS_ATOMIC=Ok(\"[..]64[..]\")
...",
);
#[cfg(windows)]
check.with_stderr_contains("[foo 0.0.1] CARGO_CFG_WINDOWS=Ok(\"\")");
check.with_stderr_data(
"\
...
[foo 0.0.1] CARGO_CFG_WINDOWS=Ok(\"\")
...",
);
#[cfg(unix)]
check.with_stderr_contains("[foo 0.0.1] CARGO_CFG_UNIX=Ok(\"\")");
check.with_stderr_data(
"\
...
[foo 0.0.1] CARGO_CFG_UNIX=Ok(\"\")
...",
);
check.run();
}