1
Fork 0

compiletest: Trim whitespace from environment variable names

This commit is contained in:
Zalathar 2025-04-08 12:23:01 +10:00
parent c6c179662d
commit 34e97592f4
3 changed files with 36 additions and 16 deletions

View file

@ -441,7 +441,7 @@ impl TestProps {
ln, ln,
UNSET_EXEC_ENV, UNSET_EXEC_ENV,
&mut self.unset_exec_env, &mut self.unset_exec_env,
|r| r, |r| r.trim().to_owned(),
); );
config.push_name_value_directive( config.push_name_value_directive(
ln, ln,
@ -453,7 +453,7 @@ impl TestProps {
ln, ln,
UNSET_RUSTC_ENV, UNSET_RUSTC_ENV,
&mut self.unset_rustc_env, &mut self.unset_rustc_env,
|r| r, |r| r.trim().to_owned(),
); );
config.push_name_value_directive( config.push_name_value_directive(
ln, ln,
@ -979,16 +979,13 @@ impl Config {
fn parse_env(nv: String) -> (String, String) { fn parse_env(nv: String) -> (String, String) {
// nv is either FOO or FOO=BAR // nv is either FOO or FOO=BAR
let mut strs: Vec<String> = nv.splitn(2, '=').map(str::to_owned).collect(); // FIXME(Zalathar): The form without `=` seems to be unused; should
// we drop support for it?
match strs.len() { let (name, value) = nv.split_once('=').unwrap_or((&nv, ""));
1 => (strs.pop().unwrap(), String::new()), // Trim whitespace from the name, so that `//@ exec-env: FOO=BAR`
2 => { // sees the name as `FOO` and not ` FOO`.
let end = strs.pop().unwrap(); let name = name.trim();
(strs.pop().unwrap(), end) (name.to_owned(), value.to_owned())
}
n => panic!("Expected 1 or 2 strings, not {}", n),
}
} }
fn parse_pp_exact(&self, line: &str, testfile: &Path) -> Option<PathBuf> { fn parse_pp_exact(&self, line: &str, testfile: &Path) -> Option<PathBuf> {

View file

@ -971,16 +971,16 @@ impl<'test> TestCx<'test> {
delete_after_success: bool, delete_after_success: bool,
) -> ProcRes { ) -> ProcRes {
let prepare_env = |cmd: &mut Command| { 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 { for (key, val) in &self.props.exec_env {
cmd.env(key, val); cmd.env(key, val);
} }
for (key, val) in env_extra { for (key, val) in env_extra {
cmd.env(key, val); cmd.env(key, val);
} }
for key in &self.props.unset_exec_env {
cmd.env_remove(key);
}
}; };
let proc_res = match &*self.config.target { let proc_res = match &*self.config.target {

View file

@ -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"));
}