Yo quiero Playground taconushell.

This commit is contained in:
Andrés N. Robalino 2019-08-28 19:32:42 -05:00
parent f82cc4291f
commit 55fb1f8dda
14 changed files with 698 additions and 540 deletions

View File

@ -4,8 +4,8 @@ use helpers::in_directory as cwd;
#[test]
fn cd_directory_not_found() {
let output = nu_error!(cwd("tests/fixtures"), "cd dir_that_does_not_exist");
let actual = nu_error!(cwd("tests/fixtures"), "cd dir_that_does_not_exist");
assert!(output.contains("dir_that_does_not_exist"));
assert!(output.contains("directory not found"));
assert!(actual.contains("dir_that_does_not_exist"));
assert!(actual.contains("directory not found"));
}

View File

@ -1,7 +1,6 @@
mod helpers;
use helpers::{dir_exists_at, file_exists_at, files_exist_at, Playground, Stub::*};
use helpers::{in_directory as cwd, dir_exists_at, file_exists_at, files_exist_at, Playground, Stub::*};
use nu::AbsoluteFile;
use std::path::{Path, PathBuf};
@ -9,7 +8,7 @@ use std::path::{Path, PathBuf};
fn copies_a_file() {
Playground::setup("cp_test_1", |dirs, _| {
nu!(
dirs.root(),
cwd(dirs.root()),
"cp {} cp_test_1/sample.ini",
dirs.formats().join("sample.ini")
);
@ -24,7 +23,7 @@ fn copies_the_file_inside_directory_if_path_to_copy_is_directory() {
let expected_file = AbsoluteFile::new(dirs.test().join("sample.ini"));
nu!(
dirs.formats(),
cwd(dirs.formats()),
"cp ../formats/sample.ini {}",
expected_file.dir()
);
@ -36,17 +35,17 @@ fn copies_the_file_inside_directory_if_path_to_copy_is_directory() {
#[test]
fn error_if_attempting_to_copy_a_directory_to_another_directory() {
Playground::setup("cp_test_3", |dirs, _| {
let output = nu_error!(dirs.formats(), "cp ../formats {}", dirs.test());
let actual = nu_error!(dirs.formats(), "cp ../formats {}", dirs.test());
assert!(output.contains("../formats"));
assert!(output.contains("is a directory (not copied)"));
assert!(actual.contains("../formats"));
assert!(actual.contains("is a directory (not copied)"));
});
}
#[test]
fn copies_the_directory_inside_directory_if_path_to_copy_is_directory_and_with_recursive_flag() {
Playground::setup("cp_test_4", |dirs, playground| {
playground
Playground::setup("cp_test_4", |dirs, sandbox| {
sandbox
.within("originals")
.with_files(vec![
EmptyFile("yehuda.txt"),
@ -57,7 +56,7 @@ fn copies_the_directory_inside_directory_if_path_to_copy_is_directory_and_with_r
let expected_dir = dirs.test().join("expected").join("originals");
nu!(dirs.test(), "cp originals expected --recursive");
nu!(cwd(dirs.test()), "cp originals expected --recursive");
assert!(dir_exists_at(PathBuf::from(&expected_dir)));
assert!(files_exist_at(
@ -91,8 +90,8 @@ fn deep_copies_with_recursive_flag() {
originals/contributors/yehuda/defer-evaluation.txt
"#;
Playground::setup("cp_test_5", |dirs, playground| {
playground
Playground::setup("cp_test_5", |dirs, sandbox| {
sandbox
.within("originals")
.with_files(vec![EmptyFile("manifest.txt")])
.within("originals/contributors")
@ -115,7 +114,7 @@ fn deep_copies_with_recursive_flag() {
let andres_expected_copied_dir = expected_dir.join("contributors").join("andres");
let yehudas_expected_copied_dir = expected_dir.join("contributors").join("yehuda");
nu!(dirs.test(), "cp originals expected --recursive");
nu!(cwd(dirs.test()), "cp originals expected --recursive");
assert!(dir_exists_at(PathBuf::from(&expected_dir)));
assert!(files_exist_at(
@ -136,7 +135,7 @@ fn deep_copies_with_recursive_flag() {
#[test]
fn copies_using_path_with_wildcard() {
Playground::setup("cp_test_6", |dirs, _| {
nu!(dirs.formats(), "cp ../formats/* {}", dirs.test());
nu!(cwd(dirs.formats()), "cp ../formats/* {}", dirs.test());
assert!(files_exist_at(
vec![
@ -155,7 +154,7 @@ fn copies_using_path_with_wildcard() {
#[test]
fn copies_using_a_glob() {
Playground::setup("cp_test_7", |dirs, _| {
nu!(dirs.formats(), "cp * {}", dirs.test());
nu!(cwd(dirs.formats()), "cp * {}", dirs.test());
assert!(files_exist_at(
vec![

View File

@ -1,13 +1,13 @@
mod helpers;
use h::{Playground, Stub::*};
use h::{in_directory as cwd, Playground, Stub::*};
use helpers as h;
use std::path::{Path, PathBuf};
#[test]
fn knows_the_filesystems_entered() {
Playground::setup("enter_filesystem_sessions_test", |dirs, playground| {
playground
Playground::setup("enter_test_1", |dirs, sandbox| {
sandbox
.within("red_pill")
.with_files(vec![
EmptyFile("andres.nu"),
@ -20,8 +20,7 @@ fn knows_the_filesystems_entered() {
EmptyFile("korn.nxt"),
EmptyFile("powedsh.nxt"),
])
.mkdir("expected")
.test_dir_name();
.mkdir("expected");
let red_pill_dir = dirs.test().join("red_pill");
let blue_pill_dir = dirs.test().join("blue_pill");
@ -29,7 +28,7 @@ fn knows_the_filesystems_entered() {
let expected_recycled = expected.join("recycled");
nu!(
dirs.test(),
cwd(dirs.test()),
r#"
enter expected
mkdir recycled

View File

@ -5,62 +5,86 @@ use helpers as h;
#[test]
fn ls_lists_regular_files() {
Playground::setup("ls_lists_files_test", |dirs, playground| {
playground
Playground::setup("ls_test_1", |dirs, sandbox| {
sandbox
.with_files(vec![
EmptyFile("yehuda.10.txt"),
EmptyFile("jonathan.10.txt"),
EmptyFile("andres.10.txt"),
])
.test_dir_name();
]);
let output = nu!(
dirs.test(),
r#"ls | get name | lines | split-column "." | get Column2 | str --to-int | sum | echo $it"#
);
let actual = nu!(
cwd(dirs.test()), h::pipeline(
r#"
ls
| get name
| lines
| split-column "."
| get Column2
| str --to-int
| sum
| echo $it
"#
));
assert_eq!(output, "30");
assert_eq!(actual, "30");
})
}
#[test]
fn ls_lists_regular_files_using_asterisk_wildcard() {
Playground::setup("ls_asterisk_wildcard_test", |dirs, playground| {
playground
Playground::setup("ls_test_2", |dirs, sandbox| {
sandbox
.with_files(vec![
EmptyFile("los.1.txt"),
EmptyFile("tres.1.txt"),
EmptyFile("amigos.1.txt"),
EmptyFile("arepas.1.clu"),
])
.test_dir_name();
]);
let output = nu!(
dirs.test(),
r#"ls *.txt | get name | lines| split-column "." | get Column2 | str --to-int | sum | echo $it"#
);
let actual = nu!(
cwd(dirs.test()), h::pipeline(
r#"
ls *.txt
| get name
| lines
| split-column "."
| get Column2
| str --to-int
| sum
| echo $it
"#
));
assert_eq!(output, "3");
assert_eq!(actual, "3");
})
}
#[test]
fn ls_lists_regular_files_using_question_mark_wildcard() {
Playground::setup("ls_question_mark_wildcard_test", |dirs, playground| {
playground
Playground::setup("ls_test_3", |dirs, sandbox| {
sandbox
.with_files(vec![
EmptyFile("yehuda.10.txt"),
EmptyFile("jonathan.10.txt"),
EmptyFile("andres.10.txt"),
EmptyFile("chicken_not_to_be_picked_up.100.txt"),
])
.test_dir_name();
]);
let output = nu!(
dirs.test(),
r#"ls *.??.txt | get name | lines| split-column "." | get Column2 | str --to-int | sum | echo $it"#
);
let actual = nu!(
cwd(dirs.test()), h::pipeline(
r#"
ls *.??.txt
| get name
| lines
| split-column "."
| get Column2
| str --to-int
| sum
| echo $it
"#
));
assert_eq!(output, "30");
assert_eq!(actual, "30");
})
}

View File

@ -1,13 +1,13 @@
mod helpers;
use h::Playground;
use h::{in_directory as cwd, Playground};
use helpers as h;
use std::path::{Path, PathBuf};
#[test]
fn creates_directory() {
Playground::setup("mkdir_test_1", |dirs, _| {
nu!(dirs.test(), "mkdir my_new_directory");
nu!(cwd(dirs.test()), "mkdir my_new_directory");
let expected = dirs.test().join("my_new_directory");
@ -18,7 +18,7 @@ fn creates_directory() {
#[test]
fn accepts_and_creates_directories() {
Playground::setup("mkdir_test_2", |dirs, _| {
nu!(dirs.test(), "mkdir dir_1 dir_2 dir_3");
nu!(cwd(dirs.test()), "mkdir dir_1 dir_2 dir_3");
assert!(h::files_exist_at(
vec![Path::new("dir_1"), Path::new("dir_2"), Path::new("dir_3")],
@ -30,7 +30,7 @@ fn accepts_and_creates_directories() {
#[test]
fn creates_intermediary_directories() {
Playground::setup("mkdir_test_3", |dirs, _| {
nu!(dirs.test(), "mkdir some_folder/another/deeper_one");
nu!(cwd(dirs.test()), "mkdir some_folder/another/deeper_one");
let mut expected = PathBuf::from(dirs.test());
expected.push("some_folder/another/deeper_one");

View File

@ -3,20 +3,17 @@ mod helpers;
use h::{in_directory as cwd, Playground, Stub::*};
use helpers as h;
use std::path::{Path, PathBuf};
#[test]
fn moves_a_file() {
Playground::setup("mv_test_1", |dirs, playground| {
playground
Playground::setup("mv_test_1", |dirs, sandbox| {
sandbox
.with_files(vec![EmptyFile("andres.txt")])
.mkdir("expected")
.test_dir_name();
.mkdir("expected");
let original = dirs.test().join("andres.txt");
let expected = dirs.test().join("expected/yehuda.txt");
nu!(dirs.test(), "mv andres.txt expected/yehuda.txt");
nu!(cwd(dirs.test()), "mv andres.txt expected/yehuda.txt");
assert!(!h::file_exists_at(original));
assert!(h::file_exists_at(expected));
@ -25,15 +22,17 @@ fn moves_a_file() {
#[test]
fn overwrites_if_moving_to_existing_file() {
Playground::setup("mv_test_2", |dirs, playground| {
playground
.with_files(vec![EmptyFile("andres.txt"), EmptyFile("jonathan.txt")])
.test_dir_name();
Playground::setup("mv_test_2", |dirs, sandbox| {
sandbox
.with_files(vec![
EmptyFile("andres.txt"),
EmptyFile("jonathan.txt")
]);
let original = dirs.test().join("andres.txt");
let expected = dirs.test().join("jonathan.txt");
nu!(dirs.test(), "mv andres.txt jonathan.txt");
nu!(cwd(dirs.test()), "mv andres.txt jonathan.txt");
assert!(!h::file_exists_at(original));
assert!(h::file_exists_at(expected));
@ -42,13 +41,13 @@ fn overwrites_if_moving_to_existing_file() {
#[test]
fn moves_a_directory() {
Playground::setup("mv_test_3", |dirs, playground| {
playground.mkdir("empty_dir");
Playground::setup("mv_test_3", |dirs, sandbox| {
sandbox.mkdir("empty_dir");
let original_dir = dirs.test().join("empty_dir");
let expected = dirs.test().join("renamed_dir");
nu!(dirs.test(), "mv empty_dir renamed_dir");
nu!(cwd(dirs.test()), "mv empty_dir renamed_dir");
assert!(!h::dir_exists_at(original_dir));
assert!(h::dir_exists_at(expected));
@ -57,11 +56,10 @@ fn moves_a_directory() {
#[test]
fn moves_the_file_inside_directory_if_path_to_move_is_existing_directory() {
Playground::setup("mv_test_4", |dirs, playground| {
playground
Playground::setup("mv_test_4", |dirs, sandbox| {
sandbox
.with_files(vec![EmptyFile("jonathan.txt")])
.mkdir("expected")
.test_dir_name();
.mkdir("expected");
let original_dir = dirs.test().join("jonathan.txt");
let expected = dirs.test().join("expected/jonathan.txt");
@ -75,12 +73,11 @@ fn moves_the_file_inside_directory_if_path_to_move_is_existing_directory() {
#[test]
fn moves_the_directory_inside_directory_if_path_to_move_is_existing_directory() {
Playground::setup("mv_test_5", |dirs, playground| {
playground
Playground::setup("mv_test_5", |dirs, sandbox| {
sandbox
.within("contributors")
.with_files(vec![EmptyFile("jonathan.txt")])
.mkdir("expected")
.test_dir_name();
.mkdir("expected");
let original_dir = dirs.test().join("contributors");
let expected = dirs.test().join("expected/contributors");
@ -94,17 +91,16 @@ fn moves_the_directory_inside_directory_if_path_to_move_is_existing_directory()
#[test]
fn moves_the_directory_inside_directory_if_path_to_move_is_nonexistent_directory() {
Playground::setup("mv_test_6", |dirs, playground| {
playground
Playground::setup("mv_test_6", |dirs, sandbox| {
sandbox
.within("contributors")
.with_files(vec![EmptyFile("jonathan.txt")])
.mkdir("expected")
.test_dir_name();
.mkdir("expected");
let original_dir = dirs.test().join("contributors");
nu!(
dirs.test(),
cwd(dirs.test()),
"mv contributors expected/this_dir_exists_now/los_tres_amigos"
);
@ -119,8 +115,8 @@ fn moves_the_directory_inside_directory_if_path_to_move_is_nonexistent_directory
#[test]
fn moves_using_path_with_wildcard() {
Playground::setup("mv_test_7", |dirs, playground| {
playground
Playground::setup("mv_test_7", |dirs, sandbox| {
sandbox
.within("originals")
.with_files(vec![
EmptyFile("andres.ini"),
@ -131,16 +127,15 @@ fn moves_using_path_with_wildcard() {
EmptyFile("sgml_description.json"),
EmptyFile("sample.ini"),
EmptyFile("utf16.ini"),
EmptyFile("yehuda.ini"),
EmptyFile("yehuda.ini")
])
.mkdir("work_dir")
.mkdir("expected")
.test_dir_name();
.mkdir("expected");
let work_dir = dirs.test().join("work_dir");
let expected = dirs.test().join("expected");
nu!(work_dir, "mv ../originals/*.ini ../expected");
nu!(cwd(work_dir), "mv ../originals/*.ini ../expected");
assert!(h::files_exist_at(
vec!["yehuda.ini", "jonathan.ini", "sample.ini", "andres.ini",],
@ -151,23 +146,22 @@ fn moves_using_path_with_wildcard() {
#[test]
fn moves_using_a_glob() {
Playground::setup("mv_test_8", |dirs, playground| {
playground
Playground::setup("mv_test_8", |dirs, sandbox| {
sandbox
.within("meals")
.with_files(vec![
EmptyFile("arepa.txt"),
EmptyFile("empanada.txt"),
EmptyFile("taquiza.txt"),
EmptyFile("taquiza.txt")
])
.mkdir("work_dir")
.mkdir("expected")
.test_dir_name();
.mkdir("expected");
let meal_dir = dirs.test().join("meals");
let work_dir = dirs.test().join("work_dir");
let expected = dirs.test().join("expected");
nu!(work_dir, "mv ../meals/* ../expected");
nu!(cwd(work_dir), "mv ../meals/* ../expected");
assert!(h::dir_exists_at(meal_dir));
assert!(h::files_exist_at(

View File

@ -1,106 +1,126 @@
mod helpers;
use helpers::{in_directory as cwd, Playground, Stub::*};
use helpers as h;
#[test]
fn recognizes_csv() {
Playground::setup_for("open_recognizes_csv_test").with_files(vec![FileWithContentToBeTrimmed(
"nu.zion.csv",
r#"
author,lang,source
Jonathan Turner,Rust,New Zealand
Andres N. Robalino,Rust,Ecuador
Yehuda Katz,Rust,Estados Unidos
"#,
)]);
Playground::setup("open_test_1", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"nu.zion.csv",
r#"
author,lang,source
Jonathan Turner,Rust,New Zealand
Andres N. Robalino,Rust,Ecuador
Yehuda Katz,Rust,Estados Unidos
"#
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/open_recognizes_csv_test"),
r#"open nu.zion.csv | where author == "Andres N. Robalino" | get source | echo $it"#
);
let actual = nu!(
cwd(dirs.test()), h::pipeline(
r#"
open nu.zion.csv
| where author == "Andres N. Robalino"
| get source
| echo $it
"#
));
assert_eq!(output, "Ecuador");
assert_eq!(actual, "Ecuador");
})
}
#[test]
fn open_can_parse_bson_1() {
let output = nu!(
let actual = nu!(
cwd("tests/fixtures/formats"),
"open sample.bson | get root | nth 0 | get b | echo $it"
);
assert_eq!(output, "hello");
assert_eq!(actual, "hello");
}
#[test]
fn open_can_parse_bson_2() {
let output = nu!(
cwd("tests/fixtures/formats"),
"open sample.bson | get root | nth 6 | get b | get '$binary_subtype' | echo $it "
);
let actual = nu!(
cwd("tests/fixtures/formats"), h::pipeline(
r#"
open sample.bson
| get root
| nth 6
| get b
| get '$binary_subtype'
| echo $it
"#
));
assert_eq!(output, "function");
assert_eq!(actual, "function");
}
#[test]
fn open_can_parse_toml() {
let output = nu!(
let actual = nu!(
cwd("tests/fixtures/formats"),
"open cargo_sample.toml | get package.edition | echo $it"
);
assert_eq!(output, "2018");
assert_eq!(actual, "2018");
}
#[test]
fn open_can_parse_json() {
let output = nu!(
cwd("tests/fixtures/formats"),
"open sgml_description.json | get glossary.GlossDiv.GlossList.GlossEntry.GlossSee | echo $it"
);
let actual = nu!(
cwd("tests/fixtures/formats"), h::pipeline(
r#"
open sgml_description.json
| get glossary.GlossDiv.GlossList.GlossEntry.GlossSee
| echo $it
"#
));
assert_eq!(output, "markup")
assert_eq!(actual, "markup")
}
#[test]
fn open_can_parse_xml() {
let output = nu!(
let actual = nu!(
cwd("tests/fixtures/formats"),
"open jonathan.xml | get rss.channel.item.link | echo $it"
);
assert_eq!(
output,
actual,
"http://www.jonathanturner.org/2015/10/off-to-new-adventures.html"
)
}
#[test]
fn open_can_parse_ini() {
let output = nu!(
let actual = nu!(
cwd("tests/fixtures/formats"),
"open sample.ini | get SectionOne.integer | echo $it"
);
assert_eq!(output, "1234")
assert_eq!(actual, "1234")
}
#[test]
fn open_can_parse_utf16_ini() {
let output = nu!(
let actual = nu!(
cwd("tests/fixtures/formats"),
"open utf16.ini | get .ShellClassInfo | get IconIndex | echo $it"
);
assert_eq!(output, "-236")
assert_eq!(actual, "-236")
}
#[test]
fn errors_if_file_not_found() {
let output = nu_error!(
let actual = nu_error!(
cwd("tests/fixtures/formats"),
"open i_dont_exist.txt | echo $it"
);
assert!(output.contains("File could not be opened"));
assert!(actual.contains("File could not be opened"));
}

View File

@ -2,16 +2,15 @@ mod helpers;
use h::{in_directory as cwd, Playground, Stub::*};
use helpers as h;
use std::path::{Path, PathBuf};
#[test]
fn rm_removes_a_file() {
Playground::setup("rm_regular_file_test", |dirs, playground| {
playground
.with_files(vec![EmptyFile("i_will_be_deleted.txt")])
.test_dir_name();
Playground::setup("rm_test_1", |dirs, sandbox| {
sandbox
.with_files(vec![EmptyFile("i_will_be_deleted.txt")
]);
nu!(dirs.root(), "rm rm_regular_file_test/i_will_be_deleted.txt");
nu!(cwd(dirs.root()), "rm rm_test_1/i_will_be_deleted.txt");
let path = dirs.test().join("i_will_be_deleted.txt");
@ -21,13 +20,13 @@ fn rm_removes_a_file() {
#[test]
fn rm_removes_files_with_wildcard() {
Playground::setup("rm_wildcard_test_1", |dirs, playground| {
playground
Playground::setup("rm_test_2", |dirs, sandbox| {
sandbox
.within("src")
.with_files(vec![
EmptyFile("cli.rs"),
EmptyFile("lib.rs"),
EmptyFile("prelude.rs"),
EmptyFile("prelude.rs")
])
.within("src/parser")
.with_files(vec![EmptyFile("parse.rs"), EmptyFile("parser.rs")])
@ -36,11 +35,10 @@ fn rm_removes_files_with_wildcard() {
.within("src/parser/hir")
.with_files(vec![
EmptyFile("baseline_parse.rs"),
EmptyFile("baseline_parse_tokens.rs"),
])
.test_dir_name();
EmptyFile("baseline_parse_tokens.rs")
]);
nu!(dirs.test(), r#"rm "src/*/*/*.rs""#);
nu!(cwd(dirs.test()), r#"rm "src/*/*/*.rs""#);
assert!(!h::files_exist_at(
vec![
@ -53,20 +51,20 @@ fn rm_removes_files_with_wildcard() {
assert_eq!(
Playground::glob_vec(&format!("{}/src/*/*/*.rs", dirs.test().display())),
Vec::<PathBuf>::new()
Vec::<std::path::PathBuf>::new()
);
})
}
#[test]
fn rm_removes_deeply_nested_directories_with_wildcard_and_recursive_flag() {
Playground::setup("rm_wildcard_test_2", |dirs, playground| {
playground
Playground::setup("rm_test_3", |dirs, sandbox| {
sandbox
.within("src")
.with_files(vec![
EmptyFile("cli.rs"),
EmptyFile("lib.rs"),
EmptyFile("prelude.rs"),
EmptyFile("prelude.rs")
])
.within("src/parser")
.with_files(vec![EmptyFile("parse.rs"), EmptyFile("parser.rs")])
@ -75,11 +73,10 @@ fn rm_removes_deeply_nested_directories_with_wildcard_and_recursive_flag() {
.within("src/parser/hir")
.with_files(vec![
EmptyFile("baseline_parse.rs"),
EmptyFile("baseline_parse_tokens.rs"),
])
.test_dir_name();
EmptyFile("baseline_parse_tokens.rs")
]);
nu!(dirs.test(), "rm src/* --recursive");
nu!(cwd(dirs.test()), "rm src/* --recursive");
assert!(!h::files_exist_at(
vec!["src/parser/parse", "src/parser/hir"],
@ -90,8 +87,8 @@ fn rm_removes_deeply_nested_directories_with_wildcard_and_recursive_flag() {
#[test]
fn rm_removes_directory_contents_without_recursive_flag_if_empty() {
Playground::setup("rm_directory_removal_recursively_test_1", |dirs, _| {
nu!(dirs.root(), "rm rm_directory_removal_recursively_test_1");
Playground::setup("rm_test_4", |dirs, _| {
nu!(cwd(dirs.root()), "rm rm_test_4");
assert!(!h::file_exists_at(dirs.test()));
})
@ -99,67 +96,51 @@ fn rm_removes_directory_contents_without_recursive_flag_if_empty() {
#[test]
fn rm_removes_directory_contents_with_recursive_flag() {
Playground::setup(
"rm_directory_removal_recursively_test_2",
|dirs, playground| {
playground
.with_files(vec![
EmptyFile("yehuda.txt"),
EmptyFile("jonathan.txt"),
EmptyFile("andres.txt"),
])
.test_dir_name();
Playground::setup("rm_test_5", |dirs, sandbox| {
sandbox
.with_files(vec![
EmptyFile("yehuda.txt"),
EmptyFile("jonathan.txt"),
EmptyFile("andres.txt")
]);
nu!(
dirs.root(),
"rm rm_directory_removal_recursively_test_2 --recursive"
);
nu!(cwd(dirs.root()), "rm rm_test_5 --recursive");
assert!(!h::file_exists_at(dirs.test()));
},
)
assert!(!h::file_exists_at(dirs.test()));
})
}
#[test]
fn rm_errors_if_attempting_to_delete_a_directory_with_content_without_recursive_flag() {
Playground::setup(
"rm_prevent_directory_removal_without_flag_test",
|dirs, playground| {
playground
.with_files(vec![EmptyFile("some_empty_file.txt")])
.test_dir_name();
Playground::setup("rm_test_6", |dirs, sandbox| {
sandbox
.with_files(vec![EmptyFile("some_empty_file.txt")
]);
let output = nu_error!(
dirs.root(),
"rm rm_prevent_directory_removal_without_flag_test"
);
let actual = nu_error!(
cwd(dirs.root()),
"rm rm_test_6"
);
assert!(h::file_exists_at(dirs.test()));
assert!(output.contains("is a directory"));
},
)
assert!(h::file_exists_at(dirs.test()));
assert!(actual.contains("is a directory"));
})
}
#[test]
fn rm_errors_if_attempting_to_delete_single_dot_as_argument() {
Playground::setup(
"rm_errors_if_attempting_to_delete_single_dot_as_argument",
|dirs, _| {
let output = nu_error!(dirs.root(), "rm .");
Playground::setup("rm_test_7", |dirs, _| {
let actual = nu_error!(cwd(dirs.root()), "rm .");
assert!(output.contains("may not be removed"));
},
)
assert!(actual.contains("may not be removed"));
})
}
#[test]
fn rm_errors_if_attempting_to_delete_two_dot_as_argument() {
Playground::setup(
"rm_errors_if_attempting_to_delete_single_dot_as_argument",
|dirs, _| {
let output = nu_error!(dirs.root(), "rm ..");
Playground::setup("rm_test_8", |dirs, _| {
let actual = nu_error!(cwd(dirs.root()), "rm ..");
assert!(output.contains("may not be removed"));
},
)
assert!(actual.contains("may not be removed"));
})
}

View File

@ -5,21 +5,20 @@ use helpers as h;
#[test]
fn lines() {
let output = nu!(
let actual = nu!(
cwd("tests/fixtures/formats"),
r#"open cargo_sample.toml --raw | lines | skip-while $it != "[dependencies]" | skip 1 | first 1 | split-column "=" | get Column1 | trim | echo $it"#
);
assert_eq!(output, "rustyline");
assert_eq!(actual, "rustyline");
}
#[test]
fn save_figures_out_intelligently_where_to_write_out_with_metadata() {
Playground::setup("save_smart_test", |dirs, playground| {
playground
.with_files(vec![FileWithContent(
"cargo_sample.toml",
r#"
Playground::setup("save_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"cargo_sample.toml",
r#"
[package]
name = "nu"
version = "0.1.1"
@ -27,15 +26,14 @@ fn save_figures_out_intelligently_where_to_write_out_with_metadata() {
description = "A shell for the GitHub era"
license = "ISC"
edition = "2018"
"#,
)])
.test_dir_name();
"#)
]);
let subject_file = dirs.test().join("cargo_sample.toml");
nu!(
dirs.root(),
"open save_smart_test/cargo_sample.toml | inc package.version --minor | save"
cwd(dirs.root()),
"open save_test_1/cargo_sample.toml | inc package.version --minor | save"
);
let actual = h::file_contents(&subject_file);
@ -45,12 +43,12 @@ fn save_figures_out_intelligently_where_to_write_out_with_metadata() {
#[test]
fn save_can_write_out_csv() {
Playground::setup("save_writes_out_csv_test", |dirs, playground| {
Playground::setup("save_test_2", |dirs, _| {
let expected_file = dirs.test().join("cargo_sample.csv");
nu!(
dirs.root(),
"open {}/cargo_sample.toml | inc package.version --minor | get package | save save_writes_out_csv_test/cargo_sample.csv",
"open {}/cargo_sample.toml | inc package.version --minor | get package | save save_test_2/cargo_sample.csv",
dirs.formats()
);

View File

@ -4,7 +4,7 @@ use helpers::in_directory as cwd;
#[test]
fn external_command() {
let output = nu!(cwd("tests/fixtures"), "echo 1");
let actual = nu!(cwd("tests/fixtures"), "echo 1");
assert!(output.contains("1"));
assert!(actual.contains("1"));
}

View File

@ -5,124 +5,136 @@ use helpers as h;
#[test]
fn can_only_apply_one() {
let output = nu_error!(
let actual = nu_error!(
cwd("tests/fixtures/formats"),
"open cargo_sample.toml | first 1 | inc package.version --major --minor"
);
assert!(output.contains("Usage: inc field [--major|--minor|--patch]"));
assert!(actual.contains("Usage: inc field [--major|--minor|--patch]"));
}
#[test]
fn by_one_with_field_passed() {
Playground::setup_for("plugin_inc_by_one_with_field_passed_test").with_files(vec![
FileWithContent(
"sample.toml",
r#"
[package]
edition = "2018"
"#,
),
]);
Playground::setup("plugin_inc_test_1", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
edition = "2018"
"#
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/plugin_inc_by_one_with_field_passed_test"),
"open sample.toml | inc package.edition | get package.edition | echo $it"
);
let actual = nu!(
cwd(dirs.test()),
"open sample.toml | inc package.edition | get package.edition | echo $it"
);
assert_eq!(output, "2019");
assert_eq!(actual, "2019");
})
}
#[test]
fn by_one_with_no_field_passed() {
Playground::setup_for("plugin_inc_by_one_with_no_field_passed_test").with_files(vec![
FileWithContent(
"sample.toml",
r#"
[package]
contributors = "2"
"#,
),
]);
Playground::setup("plugin_inc_test_2", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
contributors = "2"
"#
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/plugin_inc_by_one_with_no_field_passed_test"),
"open sample.toml | get package.contributors | inc | echo $it"
);
let actual = nu!(
cwd(dirs.test()),
"open sample.toml | get package.contributors | inc | echo $it"
);
assert_eq!(output, "3");
assert_eq!(actual, "3");
})
}
#[test]
fn semversion_major_inc() {
Playground::setup_for("plugin_inc_major_semversion_test").with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
version = "0.1.3"
"#,
)]);
Playground::setup("plugin_inc_test_3", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
version = "0.1.3"
"#
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/plugin_inc_major_semversion_test"),
"open sample.toml | inc package.version --major | get package.version | echo $it"
);
let actual = nu!(
cwd(dirs.test()),
"open sample.toml | inc package.version --major | get package.version | echo $it"
);
assert_eq!(output, "1.0.0");
assert_eq!(actual, "1.0.0");
})
}
#[test]
fn semversion_minor_inc() {
Playground::setup_for("plugin_inc_minor_semversion_test").with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
version = "0.1.3"
"#,
)]);
Playground::setup("plugin_inc_test_4", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
version = "0.1.3"
"#
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/plugin_inc_minor_semversion_test"),
"open sample.toml | inc package.version --minor | get package.version | echo $it"
);
let actual = nu!(
cwd(dirs.test()),
"open sample.toml | inc package.version --minor | get package.version | echo $it"
);
assert_eq!(output, "0.2.0");
assert_eq!(actual, "0.2.0");
})
}
#[test]
fn semversion_patch_inc() {
Playground::setup_for("plugin_inc_patch_semversion_test").with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
version = "0.1.3"
"#,
)]);
Playground::setup("plugin_inc_test_5", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
version = "0.1.3"
"#
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/plugin_inc_patch_semversion_test"),
"open sample.toml | inc package.version --patch | get package.version | echo $it"
);
let actual = nu!(
cwd(dirs.test()),
"open sample.toml | inc package.version --patch | get package.version | echo $it"
);
assert_eq!(output, "0.1.4");
assert_eq!(actual, "0.1.4");
})
}
#[test]
fn semversion_without_passing_field() {
Playground::setup_for("plugin_inc_semversion_without_passing_field_test").with_files(vec![
FileWithContent(
"sample.toml",
r#"
[package]
version = "0.1.3"
"#,
),
]);
Playground::setup("plugin_inc_test_6", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
version = "0.1.3"
"#
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/plugin_inc_semversion_without_passing_field_test"),
"open sample.toml | get package.version | inc --patch | echo $it"
);
let actual = nu!(
cwd(dirs.test()),
"open sample.toml | get package.version | inc --patch | echo $it"
);
assert_eq!(output, "0.1.4");
assert_eq!(actual, "0.1.4");
})
}

View File

@ -5,135 +5,170 @@ use helpers as h;
#[test]
fn can_only_apply_one() {
let output = nu_error!(
let actual = nu_error!(
cwd("tests/fixtures/formats"),
"open caco3_plastics.csv | first 1 | str origin --downcase --upcase"
);
assert!(
output.contains("Usage: str field [--downcase|--upcase|--to-int|--replace|--find-replace]")
actual.contains("Usage: str field [--downcase|--upcase|--to-int|--replace|--find-replace]")
);
}
#[test]
fn acts_without_passing_field() {
Playground::setup_for("plugin_str_acts_without_passing_field_test").with_files(vec![
FileWithContent(
Playground::setup("plugin_str_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.yml",
r#"
environment:
global:
PROJECT_NAME: nushell
"#,
),
]);
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/plugin_str_acts_without_passing_field_test"),
"open sample.yml | get environment.global.PROJECT_NAME | str --upcase | echo $it"
);
let actual = nu!(
cwd(dirs.test()),
"open sample.yml | get environment.global.PROJECT_NAME | str --upcase | echo $it"
);
assert_eq!(output, "NUSHELL");
assert_eq!(actual, "NUSHELL");
})
}
#[test]
fn downcases() {
Playground::setup_for("plugin_str_downcases_test").with_files(vec![FileWithContent(
"sample.toml",
r#"
[dependency]
name = "LIGHT"
"#,
)]);
Playground::setup("plugin_str_test_2", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContent(
"sample.toml",
r#"
[dependency]
name = "LIGHT"
"#,
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/plugin_str_downcases_test"),
"open sample.toml | str dependency.name --downcase | get dependency.name | echo $it"
);
let actual = nu!(
cwd(dirs.test()),
"open sample.toml | str dependency.name --downcase | get dependency.name | echo $it"
);
assert_eq!(output, "light");
assert_eq!(actual, "light");
})
}
#[test]
fn upcases() {
Playground::setup_for("plugin_str_upcases_test").with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
name = "nushell"
"#,
)]);
Playground::setup("plugin_str_test_3", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
name = "nushell"
"#,
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/plugin_str_upcases_test"),
"open sample.toml | str package.name --upcase | get package.name | echo $it"
);
assert_eq!(output, "NUSHELL");
let actual = nu!(
cwd(dirs.test()),
"open sample.toml | str package.name --upcase | get package.name | echo $it"
);
assert_eq!(actual, "NUSHELL");
})
}
#[test]
fn converts_to_int() {
let output = nu!(
cwd("tests/fixtures/formats"),
"open caco3_plastics.csv | first 1 | str tariff_item --to-int | where tariff_item == 2509000000 | get tariff_item | echo $it"
);
let actual = nu!(
cwd("tests/fixtures/formats"), h::pipeline(
r#"
open caco3_plastics.csv
| first 1
| str tariff_item --to-int
| where tariff_item == 2509000000
| get tariff_item
| echo $it
"#
));
assert_eq!(output, "2509000000");
assert_eq!(actual, "2509000000");
}
#[test]
fn replaces() {
Playground::setup_for("plugin_str_replaces_test").with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
name = "nushell"
"#,
)]);
Playground::setup("plugin_str_test_4", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContent(
"sample.toml",
r#"
[package]
name = "nushell"
"#,
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/plugin_str_replaces_test"),
"open sample.toml | str package.name --replace wykittenshell | get package.name | echo $it"
);
let actual = nu!(
cwd(dirs.test()), h::pipeline(
r#"
open sample.toml
| str package.name --replace wykittenshell
| get package.name
| echo $it
"#
));
assert_eq!(output, "wykittenshell");
assert_eq!(actual, "wykittenshell");
})
}
#[test]
fn find_and_replaces() {
Playground::setup_for("plugin_str_find_and_replaces_test").with_files(vec![FileWithContent(
"sample.toml",
r#"
[fortune.teller]
phone = "1-800-KATZ"
"#,
)]);
Playground::setup("plugin_str_test_5", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContent(
"sample.toml",
r#"
[fortune.teller]
phone = "1-800-KATZ"
"#,
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/plugin_str_find_and_replaces_test"),
r#"open sample.toml | str fortune.teller.phone --find-replace KATZ "5289" | get fortune.teller.phone | echo $it"#
);
let actual = nu!(
cwd(dirs.test()), h::pipeline(
r#"
open sample.toml
| str fortune.teller.phone --find-replace KATZ "5289"
| get fortune.teller.phone
| echo $it
"#
));
assert_eq!(output, "1-800-5289");
assert_eq!(actual, "1-800-5289");
})
}
#[test]
fn find_and_replaces_without_passing_field() {
Playground::setup_for("plugin_str_find_and_replaces_without_passing_field_test").with_files(
vec![FileWithContent(
Playground::setup("plugin_str_test_6", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContent(
"sample.toml",
r#"
[fortune.teller]
phone = "1-800-KATZ"
"#,
)]);
let actual = nu!(
cwd(dirs.test()), h::pipeline(
r#"
[fortune.teller]
phone = "1-800-KATZ"
"#,
)],
);
open sample.toml
| get fortune.teller.phone
| str --find-replace KATZ "5289"
| echo $it
"#
));
let output = nu!(
cwd("tests/fixtures/nuplayground/plugin_str_find_and_replaces_without_passing_field_test"),
r#"open sample.toml | get fortune.teller.phone | str --find-replace KATZ "5289" | echo $it"#
);
assert_eq!(output, "1-800-5289");
assert_eq!(actual, "1-800-5289");
})
}

View File

@ -1,263 +1,360 @@
mod helpers;
use helpers::{in_directory as cwd, Playground, Stub::*};
use helpers as h;
#[test]
fn can_convert_table_to_csv_text_and_from_csv_text_back_into_table() {
let output = nu!(
let actual = nu!(
cwd("tests/fixtures/formats"),
"open caco3_plastics.csv | to-csv | from-csv | first 1 | get origin | echo $it"
);
assert_eq!(output, "SPAIN");
assert_eq!(actual, "SPAIN");
}
#[test]
fn converts_structured_table_to_csv_text() {
Playground::setup_for("filter_to_csv_test_1").with_files(vec![FileWithContentToBeTrimmed(
"sample.txt",
r#"
importer,shipper,tariff_item,name,origin
Plasticos Rival,Reverte,2509000000,Calcium carbonate,Spain
Tigre Ecuador,OMYA Andina,3824909999,Calcium carbonate,Colombia
"#,
)]);
Playground::setup("filter_to_csv_test_1", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"csv_text_sample.txt",
r#"
importer,shipper,tariff_item,name,origin
Plasticos Rival,Reverte,2509000000,Calcium carbonate,Spain
Tigre Ecuador,OMYA Andina,3824909999,Calcium carbonate,Colombia
"#
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/filter_to_csv_test_1"),
r#"open sample.txt | lines | split-column "," a b c d origin | last 1 | to-csv | lines | nth 1 | echo "$it""#
);
let actual = nu!(
cwd(dirs.test()), h::pipeline(
r#"
open csv_text_sample.txt
| lines
| split-column "," a b c d origin
| last 1
| to-csv
| lines
| nth 1
| echo "$it"
"#
));
assert!(output.contains("Tigre Ecuador,OMYA Andina,3824909999,Calcium carbonate,Colombia"));
assert!(actual.contains("Tigre Ecuador,OMYA Andina,3824909999,Calcium carbonate,Colombia"));
})
}
#[test]
fn converts_structured_table_to_csv_text_skipping_headers_after_conversion() {
Playground::setup_for("filter_to_csv_test_2").with_files(vec![FileWithContentToBeTrimmed(
"sample.txt",
r#"
importer,shipper,tariff_item,name,origin
Plasticos Rival,Reverte,2509000000,Calcium carbonate,Spain
Tigre Ecuador,OMYA Andina,3824909999,Calcium carbonate,Colombia
"#,
)]);
Playground::setup("filter_to_csv_test_2", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"csv_text_sample.txt",
r#"
importer,shipper,tariff_item,name,origin
Plasticos Rival,Reverte,2509000000,Calcium carbonate,Spain
Tigre Ecuador,OMYA Andina,3824909999,Calcium carbonate,Colombia
"#
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/filter_to_csv_test_2"),
r#"open sample.txt | lines | split-column "," a b c d origin | last 1 | to-csv --headerless | echo "$it""#
);
let actual = nu!(
cwd(dirs.test()), h::pipeline(
r#"
open csv_text_sample.txt
| lines
| split-column "," a b c d origin
| last 1
| to-csv --headerless
| echo "$it"
"#
));
assert!(output.contains("Tigre Ecuador,OMYA Andina,3824909999,Calcium carbonate,Colombia"));
assert!(actual.contains("Tigre Ecuador,OMYA Andina,3824909999,Calcium carbonate,Colombia"));
})
}
#[test]
fn converts_from_csv_text_to_structured_table() {
Playground::setup_for("filter_from_csv_test_1").with_files(vec![FileWithContentToBeTrimmed(
"los_tres_amigos.txt",
r#"
first_name,last_name,rusty_luck
Andrés,Robalino,1
Jonathan,Turner,1
Yehuda,Katz,1
"#,
)]);
Playground::setup("filter_from_csv_test_1", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"los_tres_amigos.txt",
r#"
first_name,last_name,rusty_luck
Andrés,Robalino,1
Jonathan,Turner,1
Yehuda,Katz,1
"#,
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/filter_from_csv_test_1"),
"open los_tres_amigos.txt | from-csv | get rusty_luck | str --to-int | sum | echo $it"
);
let actual = nu!(
cwd(dirs.test()), h::pipeline(
r#"
open los_tres_amigos.txt
| from-csv
| get rusty_luck
| str --to-int
| sum
| echo "$it"
"#
));
assert_eq!(output, "3");
assert_eq!(actual, "3");
})
}
#[test]
fn converts_from_csv_text_skipping_headers_to_structured_table() {
Playground::setup_for("filter_from_csv_test_2").with_files(vec![FileWithContentToBeTrimmed(
"los_tres_amigos.txt",
r#"
first_name,last_name,rusty_luck
Andrés,Robalino,1
Jonathan,Turner,1
Yehuda,Katz,1
"#,
)]);
Playground::setup("filter_from_csv_test_2", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"los_tres_amigos.txt",
r#"
first_name,last_name,rusty_luck
Andrés,Robalino,1
Jonathan,Turner,1
Yehuda,Katz,1
"#,
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/filter_from_csv_test_2"),
"open los_tres_amigos.txt | from-csv --headerless | get Column3 | str --to-int | sum | echo $it"
);
let actual = nu!(
cwd(dirs.test()), h::pipeline(
r#"
open los_tres_amigos.txt
| from-csv --headerless
| get Column3
| str --to-int
| sum
| echo $it
"#
));
assert_eq!(output, "3");
assert_eq!(actual, "3");
})
}
#[test]
fn can_convert_table_to_json_text_and_from_json_text_back_into_table() {
let output = nu!(
cwd("tests/fixtures/formats"),
"open sgml_description.json | to-json | from-json | get glossary.GlossDiv.GlossList.GlossEntry.GlossSee | echo $it"
);
let actual = nu!(
cwd("tests/fixtures/formats"), h::pipeline(
r#"
open sgml_description.json
| to-json
| from-json
| get glossary.GlossDiv.GlossList.GlossEntry.GlossSee
| echo $it
"#
));
assert_eq!(output, "markup");
assert_eq!(actual, "markup");
}
#[test]
fn converts_from_json_text_to_structured_table() {
Playground::setup_for("filter_from_json_test_1").with_files(vec![FileWithContentToBeTrimmed(
"katz.txt",
r#"
{
"katz": [
{"name": "Yehuda", "rusty_luck": 1},
{"name": "Jonathan", "rusty_luck": 1},
{"name": "Andres", "rusty_luck": 1},
{"name":"GorbyPuff", "rusty_luck": 1}
]
}
"#,
)]);
Playground::setup("filter_from_json_test_1", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"katz.txt",
r#"
{
"katz": [
{"name": "Yehuda", "rusty_luck": 1},
{"name": "Jonathan", "rusty_luck": 1},
{"name": "Andres", "rusty_luck": 1},
{"name":"GorbyPuff", "rusty_luck": 1}
]
}
"#,
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/filter_from_json_test_1"),
"open katz.txt | from-json | get katz | get rusty_luck | sum | echo $it"
);
let actual = nu!(
cwd(dirs.test()),
"open katz.txt | from-json | get katz | get rusty_luck | sum | echo $it"
);
assert_eq!(output, "4");
assert_eq!(actual, "4");
})
}
#[test]
fn converts_from_json_text_recognizing_objects_independendtly_to_structured_table() {
Playground::setup_for("filter_from_json_test_2").with_files(vec![FileWithContentToBeTrimmed(
"katz.txt",
r#"
{"name": "Yehuda", "rusty_luck": 1}
{"name": "Jonathan", "rusty_luck": 1}
{"name": "Andres", "rusty_luck": 1}
{"name":"GorbyPuff", "rusty_luck": 3}
"#,
)]);
Playground::setup("filter_from_json_test_2", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"katz.txt",
r#"
{"name": "Yehuda", "rusty_luck": 1}
{"name": "Jonathan", "rusty_luck": 1}
{"name": "Andres", "rusty_luck": 1}
{"name":"GorbyPuff", "rusty_luck": 3}
"#,
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/filter_from_json_test_2"),
r#"open katz.txt | from-json --objects | where name == "GorbyPuff" | get rusty_luck | echo $it"#
);
let actual = nu!(
cwd(dirs.test()), h::pipeline(
r#"
open katz.txt
| from-json --objects
| where name == "GorbyPuff"
| get rusty_luck
| echo $it
"#
));
assert_eq!(output, "3");
assert_eq!(actual, "3");
})
}
#[test]
fn converts_structured_table_to_json_text() {
Playground::setup_for("filter_to_json_test_1").with_files(vec![FileWithContentToBeTrimmed(
"sample.txt",
r#"
JonAndrehudaTZ,3
GorbyPuff,100
"#,
)]);
Playground::setup("filter_to_json_test", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"sample.txt",
r#"
JonAndrehudaTZ,3
GorbyPuff,100
"#,
)]);
let output = nu!(
cwd("tests/fixtures/nuplayground/filter_to_json_test_1"),
r#"open sample.txt | lines | split-column "," name luck | pick name | to-json | nth 0 | from-json | get name | echo $it"#
);
let actual = nu!(
cwd(dirs.test()), h::pipeline(
r#"
open sample.txt
| lines
| split-column "," name luck
| pick name
| to-json
| nth 0
| from-json
| get name
| echo $it
"#
));
assert_eq!(output, "JonAndrehudaTZ");
assert_eq!(actual, "JonAndrehudaTZ");
})
}
#[test]
fn can_convert_json_text_to_bson_and_back_into_table() {
let output = nu!(
let actual = nu!(
cwd("tests/fixtures/formats"),
"open sample.bson | to-bson | from-bson | get root | nth 1 | get b | echo $it"
);
assert_eq!(output, "whel");
assert_eq!(actual, "whel");
}
#[test]
fn can_convert_table_to_toml_text_and_from_toml_text_back_into_table() {
let output = nu!(
let actual = nu!(
cwd("tests/fixtures/formats"),
"open cargo_sample.toml | to-toml | from-toml | get package.name | echo $it"
);
assert_eq!(output, "nu");
assert_eq!(actual, "nu");
}
#[test]
fn can_convert_table_to_yaml_text_and_from_yaml_text_back_into_table() {
let output = nu!(
cwd("tests/fixtures/formats"),
"open appveyor.yml | to-yaml | from-yaml | get environment.global.PROJECT_NAME | echo $it"
);
let actual = nu!(
cwd("tests/fixtures/formats"), h::pipeline(
r#"
open appveyor.yml
| to-yaml
| from-yaml
| get environment.global.PROJECT_NAME
| echo $it
"#
));
assert_eq!(output, "nushell");
assert_eq!(actual, "nushell");
}
#[test]
fn can_sort_by_column() {
let output = nu!(
cwd("tests/fixtures/formats"),
r#"open cargo_sample.toml --raw | lines | skip 1 | first 4 | split-column "=" | sort-by Column1 | skip 1 | first 1 | get Column1 | trim | echo $it"#
);
let actual = nu!(
cwd("tests/fixtures/formats"), h::pipeline(
r#"
open cargo_sample.toml --raw
| lines
| skip 1
| first 4
| split-column "="
| sort-by Column1
| skip 1
| first 1
| get Column1
| trim
| echo $it
"#
));
assert_eq!(output, "description");
}
#[test]
fn can_sort_by_column_reverse() {
let output = nu!(
cwd("tests/fixtures/formats"),
r#"open cargo_sample.toml --raw | lines | skip 1 | first 4 | split-column "=" | sort-by Column1 --reverse | skip 1 | first 1 | get Column1 | trim | echo $it"#
);
assert_eq!(output, "name");
assert_eq!(actual, "description");
}
#[test]
fn can_split_by_column() {
let output = nu!(
cwd("tests/fixtures/formats"),
r#"open cargo_sample.toml --raw | lines | skip 1 | first 1 | split-column "=" | get Column1 | trim | echo $it"#
);
let actual = nu!(
cwd("tests/fixtures/formats"), h::pipeline(
r#"
open cargo_sample.toml --raw
| lines
| skip 1
| first 1
| split-column "="
| get Column1
| trim
| echo $it
"#
));
assert_eq!(output, "name");
assert_eq!(actual, "name");
}
#[test]
fn can_sum() {
let output = nu!(
cwd("tests/fixtures/formats"),
"open sgml_description.json | get glossary.GlossDiv.GlossList.GlossEntry.Sections | sum | echo $it"
);
let actual = nu!(
cwd("tests/fixtures/formats"), h::pipeline(
r#"
open sgml_description.json
| get glossary.GlossDiv.GlossList.GlossEntry.Sections
| sum
| echo $it
"#
));
assert_eq!(output, "203")
assert_eq!(actual, "203")
}
#[test]
fn can_filter_by_unit_size_comparison() {
let output = nu!(
let actual = nu!(
cwd("tests/fixtures/formats"),
"ls | where size > 1kb | sort-by size | get name | skip 1 | trim | echo $it"
);
assert_eq!(output, "caco3_plastics.csv");
assert_eq!(actual, "caco3_plastics.csv");
}
#[test]
fn can_get_last() {
let output = nu!(
let actual = nu!(
cwd("tests/fixtures/formats"),
"ls | sort-by name | last 1 | get name | trim | echo $it"
);
assert_eq!(output, "utf16.ini");
assert_eq!(actual, "utf16.ini");
}
#[test]
fn can_get_reverse_first() {
let output = nu!(
let actual = nu!(
cwd("tests/fixtures/formats"),
"ls | sort-by name | reverse | first 1 | get name | trim | echo $it"
);
assert_eq!(output, "utf16.ini");
assert_eq!(actual, "utf16.ini");
}

View File

@ -176,17 +176,26 @@ impl Playground {
self.root.path()
}
pub fn test_dir_name(&self) -> String {
self.tests.clone()
}
pub fn back_to_playground(&mut self) -> &mut Self {
self.cwd = PathBuf::from(self.root()).join(self.tests.clone());
self
}
pub fn setup(topic: &str, block: impl FnOnce(Dirs, &mut Playground)) {
let mut playground = Playground::setup_for(topic);
let root = tempdir().expect("Couldn't create a tempdir");
let nuplay_dir = root.path().join(topic);
if PathBuf::from(&nuplay_dir).exists() {
std::fs::remove_dir_all(PathBuf::from(&nuplay_dir)).expect("can not remove directory");
}
std::fs::create_dir(PathBuf::from(&nuplay_dir)).expect("can not create directory");
let mut playground = Playground {
root: root,
tests: topic.to_string(),
cwd: nuplay_dir,
};
let project_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let playground_root = playground.root.path();
@ -219,23 +228,6 @@ impl Playground {
block(dirs, &mut playground);
}
pub fn setup_for(topic: &str) -> Playground {
let root = tempdir().expect("Couldn't create a tempdir");
let nuplay_dir = root.path().join(topic);
if PathBuf::from(&nuplay_dir).exists() {
std::fs::remove_dir_all(PathBuf::from(&nuplay_dir)).expect("can not remove directory");
}
std::fs::create_dir(PathBuf::from(&nuplay_dir)).expect("can not create directory");
Playground {
root: root,
tests: topic.to_string(),
cwd: nuplay_dir,
}
}
pub fn mkdir(&mut self, directory: &str) -> &mut Self {
self.cwd.push(directory);
std::fs::create_dir_all(&self.cwd).expect("can not create directory");
@ -376,3 +368,10 @@ pub fn executable_path() -> PathBuf {
pub fn in_directory(str: impl AsRef<Path>) -> String {
str.as_ref().display().to_string()
}
pub fn pipeline(commands: &str) -> String {
commands.lines()
.skip(1)
.collect::<Vec<&str>>()
.concat()
}