nushell/crates/nu-cli/tests/commands/mv.rs

253 lines
6.9 KiB
Rust
Raw Normal View History

2019-12-17 18:54:39 +00:00
use nu_test_support::fs::{files_exist_at, Stub::EmptyFile};
use nu_test_support::playground::Playground;
use nu_test_support::{nu, nu_error};
2019-08-14 20:08:10 +00:00
#[test]
fn moves_a_file() {
2019-08-29 00:32:42 +00:00
Playground::setup("mv_test_1", |dirs, sandbox| {
sandbox
.with_files(vec![EmptyFile("andres.txt")])
2019-08-29 00:32:42 +00:00
.mkdir("expected");
2019-08-14 20:08:10 +00:00
let original = dirs.test().join("andres.txt");
let expected = dirs.test().join("expected/yehuda.txt");
2019-08-14 20:08:10 +00:00
2019-08-29 06:31:56 +00:00
nu!(
cwd: dirs.test(),
"mv andres.txt expected/yehuda.txt"
);
2019-08-14 20:08:10 +00:00
2019-08-29 06:31:56 +00:00
assert!(!original.exists());
assert!(expected.exists());
})
2019-08-14 20:08:10 +00:00
}
#[test]
fn overwrites_if_moving_to_existing_file() {
2019-08-29 00:32:42 +00:00
Playground::setup("mv_test_2", |dirs, sandbox| {
2019-09-11 14:36:50 +00:00
sandbox.with_files(vec![EmptyFile("andres.txt"), EmptyFile("jonathan.txt")]);
2019-08-14 20:08:10 +00:00
let original = dirs.test().join("andres.txt");
let expected = dirs.test().join("jonathan.txt");
2019-08-14 20:08:10 +00:00
2019-08-29 06:31:56 +00:00
nu!(
cwd: dirs.test(),
"mv andres.txt jonathan.txt"
);
2019-08-14 20:08:10 +00:00
2019-08-29 06:31:56 +00:00
assert!(!original.exists());
assert!(expected.exists());
})
2019-08-14 20:08:10 +00:00
}
2019-08-28 17:28:58 +00:00
#[test]
fn moves_a_directory() {
2019-08-29 00:32:42 +00:00
Playground::setup("mv_test_3", |dirs, sandbox| {
sandbox.mkdir("empty_dir");
2019-08-28 17:28:58 +00:00
let original_dir = dirs.test().join("empty_dir");
let expected = dirs.test().join("renamed_dir");
2019-08-29 06:31:56 +00:00
nu!(
cwd: dirs.test(),
"mv empty_dir renamed_dir"
);
2019-08-28 17:28:58 +00:00
2019-08-29 06:31:56 +00:00
assert!(!original_dir.exists());
assert!(expected.exists());
2019-08-28 17:28:58 +00:00
})
}
#[test]
fn moves_the_file_inside_directory_if_path_to_move_is_existing_directory() {
2019-08-29 00:32:42 +00:00
Playground::setup("mv_test_4", |dirs, sandbox| {
sandbox
2019-08-28 17:28:58 +00:00
.with_files(vec![EmptyFile("jonathan.txt")])
2019-08-29 00:32:42 +00:00
.mkdir("expected");
2019-08-28 17:28:58 +00:00
let original_dir = dirs.test().join("jonathan.txt");
let expected = dirs.test().join("expected/jonathan.txt");
2019-08-29 06:31:56 +00:00
nu!(
cwd: dirs.test(),
"mv jonathan.txt expected"
);
2019-08-28 17:28:58 +00:00
2019-08-29 06:31:56 +00:00
assert!(!original_dir.exists());
assert!(expected.exists());
2019-08-28 17:28:58 +00:00
})
}
#[test]
fn moves_the_directory_inside_directory_if_path_to_move_is_existing_directory() {
2019-08-29 00:32:42 +00:00
Playground::setup("mv_test_5", |dirs, sandbox| {
sandbox
2019-08-28 17:28:58 +00:00
.within("contributors")
.with_files(vec![EmptyFile("jonathan.txt")])
2019-08-29 00:32:42 +00:00
.mkdir("expected");
2019-08-28 17:28:58 +00:00
let original_dir = dirs.test().join("contributors");
let expected = dirs.test().join("expected/contributors");
2019-08-29 06:31:56 +00:00
nu!(
cwd: dirs.test(),
"mv contributors expected"
);
2019-08-28 17:28:58 +00:00
2019-08-29 06:31:56 +00:00
assert!(!original_dir.exists());
assert!(expected.exists());
assert!(files_exist_at(vec!["jonathan.txt"], expected))
2019-08-28 17:28:58 +00:00
})
}
#[test]
fn moves_the_directory_inside_directory_if_path_to_move_is_nonexistent_directory() {
2019-08-29 00:32:42 +00:00
Playground::setup("mv_test_6", |dirs, sandbox| {
sandbox
2019-08-28 17:28:58 +00:00
.within("contributors")
.with_files(vec![EmptyFile("jonathan.txt")])
2019-08-29 00:32:42 +00:00
.mkdir("expected");
2019-08-28 17:28:58 +00:00
let original_dir = dirs.test().join("contributors");
nu!(
2019-08-29 06:31:56 +00:00
cwd: dirs.test(),
2019-08-28 17:28:58 +00:00
"mv contributors expected/this_dir_exists_now/los_tres_amigos"
);
let expected = dirs
.test()
.join("expected/this_dir_exists_now/los_tres_amigos");
2019-08-29 06:31:56 +00:00
assert!(!original_dir.exists());
assert!(expected.exists());
2019-08-28 17:28:58 +00:00
})
}
#[test]
fn moves_using_path_with_wildcard() {
2019-08-29 00:32:42 +00:00
Playground::setup("mv_test_7", |dirs, sandbox| {
sandbox
2019-08-28 17:28:58 +00:00
.within("originals")
.with_files(vec![
EmptyFile("andres.ini"),
EmptyFile("caco3_plastics.csv"),
EmptyFile("cargo_sample.toml"),
EmptyFile("jonathan.ini"),
EmptyFile("jonathan.xml"),
EmptyFile("sgml_description.json"),
EmptyFile("sample.ini"),
EmptyFile("utf16.ini"),
2019-09-11 14:36:50 +00:00
EmptyFile("yehuda.ini"),
2019-08-28 17:28:58 +00:00
])
.mkdir("work_dir")
2019-08-29 00:32:42 +00:00
.mkdir("expected");
2019-08-28 17:28:58 +00:00
let work_dir = dirs.test().join("work_dir");
let expected = dirs.test().join("expected");
2019-09-11 14:36:50 +00:00
nu!(cwd: work_dir, "mv ../originals/*.ini ../expected");
2019-08-28 17:28:58 +00:00
assert!(files_exist_at(
2019-08-28 17:28:58 +00:00
vec!["yehuda.ini", "jonathan.ini", "sample.ini", "andres.ini",],
expected
));
})
}
#[test]
fn moves_using_a_glob() {
2019-08-29 00:32:42 +00:00
Playground::setup("mv_test_8", |dirs, sandbox| {
sandbox
2019-08-28 17:28:58 +00:00
.within("meals")
.with_files(vec![
EmptyFile("arepa.txt"),
EmptyFile("empanada.txt"),
2019-09-11 14:36:50 +00:00
EmptyFile("taquiza.txt"),
2019-08-28 17:28:58 +00:00
])
.mkdir("work_dir")
2019-08-29 00:32:42 +00:00
.mkdir("expected");
2019-08-28 17:28:58 +00: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 14:36:50 +00:00
nu!(cwd: work_dir, "mv ../meals/* ../expected");
2019-08-28 17:28:58 +00:00
2019-08-29 06:31:56 +00:00
assert!(meal_dir.exists());
assert!(files_exist_at(
2019-08-28 17:28:58 +00:00
vec!["arepa.txt", "empanada.txt", "taquiza.txt",],
expected
));
})
}
#[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
));
})
}
#[test]
fn errors_if_source_doesnt_exist() {
Playground::setup("mv_test_10", |dirs, sandbox| {
sandbox.mkdir("test_folder");
let actual = nu_error!(
cwd: dirs.root(),
"mv non-existing-file test_folder/"
);
assert!(actual.contains("Invalid File or Pattern"));
})
}
#[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());
})
}