Skip to content

Commit 21851a0

Browse files
committed
compiletest: Trim whitespace from environment variable names
1 parent d744709 commit 21851a0

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

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

+7-10
Original file line numberDiff line numberDiff line change
@@ -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: tests/ui/compiletest-self-test/env-trim-name.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ edition: 2024
2+
//@ run-pass
3+
//@ needs-env-vars
4+
//@ rustc-env: MY_RUSTC_ENV = my-rustc-value
5+
//@ exec-env: MY_EXEC_ENV = my-exec-value
6+
7+
// Check that compiletest trims whitespace from environment variable names
8+
// specified in `rustc-env` and `exec-env` directives, so that
9+
// `//@ exec-env: FOO=bar` sees the name as `FOO` and not ` FOO`.
10+
//
11+
// Values are currently not trimmed.
12+
13+
fn main() {
14+
assert_eq!(option_env!("MY_RUSTC_ENV"), Some(" my-rustc-value"));
15+
assert_eq!(std::env::var("MY_EXEC_ENV").as_deref(), Ok(" my-exec-value"));
16+
}

0 commit comments

Comments
 (0)