Fix sandboxing of redirection tests (#11407)

When running `cargo test --workspace` a file `crates/nu-command/a.txt`
remained which we also saw as an accidential additions in some commits.

Searching for `a.txt` narrowed it down that
`redirection_keep_exit_codes` was not sandboxed in a temporary directory
and created this file.

Went through redirection tests and placed them in a `Playground` to get
sandboxing `dirs` for `nu!(cwd:`.
For those tests where redirection fails and no file should be created
now I added a check that no file is created on accident.


- Sandbox `redirection_keep_exit_codes` test
- Sandbox `no_duplicate_redirection` test
- Check that no redirect file is created on error
- Sandbox `redirection_should_have_a_target` test
This commit is contained in:
Stefan Holderbach 2023-12-23 20:01:20 +01:00 committed by GitHub
parent 9620e27e4f
commit df1fecd2cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -161,9 +161,14 @@ fn same_target_redirection_with_too_much_stderr_not_hang_nushell() {
#[test] #[test]
fn redirection_keep_exit_codes() { fn redirection_keep_exit_codes() {
let out = nu!("do -i { nu --testbin fail e> a.txt } | complete | get exit_code"); Playground::setup("redirection preserves exit code", |dirs, _| {
let out = nu!(
cwd: dirs.test(),
"do -i { nu --testbin fail e> a.txt } | complete | get exit_code"
);
// needs to use contains "1", because it complete will output `Some(RawStream)`. // needs to use contains "1", because it complete will output `Some(RawStream)`.
assert!(out.out.contains('1')); assert!(out.out.contains('1'));
});
} }
#[test] #[test]
@ -302,6 +307,7 @@ fn separate_redirection_support_variable() {
#[test] #[test]
fn redirection_should_have_a_target() { fn redirection_should_have_a_target() {
Playground::setup("redirection_should_have_a_target", |dirs, _| {
let scripts = [ let scripts = [
"echo asdf o+e>", "echo asdf o+e>",
"echo asdf o>", "echo asdf o>",
@ -313,13 +319,17 @@ fn redirection_should_have_a_target() {
"echo asdf o>; echo asdf", "echo asdf o>; echo asdf",
]; ];
for code in scripts { for code in scripts {
let actual = nu!(code); let actual = nu!(cwd: dirs.test(), code);
assert!( assert!(
actual.err.contains("expected redirection target",), actual.err.contains("expected redirection target",),
"should be error, code: {}", "should be error, code: {code}",
code );
assert!(
!dirs.test().join("tmp.txt").exists(),
"No file should be created on error: {code}",
); );
} }
});
} }
#[test] #[test]
@ -352,8 +362,24 @@ fn redirection_with_pipe() {
#[test] #[test]
fn no_duplicate_redirection() { fn no_duplicate_redirection() {
let actual = nu!("echo 3 o> a.txt o> a.txt"); Playground::setup("redirection does not accept duplicate", |dirs, _| {
let actual = nu!(
cwd: dirs.test(),
"echo 3 o> a.txt o> a.txt"
);
assert!(actual.err.contains("Redirection can be set only once")); assert!(actual.err.contains("Redirection can be set only once"));
let actual = nu!("echo 3 e> a.txt e> a.txt"); assert!(
!dirs.test().join("a.txt").exists(),
"No file should be created on error"
);
let actual = nu!(
cwd: dirs.test(),
"echo 3 e> a.txt e> a.txt"
);
assert!(actual.err.contains("Redirection can be set only once")); assert!(actual.err.contains("Redirection can be set only once"));
assert!(
!dirs.test().join("a.txt").exists(),
"No file should be created on error"
);
});
} }