Skip to content

Commit

Permalink
add NEXTEST_PROFILE to test and script environments
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshowers committed Feb 9, 2025
1 parent c872a3b commit cb26ee6
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 18 deletions.
13 changes: 8 additions & 5 deletions cargo-nextest/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1634,12 +1634,13 @@ impl App {
let target_runner = self
.base
.load_runner(&binary_list.rust_build_meta.build_platforms);
let profile =
profile.apply_build_platforms(&binary_list.rust_build_meta.build_platforms);
let ctx = TestExecuteContext {
profile_name: profile.name(),
double_spawn,
target_runner,
};
let profile =
profile.apply_build_platforms(&binary_list.rust_build_meta.build_platforms);
let ecx = profile.filterset_ecx();

let test_list =
Expand Down Expand Up @@ -1689,11 +1690,12 @@ impl App {

let double_spawn = self.base.load_double_spawn();
let target_runner = self.base.load_runner(&build_platforms);
let profile = profile.apply_build_platforms(&build_platforms);
let ctx = TestExecuteContext {
profile_name: profile.name(),
double_spawn,
target_runner,
};
let profile = profile.apply_build_platforms(&build_platforms);
let ecx = profile.filterset_ecx();

let test_list = self.build_test_list(&ctx, binary_list, test_filter_builder, &ecx)?;
Expand Down Expand Up @@ -1769,12 +1771,13 @@ impl App {
let build_platforms = &binary_list.rust_build_meta.build_platforms.clone();
let double_spawn = self.base.load_double_spawn();
let target_runner = self.base.load_runner(build_platforms);

let profile = profile.apply_build_platforms(build_platforms);
let ctx = TestExecuteContext {
profile_name: profile.name(),
double_spawn,
target_runner,
};

let profile = profile.apply_build_platforms(build_platforms);
let ecx = profile.filterset_ecx();

let test_list = self.build_test_list(&ctx, binary_list, test_filter_builder, &ecx)?;
Expand Down
4 changes: 4 additions & 0 deletions fixtures/nextest-tests/scripts/my-script.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
REM If this environment variable is set, exit with non-zero.
if defined __NEXTEST_SETUP_SCRIPT_ERROR exit 1

REM Check that NEXTEST_PROFILE is set.
if not defined NEXTEST_PROFILE exit 2

ECHO MY_ENV_VAR=my-env-var>> %NEXTEST_ENV%
ECHO SCRIPT_NEXTEST_PROFILE=%NEXTEST_PROFILE%>> %NEXTEST_ENV%
7 changes: 7 additions & 0 deletions fixtures/nextest-tests/scripts/my-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ if [ -n "$__NEXTEST_SETUP_SCRIPT_ERROR" ]; then
exit 1
fi

# If NEXTEST_PROFILE is not set, exit with non-zero.
if [ -z "$NEXTEST_PROFILE" ]; then
echo "NEXTEST_PROFILE is not set, exiting with 2"
exit 2
fi

echo MY_ENV_VAR=my-env-var >> "$NEXTEST_ENV"
echo SCRIPT_NEXTEST_PROFILE="$NEXTEST_PROFILE" >> "$NEXTEST_ENV"
4 changes: 4 additions & 0 deletions fixtures/nextest-tests/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ fn test_cargo_env_vars() {
);

assert_eq!(std::env::var("MY_ENV_VAR").as_deref(), Ok("my-env-var"));
assert_eq!(
std::env::var("SCRIPT_NEXTEST_PROFILE").expect("SCRIPT_NEXTEST_PROFILE is set by script"),
std::env::var("NEXTEST_PROFILE").expect("NEXTEST_PROFILE is set by nextest"),
);
}

#[test]
Expand Down
7 changes: 7 additions & 0 deletions integration-tests/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ pub fn set_env_vars() {
// allows tests which make sure outputs are being displayed to work.
std::env::set_var("__NEXTEST_DISPLAY_EMPTY_OUTPUTS", "1");

// Unset NEXTEST_PROFILE because we don't want to let it interfere with the
// tests.
//
// TODO: once cargo-nextest 0.9.89 is out, also check that NEXTEST_PROFILE
// is actually set before unsetting it.
std::env::remove_var("NEXTEST_PROFILE");

// Remove OUT_DIR from the environment, as it interferes with tests (some of them expect that
// OUT_DIR isn't set.)
std::env::remove_var("OUT_DIR");
Expand Down
3 changes: 3 additions & 0 deletions nextest-runner/src/config/scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ impl SetupScriptCommand {
/// Creates a new `SetupScriptCommand` for a setup script.
pub(crate) fn new(
config: &ScriptConfig,
profile_name: &str,
double_spawn: &DoubleSpawnInfo,
test_list: &TestList<'_>,
) -> Result<Self, ChildStartError> {
Expand All @@ -185,6 +186,8 @@ impl SetupScriptCommand {
cmd.current_dir(test_list.workspace_root())
// This environment variable is set to indicate that tests are being run under nextest.
.env("NEXTEST", "1")
// Set the nextest profile.
.env("NEXTEST_PROFILE", profile_name)
// Setup scripts can define environment variables which are written out here.
.env("NEXTEST_ENV", &env_path);

Expand Down
5 changes: 5 additions & 0 deletions nextest-runner/src/list/test_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ impl<'g> TestList<'g> {
rust_build_meta: &rust_build_meta,
double_spawn: ctx.double_spawn,
dylib_path: &updated_dylib_path,
profile_name: ctx.profile_name,
env: &env,
};

Expand Down Expand Up @@ -1044,6 +1045,7 @@ impl<'a> TestInstance<'a> {
rust_build_meta: &test_list.rust_build_meta,
double_spawn: ctx.double_spawn,
dylib_path: test_list.updated_dylib_path(),
profile_name: ctx.profile_name,
env: &test_list.env,
};

Expand Down Expand Up @@ -1079,6 +1081,9 @@ impl fmt::Display for TestInstanceId<'_> {
/// Context required for test execution.
#[derive(Clone, Debug)]
pub struct TestExecuteContext<'a> {
/// The name of the profile.
pub profile_name: &'a str,

/// Double-spawn info.
pub double_spawn: &'a DoubleSpawnInfo,

Expand Down
7 changes: 5 additions & 2 deletions nextest-runner/src/runner/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ impl<'a> ExecutorContext<'a> {
resp_tx: &UnboundedSender<ExecutorEvent<'a>>,
req_rx: &mut UnboundedReceiver<RunUnitRequest<'a>>,
) -> Result<InternalSetupScriptExecuteStatus<'a>, ChildStartError> {
let mut cmd = script.make_command(&self.double_spawn, self.test_list)?;
let mut cmd =
script.make_command(self.profile.name(), &self.double_spawn, self.test_list)?;
let command_mut = cmd.command_mut();

command_mut.env("NEXTEST_RUN_ID", format!("{}", self.run_id));
Expand Down Expand Up @@ -607,6 +608,7 @@ impl<'a> ExecutorContext<'a> {
req_rx: &mut UnboundedReceiver<RunUnitRequest<'a>>,
) -> Result<InternalExecuteStatus<'a>, ChildStartError> {
let ctx = TestExecuteContext {
profile_name: self.profile.name(),
double_spawn: &self.double_spawn,
target_runner: &self.target_runner,
};
Expand Down Expand Up @@ -996,10 +998,11 @@ impl<'a> SetupScriptPacket<'a> {
/// Turns self into a command that can be executed.
fn make_command(
&self,
profile_name: &str,
double_spawn: &DoubleSpawnInfo,
test_list: &TestList<'_>,
) -> Result<SetupScriptCommand, ChildStartError> {
SetupScriptCommand::new(self.config, double_spawn, test_list)
SetupScriptCommand::new(self.config, profile_name, double_spawn, test_list)
}

fn slow_event(&self, elapsed: Duration, will_terminate: Option<Duration>) -> ExecutorEvent<'a> {
Expand Down
3 changes: 3 additions & 0 deletions nextest-runner/src/test_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub(crate) struct LocalExecuteContext<'a> {
pub(crate) rust_build_meta: &'a RustBuildMeta<TestListState>,
pub(crate) double_spawn: &'a DoubleSpawnInfo,
pub(crate) dylib_path: &'a OsStr,
pub(crate) profile_name: &'a str,
pub(crate) env: &'a EnvironmentMap,
}

Expand Down Expand Up @@ -74,6 +75,8 @@ impl TestCommand {
.env("NEXTEST", "1")
// This environment variable is set to indicate that each test is being run in its own process.
.env("NEXTEST_EXECUTION_MODE", "process-per-test")
// Set the nextest profile.
.env("NEXTEST_PROFILE", lctx.profile_name)
.env(
"CARGO_MANIFEST_DIR",
// CARGO_MANIFEST_DIR is set to the *new* cwd after path mapping.
Expand Down
45 changes: 37 additions & 8 deletions nextest-runner/tests/integration/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ fn test_list_tests() -> Result<()> {
set_env_vars();

let test_filter = TestFilterBuilder::default_set(RunIgnored::Default);
let test_list = FIXTURE_TARGETS.make_test_list(&test_filter, &TargetRunner::empty())?;
let test_list = FIXTURE_TARGETS.make_test_list(
NextestConfig::DEFAULT_PROFILE,
&test_filter,
&TargetRunner::empty(),
)?;
let mut summary = test_list.to_summary();

for (name, expected) in &*EXPECTED_TEST_SUITES {
Expand Down Expand Up @@ -111,7 +115,11 @@ fn test_run() -> Result<()> {
set_env_vars();

let test_filter = TestFilterBuilder::default_set(RunIgnored::Default);
let test_list = FIXTURE_TARGETS.make_test_list(&test_filter, &TargetRunner::empty())?;
let test_list = FIXTURE_TARGETS.make_test_list(
NextestConfig::DEFAULT_PROFILE,
&test_filter,
&TargetRunner::empty(),
)?;
let config = load_config();
let profile = config
.profile(NextestConfig::DEFAULT_PROFILE)
Expand Down Expand Up @@ -243,7 +251,11 @@ fn test_run_ignored() -> Result<()> {
vec![expr],
)
.unwrap();
let test_list = FIXTURE_TARGETS.make_test_list(&test_filter, &TargetRunner::empty())?;
let test_list = FIXTURE_TARGETS.make_test_list(
NextestConfig::DEFAULT_PROFILE,
&test_filter,
&TargetRunner::empty(),
)?;
let config = load_config();
let profile = config
.profile(NextestConfig::DEFAULT_PROFILE)
Expand Down Expand Up @@ -344,7 +356,11 @@ fn test_filter_expr_with_string_filters() -> Result<()> {
vec![expr],
)
.unwrap();
let test_list = FIXTURE_TARGETS.make_test_list(&test_filter, &TargetRunner::empty())?;
let test_list = FIXTURE_TARGETS.make_test_list(
NextestConfig::DEFAULT_PROFILE,
&test_filter,
&TargetRunner::empty(),
)?;
for test in test_list.iter_tests() {
if test.name == "tests::call_dylib_add_two" {
assert!(
Expand Down Expand Up @@ -411,7 +427,11 @@ fn test_filter_expr_without_string_filters() -> Result<()> {
vec![expr],
)
.unwrap();
let test_list = FIXTURE_TARGETS.make_test_list(&test_filter, &TargetRunner::empty())?;
let test_list = FIXTURE_TARGETS.make_test_list(
NextestConfig::DEFAULT_PROFILE,
&test_filter,
&TargetRunner::empty(),
)?;
for test in test_list.iter_tests() {
if test.name.contains("test_multiply_two") || test.name == "tests::call_dylib_add_two" {
assert!(
Expand Down Expand Up @@ -443,7 +463,11 @@ fn test_string_filters_without_filter_expr() -> Result<()> {
vec![],
)
.unwrap();
let test_list = FIXTURE_TARGETS.make_test_list(&test_filter, &TargetRunner::empty())?;
let test_list = FIXTURE_TARGETS.make_test_list(
NextestConfig::DEFAULT_PROFILE,
&test_filter,
&TargetRunner::empty(),
)?;
for test in test_list.iter_tests() {
if test.name.contains("test_multiply_two")
|| test.name.contains("tests::call_dylib_add_two")
Expand Down Expand Up @@ -475,7 +499,11 @@ fn test_retries(retries: Option<RetryPolicy>) -> Result<()> {
set_env_vars();

let test_filter = TestFilterBuilder::default_set(RunIgnored::Default);
let test_list = FIXTURE_TARGETS.make_test_list(&test_filter, &TargetRunner::empty())?;
let test_list = FIXTURE_TARGETS.make_test_list(
NextestConfig::DEFAULT_PROFILE,
&test_filter,
&TargetRunner::empty(),
)?;
let config = load_config();
let profile = config
.profile("with-retries")
Expand Down Expand Up @@ -638,7 +666,8 @@ fn test_termination() -> Result<()> {
)
.unwrap();

let test_list = FIXTURE_TARGETS.make_test_list(&test_filter, &TargetRunner::empty())?;
let test_list =
FIXTURE_TARGETS.make_test_list("with-termination", &test_filter, &TargetRunner::empty())?;
let config = load_config();
let profile = config
.profile("with-termination")
Expand Down
2 changes: 2 additions & 0 deletions nextest-runner/tests/integration/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,14 @@ impl FixtureTargets {

pub(crate) fn make_test_list(
&self,
profile_name: &str,
test_filter: &TestFilterBuilder,
target_runner: &TargetRunner,
) -> Result<TestList<'_>> {
let test_bins: Vec<_> = self.test_artifacts.values().cloned().collect();
let double_spawn = DoubleSpawnInfo::disabled();
let ctx = TestExecuteContext {
profile_name,
double_spawn: &double_spawn,
target_runner,
};
Expand Down
18 changes: 15 additions & 3 deletions nextest-runner/tests/integration/target_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ fn test_listing_with_target_runner() -> Result<()> {
set_env_vars();

let test_filter = TestFilterBuilder::default_set(RunIgnored::Default);
let test_list = FIXTURE_TARGETS.make_test_list(&test_filter, &TargetRunner::empty())?;
let test_list = FIXTURE_TARGETS.make_test_list(
NextestConfig::DEFAULT_PROFILE,
&test_filter,
&TargetRunner::empty(),
)?;

let bin_count = test_list.binary_count();
let test_count = test_list.test_count();
Expand All @@ -191,7 +195,11 @@ fn test_listing_with_target_runner() -> Result<()> {
);
let (_, target_runner) = runner_for_target(None).unwrap();

let test_list = FIXTURE_TARGETS.make_test_list(&test_filter, &target_runner)?;
let test_list = FIXTURE_TARGETS.make_test_list(
NextestConfig::DEFAULT_PROFILE,
&test_filter,
&target_runner,
)?;

assert_eq!(bin_count, test_list.binary_count());
assert_eq!(test_count, test_list.test_count());
Expand Down Expand Up @@ -224,7 +232,11 @@ fn test_run_with_target_runner() -> Result<()> {
assert_eq!(passthrough_path(), runner.binary());
}

let test_list = FIXTURE_TARGETS.make_test_list(&test_filter, &target_runner)?;
let test_list = FIXTURE_TARGETS.make_test_list(
NextestConfig::DEFAULT_PROFILE,
&test_filter,
&target_runner,
)?;

let config = load_config();
let profile = config
Expand Down

0 comments on commit cb26ee6

Please sign in to comment.