Skip to content

Commit c9e82ec

Browse files
committed
feat(test-support): Expose test-env setup
1 parent 79ef00c commit c9e82ec

File tree

2 files changed

+102
-114
lines changed

2 files changed

+102
-114
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 99 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub mod tools;
6161

6262
pub mod prelude {
6363
pub use crate::ChannelChanger;
64+
pub use crate::TestEnv;
6465
}
6566

6667
/*
@@ -1101,75 +1102,7 @@ pub fn process<T: AsRef<OsStr>>(t: T) -> ProcessBuilder {
11011102

11021103
fn _process(t: &OsStr) -> ProcessBuilder {
11031104
let mut p = ProcessBuilder::new(t);
1104-
1105-
// In general just clear out all cargo-specific configuration already in the
1106-
// environment. Our tests all assume a "default configuration" unless
1107-
// specified otherwise.
1108-
for (k, _v) in env::vars() {
1109-
if k.starts_with("CARGO_") {
1110-
p.env_remove(&k);
1111-
}
1112-
}
1113-
if env::var_os("RUSTUP_TOOLCHAIN").is_some() {
1114-
// Override the PATH to avoid executing the rustup wrapper thousands
1115-
// of times. This makes the testsuite run substantially faster.
1116-
lazy_static::lazy_static! {
1117-
static ref RUSTC_DIR: PathBuf = {
1118-
match ProcessBuilder::new("rustup")
1119-
.args(&["which", "rustc"])
1120-
.exec_with_output()
1121-
{
1122-
Ok(output) => {
1123-
let s = str::from_utf8(&output.stdout).expect("utf8").trim();
1124-
let mut p = PathBuf::from(s);
1125-
p.pop();
1126-
p
1127-
}
1128-
Err(e) => {
1129-
panic!("RUSTUP_TOOLCHAIN was set, but could not run rustup: {}", e);
1130-
}
1131-
}
1132-
};
1133-
}
1134-
let path = env::var_os("PATH").unwrap_or_default();
1135-
let paths = env::split_paths(&path);
1136-
let new_path = env::join_paths(std::iter::once(RUSTC_DIR.clone()).chain(paths)).unwrap();
1137-
p.env("PATH", new_path);
1138-
}
1139-
1140-
p.cwd(&paths::root())
1141-
.env("HOME", paths::home())
1142-
.env("CARGO_HOME", paths::home().join(".cargo"))
1143-
.env("__CARGO_TEST_ROOT", paths::root())
1144-
// Force Cargo to think it's on the stable channel for all tests, this
1145-
// should hopefully not surprise us as we add cargo features over time and
1146-
// cargo rides the trains.
1147-
.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "stable")
1148-
// For now disable incremental by default as support hasn't ridden to the
1149-
// stable channel yet. Once incremental support hits the stable compiler we
1150-
// can switch this to one and then fix the tests.
1151-
.env("CARGO_INCREMENTAL", "0")
1152-
.env_remove("__CARGO_DEFAULT_LIB_METADATA")
1153-
.env_remove("RUSTC")
1154-
.env_remove("RUSTDOC")
1155-
.env_remove("RUSTC_WRAPPER")
1156-
.env_remove("RUSTFLAGS")
1157-
.env_remove("RUSTDOCFLAGS")
1158-
.env_remove("XDG_CONFIG_HOME") // see #2345
1159-
.env("GIT_CONFIG_NOSYSTEM", "1") // keep trying to sandbox ourselves
1160-
.env_remove("EMAIL")
1161-
.env_remove("USER") // not set on some rust-lang docker images
1162-
.env_remove("MFLAGS")
1163-
.env_remove("MAKEFLAGS")
1164-
.env_remove("GIT_AUTHOR_NAME")
1165-
.env_remove("GIT_AUTHOR_EMAIL")
1166-
.env_remove("GIT_COMMITTER_NAME")
1167-
.env_remove("GIT_COMMITTER_EMAIL")
1168-
.env_remove("MSYSTEM"); // assume cmd.exe everywhere on windows
1169-
if cfg!(target_os = "macos") {
1170-
// Work-around a bug in macOS 10.15, see `link_or_copy` for details.
1171-
p.env("__CARGO_COPY_DONT_LINK_DO_NOT_USE_THIS", "1");
1172-
}
1105+
p.cwd(&paths::root()).test_env();
11731106
p
11741107
}
11751108

@@ -1190,6 +1123,103 @@ impl ChannelChanger for snapbox::cmd::Command {
11901123
}
11911124
}
11921125

1126+
/// Establish a process's test environment
1127+
pub trait TestEnv: Sized {
1128+
fn test_env(mut self) -> Self {
1129+
// In general just clear out all cargo-specific configuration already in the
1130+
// environment. Our tests all assume a "default configuration" unless
1131+
// specified otherwise.
1132+
for (k, _v) in env::vars() {
1133+
if k.starts_with("CARGO_") {
1134+
self = self.env_remove(&k);
1135+
}
1136+
}
1137+
if env::var_os("RUSTUP_TOOLCHAIN").is_some() {
1138+
// Override the PATH to avoid executing the rustup wrapper thousands
1139+
// of times. This makes the testsuite run substantially faster.
1140+
lazy_static::lazy_static! {
1141+
static ref RUSTC_DIR: PathBuf = {
1142+
match ProcessBuilder::new("rustup")
1143+
.args(&["which", "rustc"])
1144+
.exec_with_output()
1145+
{
1146+
Ok(output) => {
1147+
let s = str::from_utf8(&output.stdout).expect("utf8").trim();
1148+
let mut p = PathBuf::from(s);
1149+
p.pop();
1150+
p
1151+
}
1152+
Err(e) => {
1153+
panic!("RUSTUP_TOOLCHAIN was set, but could not run rustup: {}", e);
1154+
}
1155+
}
1156+
};
1157+
}
1158+
let path = env::var_os("PATH").unwrap_or_default();
1159+
let paths = env::split_paths(&path);
1160+
let new_path =
1161+
env::join_paths(std::iter::once(RUSTC_DIR.clone()).chain(paths)).unwrap();
1162+
self = self.env("PATH", new_path);
1163+
}
1164+
1165+
self = self
1166+
.env("HOME", paths::home())
1167+
.env("CARGO_HOME", paths::home().join(".cargo"))
1168+
.env("__CARGO_TEST_ROOT", paths::root())
1169+
// Force Cargo to think it's on the stable channel for all tests, this
1170+
// should hopefully not surprise us as we add cargo features over time and
1171+
// cargo rides the trains.
1172+
.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "stable")
1173+
// For now disable incremental by default as support hasn't ridden to the
1174+
// stable channel yet. Once incremental support hits the stable compiler we
1175+
// can switch this to one and then fix the tests.
1176+
.env("CARGO_INCREMENTAL", "0")
1177+
.env_remove("__CARGO_DEFAULT_LIB_METADATA")
1178+
.env_remove("RUSTC")
1179+
.env_remove("RUSTDOC")
1180+
.env_remove("RUSTC_WRAPPER")
1181+
.env_remove("RUSTFLAGS")
1182+
.env_remove("RUSTDOCFLAGS")
1183+
.env_remove("XDG_CONFIG_HOME") // see #2345
1184+
.env("GIT_CONFIG_NOSYSTEM", "1") // keep trying to sandbox ourselves
1185+
.env_remove("EMAIL")
1186+
.env_remove("USER") // not set on some rust-lang docker images
1187+
.env_remove("MFLAGS")
1188+
.env_remove("MAKEFLAGS")
1189+
.env_remove("GIT_AUTHOR_NAME")
1190+
.env_remove("GIT_AUTHOR_EMAIL")
1191+
.env_remove("GIT_COMMITTER_NAME")
1192+
.env_remove("GIT_COMMITTER_EMAIL")
1193+
.env_remove("MSYSTEM"); // assume cmd.exe everywhere on windows
1194+
if cfg!(target_os = "macos") {
1195+
// Work-around a bug in macOS 10.15, see `link_or_copy` for details.
1196+
self = self.env("__CARGO_COPY_DONT_LINK_DO_NOT_USE_THIS", "1");
1197+
}
1198+
self
1199+
}
1200+
1201+
fn env<S: AsRef<std::ffi::OsStr>>(self, key: &str, value: S) -> Self;
1202+
fn env_remove(self, key: &str) -> Self;
1203+
}
1204+
1205+
impl TestEnv for &mut ProcessBuilder {
1206+
fn env<S: AsRef<std::ffi::OsStr>>(self, key: &str, value: S) -> Self {
1207+
self.env(key, value)
1208+
}
1209+
fn env_remove(self, key: &str) -> Self {
1210+
self.env_remove(key)
1211+
}
1212+
}
1213+
1214+
impl TestEnv for snapbox::cmd::Command {
1215+
fn env<S: AsRef<std::ffi::OsStr>>(self, key: &str, value: S) -> Self {
1216+
self.env(key, value)
1217+
}
1218+
fn env_remove(self, key: &str) -> Self {
1219+
self.env_remove(key)
1220+
}
1221+
}
1222+
11931223
fn split_and_add_args(p: &mut ProcessBuilder, s: &str) {
11941224
for mut arg in s.split_whitespace() {
11951225
if (arg.starts_with('"') && arg.ends_with('"'))

tests/testsuite/cargo_add.rs

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,9 @@ use cargo_test_support::prelude::*;
44
use cargo_test_support::Project;
55

66
pub fn cargo_command() -> snapbox::cmd::Command {
7-
let mut cmd = snapbox::cmd::Command::new(cargo_exe()).with_assert(assert());
8-
9-
// In general just clear out all cargo-specific configuration already in the
10-
// environment. Our tests all assume a "default configuration" unless
11-
// specified otherwise.
12-
for (k, _v) in std::env::vars() {
13-
if k.starts_with("CARGO_") {
14-
cmd = cmd.env_remove(&k);
15-
}
16-
}
17-
18-
cmd = cmd
19-
.env("HOME", cargo_test_support::paths::home())
20-
.env(
21-
"CARGO_HOME",
22-
cargo_test_support::paths::home().join(".cargo"),
23-
)
24-
.env("__CARGO_TEST_ROOT", cargo_test_support::paths::root())
25-
// Force Cargo to think it's on the stable channel for all tests, this
26-
// should hopefully not surprise us as we add cargo features over time and
27-
// cargo rides the trains.
28-
.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "stable")
29-
// For now disable incremental by default as support hasn't ridden to the
30-
// stable channel yet. Once incremental support hits the stable compiler we
31-
// can switch this to one and then fix the tests.
32-
.env("CARGO_INCREMENTAL", "0")
33-
.env_remove("__CARGO_DEFAULT_LIB_METADATA")
34-
.env_remove("RUSTC")
35-
.env_remove("RUSTDOC")
36-
.env_remove("RUSTC_WRAPPER")
37-
.env_remove("RUSTFLAGS")
38-
.env_remove("RUSTDOCFLAGS")
39-
.env_remove("XDG_CONFIG_HOME") // see #2345
40-
.env("GIT_CONFIG_NOSYSTEM", "1") // keep trying to sandbox ourselves
41-
.env_remove("EMAIL")
42-
.env_remove("USER") // not set on some rust-lang docker images
43-
.env_remove("MFLAGS")
44-
.env_remove("MAKEFLAGS")
45-
.env_remove("GIT_AUTHOR_NAME")
46-
.env_remove("GIT_AUTHOR_EMAIL")
47-
.env_remove("GIT_COMMITTER_NAME")
48-
.env_remove("GIT_COMMITTER_EMAIL")
49-
.env_remove("MSYSTEM"); // assume cmd.exe everywhere on windows
50-
51-
cmd
7+
snapbox::cmd::Command::new(cargo_exe())
8+
.with_assert(assert())
9+
.test_env()
5210
}
5311

5412
fn init_registry() {

0 commit comments

Comments
 (0)