Skip to content

Commit 5218f82

Browse files
committed
temp: pass through env
1 parent 224bc94 commit 5218f82

File tree

8 files changed

+113
-14
lines changed

8 files changed

+113
-14
lines changed

cargo-nextest/src/dispatch.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use nextest_runner::{
2929
use once_cell::sync::OnceCell;
3030
use owo_colors::{OwoColorize, Style};
3131
use std::{
32+
collections::BTreeMap,
3233
fmt::Write as _,
3334
io::{Cursor, Write},
3435
sync::Arc,
@@ -563,6 +564,7 @@ impl CargoOptions {
563564
manifest_path: Option<&Utf8Path>,
564565
output: OutputContext,
565566
target_triple: Option<TargetTriple>,
567+
env: BTreeMap<String, String>,
566568
) -> Result<BinaryList> {
567569
// Don't use the manifest path from the graph to ensure that if the user cd's into a
568570
// particular crate and runs cargo nextest, then it behaves identically to cargo test.
@@ -586,7 +588,7 @@ impl CargoOptions {
586588
}
587589

588590
let test_binaries =
589-
BinaryList::from_messages(Cursor::new(output.stdout), graph, target_triple)?;
591+
BinaryList::from_messages(Cursor::new(output.stdout), graph, target_triple, env)?;
590592
Ok(test_binaries)
591593
}
592594
}
@@ -910,11 +912,13 @@ impl BaseApp {
910912
None => {
911913
let target_triple =
912914
discover_target_triple(&self.cargo_configs, self.cargo_opts.target.as_deref());
915+
let env = self.cargo_configs.env()?;
913916
Arc::new(self.cargo_opts.compute_binary_list(
914917
self.graph(),
915918
self.manifest_path.as_deref(),
916919
self.output,
917920
target_triple,
921+
env,
918922
)?)
919923
}
920924
};

nextest-metadata/src/test_list.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ pub struct RustBuildMetaSummary {
275275
/// The target platform used while compiling the Rust artifacts
276276
#[serde(default)]
277277
pub target_platform: Option<String>,
278+
279+
/// The environment variables to set when running the test.
280+
pub env: BTreeMap<String, String>,
278281
}
279282

280283
/// A non-test Rust binary. Used to set the correct environment
@@ -358,6 +361,9 @@ pub struct RustTestSuiteSummary {
358361
/// Test cases within this test suite.
359362
#[serde(rename = "testcases")]
360363
pub test_cases: BTreeMap<String, RustTestCaseSummary>,
364+
365+
/// The environment variables to set when running the test.
366+
pub env: BTreeMap<String, String>,
361367
}
362368

363369
fn listed_status() -> RustTestSuiteStatusSummary {
@@ -485,6 +491,7 @@ mod tests {
485491
non_test_binaries: BTreeMap::new(),
486492
linked_paths: BTreeSet::new(),
487493
target_platform: None,
494+
env: BTreeMap::new(),
488495
}; "no target platform")]
489496
fn test_deserialize_old_rust_build_meta(input: &str, expected: RustBuildMetaSummary) {
490497
let build_meta: RustBuildMetaSummary =

nextest-runner/src/cargo_config.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,23 @@ impl CargoConfigs {
241241
&self.cwd
242242
}
243243

244+
/// The environment variables to set when running Cargo commands.
245+
pub fn env(&self) -> Result<BTreeMap<String, String>, CargoConfigError> {
246+
let env = self
247+
.discovered_configs()?
248+
.filter_map(|config| match config {
249+
DiscoveredConfig::CliOption { config, source: _ }
250+
| DiscoveredConfig::File { config, source: _ } => Some(config),
251+
DiscoveredConfig::Env => None,
252+
})
253+
.flat_map(|config| {
254+
dbg!(config);
255+
config.env.iter().map(|(k, v)| (k.to_owned(), v.to_owned()))
256+
})
257+
.collect();
258+
Ok(env)
259+
}
260+
244261
pub(crate) fn discovered_configs(
245262
&self,
246263
) -> Result<
@@ -498,6 +515,8 @@ pub(crate) struct CargoConfig {
498515
#[serde(default)]
499516
pub(crate) build: CargoConfigBuild,
500517
pub(crate) target: Option<BTreeMap<String, CargoConfigRunner>>,
518+
#[serde(default)]
519+
pub(crate) env: BTreeMap<String, String>,
501520
}
502521

503522
#[derive(Deserialize, Default, Debug)]

nextest-runner/src/list/binary_list.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use nextest_metadata::{
1515
RustTestBinaryKind, RustTestBinarySummary,
1616
};
1717
use owo_colors::OwoColorize;
18-
use std::{fmt::Write as _, io, io::Write};
18+
use std::{collections::BTreeMap, fmt::Write as _, io, io::Write};
1919

2020
/// A Rust test binary built by Cargo.
2121
#[derive(Clone, Debug)]
@@ -51,8 +51,9 @@ impl BinaryList {
5151
reader: impl io::BufRead,
5252
graph: &PackageGraph,
5353
target_triple: Option<TargetTriple>,
54+
env: BTreeMap<String, String>,
5455
) -> Result<Self, FromMessagesError> {
55-
let mut state = BinaryListBuildState::new(graph, target_triple);
56+
let mut state = BinaryListBuildState::new(graph, target_triple, env);
5657

5758
for message in Message::parse_stream(reader) {
5859
let message = message.map_err(FromMessagesError::ReadMessages)?;
@@ -161,13 +162,17 @@ struct BinaryListBuildState<'g> {
161162
}
162163

163164
impl<'g> BinaryListBuildState<'g> {
164-
fn new(graph: &'g PackageGraph, target_triple: Option<TargetTriple>) -> Self {
165+
fn new(
166+
graph: &'g PackageGraph,
167+
target_triple: Option<TargetTriple>,
168+
env: BTreeMap<String, String>,
169+
) -> Self {
165170
let rust_target_dir = graph.workspace().target_directory().to_path_buf();
166171

167172
Self {
168173
graph,
169174
rust_binaries: vec![],
170-
rust_build_meta: RustBuildMeta::new(rust_target_dir, target_triple),
175+
rust_build_meta: RustBuildMeta::new(rust_target_dir, target_triple, env),
171176
}
172177
}
173178

@@ -393,7 +398,8 @@ mod tests {
393398
triple: "fake-triple".to_owned(),
394399
source: TargetTripleSource::CliOption,
395400
};
396-
let mut rust_build_meta = RustBuildMeta::new("/fake/target", Some(fake_triple));
401+
let mut rust_build_meta =
402+
RustBuildMeta::new("/fake/target", Some(fake_triple), BTreeMap::new());
397403
rust_build_meta
398404
.base_output_directories
399405
.insert("my-profile".into());

nextest-runner/src/list/rust_build_meta.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ pub struct RustBuildMeta<State> {
3939
/// The target triple used while compiling the artifacts
4040
pub target_triple: Option<TargetTriple>,
4141

42+
/// The environment variables to set when running the test.
43+
pub env: BTreeMap<String, String>,
44+
4245
state: PhantomData<State>,
4346
}
4447

@@ -47,6 +50,7 @@ impl RustBuildMeta<BinaryListState> {
4750
pub fn new(
4851
target_directory: impl Into<Utf8PathBuf>,
4952
target_triple: Option<TargetTriple>,
53+
env: BTreeMap<String, String>,
5054
) -> Self {
5155
Self {
5256
target_directory: target_directory.into(),
@@ -55,6 +59,7 @@ impl RustBuildMeta<BinaryListState> {
5559
linked_paths: BTreeMap::new(),
5660
state: PhantomData,
5761
target_triple,
62+
env,
5863
}
5964
}
6065

@@ -69,8 +74,9 @@ impl RustBuildMeta<BinaryListState> {
6974
base_output_directories: self.base_output_directories.clone(),
7075
non_test_binaries: self.non_test_binaries.clone(),
7176
linked_paths: self.linked_paths.clone(),
72-
state: PhantomData,
7377
target_triple: self.target_triple.clone(),
78+
env: self.env.clone(),
79+
state: PhantomData,
7480
}
7581
}
7682
}
@@ -86,6 +92,7 @@ impl RustBuildMeta<TestListState> {
8692
linked_paths: BTreeMap::new(),
8793
state: PhantomData,
8894
target_triple: None,
95+
env: BTreeMap::new(),
8996
}
9097
}
9198

@@ -135,6 +142,7 @@ impl<State> RustBuildMeta<State> {
135142
.collect(),
136143
state: PhantomData,
137144
target_triple: TargetTriple::deserialize(summary.target_platform),
145+
env: summary.env,
138146
}
139147
}
140148

@@ -146,6 +154,7 @@ impl<State> RustBuildMeta<State> {
146154
non_test_binaries: self.non_test_binaries.clone(),
147155
linked_paths: self.linked_paths.keys().cloned().collect(),
148156
target_platform: TargetTriple::serialize(self.target_triple.as_ref()),
157+
env: self.env.clone(),
149158
}
150159
}
151160
}

nextest-runner/src/list/test_list.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ pub struct RustTestArtifact<'g> {
5959
/// The working directory that this test should be executed in.
6060
pub cwd: Utf8PathBuf,
6161

62+
/// The environment variables to set when running the test.
63+
pub env: BTreeMap<String, String>,
64+
6265
/// The platform for which this test artifact was built.
6366
pub build_platform: BuildPlatform,
6467
}
@@ -132,6 +135,7 @@ impl<'g> RustTestArtifact<'g> {
132135
kind: binary.kind.clone(),
133136
cwd,
134137
non_test_binaries,
138+
env: rust_build_meta.env.clone(),
135139
build_platform: binary.build_platform,
136140
})
137141
}
@@ -152,6 +156,7 @@ impl<'g> RustTestArtifact<'g> {
152156
non_test_binaries,
153157
cwd,
154158
build_platform,
159+
env,
155160
} = self;
156161
(
157162
binary_path,
@@ -162,6 +167,7 @@ impl<'g> RustTestArtifact<'g> {
162167
kind,
163168
non_test_binaries,
164169
cwd,
170+
env,
165171
build_platform,
166172
status,
167173
},
@@ -340,6 +346,7 @@ impl<'g> TestList<'g> {
340346
cwd: info.cwd.clone(),
341347
status,
342348
test_cases,
349+
env: info.env.clone(),
343350
};
344351
(info.binary_id.clone(), testsuite)
345352
})
@@ -609,6 +616,9 @@ pub struct RustTestSuite<'g> {
609616
/// will not be changed.
610617
pub cwd: Utf8PathBuf,
611618

619+
/// The environment variables to set when running the test.
620+
pub env: BTreeMap<String, String>,
621+
612622
/// The platform the test suite is for (host or target).
613623
pub build_platform: BuildPlatform,
614624

@@ -676,6 +686,7 @@ impl<'g> RustTestArtifact<'g> {
676686
&self.package,
677687
dylib_path,
678688
&self.non_test_binaries,
689+
&self.env,
679690
);
680691
let mut cmd = tokio::process::Command::from(cmd);
681692
match cmd.output().await {
@@ -837,6 +848,7 @@ impl<'a> TestInstance<'a> {
837848
&self.suite_info.package,
838849
test_list.updated_dylib_path(),
839850
&self.suite_info.non_test_binaries,
851+
&self.suite_info.env,
840852
)
841853
}
842854
}
@@ -849,6 +861,7 @@ pub(crate) fn make_test_command(
849861
package: &PackageMetadata<'_>,
850862
dylib_path: &OsStr,
851863
non_test_binaries: &BTreeSet<(String, Utf8PathBuf)>,
864+
env: &BTreeMap<String, String>,
852865
) -> std::process::Command {
853866
// This is a workaround for a macOS SIP issue:
854867
// https://github.com/nextest-rs/nextest/pull/84
@@ -952,6 +965,8 @@ pub(crate) fn make_test_command(
952965
cmd.env(format!("NEXTEST_BIN_EXE_{}", name), &path);
953966
}
954967

968+
cmd.envs(env);
969+
955970
cmd
956971
}
957972

@@ -1006,6 +1021,7 @@ mod tests {
10061021
kind: RustTestBinaryKind::LIB,
10071022
non_test_binaries: BTreeSet::new(),
10081023
build_platform: BuildPlatform::Target,
1024+
env: BTreeMap::new(),
10091025
};
10101026

10111027
let skipped_binary_name = "skipped-binary".to_owned();
@@ -1019,14 +1035,15 @@ mod tests {
10191035
kind: RustTestBinaryKind::PROC_MACRO,
10201036
non_test_binaries: BTreeSet::new(),
10211037
build_platform: BuildPlatform::Host,
1038+
env: BTreeMap::new(),
10221039
};
10231040

10241041
let fake_triple = TargetTriple {
10251042
triple: "fake-triple".to_owned(),
10261043
source: TargetTripleSource::CliOption,
10271044
};
1028-
let rust_build_meta =
1029-
RustBuildMeta::new("/fake", Some(fake_triple)).map_paths(&PathMapper::noop());
1045+
let rust_build_meta = RustBuildMeta::new("/fake", Some(fake_triple), BTreeMap::new())
1046+
.map_paths(&PathMapper::noop());
10301047
let test_list = TestList::new_with_outputs(
10311048
[
10321049
(test_binary, &non_ignored_output, &ignored_output),
@@ -1079,6 +1096,7 @@ mod tests {
10791096
binary_id: fake_binary_id,
10801097
kind: RustTestBinaryKind::LIB,
10811098
non_test_binaries: BTreeSet::new(),
1099+
env: BTreeMap::new(),
10821100
},
10831101
"/fake/skipped-binary".into() => RustTestSuite {
10841102
status: RustTestSuiteStatus::Skipped,
@@ -1089,6 +1107,7 @@ mod tests {
10891107
binary_id: skipped_binary_id,
10901108
kind: RustTestBinaryKind::PROC_MACRO,
10911109
non_test_binaries: BTreeSet::new(),
1110+
env: BTreeMap::new(),
10921111
},
10931112
}
10941113
);

nextest-runner/tests/integration/basic.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ use nextest_runner::{
1616
test_filter::{RunIgnored, TestFilterBuilder},
1717
};
1818
use pretty_assertions::assert_eq;
19-
use std::{io::Cursor, time::Duration};
19+
use std::{collections::BTreeMap, io::Cursor, time::Duration};
2020
use test_case::test_case;
2121

2222
#[test]
2323
fn test_list_binaries() -> Result<()> {
2424
set_rustflags();
2525

2626
let graph = &*PACKAGE_GRAPH;
27-
let binary_list =
28-
BinaryList::from_messages(Cursor::new(&*FIXTURE_RAW_CARGO_TEST_OUTPUT), graph, None)?;
27+
let binary_list = BinaryList::from_messages(
28+
Cursor::new(&*FIXTURE_RAW_CARGO_TEST_OUTPUT),
29+
graph,
30+
None,
31+
BTreeMap::new(),
32+
)?;
2933

3034
for (id, name, platform_is_target) in &EXPECTED_BINARY_LIST {
3135
let bin = binary_list
@@ -571,3 +575,29 @@ fn test_termination() -> Result<()> {
571575

572576
Ok(())
573577
}
578+
579+
#[test]
580+
fn test_set_env_vars() -> Result<()> {
581+
set_rustflags();
582+
583+
let test_filter = TestFilterBuilder::any(RunIgnored::Default);
584+
let test_list = FIXTURE_TARGETS.make_test_list(&test_filter, &TargetRunner::empty());
585+
let config = load_config();
586+
let profile = config
587+
.profile(NextestConfig::DEFAULT_PROFILE)
588+
.expect("default config is valid");
589+
590+
let mut runner = TestRunnerBuilder::default()
591+
.build(
592+
&test_list,
593+
profile,
594+
SignalHandlerKind::Noop,
595+
TargetRunner::empty(),
596+
)
597+
.unwrap();
598+
599+
let (instance_statuses, run_stats) = execute_collect(&mut runner);
600+
todo!("test this somehow");
601+
602+
Ok(())
603+
}

0 commit comments

Comments
 (0)