Skip to content

Commit 34e9759

Browse files
committed
compiletest: Trim whitespace from environment variable names
1 parent c6c1796 commit 34e9759

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

Diff for: src/tools/compiletest/src/header.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ impl TestProps {
441441
ln,
442442
UNSET_EXEC_ENV,
443443
&mut self.unset_exec_env,
444-
|r| r,
444+
|r| r.trim().to_owned(),
445445
);
446446
config.push_name_value_directive(
447447
ln,
@@ -453,7 +453,7 @@ impl TestProps {
453453
ln,
454454
UNSET_RUSTC_ENV,
455455
&mut self.unset_rustc_env,
456-
|r| r,
456+
|r| r.trim().to_owned(),
457457
);
458458
config.push_name_value_directive(
459459
ln,
@@ -979,16 +979,13 @@ impl Config {
979979

980980
fn parse_env(nv: String) -> (String, String) {
981981
// nv is either FOO or FOO=BAR
982-
let mut strs: Vec<String> = nv.splitn(2, '=').map(str::to_owned).collect();
983-
984-
match strs.len() {
985-
1 => (strs.pop().unwrap(), String::new()),
986-
2 => {
987-
let end = strs.pop().unwrap();
988-
(strs.pop().unwrap(), end)
989-
}
990-
n => panic!("Expected 1 or 2 strings, not {}", n),
991-
}
982+
// FIXME(Zalathar): The form without `=` seems to be unused; should
983+
// we drop support for it?
984+
let (name, value) = nv.split_once('=').unwrap_or((&nv, ""));
985+
// Trim whitespace from the name, so that `//@ exec-env: FOO=BAR`
986+
// sees the name as `FOO` and not ` FOO`.
987+
let name = name.trim();
988+
(name.to_owned(), value.to_owned())
992989
}
993990

994991
fn parse_pp_exact(&self, line: &str, testfile: &Path) -> Option<PathBuf> {

Diff for: src/tools/compiletest/src/runtest.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -971,16 +971,16 @@ impl<'test> TestCx<'test> {
971971
delete_after_success: bool,
972972
) -> ProcRes {
973973
let prepare_env = |cmd: &mut Command| {
974-
for key in &self.props.unset_exec_env {
975-
cmd.env_remove(key);
976-
}
977-
978974
for (key, val) in &self.props.exec_env {
979975
cmd.env(key, val);
980976
}
981977
for (key, val) in env_extra {
982978
cmd.env(key, val);
983979
}
980+
981+
for key in &self.props.unset_exec_env {
982+
cmd.env_remove(key);
983+
}
984984
};
985985

986986
let proc_res = match &*self.config.target {

Diff for: tests/ui/compiletest-self-test/trim-env-name.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ edition: 2024
2+
//@ revisions: set unset
3+
//@ run-pass
4+
//@ ignore-cross-compile (assume that non-cross targets have working env vars)
5+
//@ rustc-env: MY_RUSTC_ENV = my-rustc-value
6+
//@ exec-env: MY_EXEC_ENV = my-exec-value
7+
//@[unset] unset-rustc-env: MY_RUSTC_ENV
8+
//@[unset] unset-exec-env: MY_EXEC_ENV
9+
10+
// Check that compiletest trims whitespace from environment variable names
11+
// specified in `rustc-env` and `exec-env` directives, so that
12+
// `//@ exec-env: FOO=bar` sees the name as `FOO` and not ` FOO`.
13+
//
14+
// Values are currently not trimmed.
15+
//
16+
// Since this is a compiletest self-test, only run it on non-cross targets,
17+
// to avoid having to worry about weird targets that don't support env vars.
18+
19+
fn main() {
20+
let is_set = cfg!(set);
21+
assert_eq!(option_env!("MY_RUSTC_ENV"), is_set.then_some(" my-rustc-value"));
22+
assert_eq!(std::env::var("MY_EXEC_ENV").ok().as_deref(), is_set.then_some(" my-exec-value"));
23+
}

0 commit comments

Comments
 (0)