Skip to content

Commit 45c29a4

Browse files
committed
temp: pass through env
1 parent 224bc94 commit 45c29a4

File tree

8 files changed

+206
-19
lines changed

8 files changed

+206
-19
lines changed

cargo-nextest/src/dispatch.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use clap::{ArgEnum, Args, Parser, Subcommand};
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, TestThreads, ToolConfigFile},
@@ -563,6 +563,7 @@ impl CargoOptions {
563563
manifest_path: Option<&Utf8Path>,
564564
output: OutputContext,
565565
target_triple: Option<TargetTriple>,
566+
env: EnvironmentMap,
566567
) -> Result<BinaryList> {
567568
// Don't use the manifest path from the graph to ensure that if the user cd's into a
568569
// particular crate and runs cargo nextest, then it behaves identically to cargo test.
@@ -586,7 +587,7 @@ impl CargoOptions {
586587
}
587588

588589
let test_binaries =
589-
BinaryList::from_messages(Cursor::new(output.stdout), graph, target_triple)?;
590+
BinaryList::from_messages(Cursor::new(output.stdout), graph, target_triple, env)?;
590591
Ok(test_binaries)
591592
}
592593
}
@@ -910,11 +911,13 @@ impl BaseApp {
910911
None => {
911912
let target_triple =
912913
discover_target_triple(&self.cargo_configs, self.cargo_opts.target.as_deref());
914+
let env = self.cargo_configs.env()?;
913915
Arc::new(self.cargo_opts.compute_binary_list(
914916
self.graph(),
915917
self.manifest_path.as_deref(),
916918
self.output,
917919
target_triple,
920+
env,
918921
)?)
919922
}
920923
};

nextest-metadata/src/test_list.rs

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

15+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
16+
pub struct CargoEnvironmentVariable {
17+
pub source: Option<Utf8PathBuf>,
18+
pub name: String,
19+
pub value: String,
20+
pub force: bool,
21+
pub relative: bool,
22+
}
23+
24+
pub type EnvironmentMap = Vec<CargoEnvironmentVariable>;
25+
1526
/// Command builder for `cargo nextest list`.
1627
#[derive(Clone, Debug, Default)]
1728
pub struct ListCommand {
@@ -275,6 +286,9 @@ pub struct RustBuildMetaSummary {
275286
/// The target platform used while compiling the Rust artifacts
276287
#[serde(default)]
277288
pub target_platform: Option<String>,
289+
290+
/// The environment variables to set when running the test.
291+
pub env: EnvironmentMap,
278292
}
279293

280294
/// A non-test Rust binary. Used to set the correct environment
@@ -358,6 +372,9 @@ pub struct RustTestSuiteSummary {
358372
/// Test cases within this test suite.
359373
#[serde(rename = "testcases")]
360374
pub test_cases: BTreeMap<String, RustTestCaseSummary>,
375+
376+
/// The environment variables to set when running the test.
377+
pub env: EnvironmentMap,
361378
}
362379

363380
fn listed_status() -> RustTestSuiteStatusSummary {
@@ -485,6 +502,7 @@ mod tests {
485502
non_test_binaries: BTreeMap::new(),
486503
linked_paths: BTreeSet::new(),
487504
target_platform: None,
505+
env: Vec::new(),
488506
}; "no target platform")]
489507
fn test_deserialize_old_rust_build_meta(input: &str, expected: RustBuildMetaSummary) {
490508
let build_meta: RustBuildMetaSummary =

nextest-runner/src/cargo_config.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
99
use crate::errors::{CargoConfigError, InvalidCargoCliConfigReason, TargetTripleError};
1010
use camino::{Utf8Path, Utf8PathBuf};
11+
use nextest_metadata::{CargoEnvironmentVariable, EnvironmentMap};
1112
use once_cell::sync::OnceCell;
1213
use serde::Deserialize;
1314
use std::{collections::BTreeMap, fmt};
@@ -241,6 +242,50 @@ impl CargoConfigs {
241242
&self.cwd
242243
}
243244

245+
/// The environment variables to set when running Cargo commands.
246+
pub fn env(&self) -> Result<EnvironmentMap, CargoConfigError> {
247+
let env = self
248+
.discovered_configs()?
249+
.rev() // TODO: necessary? what order are the discovered configs?
250+
.filter_map(|config| match config {
251+
DiscoveredConfig::CliOption { config, source }
252+
| DiscoveredConfig::File { config, source } => Some((config, source)),
253+
DiscoveredConfig::Env => None,
254+
})
255+
.flat_map(|(config, source)| {
256+
let source = match source {
257+
CargoConfigSource::CliOption => None,
258+
CargoConfigSource::File(path) => Some(path.clone()),
259+
};
260+
config
261+
.env
262+
.clone()
263+
.into_iter()
264+
.map(move |(name, value)| match value {
265+
CargoConfigEnv::Value(value) => CargoEnvironmentVariable {
266+
source: source.clone(),
267+
name,
268+
value,
269+
force: false,
270+
relative: false,
271+
},
272+
CargoConfigEnv::Fields {
273+
value,
274+
force,
275+
relative,
276+
} => CargoEnvironmentVariable {
277+
source: source.clone(),
278+
name,
279+
value,
280+
force,
281+
relative,
282+
},
283+
})
284+
})
285+
.collect();
286+
Ok(env)
287+
}
288+
244289
pub(crate) fn discovered_configs(
245290
&self,
246291
) -> Result<
@@ -493,11 +538,26 @@ fn load_file(
493538
Ok((CargoConfigSource::File(path), config))
494539
}
495540

541+
#[derive(Clone, Deserialize, Debug)]
542+
#[serde(untagged)]
543+
pub(crate) enum CargoConfigEnv {
544+
Value(String),
545+
Fields {
546+
value: String,
547+
#[serde(default)]
548+
force: bool,
549+
#[serde(default)]
550+
relative: bool,
551+
},
552+
}
553+
496554
#[derive(Deserialize, Debug)]
497555
pub(crate) struct CargoConfig {
498556
#[serde(default)]
499557
pub(crate) build: CargoConfigBuild,
500558
pub(crate) target: Option<BTreeMap<String, CargoConfigRunner>>,
559+
#[serde(default)]
560+
pub(crate) env: BTreeMap<String, CargoConfigEnv>,
501561
}
502562

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

nextest-runner/src/list/binary_list.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use camino::{Utf8Path, Utf8PathBuf};
1111
use cargo_metadata::{Artifact, BuildScript, Message, PackageId};
1212
use guppy::graph::PackageGraph;
1313
use nextest_metadata::{
14-
BinaryListSummary, BuildPlatform, RustNonTestBinaryKind, RustNonTestBinarySummary,
15-
RustTestBinaryKind, RustTestBinarySummary,
14+
BinaryListSummary, BuildPlatform, EnvironmentMap, RustNonTestBinaryKind,
15+
RustNonTestBinarySummary, RustTestBinaryKind, RustTestBinarySummary,
1616
};
1717
use owo_colors::OwoColorize;
1818
use std::{fmt::Write as _, io, io::Write};
@@ -51,8 +51,9 @@ impl BinaryList {
5151
reader: impl io::BufRead,
5252
graph: &PackageGraph,
5353
target_triple: Option<TargetTriple>,
54+
env: EnvironmentMap,
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: EnvironmentMap,
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,7 @@ 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 = RustBuildMeta::new("/fake/target", Some(fake_triple), Vec::new());
397402
rust_build_meta
398403
.base_output_directories
399404
.insert("my-profile".into());

nextest-runner/src/list/rust_build_meta.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
reuse_build::PathMapper,
99
};
1010
use camino::Utf8PathBuf;
11-
use nextest_metadata::{RustBuildMetaSummary, RustNonTestBinarySummary};
11+
use nextest_metadata::{EnvironmentMap, RustBuildMetaSummary, RustNonTestBinarySummary};
1212
use std::{
1313
collections::{BTreeMap, BTreeSet},
1414
marker::PhantomData,
@@ -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: EnvironmentMap,
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: EnvironmentMap,
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: Vec::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
}

0 commit comments

Comments
 (0)