test: migrate build_script_env to snapbox

This commit is contained in:
heisen-li 2024-06-12 11:12:18 +08:00
parent 78d266d1e8
commit 2d10bd5a9f

View File

@ -1,10 +1,9 @@
//! Tests for build.rs rerun-if-env-changed and rustc-env //! Tests for build.rs rerun-if-env-changed and rustc-env
#![allow(deprecated)]
use cargo_test_support::basic_manifest; use cargo_test_support::basic_manifest;
use cargo_test_support::project; use cargo_test_support::project;
use cargo_test_support::sleep_ms; use cargo_test_support::sleep_ms;
use cargo_test_support::str;
#[cargo_test] #[cargo_test]
fn rerun_if_env_changes() { fn rerun_if_env_changes() {
@ -21,42 +20,42 @@ fn rerun_if_env_changes() {
.build(); .build();
p.cargo("check") p.cargo("check")
.with_stderr( .with_stderr_data(str![[r#"
"\ [COMPILING] foo v0.0.1 ([ROOT]/foo)
[COMPILING] foo v0.0.1 ([..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[FINISHED] [..]
", "#]])
)
.run(); .run();
p.cargo("check") p.cargo("check")
.env("FOO", "bar") .env("FOO", "bar")
.with_stderr( .with_stderr_data(str![[r#"
"\ [COMPILING] foo v0.0.1 ([ROOT]/foo)
[COMPILING] foo v0.0.1 ([..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[FINISHED] [..]
", "#]])
)
.run(); .run();
p.cargo("check") p.cargo("check")
.env("FOO", "baz") .env("FOO", "baz")
.with_stderr( .with_stderr_data(str![[r#"
"\ [COMPILING] foo v0.0.1 ([ROOT]/foo)
[COMPILING] foo v0.0.1 ([..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[FINISHED] [..]
", "#]])
)
.run(); .run();
p.cargo("check") p.cargo("check")
.env("FOO", "baz") .env("FOO", "baz")
.with_stderr("[FINISHED] [..]") .with_stderr_data(str![[r#"
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run(); .run();
p.cargo("check") p.cargo("check")
.with_stderr( .with_stderr_data(str![[r#"
"\ [COMPILING] foo v0.0.1 ([ROOT]/foo)
[COMPILING] foo v0.0.1 ([..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[FINISHED] [..]
", "#]])
)
.run(); .run();
} }
@ -77,36 +76,36 @@ fn rerun_if_env_or_file_changes() {
.build(); .build();
p.cargo("check") p.cargo("check")
.with_stderr( .with_stderr_data(str![[r#"
"\ [COMPILING] foo v0.0.1 ([ROOT]/foo)
[COMPILING] foo v0.0.1 ([..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[FINISHED] [..]
", "#]])
)
.run(); .run();
p.cargo("check") p.cargo("check")
.env("FOO", "bar") .env("FOO", "bar")
.with_stderr( .with_stderr_data(str![[r#"
"\ [COMPILING] foo v0.0.1 ([ROOT]/foo)
[COMPILING] foo v0.0.1 ([..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[FINISHED] [..]
", "#]])
)
.run(); .run();
p.cargo("check") p.cargo("check")
.env("FOO", "bar") .env("FOO", "bar")
.with_stderr("[FINISHED] [..]") .with_stderr_data(str![[r#"
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run(); .run();
sleep_ms(1000); sleep_ms(1000);
p.change_file("foo", "// modified"); p.change_file("foo", "// modified");
p.cargo("check") p.cargo("check")
.env("FOO", "bar") .env("FOO", "bar")
.with_stderr( .with_stderr_data(str![[r#"
"\ [COMPILING] foo v0.0.1 ([ROOT]/foo)
[COMPILING] foo v0.0.1 ([..]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[FINISHED] [..]
", "#]])
)
.run(); .run();
} }
@ -127,10 +126,13 @@ fn rustc_bootstrap() {
.build(); .build();
// RUSTC_BOOTSTRAP unset on stable should error // RUSTC_BOOTSTRAP unset on stable should error
p.cargo("check") p.cargo("check")
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]") .with_stderr_data(str![[r#"
.with_stderr_contains( [COMPILING] has-dashes v0.0.1 ([ROOT]/foo)
"help: [..] set the environment variable `RUSTC_BOOTSTRAP=has_dashes` [..]", [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) .with_status(101)
.run(); .run();
// nightly should warn whether or not RUSTC_BOOTSTRAP is set // nightly should warn whether or not RUSTC_BOOTSTRAP is set
@ -139,20 +141,33 @@ fn rustc_bootstrap() {
// NOTE: uses RUSTC_BOOTSTRAP so it will be propagated to rustc // NOTE: uses RUSTC_BOOTSTRAP so it will be propagated to rustc
// (this matters when tests are being run with a beta or stable cargo) // (this matters when tests are being run with a beta or stable cargo)
.env("RUSTC_BOOTSTRAP", "1") .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(); .run();
// RUSTC_BOOTSTRAP set to the name of the library should warn // RUSTC_BOOTSTRAP set to the name of the library should warn
p.cargo("check") p.cargo("check")
.env("RUSTC_BOOTSTRAP", "has_dashes") .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(); .run();
// RUSTC_BOOTSTRAP set to some random value should error // RUSTC_BOOTSTRAP set to some random value should error
p.cargo("check") p.cargo("check")
.env("RUSTC_BOOTSTRAP", "bar") .env("RUSTC_BOOTSTRAP", "bar")
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]") .with_stderr_data(str![[r#"
.with_stderr_contains( [ERROR] Cannot set `RUSTC_BOOTSTRAP=1` from build script of `has-dashes v0.0.1 ([ROOT]/foo)`.
"help: [..] set the environment variable `RUSTC_BOOTSTRAP=has_dashes` [..]", [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) .with_status(101)
.run(); .run();
@ -171,13 +186,23 @@ fn rustc_bootstrap() {
// NOTE: uses RUSTC_BOOTSTRAP so it will be propagated to rustc // NOTE: uses RUSTC_BOOTSTRAP so it will be propagated to rustc
// (this matters when tests are being run with a beta or stable cargo) // (this matters when tests are being run with a beta or stable cargo)
.env("RUSTC_BOOTSTRAP", "1") .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(); .run();
// RUSTC_BOOTSTRAP conditionally set when there's no library should error (regardless of the value) // RUSTC_BOOTSTRAP conditionally set when there's no library should error (regardless of the value)
p.cargo("check") p.cargo("check")
.env("RUSTC_BOOTSTRAP", "foo") .env("RUSTC_BOOTSTRAP", "foo")
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]") .with_stderr_data(str![[r#"
.with_stderr_contains("help: [..] set the environment variable `RUSTC_BOOTSTRAP=1` [..]") [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) .with_status(101)
.run(); .run();
} }
@ -194,7 +219,12 @@ fn build_script_env_verbose() {
.build(); .build();
p.cargo("check -vv") p.cargo("check -vv")
.with_stderr_contains("[RUNNING] `[..]CARGO=[..]build-script-build`") .with_stderr_data(
"\
...
[RUNNING] `[..]CARGO=[..]build-script-build`
...",
)
.run(); .run();
} }
@ -227,8 +257,14 @@ fn build_script_sees_cfg_target_feature() {
.build(); .build();
p.cargo("check -vv") p.cargo("check -vv")
.with_stderr_contains("[foo 0.0.1] CARGO_CFG_TARGET_FEATURE=[..]sse4.2[..]") .with_stderr_data(
.with_stderr_contains("[..]-Ctarget-feature=[..]+sse4.2[..]") "\
...
[foo 0.0.1] CARGO_CFG_TARGET_FEATURE=[..]sse4.2[..]
...
[..]-Ctarget-feature=[..]+sse4.2[..]
...",
)
.run(); .run();
} }
} }
@ -258,9 +294,15 @@ fn cfg_paradox() {
.build(); .build();
p.cargo("check -vv") p.cargo("check -vv")
.with_stderr_contains("[WARNING] non-trivial mutual dependency between target-specific configuration and RUSTFLAGS") .with_stderr_data(
.with_stderr_contains("[foo 0.0.1] cfg!(bertrand)=true") "\
.with_stderr_contains("[..]--cfg=bertrand[..]") [WARNING] non-trivial mutual dependency between target-specific configuration and RUSTFLAGS
...
[foo 0.0.1] cfg!(bertrand)=true
...
[..]--cfg=bertrand[..]
...",
)
.run(); .run();
} }
@ -318,10 +360,25 @@ fn rustc_cfg_with_and_without_value() {
let mut check = p.cargo("check -vv"); let mut check = p.cargo("check -vv");
#[cfg(target_has_atomic = "64")] #[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)] #[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)] #[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(); check.run();
} }