nushell/tests/command_cp_tests.rs

170 lines
4.9 KiB
Rust
Raw Normal View History

2019-08-06 07:05:47 +00:00
mod helpers;
use nu::AbsoluteFile;
2019-08-29 06:31:56 +00:00
use helpers::{files_exist_at, Playground, Stub::*};
use std::path::Path;
2019-08-06 07:05:47 +00:00
#[test]
fn copies_a_file() {
Playground::setup("cp_test_1", |dirs, _| {
nu!(
2019-08-29 06:31:56 +00:00
cwd: dirs.root(),
"cp {} cp_test_1/sample.ini",
dirs.formats().join("sample.ini")
);
2019-08-29 06:31:56 +00:00
assert!(dirs.test().join("sample.ini").exists());
});
2019-08-06 07:05:47 +00:00
}
#[test]
fn copies_the_file_inside_directory_if_path_to_copy_is_directory() {
Playground::setup("cp_test_2", |dirs, _| {
let expected_file = AbsoluteFile::new(dirs.test().join("sample.ini"));
2019-08-06 07:05:47 +00:00
nu!(
2019-08-29 06:31:56 +00:00
cwd: dirs.formats(),
"cp ../formats/sample.ini {}",
expected_file.dir()
);
2019-08-06 07:05:47 +00:00
2019-08-29 06:31:56 +00:00
assert!(dirs.test().join("sample.ini").exists());
})
2019-08-06 07:05:47 +00:00
}
#[test]
fn error_if_attempting_to_copy_a_directory_to_another_directory() {
Playground::setup("cp_test_3", |dirs, _| {
2019-08-29 06:31:56 +00:00
let actual = nu_error!(
cwd: dirs.formats(),
"cp ../formats {}", dirs.test()
);
2019-08-06 07:05:47 +00:00
2019-08-29 00:32:42 +00:00
assert!(actual.contains("../formats"));
assert!(actual.contains("is a directory (not copied)"));
});
2019-08-06 07:05:47 +00:00
}
#[test]
fn copies_the_directory_inside_directory_if_path_to_copy_is_directory_and_with_recursive_flag() {
2019-08-29 00:32:42 +00:00
Playground::setup("cp_test_4", |dirs, sandbox| {
sandbox
.within("originals")
.with_files(vec![
EmptyFile("yehuda.txt"),
EmptyFile("jonathan.txt"),
EmptyFile("andres.txt"),
])
.mkdir("expected");
let expected_dir = dirs.test().join("expected").join("originals");
2019-08-29 06:31:56 +00:00
nu!(
cwd: dirs.test(),
"cp originals expected --recursive"
);
2019-08-29 06:31:56 +00:00
assert!(expected_dir.exists());
assert!(files_exist_at(
vec![
Path::new("yehuda.txt"),
Path::new("jonathan.txt"),
Path::new("andres.txt")
],
2019-08-29 06:31:56 +00:00
expected_dir
));
})
}
#[test]
fn deep_copies_with_recursive_flag() {
2019-08-29 00:32:42 +00:00
Playground::setup("cp_test_5", |dirs, sandbox| {
sandbox
.within("originals")
.with_files(vec![EmptyFile("manifest.txt")])
.within("originals/contributors")
.with_files(vec![
EmptyFile("yehuda.txt"),
EmptyFile("jonathan.txt"),
EmptyFile("andres.txt"),
])
.within("originals/contributors/jonathan")
.with_files(vec![EmptyFile("errors.txt"), EmptyFile("multishells.txt")])
.within("originals/contributors/andres")
.with_files(vec![EmptyFile("coverage.txt"), EmptyFile("commands.txt")])
.within("originals/contributors/yehuda")
.with_files(vec![EmptyFile("defer-evaluation.txt")])
.mkdir("expected");
let expected_dir = dirs.test().join("expected").join("originals");
let jonathans_expected_copied_dir = expected_dir.join("contributors").join("jonathan");
let andres_expected_copied_dir = expected_dir.join("contributors").join("andres");
let yehudas_expected_copied_dir = expected_dir.join("contributors").join("yehuda");
2019-08-29 06:31:56 +00:00
nu!(
cwd: dirs.test(),
"cp originals expected --recursive"
);
2019-08-29 06:31:56 +00:00
assert!(expected_dir.exists());
assert!(files_exist_at(
vec![Path::new("errors.txt"), Path::new("multishells.txt")],
2019-08-29 06:31:56 +00:00
jonathans_expected_copied_dir
));
assert!(files_exist_at(
vec![Path::new("coverage.txt"), Path::new("commands.txt")],
2019-08-29 06:31:56 +00:00
andres_expected_copied_dir
));
assert!(files_exist_at(
vec![Path::new("defer-evaluation.txt")],
2019-08-29 06:31:56 +00:00
yehudas_expected_copied_dir
));
})
}
#[test]
fn copies_using_path_with_wildcard() {
Playground::setup("cp_test_6", |dirs, _| {
2019-08-29 06:31:56 +00:00
nu!(
cwd: dirs.formats(),
"cp ../formats/* {}", dirs.test()
);
assert!(files_exist_at(
vec![
Path::new("caco3_plastics.csv"),
Path::new("cargo_sample.toml"),
Path::new("jonathan.xml"),
Path::new("sample.ini"),
Path::new("sgml_description.json"),
Path::new("utf16.ini"),
],
dirs.test()
));
})
}
#[test]
fn copies_using_a_glob() {
Playground::setup("cp_test_7", |dirs, _| {
2019-08-29 06:31:56 +00:00
nu!(
cwd: dirs.formats(),
"cp * {}", dirs.test()
);
assert!(files_exist_at(
vec![
Path::new("caco3_plastics.csv"),
Path::new("cargo_sample.toml"),
Path::new("jonathan.xml"),
Path::new("sample.ini"),
Path::new("sgml_description.json"),
Path::new("utf16.ini"),
],
dirs.test()
));
});
}