2023-05-20 18:48:57 +02:00
|
|
|
use nu_test_support::fs::{files_exist_at, Stub::EmptyFile, Stub::FileWithContent};
|
2020-05-07 13:03:43 +02:00
|
|
|
use nu_test_support::nu;
|
2019-12-17 19:54:39 +01:00
|
|
|
use nu_test_support::playground::Playground;
|
2019-08-14 22:08:10 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn moves_a_file() {
|
2019-08-29 02:32:42 +02:00
|
|
|
Playground::setup("mv_test_1", |dirs, sandbox| {
|
|
|
|
sandbox
|
2019-08-28 19:01:16 +02:00
|
|
|
.with_files(vec![EmptyFile("andres.txt")])
|
2019-08-29 02:32:42 +02:00
|
|
|
.mkdir("expected");
|
2019-08-14 22:08:10 +02:00
|
|
|
|
2019-08-28 19:01:16 +02:00
|
|
|
let original = dirs.test().join("andres.txt");
|
|
|
|
let expected = dirs.test().join("expected/yehuda.txt");
|
2019-08-14 22:08:10 +02:00
|
|
|
|
2019-08-29 08:31:56 +02:00
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"mv andres.txt expected/yehuda.txt"
|
|
|
|
);
|
2019-08-14 22:08:10 +02:00
|
|
|
|
2019-08-29 08:31:56 +02:00
|
|
|
assert!(!original.exists());
|
|
|
|
assert!(expected.exists());
|
2019-08-28 19:01:16 +02:00
|
|
|
})
|
2019-08-14 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-10-29 22:16:29 +02:00
|
|
|
fn overwrites_if_moving_to_existing_file_and_force_provided() {
|
2019-08-29 02:32:42 +02:00
|
|
|
Playground::setup("mv_test_2", |dirs, sandbox| {
|
2023-03-15 06:54:55 +01:00
|
|
|
sandbox.with_files(vec![EmptyFile("andres.txt"), EmptyFile("jttxt")]);
|
2019-08-14 22:08:10 +02:00
|
|
|
|
2019-08-28 19:01:16 +02:00
|
|
|
let original = dirs.test().join("andres.txt");
|
2023-03-15 06:54:55 +01:00
|
|
|
let expected = dirs.test().join("jttxt");
|
2019-08-14 22:08:10 +02:00
|
|
|
|
2019-08-29 08:31:56 +02:00
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-03-15 06:54:55 +01:00
|
|
|
"mv andres.txt -f jttxt"
|
2019-08-29 08:31:56 +02:00
|
|
|
);
|
2019-08-14 22:08:10 +02:00
|
|
|
|
2019-08-29 08:31:56 +02:00
|
|
|
assert!(!original.exists());
|
|
|
|
assert!(expected.exists());
|
2019-08-28 19:01:16 +02:00
|
|
|
})
|
2019-08-14 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
2019-08-28 19:28:58 +02:00
|
|
|
#[test]
|
|
|
|
fn moves_a_directory() {
|
2019-08-29 02:32:42 +02:00
|
|
|
Playground::setup("mv_test_3", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("empty_dir");
|
2019-08-28 19:28:58 +02:00
|
|
|
|
|
|
|
let original_dir = dirs.test().join("empty_dir");
|
|
|
|
let expected = dirs.test().join("renamed_dir");
|
|
|
|
|
2019-08-29 08:31:56 +02:00
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"mv empty_dir renamed_dir"
|
|
|
|
);
|
2019-08-28 19:28:58 +02:00
|
|
|
|
2019-08-29 08:31:56 +02:00
|
|
|
assert!(!original_dir.exists());
|
|
|
|
assert!(expected.exists());
|
2019-08-28 19:28:58 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn moves_the_file_inside_directory_if_path_to_move_is_existing_directory() {
|
2019-08-29 02:32:42 +02:00
|
|
|
Playground::setup("mv_test_4", |dirs, sandbox| {
|
|
|
|
sandbox
|
2023-03-15 06:54:55 +01:00
|
|
|
.with_files(vec![EmptyFile("jttxt")])
|
2019-08-29 02:32:42 +02:00
|
|
|
.mkdir("expected");
|
2019-08-28 19:28:58 +02:00
|
|
|
|
2023-03-15 06:54:55 +01:00
|
|
|
let original_dir = dirs.test().join("jttxt");
|
|
|
|
let expected = dirs.test().join("expected/jttxt");
|
2019-08-28 19:28:58 +02:00
|
|
|
|
2019-08-29 08:31:56 +02:00
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-03-15 06:54:55 +01:00
|
|
|
"mv jttxt expected"
|
2019-08-29 08:31:56 +02:00
|
|
|
);
|
2019-08-28 19:28:58 +02:00
|
|
|
|
2019-08-29 08:31:56 +02:00
|
|
|
assert!(!original_dir.exists());
|
|
|
|
assert!(expected.exists());
|
2019-08-28 19:28:58 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn moves_the_directory_inside_directory_if_path_to_move_is_existing_directory() {
|
2019-08-29 02:32:42 +02:00
|
|
|
Playground::setup("mv_test_5", |dirs, sandbox| {
|
|
|
|
sandbox
|
2019-08-28 19:28:58 +02:00
|
|
|
.within("contributors")
|
2023-03-15 06:54:55 +01:00
|
|
|
.with_files(vec![EmptyFile("jttxt")])
|
2019-08-29 02:32:42 +02:00
|
|
|
.mkdir("expected");
|
2019-08-28 19:28:58 +02:00
|
|
|
|
|
|
|
let original_dir = dirs.test().join("contributors");
|
|
|
|
let expected = dirs.test().join("expected/contributors");
|
|
|
|
|
2019-08-29 08:31:56 +02:00
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"mv contributors expected"
|
|
|
|
);
|
2019-08-28 19:28:58 +02:00
|
|
|
|
2019-08-29 08:31:56 +02:00
|
|
|
assert!(!original_dir.exists());
|
|
|
|
assert!(expected.exists());
|
2023-03-15 06:54:55 +01:00
|
|
|
assert!(files_exist_at(vec!["jttxt"], expected))
|
2019-08-28 19:28:58 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn moves_using_path_with_wildcard() {
|
2019-08-29 02:32:42 +02:00
|
|
|
Playground::setup("mv_test_7", |dirs, sandbox| {
|
|
|
|
sandbox
|
2019-08-28 19:28:58 +02:00
|
|
|
.within("originals")
|
|
|
|
.with_files(vec![
|
|
|
|
EmptyFile("andres.ini"),
|
|
|
|
EmptyFile("caco3_plastics.csv"),
|
|
|
|
EmptyFile("cargo_sample.toml"),
|
2023-03-15 06:54:55 +01:00
|
|
|
EmptyFile("jt.ini"),
|
|
|
|
EmptyFile("jt.xml"),
|
2019-08-28 19:28:58 +02:00
|
|
|
EmptyFile("sgml_description.json"),
|
|
|
|
EmptyFile("sample.ini"),
|
|
|
|
EmptyFile("utf16.ini"),
|
2019-09-11 16:36:50 +02:00
|
|
|
EmptyFile("yehuda.ini"),
|
2019-08-28 19:28:58 +02:00
|
|
|
])
|
|
|
|
.mkdir("work_dir")
|
2019-08-29 02:32:42 +02:00
|
|
|
.mkdir("expected");
|
2019-08-28 19:28:58 +02:00
|
|
|
|
|
|
|
let work_dir = dirs.test().join("work_dir");
|
|
|
|
let expected = dirs.test().join("expected");
|
|
|
|
|
2019-09-11 16:36:50 +02:00
|
|
|
nu!(cwd: work_dir, "mv ../originals/*.ini ../expected");
|
2019-08-28 19:28:58 +02:00
|
|
|
|
2019-12-15 17:15:06 +01:00
|
|
|
assert!(files_exist_at(
|
2023-03-15 06:54:55 +01:00
|
|
|
vec!["yehuda.ini", "jt.ini", "sample.ini", "andres.ini",],
|
2019-08-28 19:28:58 +02:00
|
|
|
expected
|
|
|
|
));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn moves_using_a_glob() {
|
2019-08-29 02:32:42 +02:00
|
|
|
Playground::setup("mv_test_8", |dirs, sandbox| {
|
|
|
|
sandbox
|
2019-08-28 19:28:58 +02:00
|
|
|
.within("meals")
|
|
|
|
.with_files(vec![
|
|
|
|
EmptyFile("arepa.txt"),
|
|
|
|
EmptyFile("empanada.txt"),
|
2019-09-11 16:36:50 +02:00
|
|
|
EmptyFile("taquiza.txt"),
|
2019-08-28 19:28:58 +02:00
|
|
|
])
|
|
|
|
.mkdir("work_dir")
|
2019-08-29 02:32:42 +02:00
|
|
|
.mkdir("expected");
|
2019-08-28 19:28:58 +02:00
|
|
|
|
|
|
|
let meal_dir = dirs.test().join("meals");
|
|
|
|
let work_dir = dirs.test().join("work_dir");
|
|
|
|
let expected = dirs.test().join("expected");
|
|
|
|
|
2019-09-11 16:36:50 +02:00
|
|
|
nu!(cwd: work_dir, "mv ../meals/* ../expected");
|
2019-08-28 19:28:58 +02:00
|
|
|
|
2019-08-29 08:31:56 +02:00
|
|
|
assert!(meal_dir.exists());
|
2019-12-15 17:15:06 +01:00
|
|
|
assert!(files_exist_at(
|
2019-08-28 19:28:58 +02:00
|
|
|
vec!["arepa.txt", "empanada.txt", "taquiza.txt",],
|
|
|
|
expected
|
|
|
|
));
|
|
|
|
})
|
|
|
|
}
|
2020-02-07 17:24:01 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn moves_a_directory_with_files() {
|
|
|
|
Playground::setup("mv_test_9", |dirs, sandbox| {
|
|
|
|
sandbox
|
|
|
|
.mkdir("vehicles/car")
|
|
|
|
.mkdir("vehicles/bicycle")
|
|
|
|
.with_files(vec![
|
|
|
|
EmptyFile("vehicles/car/car1.txt"),
|
|
|
|
EmptyFile("vehicles/car/car2.txt"),
|
|
|
|
])
|
|
|
|
.with_files(vec![
|
|
|
|
EmptyFile("vehicles/bicycle/bicycle1.txt"),
|
|
|
|
EmptyFile("vehicles/bicycle/bicycle2.txt"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let original_dir = dirs.test().join("vehicles");
|
|
|
|
let expected_dir = dirs.test().join("expected");
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"mv vehicles expected"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(!original_dir.exists());
|
|
|
|
assert!(expected_dir.exists());
|
|
|
|
assert!(files_exist_at(
|
|
|
|
vec![
|
|
|
|
"car/car1.txt",
|
|
|
|
"car/car2.txt",
|
|
|
|
"bicycle/bicycle1.txt",
|
|
|
|
"bicycle/bicycle2.txt"
|
|
|
|
],
|
|
|
|
expected_dir
|
|
|
|
));
|
|
|
|
})
|
|
|
|
}
|
2020-02-07 18:40:48 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn errors_if_source_doesnt_exist() {
|
|
|
|
Playground::setup("mv_test_10", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("test_folder");
|
2020-05-07 13:03:43 +02:00
|
|
|
let actual = nu!(
|
2020-05-12 22:40:45 +02:00
|
|
|
cwd: dirs.test(),
|
2020-02-07 18:40:48 +01:00
|
|
|
"mv non-existing-file test_folder/"
|
|
|
|
);
|
Allow filesystem commands to access files with glob metachars in name (#10694)
(squashed version of #10557, clean commit history and review thread)
Fixes #10571, also potentially: #10364, #10211, #9558, #9310,
# Description
Changes processing of arguments to filesystem commands that are source
paths or globs.
Applies to `cp, cp-old, mv, rm, du` but not `ls` (because it uses a
different globbing interface) or `glob` (because it uses a different
globbing library).
The core of the change is to lookup the argument first as a file and
only glob if it is not. That way,
a path containing glob metacharacters can be referenced without glob
quoting, though it will have to be single quoted to avoid nushell
parsing.
Before: A file path that looks like a glob is not matched by the glob
specified as a (source) argument and takes some thinking about to
access. You might say the glob pattern shadows a file with the same
spelling.
```
> ls a*
╭───┬────────┬──────┬──────┬────────────────╮
│ # │ name │ type │ size │ modified │
├───┼────────┼──────┼──────┼────────────────┤
│ 0 │ a[bc]d │ file │ 0 B │ 34 seconds ago │
│ 1 │ abd │ file │ 0 B │ now │
│ 2 │ acd │ file │ 0 B │ now │
╰───┴────────┴──────┴──────┴────────────────╯
> cp --verbose 'a[bc]d' dest
copied /home/bobhy/src/rust/work/r4/abd to /home/bobhy/src/rust/work/r4/dest/abd
copied /home/bobhy/src/rust/work/r4/acd to /home/bobhy/src/rust/work/r4/dest/acd
> ## Note -- a[bc]d *not* copied, and seemingly hard to access.
> cp --verbose 'a\[bc\]d' dest
Error: × No matches found
╭─[entry #33:1:1]
1 │ cp --verbose 'a\[bc\]d' dest
· ─────┬────
· ╰── no matches found
╰────
> #.. but is accessible with enough glob quoting.
> cp --verbose 'a[[]bc[]]d' dest
copied /home/bobhy/src/rust/work/r4/a[bc]d to /home/bobhy/src/rust/work/r4/dest/a[bc]d
```
Before_2: if file has glob metachars but isn't a valid pattern, user
gets a confusing error:
```
> touch 'a[b'
> cp 'a[b' dest
Error: × Pattern syntax error near position 30: invalid range pattern
╭─[entry #13:1:1]
1 │ cp 'a[b' dest
· ──┬──
· ╰── invalid pattern
╰────
```
After: Args to cp, mv, etc. are tried first as literal files, and only
as globs if not found to be files.
```
> cp --verbose 'a[bc]d' dest
copied /home/bobhy/src/rust/work/r4/a[bc]d to /home/bobhy/src/rust/work/r4/dest/a[bc]d
> cp --verbose '[a][bc]d' dest
copied /home/bobhy/src/rust/work/r4/abd to /home/bobhy/src/rust/work/r4/dest/abd
copied /home/bobhy/src/rust/work/r4/acd to /home/bobhy/src/rust/work/r4/dest/acd
```
After_2: file with glob metachars but invalid pattern just works.
(though Windows does not allow file name to contain `*`.).
```
> cp --verbose 'a[b' dest
copied /home/bobhy/src/rust/work/r4/a[b to /home/bobhy/src/rust/work/r4/dest/a[b
```
So, with this fix, a file shadows a glob pattern with the same spelling.
If you have such a file and really want to use the glob pattern, you
will have to glob quote some of the characters in the pattern. I think
that's less confusing to the user: if ls shows a file with a weird name,
s/he'll still be able to copy, rename or delete it.
# User-Facing Changes
Could break some existing scripts. If user happened to have a file with
a globbish name but was using a glob pattern with the same spelling, the
new version will process the file and not expand the glob.
# 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` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` 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.
-->
---------
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-10-18 20:31:15 +02:00
|
|
|
assert!(actual.err.contains("file not found"));
|
2020-02-07 18:40:48 +01:00
|
|
|
})
|
|
|
|
}
|
2020-03-24 02:00:48 +01:00
|
|
|
|
2022-10-29 22:16:29 +02:00
|
|
|
#[test]
|
|
|
|
fn error_if_moving_to_existing_file_without_force() {
|
|
|
|
Playground::setup("mv_test_10_0", |dirs, sandbox| {
|
2023-03-15 06:54:55 +01:00
|
|
|
sandbox.with_files(vec![EmptyFile("andres.txt"), EmptyFile("jttxt")]);
|
2022-10-29 22:16:29 +02:00
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-03-15 06:54:55 +01:00
|
|
|
"mv andres.txt jttxt"
|
2022-10-29 22:16:29 +02:00
|
|
|
);
|
|
|
|
assert!(actual.err.contains("file already exists"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-12 22:40:45 +02:00
|
|
|
#[test]
|
|
|
|
fn errors_if_destination_doesnt_exist() {
|
|
|
|
Playground::setup("mv_test_10_1", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![EmptyFile("empty.txt")]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"mv empty.txt does/not/exist"
|
|
|
|
);
|
|
|
|
|
2022-02-16 19:24:45 +01:00
|
|
|
assert!(actual.err.contains("directory not found"));
|
2020-05-12 22:40:45 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn errors_if_multiple_sources_but_destination_not_a_directory() {
|
|
|
|
Playground::setup("mv_test_10_2", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![
|
|
|
|
EmptyFile("file1.txt"),
|
|
|
|
EmptyFile("file2.txt"),
|
|
|
|
EmptyFile("file3.txt"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"mv file?.txt not_a_dir"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual
|
|
|
|
.err
|
|
|
|
.contains("Can only move multiple sources if destination is a directory"));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn errors_if_renaming_directory_to_an_existing_file() {
|
|
|
|
Playground::setup("mv_test_10_3", |dirs, sandbox| {
|
|
|
|
sandbox
|
|
|
|
.mkdir("mydir")
|
|
|
|
.with_files(vec![EmptyFile("empty.txt")]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"mv mydir empty.txt"
|
|
|
|
);
|
|
|
|
|
2022-02-16 19:24:45 +01:00
|
|
|
assert!(actual.err.contains("Can't move a directory"),);
|
|
|
|
assert!(actual.err.contains("to a file"),);
|
2020-05-12 22:40:45 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-01 03:01:05 +02:00
|
|
|
#[test]
|
|
|
|
fn errors_if_moving_to_itself() {
|
|
|
|
Playground::setup("mv_test_10_4", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("mydir").mkdir("mydir/mydir_2");
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
"mv mydir mydir/mydir_2/"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual.err.contains("cannot move to itself"));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-24 02:00:48 +01:00
|
|
|
#[test]
|
|
|
|
fn does_not_error_on_relative_parent_path() {
|
|
|
|
Playground::setup("mv_test_11", |dirs, sandbox| {
|
|
|
|
sandbox
|
|
|
|
.mkdir("first")
|
|
|
|
.with_files(vec![EmptyFile("first/william_hartnell.txt")]);
|
|
|
|
|
|
|
|
let original = dirs.test().join("first/william_hartnell.txt");
|
|
|
|
let expected = dirs.test().join("william_hartnell.txt");
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test().join("first"),
|
|
|
|
"mv william_hartnell.txt ./.."
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(!original.exists());
|
|
|
|
assert!(expected.exists());
|
|
|
|
})
|
|
|
|
}
|
2020-04-06 13:28:56 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_files_using_glob_two_parents_up_using_multiple_dots() {
|
|
|
|
Playground::setup("mv_test_12", |dirs, sandbox| {
|
2020-05-12 22:40:45 +02:00
|
|
|
sandbox.within("foo").within("bar").with_files(vec![
|
2023-03-15 06:54:55 +01:00
|
|
|
EmptyFile("jtjson"),
|
2020-04-06 13:28:56 +02:00
|
|
|
EmptyFile("andres.xml"),
|
|
|
|
EmptyFile("yehuda.yaml"),
|
|
|
|
EmptyFile("kevin.txt"),
|
|
|
|
EmptyFile("many_more.ppl"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test().join("foo/bar"),
|
|
|
|
r#"
|
|
|
|
mv * ...
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
let files = vec![
|
|
|
|
"yehuda.yaml",
|
2023-03-15 06:54:55 +01:00
|
|
|
"jtjson",
|
2020-04-06 13:28:56 +02:00
|
|
|
"andres.xml",
|
|
|
|
"kevin.txt",
|
|
|
|
"many_more.ppl",
|
|
|
|
];
|
|
|
|
|
|
|
|
let original_dir = dirs.test().join("foo/bar");
|
|
|
|
let destination_dir = dirs.test();
|
|
|
|
|
|
|
|
assert!(files_exist_at(files.clone(), destination_dir));
|
|
|
|
assert!(!files_exist_at(files, original_dir))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_file_from_two_parents_up_using_multiple_dots_to_current_dir() {
|
|
|
|
Playground::setup("cp_test_10", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![EmptyFile("hello_there")]);
|
|
|
|
sandbox.within("foo").mkdir("bar");
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test().join("foo/bar"),
|
|
|
|
r#"
|
|
|
|
mv .../hello_there .
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
let expected = dirs.test().join("foo/bar/hello_there");
|
|
|
|
let original = dirs.test().join("hello_there");
|
|
|
|
|
|
|
|
assert!(expected.exists());
|
|
|
|
assert!(!original.exists());
|
|
|
|
})
|
|
|
|
}
|
2020-10-01 03:01:05 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn does_not_error_when_some_file_is_moving_into_itself() {
|
|
|
|
Playground::setup("mv_test_13", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("11").mkdir("12");
|
|
|
|
|
|
|
|
let original_dir = dirs.test().join("11");
|
|
|
|
let expected = dirs.test().join("12/11");
|
2023-04-11 19:21:31 +02:00
|
|
|
nu!(cwd: dirs.test(), "mv 1* 12");
|
2020-10-01 03:01:05 +02:00
|
|
|
|
|
|
|
assert!(!original_dir.exists());
|
|
|
|
assert!(expected.exists());
|
|
|
|
})
|
|
|
|
}
|
2022-08-04 13:59:20 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mv_ignores_ansi() {
|
|
|
|
Playground::setup("mv_test_ansi", |_dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![EmptyFile("test.txt")]);
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: sandbox.cwd(),
|
|
|
|
r#"
|
|
|
|
ls | find test | mv $in.0.name success.txt; ls | $in.0.name
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "success.txt");
|
|
|
|
})
|
|
|
|
}
|
2022-08-10 13:51:11 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mv_directory_with_same_name() {
|
|
|
|
Playground::setup("mv_test_directory_with_same_name", |_dirs, sandbox| {
|
|
|
|
sandbox.mkdir("testdir");
|
|
|
|
sandbox.mkdir("testdir/testdir");
|
|
|
|
|
|
|
|
let cwd = sandbox.cwd().join("testdir");
|
|
|
|
let actual = nu!(
|
2022-08-13 04:13:50 +02:00
|
|
|
cwd: cwd,
|
2022-08-10 13:51:11 +02:00
|
|
|
r#"
|
|
|
|
mv testdir ..
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
2022-11-23 04:55:13 +01:00
|
|
|
assert!(actual.err.contains("is not empty"));
|
2022-08-10 13:51:11 +02:00
|
|
|
})
|
|
|
|
}
|
2022-09-23 20:09:31 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
// Test that changing the case of a file/directory name works;
|
|
|
|
// this is an important edge case on Windows (and any other case-insensitive file systems).
|
|
|
|
// We were bitten badly by this once: https://github.com/nushell/nushell/issues/6583
|
|
|
|
fn mv_change_case_of_directory() {
|
|
|
|
Playground::setup("mv_change_case_of_directory", |dirs, sandbox| {
|
|
|
|
sandbox
|
|
|
|
.mkdir("somedir")
|
|
|
|
.with_files(vec![EmptyFile("somedir/somefile.txt")]);
|
|
|
|
|
|
|
|
let original_dir = String::from("somedir");
|
|
|
|
let new_dir = String::from("SomeDir");
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
format!("mv {original_dir} {new_dir}")
|
|
|
|
);
|
|
|
|
|
|
|
|
// Doing this instead of `Path::exists()` because we need to check file existence in
|
|
|
|
// a case-sensitive way. `Path::exists()` is understandably case-insensitive on NTFS
|
|
|
|
let files_in_test_directory: Vec<String> = std::fs::read_dir(dirs.test())
|
|
|
|
.unwrap()
|
|
|
|
.map(|de| de.unwrap().file_name().to_string_lossy().into_owned())
|
|
|
|
.collect();
|
|
|
|
assert!(!files_in_test_directory.contains(&original_dir));
|
|
|
|
assert!(files_in_test_directory.contains(&new_dir));
|
|
|
|
|
|
|
|
assert!(files_exist_at(
|
|
|
|
vec!["somefile.txt",],
|
|
|
|
dirs.test().join(new_dir)
|
|
|
|
));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mv_change_case_of_file() {
|
|
|
|
Playground::setup("mv_change_case_of_file", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![EmptyFile("somefile.txt")]);
|
|
|
|
|
|
|
|
let original_file_name = String::from("somefile.txt");
|
|
|
|
let new_file_name = String::from("SomeFile.txt");
|
|
|
|
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
2022-10-29 22:16:29 +02:00
|
|
|
format!("mv {original_file_name} -f {new_file_name}")
|
2022-09-23 20:09:31 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Doing this instead of `Path::exists()` because we need to check file existence in
|
|
|
|
// a case-sensitive way. `Path::exists()` is understandably case-insensitive on NTFS
|
|
|
|
let files_in_test_directory: Vec<String> = std::fs::read_dir(dirs.test())
|
|
|
|
.unwrap()
|
|
|
|
.map(|de| de.unwrap().file_name().to_string_lossy().into_owned())
|
|
|
|
.collect();
|
|
|
|
assert!(!files_in_test_directory.contains(&original_file_name));
|
|
|
|
assert!(files_in_test_directory.contains(&new_file_name));
|
|
|
|
})
|
|
|
|
}
|
2023-05-20 18:48:57 +02:00
|
|
|
|
|
|
|
#[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");
|
2023-07-12 18:12:59 +02:00
|
|
|
|
|
|
|
// when destination doesn't exist
|
|
|
|
sandbox.with_files(vec![FileWithContent("newest_valid.txt", "newest_body")]);
|
|
|
|
let actual = nu!(cwd: sandbox.cwd(), "mv -uf newest_valid.txt des_missing.txt; open des_missing.txt");
|
|
|
|
assert_eq!(actual.out, "newest_body");
|
2023-05-20 18:48:57 +02:00
|
|
|
});
|
|
|
|
}
|