Skip to content

compiletest: Trim whitespace from environment variable names #139507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions src/tools/compiletest/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ impl TestProps {
ln,
UNSET_EXEC_ENV,
&mut self.unset_exec_env,
|r| r,
|r| r.trim().to_owned(),
);
config.push_name_value_directive(
ln,
Expand All @@ -453,7 +453,7 @@ impl TestProps {
ln,
UNSET_RUSTC_ENV,
&mut self.unset_rustc_env,
|r| r,
|r| r.trim().to_owned(),
);
config.push_name_value_directive(
ln,
Expand Down Expand Up @@ -979,16 +979,13 @@ impl Config {

fn parse_env(nv: String) -> (String, String) {
// nv is either FOO or FOO=BAR
let mut strs: Vec<String> = nv.splitn(2, '=').map(str::to_owned).collect();

match strs.len() {
1 => (strs.pop().unwrap(), String::new()),
2 => {
let end = strs.pop().unwrap();
(strs.pop().unwrap(), end)
}
n => panic!("Expected 1 or 2 strings, not {}", n),
}
// FIXME(Zalathar): The form without `=` seems to be unused; should
// we drop support for it?
let (name, value) = nv.split_once('=').unwrap_or((&nv, ""));
// Trim whitespace from the name, so that `//@ exec-env: FOO=BAR`
// sees the name as `FOO` and not ` FOO`.
let name = name.trim();
(name.to_owned(), value.to_owned())
}

fn parse_pp_exact(&self, line: &str, testfile: &Path) -> Option<PathBuf> {
Expand Down
8 changes: 4 additions & 4 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,16 +971,16 @@ impl<'test> TestCx<'test> {
delete_after_success: bool,
) -> ProcRes {
let prepare_env = |cmd: &mut Command| {
for key in &self.props.unset_exec_env {
cmd.env_remove(key);
}

for (key, val) in &self.props.exec_env {
cmd.env(key, val);
}
for (key, val) in env_extra {
cmd.env(key, val);
}

for key in &self.props.unset_exec_env {
cmd.env_remove(key);
}
};

let proc_res = match &*self.config.target {
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/compiletest-self-test/trim-env-name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//@ edition: 2024
//@ revisions: set unset
//@ run-pass
//@ ignore-cross-compile (assume that non-cross targets have working env vars)
//@ rustc-env: MY_RUSTC_ENV = my-rustc-value
//@ exec-env: MY_EXEC_ENV = my-exec-value
//@[unset] unset-rustc-env: MY_RUSTC_ENV
//@[unset] unset-exec-env: MY_EXEC_ENV

// Check that compiletest trims whitespace from environment variable names
// specified in `rustc-env` and `exec-env` directives, so that
// `//@ exec-env: FOO=bar` sees the name as `FOO` and not ` FOO`.
//
// Values are currently not trimmed.
//
// Since this is a compiletest self-test, only run it on non-cross targets,
// to avoid having to worry about weird targets that don't support env vars.

fn main() {
let is_set = cfg!(set);
assert_eq!(option_env!("MY_RUSTC_ENV"), is_set.then_some(" my-rustc-value"));
assert_eq!(std::env::var("MY_EXEC_ENV").ok().as_deref(), is_set.then_some(" my-exec-value"));
}
Loading