nushell/tests/command_mv_tests.rs

188 lines
5.9 KiB
Rust
Raw Normal View History

2019-08-14 22:08:10 +02:00
mod helpers;
use h::{in_directory as cwd, Playground, Stub::*};
use helpers as h;
use std::path::{Path, PathBuf};
#[test]
fn moves_a_file() {
let sandbox = Playground::setup_for("mv_test_1")
2019-08-26 20:19:05 +02:00
.with_files(vec![EmptyFile("andres.txt")])
2019-08-14 22:08:10 +02:00
.mkdir("expected")
.test_dir_name();
let full_path = format!("{}/{}", Playground::root(), sandbox);
let original = format!("{}/{}", full_path, "andres.txt");
2019-08-26 20:19:05 +02:00
let expected = format!("{}/{}", full_path, "expected/yehuda.txt");
2019-08-14 22:08:10 +02:00
nu!(
_output,
cwd(&full_path),
"mv andres.txt expected/yehuda.txt"
);
assert!(!h::file_exists_at(PathBuf::from(original)));
assert!(h::file_exists_at(PathBuf::from(expected)));
}
#[test]
fn overwrites_if_moving_to_existing_file() {
let sandbox = Playground::setup_for("mv_test_2")
2019-08-26 20:19:05 +02:00
.with_files(vec![EmptyFile("andres.txt"), EmptyFile("jonathan.txt")])
2019-08-14 22:08:10 +02:00
.test_dir_name();
let full_path = format!("{}/{}", Playground::root(), sandbox);
let original = format!("{}/{}", full_path, "andres.txt");
2019-08-26 20:19:05 +02:00
let expected = format!("{}/{}", full_path, "jonathan.txt");
2019-08-14 22:08:10 +02:00
2019-08-26 20:19:05 +02:00
nu!(_output, cwd(&full_path), "mv andres.txt jonathan.txt");
2019-08-14 22:08:10 +02:00
assert!(!h::file_exists_at(PathBuf::from(original)));
assert!(h::file_exists_at(PathBuf::from(expected)));
}
#[test]
fn moves_a_directory() {
let sandbox = Playground::setup_for("mv_test_3")
.mkdir("empty_dir")
.test_dir_name();
let full_path = format!("{}/{}", Playground::root(), sandbox);
2019-08-26 20:19:05 +02:00
let original_dir = format!("{}/{}", full_path, "empty_dir");
let expected = format!("{}/{}", full_path, "renamed_dir");
2019-08-14 22:08:10 +02:00
2019-08-26 20:19:05 +02:00
nu!(_output, cwd(&full_path), "mv empty_dir renamed_dir");
2019-08-14 22:08:10 +02:00
assert!(!h::dir_exists_at(PathBuf::from(original_dir)));
assert!(h::dir_exists_at(PathBuf::from(expected)));
}
#[test]
fn moves_the_file_inside_directory_if_path_to_move_is_existing_directory() {
let sandbox = Playground::setup_for("mv_test_4")
2019-08-26 20:19:05 +02:00
.with_files(vec![EmptyFile("jonathan.txt")])
2019-08-14 22:08:10 +02:00
.mkdir("expected")
.test_dir_name();
let full_path = format!("{}/{}", Playground::root(), sandbox);
2019-08-26 20:19:05 +02:00
let original_dir = format!("{}/{}", full_path, "jonathan.txt");
let expected = format!("{}/{}", full_path, "expected/jonathan.txt");
2019-08-14 22:08:10 +02:00
2019-08-26 20:19:05 +02:00
nu!(_output, cwd(&full_path), "mv jonathan.txt expected");
2019-08-14 22:08:10 +02:00
assert!(!h::file_exists_at(PathBuf::from(original_dir)));
assert!(h::file_exists_at(PathBuf::from(expected)));
}
#[test]
fn moves_the_directory_inside_directory_if_path_to_move_is_existing_directory() {
let sandbox = Playground::setup_for("mv_test_5")
.within("contributors")
2019-08-26 20:19:05 +02:00
.with_files(vec![EmptyFile("jonathan.txt")])
2019-08-14 22:08:10 +02:00
.mkdir("expected")
.test_dir_name();
let full_path = format!("{}/{}", Playground::root(), sandbox);
2019-08-26 20:19:05 +02:00
let original_dir = format!("{}/{}", full_path, "contributors");
let expected = format!("{}/{}", full_path, "expected/contributors");
2019-08-14 22:08:10 +02:00
2019-08-26 20:19:05 +02:00
nu!(_output, cwd(&full_path), "mv contributors expected");
2019-08-14 22:08:10 +02:00
assert!(!h::dir_exists_at(PathBuf::from(original_dir)));
assert!(h::file_exists_at(PathBuf::from(expected)));
}
#[test]
fn moves_the_directory_inside_directory_if_path_to_move_is_nonexistent_directory() {
let sandbox = Playground::setup_for("mv_test_6")
.within("contributors")
2019-08-26 20:19:05 +02:00
.with_files(vec![EmptyFile("jonathan.txt")])
2019-08-14 22:08:10 +02:00
.mkdir("expected")
.test_dir_name();
let full_path = format!("{}/{}", Playground::root(), sandbox);
2019-08-26 20:19:05 +02:00
let original_dir = format!("{}/{}", full_path, "contributors");
2019-08-14 22:08:10 +02:00
nu!(
_output,
cwd(&full_path),
"mv contributors expected/this_dir_exists_now/los_tres_amigos"
);
2019-08-26 20:19:05 +02:00
let expected = format!(
"{}/{}",
full_path, "expected/this_dir_exists_now/los_tres_amigos"
);
2019-08-14 22:08:10 +02:00
assert!(!h::dir_exists_at(PathBuf::from(original_dir)));
assert!(h::file_exists_at(PathBuf::from(expected)));
}
#[test]
fn moves_using_path_with_wildcard() {
let sandbox = Playground::setup_for("mv_test_7")
.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"),
EmptyFile("yehuda.ini"),
])
.mkdir("work_dir")
.mkdir("expected")
.test_dir_name();
let full_path = format!("{}/{}", Playground::root(), sandbox);
let work_dir = format!("{}/{}", full_path, "work_dir");
let expected_copies_path = format!("{}/{}", full_path, "expected");
2019-08-26 20:19:05 +02:00
nu!(_output, cwd(&work_dir), "mv ../originals/*.ini ../expected");
2019-08-14 22:08:10 +02:00
assert!(h::files_exist_at(
vec![
Path::new("yehuda.ini"),
Path::new("jonathan.ini"),
Path::new("sample.ini"),
Path::new("andres.ini"),
],
PathBuf::from(&expected_copies_path)
));
}
#[test]
fn moves_using_a_glob() {
let sandbox = Playground::setup_for("mv_test_8")
.within("meals")
.with_files(vec![
EmptyFile("arepa.txt"),
EmptyFile("empanada.txt"),
EmptyFile("taquiza.txt"),
])
.mkdir("work_dir")
.mkdir("expected")
.test_dir_name();
let full_path = format!("{}/{}", Playground::root(), sandbox);
let meal_dir = format!("{}/{}", full_path, "meals");
let work_dir = format!("{}/{}", full_path, "work_dir");
let expected_copies_path = format!("{}/{}", full_path, "expected");
2019-08-26 20:19:05 +02:00
nu!(_output, cwd(&work_dir), "mv ../meals/* ../expected");
2019-08-14 22:08:10 +02:00
assert!(h::dir_exists_at(PathBuf::from(meal_dir)));
assert!(h::files_exist_at(
vec![
Path::new("arepa.txt"),
Path::new("empanada.txt"),
Path::new("taquiza.txt"),
],
PathBuf::from(&expected_copies_path)
));
2019-08-26 20:19:05 +02:00
}