diff --git a/src/tests.rs b/src/tests.rs index eb1575a035..6d27785b68 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -16,12 +16,25 @@ mod test_type_check; use assert_cmd::prelude::*; use pretty_assertions::assert_eq; +use std::collections::HashMap; use std::io::Write; use std::process::Command; use tempfile::NamedTempFile; pub type TestResult = Result<(), Box>; +pub fn run_test_with_env(input: &str, expected: &str, env: &HashMap<&str, &str>) -> TestResult { + let mut file = NamedTempFile::new()?; + let name = file.path(); + + let mut cmd = Command::cargo_bin("nu")?; + cmd.arg(name).envs(env); + + writeln!(file, "{}", input)?; + + run_cmd_and_assert(cmd, expected) +} + #[cfg(test)] pub fn run_test(input: &str, expected: &str) -> TestResult { let mut file = NamedTempFile::new()?; @@ -32,6 +45,11 @@ pub fn run_test(input: &str, expected: &str) -> TestResult { writeln!(file, "{}", input)?; + run_cmd_and_assert(cmd, expected) +} + +#[cfg(test)] +fn run_cmd_and_assert(mut cmd: Command, expected: &str) -> TestResult { let output = cmd.output()?; let stderr = String::from_utf8_lossy(&output.stderr).to_string(); diff --git a/src/tests/test_parser.rs b/src/tests/test_parser.rs index 849865bf17..44e7f54501 100644 --- a/src/tests/test_parser.rs +++ b/src/tests/test_parser.rs @@ -1,4 +1,5 @@ -use crate::tests::{fail_test, run_test, TestResult}; +use crate::tests::{fail_test, run_test, run_test_with_env, TestResult}; +use std::collections::HashMap; use super::run_test_contains; @@ -197,9 +198,11 @@ fn equals_separates_long_flag() -> TestResult { #[test] fn let_env_expressions() -> TestResult { - run_test( - r#"let-env PATH = if (env | any? name == VENV_OLD_PATH) { $env.VENV_OLD_PATH } else { $env.PATH }; echo done"#, - "done", + let env = HashMap::from([("VENV_OLD_PATH", "Foobar"), ("Path", "Quux")]); + run_test_with_env( + r#"let-env Path = if (env | any? name == VENV_OLD_PATH) { $env.VENV_OLD_PATH } else { $env.Path }; echo $env.Path"#, + "Foobar", + &env, ) } diff --git a/tests/shell/pipeline/commands/external.rs b/tests/shell/pipeline/commands/external.rs index 149e7c24e8..83bb0d73c8 100644 --- a/tests/shell/pipeline/commands/external.rs +++ b/tests/shell/pipeline/commands/external.rs @@ -66,7 +66,7 @@ fn automatically_change_directory_with_trailing_slash_and_same_name_as_command() #[test] fn correctly_escape_external_arguments() { - let actual = nu!(cwd: ".", r#"^echo '$0'"#); + let actual = nu!(cwd: ".", r#"^nu --testbin cococo '$0'"#); assert_eq!(actual.out, "$0"); } @@ -76,8 +76,8 @@ fn execute_binary_in_string() { let actual = nu!( cwd: ".", r#" - let cmd = "echo" - ^$"($cmd)" "$0" + let cmd = "nu" + ^$"($cmd)" --testbin cococo "$0" "#); assert_eq!(actual.out, "$0");