Skip to content

Commit 65977f9

Browse files
arxanassunshowers
authored andcommitted
XXX this fails tests: Respect env defined in config.toml
1 parent d8a0e0c commit 65977f9

File tree

9 files changed

+438
-9
lines changed

9 files changed

+438
-9
lines changed

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/tests/basic.rs

Lines changed: 76 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,77 @@ 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+
assert_eq!(
174+
std::env::var("__NEXTEST_ENV_VAR_FOR_TESTING_NOT_IN_PARENT_ENV").as_deref(),
175+
Ok("test-PASSED-value-set-by-main-config")
176+
);
177+
assert_eq!(
178+
std::env::var("__NEXTEST_ENV_VAR_FOR_TESTING_IN_PARENT_ENV_NO_OVERRIDE").as_deref(),
179+
Ok("test-PASSED-value-set-by-environment")
180+
);
181+
assert_eq!(
182+
std::env::var("__NEXTEST_ENV_VAR_FOR_TESTING_IN_PARENT_ENV_OVERRIDDEN").as_deref(),
183+
Ok("test-PASSED-value-set-by-main-config")
184+
);
185+
assert_eq!(
186+
std::env::var("__NEXTEST_ENV_VAR_FOR_TESTING_IN_PARENT_ENV_RELATIVE_NO_OVERRIDE")
187+
.as_deref(),
188+
Ok("test-PASSED-value-set-by-environment")
189+
);
190+
let overridden_path =
191+
std::env::var("__NEXTEST_ENV_VAR_FOR_TESTING_IN_PARENT_ENV_RELATIVE_OVERRIDDEN")
192+
.unwrap();
193+
assert_eq!(
194+
&overridden_path,
195+
config_workspace_dir
196+
.join("test-PASSED-value-set-by-main-config")
197+
.to_str()
198+
.unwrap(),
199+
);
200+
201+
assert_eq!(
202+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_IN_EXTRA").as_deref(),
203+
Ok("test-PASSED-value-set-by-extra-config"),
204+
);
205+
assert_eq!(
206+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_IN_MAIN").as_deref(),
207+
Ok("test-PASSED-value-set-by-extra-config")
208+
);
209+
assert_eq!(
210+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_IN_BOTH").as_deref(),
211+
Ok("test-PASSED-value-set-by-extra-config")
212+
);
213+
assert_eq!(
214+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_FORCE_NONE").as_deref(),
215+
Ok("test-PASSED-value-set-by-extra-config")
216+
);
217+
assert_eq!(
218+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_IN_EXTRA").as_deref(),
219+
Ok("test-PASSED-value-set-by-extra-config")
220+
);
221+
assert_eq!(
222+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_IN_MAIN").as_deref(),
223+
Ok("test-PASSED-value-set-by-environment")
224+
);
225+
assert_eq!(
226+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_IN_BOTH").as_deref(),
227+
Ok("test-PASSED-value-set-by-extra-config")
228+
);
229+
assert_eq!(
230+
std::env::var("__NEXTEST_TESTING_EXTRA_CONFIG_OVERRIDE_FORCE_NONE").as_deref(),
231+
Ok("test-PASSED-value-set-by-environment")
232+
);
233+
}
159234
}
160235

161236
#[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 {

0 commit comments

Comments
 (0)