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, _| {
// needs to use contains "1", because it complete will output `Some(RawStream)`. let out = nu!(
assert!(out.out.contains('1')); 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)`.
assert!(out.out.contains('1'));
});
} }
#[test] #[test]
@ -302,24 +307,29 @@ fn separate_redirection_support_variable() {
#[test] #[test]
fn redirection_should_have_a_target() { fn redirection_should_have_a_target() {
let scripts = [ Playground::setup("redirection_should_have_a_target", |dirs, _| {
"echo asdf o+e>", let scripts = [
"echo asdf o>", "echo asdf o+e>",
"echo asdf e>", "echo asdf o>",
"echo asdf o> e>", "echo asdf e>",
"echo asdf o> tmp.txt e>", "echo asdf o> e>",
"echo asdf o> e> tmp.txt", "echo asdf o> tmp.txt e>",
"echo asdf o> | ignore", "echo asdf o> e> tmp.txt",
"echo asdf o>; echo asdf", "echo asdf o> | ignore",
]; "echo asdf o>; echo asdf",
for code in scripts { ];
let actual = nu!(code); for code in scripts {
assert!( let actual = nu!(cwd: dirs.test(), code);
actual.err.contains("expected redirection target",), assert!(
"should be error, code: {}", actual.err.contains("expected redirection target",),
code "should be error, 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, _| {
assert!(actual.err.contains("Redirection can be set only once")); let actual = nu!(
let actual = nu!("echo 3 e> a.txt e> a.txt"); cwd: dirs.test(),
assert!(actual.err.contains("Redirection can be set only once")); "echo 3 o> a.txt o> a.txt"
);
assert!(actual.err.contains("Redirection can be set only once"));
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!(
!dirs.test().join("a.txt").exists(),
"No file should be created on error"
);
});
} }