From fb197f562a778b687822a15b7e9374713324a5bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Braulio=20Valdivielso=20Mart=C3=ADnez?= Date: Fri, 26 Nov 2021 18:27:41 +0000 Subject: [PATCH] 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 --- crates/nu-command/tests/commands/save.rs | 18 ++++++++++++++++++ .../src/filesystem/filesystem_shell.rs | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/tests/commands/save.rs b/crates/nu-command/tests/commands/save.rs index 70eb3937bf..ff953bcfb6 100644 --- a/crates/nu-command/tests/commands/save.rs +++ b/crates/nu-command/tests/commands/save.rs @@ -47,3 +47,21 @@ fn writes_out_csv() { 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"); + }) +} diff --git a/crates/nu-engine/src/filesystem/filesystem_shell.rs b/crates/nu-engine/src/filesystem/filesystem_shell.rs index 1f74919c1b..1967d99c9f 100644 --- a/crates/nu-engine/src/filesystem/filesystem_shell.rs +++ b/crates/nu-engine/src/filesystem/filesystem_shell.rs @@ -884,7 +884,7 @@ impl Shell for FilesystemShell { ) -> Result { let mut options = OpenOptions::new(); if append { - options.append(true) + options.append(true).create(true) } else { options.write(true).create(true).truncate(true) };