Skip to content

Commit 27342cb

Browse files
test(test-runner): run the export script test with shell builtins only
The Nix build runs the runner's tests in a sandbox without env on PATH, so the test failed there and the runner didn't build. Print the exported variables with printf instead. Signed-off-by: Matthew Grossman <[email protected]>
1 parent 0f9ad51 commit 27342cb

1 file changed

Lines changed: 25 additions & 12 deletions

File tree

‎tests/runner/src/test_env.rs‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,30 +144,42 @@ mod tests {
144144
assert!(!err.to_string().contains("hunter2"), "{err}");
145145
}
146146

147-
/// Run the export script with the local `sh` and return the variables it
148-
/// exported with the given prefix.
149-
fn exported(dir: &std::path::Path) -> Vec<String> {
150-
let script = format!("{}; env", export_script(dir.to_str().unwrap()));
147+
/// Run the export script with the local `sh` and return the values of
148+
/// `names` it exported, unset ones as empty. Only shell builtins run, as
149+
/// the Nix build sandbox has no `env` on its PATH.
150+
fn exported(dir: &std::path::Path, names: &[&str]) -> Vec<String> {
151+
let script = format!(
152+
r#"{}; for name in "$@"; do eval "printf '%s=%s\n' \"\$name\" \"\${{$name-}}\""; done"#,
153+
export_script(dir.to_str().unwrap())
154+
);
151155
let output = std::process::Command::new("sh")
152-
.args(["-c", &script])
156+
.args(["-c", &script, "sh"])
157+
.args(names)
153158
.env_clear()
154159
.output()
155160
.unwrap();
156161
assert!(output.status.success(), "{output:?}");
157-
let mut vars: Vec<String> = String::from_utf8(output.stdout)
162+
String::from_utf8(output.stdout)
158163
.unwrap()
159164
.lines()
160-
.filter(|line| line.starts_with(ENV_PREFIX))
161165
.map(str::to_string)
162-
.collect();
163-
vars.sort();
164-
vars
166+
.collect()
165167
}
166168

167169
#[test]
168170
fn export_script_exports_what_the_parser_accepts() {
171+
let names = [
172+
"OPENSHELL_TEST_EQUALS",
173+
"OPENSHELL_TEST_LAST",
174+
"OPENSHELL_TEST_SHELL",
175+
"OPENSHELL_TEST_SPACES",
176+
"OPENSHELL_TEST_X",
177+
];
169178
let dir = tempfile::tempdir().unwrap();
170-
assert_eq!(exported(dir.path()), Vec::<String>::new());
179+
assert_eq!(
180+
exported(dir.path(), &names),
181+
names.map(|name| format!("{name}="))
182+
);
171183

172184
let a = "# Published by keycloak.yaml\n\nOPENSHELL_TEST_SPACES=a b c\nOPENSHELL_TEST_SHELL=$HOME`id`$(id)\\n'\"\nOPENSHELL_TEST_EQUALS=a=b=\n";
173185
// The last line has no newline and must still be exported.
@@ -179,12 +191,13 @@ mod tests {
179191
parse_env("b.env", b).unwrap();
180192

181193
assert_eq!(
182-
exported(dir.path()),
194+
exported(dir.path(), &names),
183195
[
184196
"OPENSHELL_TEST_EQUALS=a=b=",
185197
"OPENSHELL_TEST_LAST=1",
186198
"OPENSHELL_TEST_SHELL=$HOME`id`$(id)\\n'\"",
187199
"OPENSHELL_TEST_SPACES=a b c",
200+
"OPENSHELL_TEST_X=",
188201
]
189202
);
190203
}

0 commit comments

Comments
 (0)