save --append: create file if it doesn't exist (#4156)

* have save --append create file if not exists

Currently, doing:

echo a | save --raw --append file.txt

will fail if file.txt does not exist. This PR changes that

* test that `save --append` will create new file
This commit is contained in:
Braulio Valdivielso Martínez 2021-11-26 18:27:41 +00:00 committed by GitHub
parent 91c270c14a
commit fb197f562a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -47,3 +47,21 @@ fn writes_out_csv() {
assert!(actual.contains("nu,0.14,A new type of shell,MIT,2018")); assert!(actual.contains("nu,0.14,A new type of shell,MIT,2018"));
}) })
} }
#[test]
fn save_append_will_create_file_if_not_exists() {
Playground::setup("save_test_3", |dirs, sandbox| {
sandbox.with_files(vec![]);
let expected_file = dirs.test().join("new-file.txt");
nu!(
cwd: dirs.root(),
r#"echo hello | save --raw --append save_test_3/new-file.txt"#,
);
let actual = file_contents(expected_file);
println!("{}", actual);
assert!(actual == "hello");
})
}

View File

@ -884,7 +884,7 @@ impl Shell for FilesystemShell {
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let mut options = OpenOptions::new(); let mut options = OpenOptions::new();
if append { if append {
options.append(true) options.append(true).create(true)
} else { } else {
options.write(true).create(true).truncate(true) options.write(true).create(true).truncate(true)
}; };