Skip to content

Commit e49fb3f

Browse files
arxanassunshowers
authored andcommitted
Respect env defined in config.toml
1 parent 84644eb commit e49fb3f

File tree

12 files changed

+480
-9
lines changed

12 files changed

+480
-9
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo-nextest/src/dispatch.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use clap::{ArgAction, Args, Parser, Subcommand, ValueEnum};
1212
use guppy::graph::PackageGraph;
1313
use itertools::Itertools;
1414
use nextest_filtering::FilteringExpr;
15-
use nextest_metadata::{BinaryListSummary, BuildPlatform};
15+
use nextest_metadata::{BinaryListSummary, BuildPlatform, EnvironmentMap};
1616
use nextest_runner::{
1717
cargo_config::{CargoConfigs, TargetTriple},
1818
config::{NextestConfig, NextestProfile, RetryPolicy, TestThreads, ToolConfigFile},
@@ -442,6 +442,7 @@ impl TestBuildFilter {
442442
binary_list: Arc<BinaryList>,
443443
test_filter_builder: TestFilterBuilder,
444444
runner: &TargetRunner,
445+
env: EnvironmentMap,
445446
reuse_build: &ReuseBuildInfo,
446447
) -> Result<TestList<'g>> {
447448
let path_mapper = make_path_mapper(
@@ -463,6 +464,7 @@ impl TestBuildFilter {
463464
rust_build_meta,
464465
&test_filter_builder,
465466
runner,
467+
env,
466468
// TODO: do we need to allow customizing this?
467469
num_cpus::get(),
468470
)
@@ -1037,11 +1039,13 @@ impl App {
10371039
test_filter_builder: TestFilterBuilder,
10381040
target_runner: &TargetRunner,
10391041
) -> Result<TestList> {
1042+
let env = self.base.cargo_configs.env()?;
10401043
self.build_filter.compute_test_list(
10411044
self.base.graph(),
10421045
binary_list,
10431046
test_filter_builder,
10441047
target_runner,
1048+
env,
10451049
&self.base.reuse_build,
10461050
)
10471051
}

cargo-nextest/src/tests_integration/fixtures.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ pub(super) fn set_env_vars() {
138138
// This environment variable is required to test the #[bench] fixture. Note that THIS IS FOR
139139
// TEST CODE ONLY. NEVER USE THIS IN PRODUCTION.
140140
std::env::set_var("RUSTC_BOOTSTRAP", "1");
141+
142+
// Disable the tests which check for environment variables being set in `config.toml`, as they
143+
// won't be in the search path when running integration tests.
144+
std::env::set_var("__NEXTEST_NO_CHECK_CARGO_ENV_VARS", "1");
141145
}
142146

143147
#[track_caller]

fixtures/nextest-tests/.cargo/config

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,24 @@ replace-with = "vendored-sources"
1212

1313
[source.vendored-sources]
1414
directory = "vendor"
15+
16+
[env]
17+
CONFIG_WORKSPACE_DIR = { value = "", relative = true }
18+
19+
__NEXTEST_ENV_VAR_FOR_TESTING_NOT_IN_PARENT_ENV = "test-PASSED-value-set-by-main-config"
20+
__NEXTEST_ENV_VAR_FOR_TESTING_IN_PARENT_ENV_NO_OVERRIDE = "test-FAILED-value-set-by-main-config"
21+
__NEXTEST_ENV_VAR_FOR_TESTING_IN_PARENT_ENV_OVERRIDDEN = { value = "test-PASSED-value-set-by-main-config", force = true }
22+
__NEXTEST_ENV_VAR_FOR_TESTING_IN_PARENT_ENV_RELATIVE_NO_OVERRIDE = { value = "test-FAILED-value-set-by-main-config", relative = true }
23+
__NEXTEST_ENV_VAR_FOR_TESTING_IN_PARENT_ENV_RELATIVE_OVERRIDDEN = { value = "test-PASSED-value-set-by-main-config", force = true, relative = true }
24+
25+
# See extra-config.toml in this directory for more about the __NEXTEST_TESTING_EXTRA_CONFIG variables.
26+
# The extra config should always override the main config, no matter what "force" is.
27+
__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_IN_EXTRA = "test-FAILED-value-set-by-main-config"
28+
__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_IN_MAIN = { value = "test-FAILED-value-set-by-main-config", force = true }
29+
__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_IN_BOTH = { value = "test-FAILED-value-set-by-main-config", force = true }
30+
__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_NONE = "test-FAILED-value-set-by-main-config"
31+
32+
__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_IN_EXTRA = "test-FAILED-value-set-by-main-config"
33+
__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_IN_MAIN = { value = "test-FAILED-value-set-by-main-config", force = true }
34+
__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_IN_BOTH = { value = "test-FAILED-value-set-by-main-config", force = true }
35+
__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_NONE = "test-FAILED-value-set-by-main-config"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[env]
2+
EXTRA_CONFIG_WORKS = "1"
3+
4+
# These __NEXTEST_TESTING_EXTRA_CONFIG variables are used to ensure that the extra config *always*
5+
# overrides the main config, no matter what force is set to in either.
6+
7+
# These variables are not defined in the environment.
8+
__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_IN_EXTRA = { value = "test-PASSED-value-set-by-extra-config", force = true }
9+
__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_IN_MAIN = "test-PASSED-value-set-by-extra-config"
10+
__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_IN_BOTH = { value = "test-PASSED-value-set-by-extra-config", force = true }
11+
__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_NONE = "test-PASSED-value-set-by-extra-config"
12+
13+
# These values are defined in the environment so they should be overridden as required.
14+
__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_IN_EXTRA = { value = "test-PASSED-value-set-by-extra-config", force = true }
15+
__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_IN_MAIN = "test-FAILED-value-set-by-extra-config"
16+
__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_IN_BOTH = { value = "test-PASSED-value-set-by-extra-config", force = true }
17+
__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_NONE = "test-FAILED-value-set-by-extra-config"

fixtures/nextest-tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ name = "nextest-tests"
33
version = "0.1.0"
44
description = "nextest-tests description"
55
authors = [
6-
"Diem Association <[email protected]>",
76
"Fake Author <[email protected]>",
7+
"Author 2 <[email protected]>",
88
]
99
homepage = "https://fake-homepage.example.com"
1010
# Specify both a license and a license file for test_cargo_env_vars.

fixtures/nextest-tests/tests/basic.rs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// Copyright (c) The nextest Contributors
2-
use std::{env, io::Read, path::Path};
2+
use std::{
3+
env,
4+
io::Read,
5+
path::{Path, PathBuf},
6+
};
37

48
#[test]
59
fn test_success() {}
@@ -156,6 +160,78 @@ fn test_cargo_env_vars() {
156160
// CARGO_PRIMARY_PACKAGE is missing at runtime
157161
// CARGO_TARGET_TMPDIR is missing at runtime
158162
// Dynamic library paths are tested by actually executing the tests -- they depend on the dynamic library.
163+
164+
if std::env::var("__NEXTEST_NO_CHECK_CARGO_ENV_VARS").is_err() {
165+
let config_workspace_dir = PathBuf::from(
166+
std::env::var("CONFIG_WORKSPACE_DIR")
167+
.expect("CONFIG_WORKSPACE_DIR should be present, defined in [env]"),
168+
);
169+
assert_eq!(
170+
config_workspace_dir,
171+
PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
172+
);
173+
174+
assert_eq!(
175+
std::env::var("__NEXTEST_ENV_VAR_FOR_TESTING_NOT_IN_PARENT_ENV").as_deref(),
176+
Ok("test-PASSED-value-set-by-main-config")
177+
);
178+
assert_eq!(
179+
std::env::var("__NEXTEST_ENV_VAR_FOR_TESTING_IN_PARENT_ENV_NO_OVERRIDE").as_deref(),
180+
Ok("test-PASSED-value-set-by-environment")
181+
);
182+
assert_eq!(
183+
std::env::var("__NEXTEST_ENV_VAR_FOR_TESTING_IN_PARENT_ENV_OVERRIDDEN").as_deref(),
184+
Ok("test-PASSED-value-set-by-main-config")
185+
);
186+
assert_eq!(
187+
std::env::var("__NEXTEST_ENV_VAR_FOR_TESTING_IN_PARENT_ENV_RELATIVE_NO_OVERRIDE")
188+
.as_deref(),
189+
Ok("test-PASSED-value-set-by-environment")
190+
);
191+
let overridden_path =
192+
std::env::var("__NEXTEST_ENV_VAR_FOR_TESTING_IN_PARENT_ENV_RELATIVE_OVERRIDDEN")
193+
.unwrap();
194+
assert_eq!(
195+
&overridden_path,
196+
config_workspace_dir
197+
.join("test-PASSED-value-set-by-main-config")
198+
.to_str()
199+
.unwrap(),
200+
);
201+
202+
assert_eq!(
203+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_IN_EXTRA").as_deref(),
204+
Ok("test-PASSED-value-set-by-extra-config"),
205+
);
206+
assert_eq!(
207+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_IN_MAIN").as_deref(),
208+
Ok("test-PASSED-value-set-by-extra-config")
209+
);
210+
assert_eq!(
211+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_IN_BOTH").as_deref(),
212+
Ok("test-PASSED-value-set-by-extra-config")
213+
);
214+
assert_eq!(
215+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_NONE").as_deref(),
216+
Ok("test-PASSED-value-set-by-extra-config")
217+
);
218+
assert_eq!(
219+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_IN_EXTRA").as_deref(),
220+
Ok("test-PASSED-value-set-by-extra-config")
221+
);
222+
assert_eq!(
223+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_IN_MAIN").as_deref(),
224+
Ok("test-PASSED-value-set-by-environment")
225+
);
226+
assert_eq!(
227+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_IN_BOTH").as_deref(),
228+
Ok("test-PASSED-value-set-by-extra-config")
229+
);
230+
assert_eq!(
231+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_NONE").as_deref(),
232+
Ok("test-PASSED-value-set-by-environment")
233+
);
234+
}
159235
}
160236

161237
#[test]

nextest-metadata/src/test_list.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,36 @@ use std::{
1212
process::Command,
1313
};
1414

15+
/// An environment variable set in `config.toml`. See https://doc.rust-lang.org/cargo/reference/config.html#env
16+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
17+
pub struct CargoEnvironmentVariable {
18+
/// The source `config.toml` file. See
19+
/// https://doc.rust-lang.org/cargo/reference/config.html#hierarchical-structure for the lookup
20+
/// order.
21+
pub source: Option<Utf8PathBuf>,
22+
23+
/// The name of the environment variable to set.
24+
pub name: String,
25+
26+
/// The value of the environment variable to set.
27+
pub value: String,
28+
29+
/// If the environment variable is already set in the environment, it is not reassigned unless
30+
/// `force` is set to `true`.
31+
pub force: bool,
32+
33+
/// Interpret the environment variable as a path relative to the directory containing the source
34+
/// `config.toml` file.
35+
pub relative: bool,
36+
}
37+
38+
/// A list of environment variables to set when running tests.
39+
///
40+
/// This is a `Vec` instead of a map because, on Windows, environment variables are case-insensitive
41+
/// but case-preserving. We produce the environment as-is and let the caller handle the case of
42+
/// duplicates.
43+
pub type EnvironmentMap = Vec<CargoEnvironmentVariable>;
44+
1545
/// Command builder for `cargo nextest list`.
1646
#[derive(Clone, Debug, Default)]
1747
pub struct ListCommand {

nextest-runner/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ windows = { version = "0.42.0", features = [
9191
"Win32_System_JobObjects",
9292
] }
9393
win32job = "1.0.2"
94+
dunce = "1.0.3"
9495

9596
[dev-dependencies]
9697
color-eyre = { version = "0.6.2", default-features = false }

0 commit comments

Comments
 (0)