Add shell-expanded strings (#2055)

This commit is contained in:
Casey Rodarmor
2024-05-18 22:41:38 -07:00
committed by GitHub
parent 4961f49c38
commit b1c7491486
12 changed files with 181 additions and 41 deletions

View File

@@ -91,6 +91,7 @@ mod search_arguments;
mod shadowing_parameters;
mod shebang;
mod shell;
mod shell_expansion;
mod show;
mod slash_operator;
mod string;

67
tests/shell_expansion.rs Normal file
View File

@@ -0,0 +1,67 @@
use super::*;
#[test]
fn strings_are_shell_expanded() {
Test::new()
.justfile(
"
x := x'$JUST_TEST_VARIABLE'
",
)
.env("JUST_TEST_VARIABLE", "FOO")
.args(["--evaluate", "x"])
.stdout("FOO")
.run();
}
#[test]
fn shell_expanded_error_messages_highlight_string_token() {
Test::new()
.justfile(
"
x := x'$FOOOOOOOOOOOOOOOOOOOOOOOOOOOOO'
",
)
.env("JUST_TEST_VARIABLE", "FOO")
.args(["--evaluate", "x"])
.status(1)
.stderr(
"
error: Shell expansion failed: error looking key 'FOOOOOOOOOOOOOOOOOOOOOOOOOOOOO' up: environment variable not found
——▶ justfile:1:7
1 │ x := x'$FOOOOOOOOOOOOOOOOOOOOOOOOOOOOO'
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
")
.run();
}
#[test]
fn shell_expanded_strings_are_dumped_correctly() {
Test::new()
.justfile(
"
x := x'$JUST_TEST_VARIABLE'
",
)
.env("JUST_TEST_VARIABLE", "FOO")
.args(["--dump", "--unstable"])
.stdout("x := x'$JUST_TEST_VARIABLE'\n")
.run();
}
#[test]
fn shell_expanded_strings_can_be_used_in_settings() {
Test::new()
.justfile(
"
set dotenv-filename := x'$JUST_TEST_VARIABLE'
@foo:
echo $DOTENV_KEY
",
)
.env("JUST_TEST_VARIABLE", ".env")
.stdout("dotenv-value\n")
.run();
}

View File

@@ -199,7 +199,7 @@ impl Test {
let stdout = if self.unindent_stdout {
unindent(&self.stdout)
} else {
self.stdout
self.stdout.clone()
};
let stderr = unindent(&self.stderr);
@@ -212,9 +212,9 @@ impl Test {
}
let mut child = command
.args(self.args)
.args(&self.args)
.envs(&self.env)
.current_dir(self.tempdir.path().join(self.current_dir))
.current_dir(self.tempdir.path().join(&self.current_dir))
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
@@ -266,7 +266,7 @@ impl Test {
}
if self.test_round_trip && self.status == EXIT_SUCCESS {
test_round_trip(self.tempdir.path());
self.round_trip();
}
Output {
@@ -275,42 +275,44 @@ impl Test {
tempdir: self.tempdir,
}
}
}
fn test_round_trip(tmpdir: &Path) {
println!("Reparsing...");
fn round_trip(&self) {
println!("Reparsing...");
let output = Command::new(executable_path("just"))
.current_dir(tmpdir)
.arg("--dump")
.output()
.expect("just invocation failed");
let output = Command::new(executable_path("just"))
.current_dir(self.tempdir.path())
.arg("--dump")
.envs(&self.env)
.output()
.expect("just invocation failed");
if !output.status.success() {
panic!("dump failed: {}", output.status);
if !output.status.success() {
panic!("dump failed: {} {:?}", output.status, output);
}
let dumped = String::from_utf8(output.stdout).unwrap();
let reparsed_path = self.tempdir.path().join("reparsed.just");
fs::write(&reparsed_path, &dumped).unwrap();
let output = Command::new(executable_path("just"))
.current_dir(self.tempdir.path())
.arg("--justfile")
.arg(&reparsed_path)
.arg("--dump")
.envs(&self.env)
.output()
.expect("just invocation failed");
if !output.status.success() {
panic!("reparse failed: {}", output.status);
}
let reparsed = String::from_utf8(output.stdout).unwrap();
assert_eq!(reparsed, dumped, "reparse mismatch");
}
let dumped = String::from_utf8(output.stdout).unwrap();
let reparsed_path = tmpdir.join("reparsed.just");
fs::write(&reparsed_path, &dumped).unwrap();
let output = Command::new(executable_path("just"))
.current_dir(tmpdir)
.arg("--justfile")
.arg(&reparsed_path)
.arg("--dump")
.output()
.expect("just invocation failed");
if !output.status.success() {
panic!("reparse failed: {}", output.status);
}
let reparsed = String::from_utf8(output.stdout).unwrap();
assert_eq!(reparsed, dumped, "reparse mismatch");
}
pub fn assert_eval_eq(expression: &str, result: &str) {