mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 14:40:06 +02:00
Isolate tests from user config (#12437)
# Description This is an attempt to isolate the unit tests from whatever might be in the user's config. If the user's config is broken in some way or incompatible with this version (for example, especially if there are plugins that aren't built for this version), tests can spuriously fail. This makes tests more reliably pass the same way they would on CI even if the user has config, and should also make them run faster. I think this is _good enough_, but I still think we should have a specific config dir env variable for nushell specifically (rather than having to use `XDG_CONFIG_HOME`, which would mess with other things) and then we can just have `nu-test-support` set that to a temporary dir containing the shipped default config files. # Tests + Formatting - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib`
This commit is contained in:
@ -47,7 +47,7 @@ fn ignore_shell_errors_works_for_external_with_semicolon() {
|
||||
|
||||
#[test]
|
||||
fn ignore_program_errors_works_for_external_with_semicolon() {
|
||||
let actual = nu!(r#"do -p { nu -c 'exit 1' }; "text""#);
|
||||
let actual = nu!(r#"do -p { nu -n -c 'exit 1' }; "text""#);
|
||||
|
||||
assert_eq!(actual.err, "");
|
||||
assert_eq!(actual.out, "text");
|
||||
|
@ -7,7 +7,7 @@ fn basic_exec() {
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
nu -c 'exec nu --testbin cococo a b c'
|
||||
nu -n -c 'exec nu --testbin cococo a b c'
|
||||
"#
|
||||
));
|
||||
|
||||
@ -21,7 +21,7 @@ fn exec_complex_args() {
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
nu -c 'exec nu --testbin cococo b --bar=2 -sab --arwr - -DTEEE=aasd-290 -90 --'
|
||||
nu -n -c 'exec nu --testbin cococo b --bar=2 -sab --arwr - -DTEEE=aasd-290 -90 --'
|
||||
"#
|
||||
));
|
||||
|
||||
@ -35,7 +35,7 @@ fn exec_fail_batched_short_args() {
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
nu -c 'exec nu --testbin cococo -ab 10'
|
||||
nu -n -c 'exec nu --testbin cococo -ab 10'
|
||||
"#
|
||||
));
|
||||
|
||||
@ -49,7 +49,7 @@ fn exec_misc_values() {
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
nu -c 'let x = "abc"; exec nu --testbin cococo $x ...[ a b c ]'
|
||||
nu -n -c 'let x = "abc"; exec nu --testbin cococo $x ...[ a b c ]'
|
||||
"#
|
||||
));
|
||||
|
||||
|
@ -3,8 +3,8 @@ use nu_test_support::nu;
|
||||
#[test]
|
||||
fn interleave_external_commands() {
|
||||
let result = nu!("interleave \
|
||||
{ nu -c 'print hello; print world' | lines | each { 'greeter: ' ++ $in } } \
|
||||
{ nu -c 'print nushell; print rocks' | lines | each { 'evangelist: ' ++ $in } } | \
|
||||
{ nu -n -c 'print hello; print world' | lines | each { 'greeter: ' ++ $in } } \
|
||||
{ nu -n -c 'print nushell; print rocks' | lines | each { 'evangelist: ' ++ $in } } | \
|
||||
each { print }; null");
|
||||
assert!(result.out.contains("greeter: hello"), "{}", result.out);
|
||||
assert!(result.out.contains("greeter: world"), "{}", result.out);
|
||||
|
@ -18,7 +18,7 @@ fn early_return_if_false() {
|
||||
fn return_works_in_script_without_def_main() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
"nu early_return.nu"
|
||||
"nu -n early_return.nu"
|
||||
));
|
||||
|
||||
assert!(actual.err.is_empty());
|
||||
@ -28,7 +28,7 @@ fn return_works_in_script_without_def_main() {
|
||||
fn return_works_in_script_with_def_main() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats",
|
||||
pipeline("nu early_return_outside_main.nu")
|
||||
pipeline("nu -n early_return_outside_main.nu")
|
||||
);
|
||||
assert!(actual.err.is_empty());
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ fn save_stderr_and_stdout_to_afame_file() {
|
||||
r#"
|
||||
$env.FOO = "bar";
|
||||
$env.BAZ = "ZZZ";
|
||||
do -c {nu -c 'nu --testbin echo_env FOO; nu --testbin echo_env_stderr BAZ'} | save -r save_test_5/new-file.txt --stderr save_test_5/new-file.txt
|
||||
do -c {nu -n -c 'nu --testbin echo_env FOO; nu --testbin echo_env_stderr BAZ'} | save -r save_test_5/new-file.txt --stderr save_test_5/new-file.txt
|
||||
"#,
|
||||
);
|
||||
assert!(actual
|
||||
@ -115,7 +115,7 @@ fn save_stderr_and_stdout_to_diff_file() {
|
||||
r#"
|
||||
$env.FOO = "bar";
|
||||
$env.BAZ = "ZZZ";
|
||||
do -c {nu -c 'nu --testbin echo_env FOO; nu --testbin echo_env_stderr BAZ'} | save -r save_test_6/log.txt --stderr save_test_6/err.txt
|
||||
do -c {nu -n -c 'nu --testbin echo_env FOO; nu --testbin echo_env_stderr BAZ'} | save -r save_test_6/log.txt --stderr save_test_6/err.txt
|
||||
"#,
|
||||
);
|
||||
|
||||
@ -208,7 +208,7 @@ fn save_append_works_on_stderr() {
|
||||
r#"
|
||||
$env.FOO = " New";
|
||||
$env.BAZ = " New Err";
|
||||
do -i {nu -c 'nu --testbin echo_env FOO; nu --testbin echo_env_stderr BAZ'} | save -a -r save_test_11/log.txt --stderr save_test_11/err.txt"#,
|
||||
do -i {nu -n -c 'nu --testbin echo_env FOO; nu --testbin echo_env_stderr BAZ'} | save -a -r save_test_11/log.txt --stderr save_test_11/err.txt"#,
|
||||
);
|
||||
|
||||
let actual = file_contents(expected_file);
|
||||
@ -229,7 +229,7 @@ fn save_not_overrides_err_by_default() {
|
||||
r#"
|
||||
$env.FOO = " New";
|
||||
$env.BAZ = " New Err";
|
||||
do -i {nu -c 'nu --testbin echo_env FOO; nu --testbin echo_env_stderr BAZ'} | save -r save_test_12/log.txt --stderr save_test_12/err.txt"#,
|
||||
do -i {nu -n -c 'nu --testbin echo_env FOO; nu --testbin echo_env_stderr BAZ'} | save -r save_test_12/log.txt --stderr save_test_12/err.txt"#,
|
||||
);
|
||||
|
||||
assert!(actual.err.contains("Destination file already exists"));
|
||||
@ -252,7 +252,7 @@ fn save_override_works_stderr() {
|
||||
r#"
|
||||
$env.FOO = "New";
|
||||
$env.BAZ = "New Err";
|
||||
do -i {nu -c 'nu --testbin echo_env FOO; nu --testbin echo_env_stderr BAZ'} | save -f -r save_test_13/log.txt --stderr save_test_13/err.txt"#,
|
||||
do -i {nu -n -c 'nu --testbin echo_env FOO; nu --testbin echo_env_stderr BAZ'} | save -f -r save_test_13/log.txt --stderr save_test_13/err.txt"#,
|
||||
);
|
||||
|
||||
let actual = file_contents(expected_file);
|
||||
|
@ -6,9 +6,9 @@ def run [
|
||||
--short
|
||||
] {
|
||||
if $short {
|
||||
^$nu.current-exe --commands $'use std; NU_log-level=($system_level) std log ($message_level) --short "test message"'
|
||||
^$nu.current-exe --no-config-file --commands $'use std; NU_log-level=($system_level) std log ($message_level) --short "test message"'
|
||||
} else {
|
||||
^$nu.current-exe --commands $'use std; NU_log-level=($system_level) std log ($message_level) "test message"'
|
||||
^$nu.current-exe --no-config-file --commands $'use std; NU_log-level=($system_level) std log ($message_level) "test message"'
|
||||
}
|
||||
| complete | get --ignore-errors stderr
|
||||
}
|
||||
|
@ -12,12 +12,12 @@ def run-command [
|
||||
] {
|
||||
if ($level_prefix | is-empty) {
|
||||
if ($ansi | is-empty) {
|
||||
^$nu.current-exe --commands $'use std; NU_log-level=($system_level) std log custom "($message)" "($format)" ($log_level)'
|
||||
^$nu.current-exe --no-config-file --commands $'use std; NU_log-level=($system_level) std log custom "($message)" "($format)" ($log_level)'
|
||||
} else {
|
||||
^$nu.current-exe --commands $'use std; NU_log-level=($system_level) std log custom "($message)" "($format)" ($log_level) --ansi "($ansi)"'
|
||||
^$nu.current-exe --no-config-file --commands $'use std; NU_log-level=($system_level) std log custom "($message)" "($format)" ($log_level) --ansi "($ansi)"'
|
||||
}
|
||||
} else {
|
||||
^$nu.current-exe --commands $'use std; NU_log-level=($system_level) std log custom "($message)" "($format)" ($log_level) --level-prefix "($level_prefix)" --ansi "($ansi)"'
|
||||
^$nu.current-exe --no-config-file --commands $'use std; NU_log-level=($system_level) std log custom "($message)" "($format)" ($log_level) --level-prefix "($level_prefix)" --ansi "($ansi)"'
|
||||
}
|
||||
| complete | get --ignore-errors stderr
|
||||
}
|
||||
|
@ -10,9 +10,9 @@ def run-command [
|
||||
--short
|
||||
] {
|
||||
if $short {
|
||||
^$nu.current-exe --commands $'use std; NU_log-level=($system_level) std log ($message_level) --format "($format)" --short "($message)"'
|
||||
^$nu.current-exe --no-config-file --commands $'use std; NU_log-level=($system_level) std log ($message_level) --format "($format)" --short "($message)"'
|
||||
} else {
|
||||
^$nu.current-exe --commands $'use std; NU_log-level=($system_level) std log ($message_level) --format "($format)" "($message)"'
|
||||
^$nu.current-exe --no-config-file --commands $'use std; NU_log-level=($system_level) std log ($message_level) --format "($format)" "($message)"'
|
||||
}
|
||||
| complete | get --ignore-errors stderr
|
||||
}
|
||||
|
@ -255,8 +255,8 @@ pub fn nu_run_test(opts: NuOpts, commands: impl AsRef<str>, with_std: bool) -> O
|
||||
command
|
||||
.env(nu_utils::locale::LOCALE_OVERRIDE_ENV_VAR, locale)
|
||||
.env(NATIVE_PATH_ENV_VAR, paths_joined);
|
||||
// TODO: consider adding custom plugin path for tests to
|
||||
// not interfere with user local environment
|
||||
// Ensure that the user's config doesn't interfere with the tests
|
||||
command.arg("--no-config-file");
|
||||
if !with_std {
|
||||
command.arg("--no-std-lib");
|
||||
}
|
||||
@ -265,6 +265,9 @@ pub fn nu_run_test(opts: NuOpts, commands: impl AsRef<str>, with_std: bool) -> O
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped());
|
||||
|
||||
// Uncomment to debug the command being run:
|
||||
// println!("=== command\n{command:?}\n");
|
||||
|
||||
let process = match command.spawn() {
|
||||
Ok(child) => child,
|
||||
Err(why) => panic!("Can't run test {:?} {}", crate::fs::executable_path(), why),
|
||||
@ -293,8 +296,12 @@ pub fn nu_with_plugin_run_test(cwd: impl AsRef<Path>, plugins: &[&str], command:
|
||||
});
|
||||
|
||||
let temp = tempdir().expect("couldn't create a temporary directory");
|
||||
let temp_plugin_file = temp.path().join("plugin.nu");
|
||||
std::fs::File::create(&temp_plugin_file).expect("couldn't create temporary plugin file");
|
||||
let [temp_config_file, temp_env_config_file, temp_plugin_file] =
|
||||
["config.nu", "env.nu", "plugin.nu"].map(|name| {
|
||||
let temp_file = temp.path().join(name);
|
||||
std::fs::File::create(&temp_file).expect("couldn't create temporary config file");
|
||||
temp_file
|
||||
});
|
||||
|
||||
crate::commands::ensure_plugins_built();
|
||||
|
||||
@ -320,6 +327,10 @@ pub fn nu_with_plugin_run_test(cwd: impl AsRef<Path>, plugins: &[&str], command:
|
||||
let process = match setup_command(&executable_path, &target_cwd)
|
||||
.arg("--commands")
|
||||
.arg(commands)
|
||||
.arg("--config")
|
||||
.arg(temp_config_file)
|
||||
.arg("--env-config")
|
||||
.arg(temp_env_config_file)
|
||||
.arg("--plugin-config")
|
||||
.arg(temp_plugin_file)
|
||||
.stdout(Stdio::piped())
|
||||
|
Reference in New Issue
Block a user