Skip to content

Commit eb1a16d

Browse files
chrjabssunshowers
authored andcommitted
[nextest-runner] set env vars from build script
Set environment variables specified via `cargo::rustc-env` in the build script when running tests
1 parent 10394dd commit eb1a16d

12 files changed

+138
-2
lines changed

fixture-data/src/nextest_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ pub static EXPECTED_TEST_SUITES: Lazy<BTreeMap<RustBinaryId, TestSuiteFixture>>
166166
"with-build-script",
167167
BuildPlatform::Target,
168168
vec![
169+
TestCaseFixture::new("tests::test_build_script_vars_set", TestCaseFixtureStatus::Pass),
169170
TestCaseFixture::new("tests::test_out_dir_present", TestCaseFixtureStatus::Pass),
170171
],
171172
),

fixtures/nextest-tests/with-build-script/build.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,28 @@ fn main() {
99

1010
// The presence of this file is checked by a test.
1111
std::fs::write(out_dir.join("this-is-a-test-file"), "test-contents").unwrap();
12+
13+
// Needed for 1.79+
14+
println!("cargo:rustc-check-cfg=cfg(new_format)");
15+
16+
// The presence of these environment variables is checked by a test.
17+
if rustc_minor_version().is_some_and(|minor| minor >= 77) {
18+
println!("cargo::rustc-cfg=new_format");
19+
println!("cargo::rustc-env=BUILD_SCRIPT_NEW_FMT=new_val");
20+
}
21+
println!("cargo:rustc-env=BUILD_SCRIPT_OLD_FMT=old_val");
22+
}
23+
24+
fn rustc_minor_version() -> Option<u32> {
25+
let rustc = std::env::var_os("RUSTC")?;
26+
let output = std::process::Command::new(rustc)
27+
.arg("--version")
28+
.output()
29+
.ok()?;
30+
let version = std::str::from_utf8(&output.stdout).ok()?;
31+
let mut pieces = version.split('.');
32+
if pieces.next() != Some("rustc 1") {
33+
return None;
34+
}
35+
pieces.next()?.parse().ok()
1236
}

fixtures/nextest-tests/with-build-script/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,17 @@ mod tests {
1212
let contents = std::fs::read(&path).expect("test file exists in OUT_DIR");
1313
assert_eq!(contents, b"test-contents");
1414
}
15+
16+
#[test]
17+
fn test_build_script_vars_set() {
18+
// Since the build script wrote `cargo::rustc-env` instructions, these variables are
19+
// expected to be set by nextest
20+
#[cfg(new_format)]
21+
{
22+
let val = std::env::var("BUILD_SCRIPT_NEW_FMT").expect("BUILD_SCRIPT_NEW_FMT is valid");
23+
assert_eq!(val, "new_val");
24+
}
25+
let val = std::env::var("BUILD_SCRIPT_OLD_FMT").expect("BUILD_SCRIPT_OLD_FMT is valid");
26+
assert_eq!(val, "old_val");
27+
}
1528
}

integration-tests/tests/integration/snapshots/integration__list_with_default_set_basic.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ nextest-tests::example/nextest-tests:
3737
nextest-tests::example/other:
3838
tests::other_example_success
3939
with-build-script:
40+
tests::test_build_script_vars_set
4041
tests::test_out_dir_present

integration-tests/tests/integration/snapshots/integration__list_with_default_set_bound_all.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ nextest-tests::example/nextest-tests:
4141
nextest-tests::example/other:
4242
tests::other_example_success
4343
with-build-script:
44+
tests::test_build_script_vars_set
4445
tests::test_out_dir_present

integration-tests/tests/integration/snapshots/integration__list_with_default_set_expr_all.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ nextest-tests::example/nextest-tests:
3737
nextest-tests::example/other:
3838
tests::other_example_success
3939
with-build-script:
40+
tests::test_build_script_vars_set
4041
tests::test_out_dir_present

integration-tests/tests/integration/snapshots/integration__list_with_default_set_expr_default.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ nextest-tests::example/nextest-tests:
3737
nextest-tests::example/other:
3838
tests::other_example_success
3939
with-build-script:
40+
tests::test_build_script_vars_set
4041
tests::test_out_dir_present

integration-tests/tests/integration/snapshots/integration__show_config_test_groups-2.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ group: @global
4747
nextest-tests::example/other:
4848
tests::other_example_success
4949
with-build-script:
50+
tests::test_build_script_vars_set
5051
tests::test_out_dir_present
5152

integration-tests/tests/integration/snapshots/integration__show_config_test_groups-4.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ group: @global
4848
nextest-tests::example/other:
4949
tests::other_example_success
5050
with-build-script:
51+
tests::test_build_script_vars_set
5152
tests::test_out_dir_present
5253

integration-tests/tests/integration/snapshots/integration__show_config_test_groups-6.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ group: @global
4949
nextest-tests::example/other:
5050
tests::other_example_success
5151
with-build-script:
52+
tests::test_build_script_vars_set
5253
tests::test_out_dir_present
5354

0 commit comments

Comments
 (0)