Merge pull request #236 from androbtech/meta_magic_save_test

Test coverage for pull #235
This commit is contained in:
Jonathan Turner 2019-08-02 04:44:06 +12:00 committed by GitHub
commit 1cf8413c49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 25 deletions

View File

@ -81,11 +81,30 @@ fn open_error_if_file_not_found() {
assert!(output.contains("File could not be opened")); assert!(output.contains("File could not be opened"));
} }
#[test]
fn save_figures_out_intelligently_where_to_write_out_with_metadata() {
let (playground_path, tests_dir) = h::setup_playground_for("save_smart_test");
let full_path = format!("{}/{}", playground_path, tests_dir);
let subject_file = format!("{}/{}", full_path , "cargo_sample.toml");
h::copy_file_to("tests/fixtures/formats/cargo_sample.toml", &subject_file);
nu!(
_output,
cwd("tests/fixtures"),
"open nuplayground/save_smart_test/cargo_sample.toml | inc package.version --minor | save"
);
let actual = h::file_contents(&subject_file);
assert!(actual.contains("0.2.0"));
}
#[test] #[test]
fn save_can_write_out_csv() { fn save_can_write_out_csv() {
let (playground_path, tests_dir) = h::setup_playground_for("save_test"); let (playground_path, tests_dir) = h::setup_playground_for("save_test");
let full_path = format!("{}/{}", playground_path, tests_dir ); let full_path = format!("{}/{}", playground_path, tests_dir);
let expected_file = format!("{}/{}", full_path , "cargo_sample.csv"); let expected_file = format!("{}/{}", full_path , "cargo_sample.csv");
nu!( nu!(
@ -100,10 +119,10 @@ fn save_can_write_out_csv() {
#[test] #[test]
fn cp_can_copy_a_file() { fn cp_can_copy_a_file() {
let (playground_path, tests_dir) = h::setup_playground_for("cp_test"); let (playground_path, tests_dir) = h::setup_playground_for("cp_test");
let full_path = format!("{}/{}", playground_path, tests_dir ); let full_path = format!("{}/{}", playground_path, tests_dir);
let expected_file = format!("{}/{}", full_path , "sample.ini" ); let expected_file = format!("{}/{}", full_path , "sample.ini");
nu!( nu!(
_output, _output,
@ -116,10 +135,10 @@ fn cp_can_copy_a_file() {
#[test] #[test]
fn cp_copies_the_file_inside_directory_if_path_to_copy_is_directory() { fn cp_copies_the_file_inside_directory_if_path_to_copy_is_directory() {
let (playground_path, tests_dir) = h::setup_playground_for("cp_test_2"); let (playground_path, tests_dir) = h::setup_playground_for("cp_test_2");
let full_path = format!("{}/{}", playground_path, tests_dir ); let full_path = format!("{}/{}", playground_path, tests_dir);
let expected_file = format!("{}/{}", full_path , "sample.ini" ); let expected_file = format!("{}/{}", full_path , "sample.ini");
nu!( nu!(
_output, _output,
@ -146,12 +165,16 @@ fn cp_error_if_attempting_to_copy_a_directory_to_another_directory() {
#[test] #[test]
fn rm_can_remove_a_file() { fn rm_can_remove_a_file() {
let directory = "tests/fixtures/nuplayground"; let directory = "tests/fixtures/nuplayground";
let file = format!("{}/rm_test.txt", directory); let file = format!("{}/rm_test.txt", directory);
h::create_file_at(&file); h::create_file_at(&file);
nu!(_output, cwd(directory), "rm rm_test.txt"); nu!(
_output,
cwd(directory),
"rm rm_test.txt"
);
assert!(!h::file_exists_at(&file)); assert!(!h::file_exists_at(&file));
} }
@ -178,23 +201,36 @@ fn rm_error_if_attempting_to_delete_a_directory_without_recursive_flag() {
let (playground_path, tests_dir) = h::setup_playground_for("rm_test_2"); let (playground_path, tests_dir) = h::setup_playground_for("rm_test_2");
let full_path = format!("{}/{}", playground_path, tests_dir); let full_path = format!("{}/{}", playground_path, tests_dir);
nu_error!(output, cwd("tests/fixtures/nuplayground"), "rm rm_test_2"); nu_error!(
output,
cwd("tests/fixtures/nuplayground"),
"rm rm_test_2"
);
assert!(h::file_exists_at(&full_path)); assert!(h::file_exists_at(&full_path));
assert!(output.contains("is a directory")); assert!(output.contains("is a directory"));
h::delete_directory_at(&full_path);
} }
#[test] #[test]
fn rm_error_if_attempting_to_delete_single_dot_as_argument() { fn rm_error_if_attempting_to_delete_single_dot_as_argument() {
nu_error!(output, cwd("tests/fixtures/nuplayground"), "rm .");
nu_error!(
output,
cwd("tests/fixtures/nuplayground"),
"rm ."
);
assert!(output.contains("may not be removed")); assert!(output.contains("may not be removed"));
} }
#[test] #[test]
fn rm_error_if_attempting_to_delete_two_dot_as_argument() { fn rm_error_if_attempting_to_delete_two_dot_as_argument() {
nu_error!(output, cwd("tests/fixtures/nuplayground"), "rm ..");
nu_error!(
output,
cwd("tests/fixtures/nuplayground"),
"rm .."
);
assert!(output.contains("may not be removed")); assert!(output.contains("may not be removed"));
} }

View File

@ -15,7 +15,8 @@ fn can_convert_table_to_csv_text_and_from_csv_text_back_into_table() {
#[test] #[test]
fn can_convert_table_to_json_text_and_from_json_text_back_into_table() { fn can_convert_table_to_json_text_and_from_json_text_back_into_table() {
nu!(output, nu!(
output,
cwd("tests/fixtures/formats"), cwd("tests/fixtures/formats"),
"open sgml_description.json | to-json | from-json | get glossary.GlossDiv.GlossList.GlossEntry.GlossSee | echo $it" "open sgml_description.json | to-json | from-json | get glossary.GlossDiv.GlossList.GlossEntry.GlossSee | echo $it"
); );
@ -47,7 +48,8 @@ fn can_convert_table_to_yaml_text_and_from_yaml_text_back_into_table() {
#[test] #[test]
fn can_sort_by_column() { fn can_sort_by_column() {
nu!(output, nu!(
output,
cwd("tests/fixtures/formats"), cwd("tests/fixtures/formats"),
"open cargo_sample.toml --raw | lines | skip 1 | first 4 | split-column \"=\" | sort-by Column1 | skip 1 | first 1 | get Column1 | trim | echo $it" "open cargo_sample.toml --raw | lines | skip 1 | first 4 | split-column \"=\" | sort-by Column1 | skip 1 | first 1 | get Column1 | trim | echo $it"
); );
@ -57,7 +59,8 @@ fn can_sort_by_column() {
#[test] #[test]
fn can_split_by_column() { fn can_split_by_column() {
nu!(output, nu!(
output,
cwd("tests/fixtures/formats"), cwd("tests/fixtures/formats"),
"open cargo_sample.toml --raw | lines | skip 1 | first 1 | split-column \"=\" | get Column1 | trim | echo $it" "open cargo_sample.toml --raw | lines | skip 1 | first 1 | split-column \"=\" | get Column1 | trim | echo $it"
); );

View File

@ -7,9 +7,9 @@ use std::io::Read;
#[macro_export] #[macro_export]
macro_rules! nu { macro_rules! nu {
($out:ident, $cwd:expr, $commands:expr) => { ($out:ident, $cwd:expr, $commands:expr) => {
pub use std::error::Error;
pub use std::io::prelude::*; pub use std::io::prelude::*;
pub use std::process::{Command, Stdio}; pub use std::process::{Command, Stdio};
pub use std::error::Error;
let commands = &*format!( let commands = &*format!(
" "
@ -93,16 +93,21 @@ pub fn setup_playground_for(topic: &str) -> (String, String) {
} }
pub fn file_contents(full_path: &str) -> String { pub fn file_contents(full_path: &str) -> String {
let mut file = std::fs::File::open(full_path).expect("can not open file"); let mut file = std::fs::File::open(full_path).expect("can not open file");
let mut contents = String::new(); let mut contents = String::new();
file.read_to_string(&mut contents).expect("can not read file"); file.read_to_string(&mut contents)
contents .expect("can not read file");
contents
} }
pub fn create_file_at(full_path: &str) { pub fn create_file_at(full_path: &str) {
std::fs::write(PathBuf::from(full_path), "fake data".as_bytes()).expect("can not create file"); std::fs::write(PathBuf::from(full_path), "fake data".as_bytes()).expect("can not create file");
} }
pub fn copy_file_to(source: &str, destination: &str) {
std::fs::copy(source, destination).expect("can not copy file");
}
pub fn file_exists_at(full_path: &str) -> bool { pub fn file_exists_at(full_path: &str) -> bool {
PathBuf::from(full_path).exists() PathBuf::from(full_path).exists()
} }

View File

@ -15,9 +15,11 @@ fn external_num() {
#[test] #[test]
fn inc_plugin() { fn inc_plugin() {
nu!(output, nu!(
output,
cwd("tests/fixtures/formats"), cwd("tests/fixtures/formats"),
"open sgml_description.json | get glossary.GlossDiv.GlossList.GlossEntry.Height | inc | echo $it"); "open sgml_description.json | get glossary.GlossDiv.GlossList.GlossEntry.Height | inc | echo $it"
);
assert_eq!(output, "11"); assert_eq!(output, "11");
} }
@ -26,7 +28,8 @@ fn inc_plugin() {
fn add_plugin() { fn add_plugin() {
nu!(output, nu!(output,
cwd("tests/fixtures/formats"), cwd("tests/fixtures/formats"),
"open cargo_sample.toml | add dev-dependencies.newdep \"1\" | get dev-dependencies.newdep | echo $it"); "open cargo_sample.toml | add dev-dependencies.newdep \"1\" | get dev-dependencies.newdep | echo $it"
);
assert_eq!(output, "1"); assert_eq!(output, "1");
} }
@ -35,7 +38,8 @@ fn add_plugin() {
fn edit_plugin() { fn edit_plugin() {
nu!(output, nu!(output,
cwd("tests/fixtures/formats"), cwd("tests/fixtures/formats"),
"open cargo_sample.toml | edit dev-dependencies.pretty_assertions \"7\" | get dev-dependencies.pretty_assertions | echo $it"); "open cargo_sample.toml | edit dev-dependencies.pretty_assertions \"7\" | get dev-dependencies.pretty_assertions | echo $it"
);
assert_eq!(output, "7"); assert_eq!(output, "7");
} }