add -u flag to cp, mv command (#9214)

# Description
Closes: #7853

I found that I want this feature too...

So I take over it, sorry for that @VincenzoCarlino 

# User-Facing Changes

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
This commit is contained in:
WindSoilder
2023-05-21 00:48:57 +08:00
committed by GitHub
parent 9b139330f8
commit 5a34671343
5 changed files with 113 additions and 5 deletions

View File

@ -1,7 +1,7 @@
use nu_test_support::fs::file_contents;
use nu_test_support::fs::{
files_exist_at, AbsoluteFile,
Stub::{EmptyFile, FileWithPermission},
Stub::{EmptyFile, FileWithContent, FileWithPermission},
};
use nu_test_support::nu;
use nu_test_support::playground::Playground;
@ -579,3 +579,33 @@ fn copy_file_with_read_permission_impl(progress: bool) {
);
});
}
#[test]
fn copy_file_with_update_flag() {
copy_file_with_update_flag_impl(false);
copy_file_with_update_flag_impl(true);
}
fn copy_file_with_update_flag_impl(progress: bool) {
Playground::setup("cp_test_19", |_dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("valid.txt"),
FileWithContent("newer_valid.txt", "body"),
]);
let progress_flag = if progress { "-p" } else { "" };
let actual = nu!(
cwd: sandbox.cwd(),
"cp {} -u valid.txt newer_valid.txt; open newer_valid.txt",
progress_flag,
);
assert!(actual.out.contains("body"));
// create a file after assert to make sure that newest_valid.txt is newest
std::thread::sleep(std::time::Duration::from_secs(1));
sandbox.with_files(vec![FileWithContent("newest_valid.txt", "newest_body")]);
let actual = nu!(cwd: sandbox.cwd(), "cp {} -u newest_valid.txt valid.txt; open valid.txt", progress_flag);
assert_eq!(actual.out, "newest_body");
});
}

View File

@ -1,4 +1,4 @@
use nu_test_support::fs::{files_exist_at, Stub::EmptyFile};
use nu_test_support::fs::{files_exist_at, Stub::EmptyFile, Stub::FileWithContent};
use nu_test_support::nu;
use nu_test_support::playground::Playground;
@ -464,3 +464,25 @@ fn mv_change_case_of_file() {
assert!(files_in_test_directory.contains(&new_file_name));
})
}
#[test]
fn mv_with_update_flag() {
Playground::setup("mv_with_update_flag", |_dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("valid.txt"),
FileWithContent("newer_valid.txt", "body"),
]);
let actual = nu!(
cwd: sandbox.cwd(),
"mv -uf valid.txt newer_valid.txt; open newer_valid.txt",
);
assert_eq!(actual.out, "body");
// create a file after assert to make sure that newest_valid.txt is newest
std::thread::sleep(std::time::Duration::from_secs(1));
sandbox.with_files(vec![FileWithContent("newest_valid.txt", "newest_body")]);
let actual = nu!(cwd: sandbox.cwd(), "mv -uf newest_valid.txt valid.txt; open valid.txt");
assert_eq!(actual.out, "newest_body");
});
}