mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 09:55:42 +02:00
This commit is the continuing phase of extracting functionality to subcrates. We extract test helpers and begin to change Nu shell's test organization along with it.
This commit is contained in:
17
tests/commands/append.rs
Normal file
17
tests/commands/append.rs
Normal file
@ -0,0 +1,17 @@
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn adds_a_row_to_the_end() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open fileA.txt
|
||||
| lines
|
||||
| append "testme"
|
||||
| nth 3
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "testme");
|
||||
}
|
383
tests/commands/cd.rs
Normal file
383
tests/commands/cd.rs
Normal file
@ -0,0 +1,383 @@
|
||||
use std::path::PathBuf;
|
||||
use test_support::fs::{Stub::EmptyFile, Stub::FileWithContent};
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, nu_error};
|
||||
|
||||
#[test]
|
||||
fn filesystem_change_from_current_directory_using_relative_path() {
|
||||
Playground::setup("cd_test_1", |dirs, _| {
|
||||
let actual = nu!(
|
||||
cwd: dirs.root(),
|
||||
r#"
|
||||
cd cd_test_1
|
||||
pwd | echo $it
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(PathBuf::from(actual), *dirs.test());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filesystem_change_from_current_directory_using_absolute_path() {
|
||||
Playground::setup("cd_test_2", |dirs, _| {
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
r#"
|
||||
cd {}
|
||||
pwd | echo $it
|
||||
"#,
|
||||
dirs.formats()
|
||||
);
|
||||
|
||||
assert_eq!(PathBuf::from(actual), dirs.formats());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filesystem_switch_back_to_previous_working_directory() {
|
||||
Playground::setup("cd_test_3", |dirs, sandbox| {
|
||||
sandbox.mkdir("odin");
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test().join("odin"),
|
||||
r#"
|
||||
cd {}
|
||||
cd -
|
||||
pwd | echo $it
|
||||
"#,
|
||||
dirs.test()
|
||||
);
|
||||
|
||||
assert_eq!(PathBuf::from(actual), dirs.test().join("odin"));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filesytem_change_from_current_directory_using_relative_path_and_dash() {
|
||||
Playground::setup("cd_test_4", |dirs, sandbox| {
|
||||
sandbox.within("odin").mkdir("-");
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
r#"
|
||||
cd odin/-
|
||||
pwd | echo $it
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(PathBuf::from(actual), dirs.test().join("odin").join("-"));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filesystem_change_current_directory_to_parent_directory() {
|
||||
Playground::setup("cd_test_5", |dirs, _| {
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
r#"
|
||||
cd ..
|
||||
pwd | echo $it
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(PathBuf::from(actual), *dirs.root());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filesystem_change_to_home_directory() {
|
||||
Playground::setup("cd_test_6", |dirs, _| {
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
r#"
|
||||
cd ~
|
||||
pwd | echo $it
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(PathBuf::from(actual), dirs::home_dir().unwrap());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filesystem_change_to_a_directory_containing_spaces() {
|
||||
Playground::setup("cd_test_7", |dirs, sandbox| {
|
||||
sandbox.mkdir("robalino turner katz");
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
r#"
|
||||
cd "robalino turner katz"
|
||||
pwd | echo $it
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
PathBuf::from(actual),
|
||||
dirs.test().join("robalino turner katz")
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filesystem_not_a_directory() {
|
||||
Playground::setup("cd_test_8", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![EmptyFile("ferris_did_it.txt")]);
|
||||
|
||||
let actual = nu_error!(
|
||||
cwd: dirs.test(),
|
||||
"cd ferris_did_it.txt"
|
||||
);
|
||||
|
||||
assert!(actual.contains("ferris_did_it.txt"), "actual={:?}", actual);
|
||||
assert!(actual.contains("is not a directory"), "actual={:?}", actual);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filesystem_directory_not_found() {
|
||||
let actual = nu_error!(
|
||||
cwd: "tests/fixtures",
|
||||
"cd dir_that_does_not_exist"
|
||||
);
|
||||
|
||||
assert!(
|
||||
actual.contains("dir_that_does_not_exist"),
|
||||
"actual={:?}",
|
||||
actual
|
||||
);
|
||||
assert!(
|
||||
actual.contains("directory not found"),
|
||||
"actual={:?}",
|
||||
actual
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valuesystem_change_from_current_path_using_relative_path() {
|
||||
Playground::setup("cd_test_9", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"sample.toml",
|
||||
r#"
|
||||
[[bin]]
|
||||
path = "src/plugins/turner.rs"
|
||||
|
||||
[[bin]]
|
||||
path = "src/plugins/robalino.rs"
|
||||
|
||||
[[bin]]
|
||||
path = "src/plugins/katz.rs"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
r#"
|
||||
enter sample.toml
|
||||
cd bin
|
||||
pwd | echo $it
|
||||
exit
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(PathBuf::from(actual), PathBuf::from("/bin"));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valuesystem_change_from_current_path_using_absolute_path() {
|
||||
Playground::setup("cd_test_10", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"sample.toml",
|
||||
r#"
|
||||
[dependencies]
|
||||
turner-ts = "0.1.1"
|
||||
robalino-tkd = "0.0.1"
|
||||
katz-ember = "0.2.3"
|
||||
|
||||
[[bin]]
|
||||
path = "src/plugins/arepa.rs"
|
||||
|
||||
[[bin]]
|
||||
path = "src/plugins/bbq.rs"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
r#"
|
||||
enter sample.toml
|
||||
cd bin
|
||||
cd /dependencies
|
||||
pwd | echo $it
|
||||
exit
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(PathBuf::from(actual), PathBuf::from("/dependencies"));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valuesystem_switch_back_to_previous_working_path() {
|
||||
Playground::setup("cd_test_11", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"sample.toml",
|
||||
r#"
|
||||
[dependencies]
|
||||
turner-ts = "0.1.1"
|
||||
robalino-tkd = "0.0.1"
|
||||
katz-ember = "0.2.3"
|
||||
odin-gf = "0.2.1"
|
||||
|
||||
[[bin]]
|
||||
path = "src/plugins/arepa.rs"
|
||||
|
||||
[[bin]]
|
||||
path = "src/plugins/bbq.rs"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
r#"
|
||||
enter sample.toml
|
||||
cd dependencies
|
||||
cd /bin
|
||||
cd -
|
||||
pwd | echo $it
|
||||
exit
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(PathBuf::from(actual), PathBuf::from("/dependencies"));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valuesystem_change_from_current_path_using_relative_path_and_dash() {
|
||||
Playground::setup("cd_test_12", |dirs, sandbox| {
|
||||
sandbox
|
||||
.with_files(vec![FileWithContent(
|
||||
"sample.toml",
|
||||
r#"
|
||||
[package]
|
||||
- = ["Yehuda Katz <wycats@gmail.com>", "Jonathan Turner <jonathan.d.turner@gmail.com>", "Andrés N. Robalino <andres@androbtech.com>"]
|
||||
|
||||
[[bin]]
|
||||
path = "src/plugins/arepa.rs"
|
||||
|
||||
[[bin]]
|
||||
path = "src/plugins/bbq.rs"
|
||||
"#
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
r#"
|
||||
enter sample.toml
|
||||
cd package/-
|
||||
cd /bin
|
||||
cd -
|
||||
pwd | echo $it
|
||||
exit
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(PathBuf::from(actual), PathBuf::from("/package/-"));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valuesystem_change_current_path_to_parent_path() {
|
||||
Playground::setup("cd_test_13", |dirs, sandbox| {
|
||||
sandbox
|
||||
.with_files(vec![FileWithContent(
|
||||
"sample.toml",
|
||||
r#"
|
||||
[package]
|
||||
emberenios = ["Yehuda Katz <wycats@gmail.com>", "Jonathan Turner <jonathan.d.turner@gmail.com>", "Andrés N. Robalino <andres@androbtech.com>"]
|
||||
"#
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
r#"
|
||||
enter sample.toml
|
||||
cd package/emberenios
|
||||
cd ..
|
||||
pwd | echo $it
|
||||
exit
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(PathBuf::from(actual), PathBuf::from("/package"));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valuesystem_change_to_home_directory() {
|
||||
Playground::setup("cd_test_14", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"sample.toml",
|
||||
r#"
|
||||
[paquete]
|
||||
el = "pollo loco"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
r#"
|
||||
enter sample.toml
|
||||
cd paquete
|
||||
cd ~
|
||||
pwd | echo $it
|
||||
exit
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(PathBuf::from(actual), PathBuf::from("/"));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valuesystem_change_to_a_path_containing_spaces() {
|
||||
Playground::setup("cd_test_15", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"sample.toml",
|
||||
r#"
|
||||
["pa que te"]
|
||||
el = "pollo loco"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(),
|
||||
r#"
|
||||
enter sample.toml
|
||||
cd "pa que te"
|
||||
pwd | echo $it
|
||||
exit
|
||||
"#
|
||||
);
|
||||
|
||||
assert_eq!(PathBuf::from(actual), PathBuf::from("/").join("pa que te"));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valuesystem_path_not_found() {
|
||||
let actual = nu_error!(
|
||||
cwd: "tests/fixtures/formats",
|
||||
r#"
|
||||
enter cargo_sample.toml
|
||||
cd im_a_path_that_does_not_exist
|
||||
exit
|
||||
"#
|
||||
);
|
||||
|
||||
assert!(actual.contains("Can not change to path inside"));
|
||||
assert!(actual.contains("No such path exists"));
|
||||
}
|
52
tests/commands/compact.rs
Normal file
52
tests/commands/compact.rs
Normal file
@ -0,0 +1,52 @@
|
||||
use test_support::fs::Stub::FileWithContentToBeTrimmed;
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn discards_rows_where_given_column_is_empty() {
|
||||
Playground::setup("compact_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"los_tres_amigos.json",
|
||||
r#"
|
||||
{
|
||||
"amigos": [
|
||||
{"name": "Yehuda", "rusty_luck": 1},
|
||||
{"name": "Jonathan", "rusty_luck": 1},
|
||||
{"name": "Andres", "rusty_luck": 1},
|
||||
{"name":"GorbyPuff"}
|
||||
]
|
||||
}
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open los_tres_amigos.json
|
||||
| get amigos
|
||||
| compact rusty_luck
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "3");
|
||||
});
|
||||
}
|
||||
#[test]
|
||||
fn discards_empty_rows_by_default() {
|
||||
Playground::setup("compact_test_2", |dirs, _| {
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
echo "[1,2,3,14,null]"
|
||||
| from-json
|
||||
| compact
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "4");
|
||||
});
|
||||
}
|
167
tests/commands/cp.rs
Normal file
167
tests/commands/cp.rs
Normal file
@ -0,0 +1,167 @@
|
||||
use std::path::Path;
|
||||
use test_support::fs::{files_exist_at, AbsoluteFile, Stub::EmptyFile};
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, nu_error};
|
||||
|
||||
#[test]
|
||||
fn copies_a_file() {
|
||||
Playground::setup("cp_test_1", |dirs, _| {
|
||||
nu!(
|
||||
cwd: dirs.root(),
|
||||
"cp {} cp_test_1/sample.ini",
|
||||
dirs.formats().join("sample.ini")
|
||||
);
|
||||
|
||||
assert!(dirs.test().join("sample.ini").exists());
|
||||
});
|
||||
}
|
||||
|
||||
#[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"));
|
||||
|
||||
nu!(
|
||||
cwd: dirs.formats(),
|
||||
"cp ../formats/sample.ini {}",
|
||||
expected_file.dir()
|
||||
);
|
||||
|
||||
assert!(dirs.test().join("sample.ini").exists());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_if_attempting_to_copy_a_directory_to_another_directory() {
|
||||
Playground::setup("cp_test_3", |dirs, _| {
|
||||
let actual = nu_error!(
|
||||
cwd: dirs.formats(),
|
||||
"cp ../formats {}", dirs.test()
|
||||
);
|
||||
|
||||
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, 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");
|
||||
|
||||
nu!(
|
||||
cwd: dirs.test(),
|
||||
"cp originals expected --recursive"
|
||||
);
|
||||
|
||||
assert!(expected_dir.exists());
|
||||
assert!(files_exist_at(
|
||||
vec![
|
||||
Path::new("yehuda.txt"),
|
||||
Path::new("jonathan.txt"),
|
||||
Path::new("andres.txt")
|
||||
],
|
||||
expected_dir
|
||||
));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deep_copies_with_recursive_flag() {
|
||||
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");
|
||||
|
||||
nu!(
|
||||
cwd: dirs.test(),
|
||||
"cp originals expected --recursive"
|
||||
);
|
||||
|
||||
assert!(expected_dir.exists());
|
||||
assert!(files_exist_at(
|
||||
vec![Path::new("errors.txt"), Path::new("multishells.txt")],
|
||||
jonathans_expected_copied_dir
|
||||
));
|
||||
assert!(files_exist_at(
|
||||
vec![Path::new("coverage.txt"), Path::new("commands.txt")],
|
||||
andres_expected_copied_dir
|
||||
));
|
||||
assert!(files_exist_at(
|
||||
vec![Path::new("defer-evaluation.txt")],
|
||||
yehudas_expected_copied_dir
|
||||
));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copies_using_path_with_wildcard() {
|
||||
Playground::setup("cp_test_6", |dirs, _| {
|
||||
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, _| {
|
||||
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()
|
||||
));
|
||||
});
|
||||
}
|
36
tests/commands/default.rs
Normal file
36
tests/commands/default.rs
Normal file
@ -0,0 +1,36 @@
|
||||
use test_support::fs::Stub::FileWithContentToBeTrimmed;
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn adds_row_data_if_column_missing() {
|
||||
Playground::setup("default_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"los_tres_amigos.json",
|
||||
r#"
|
||||
{
|
||||
"amigos": [
|
||||
{"name": "Yehuda"},
|
||||
{"name": "Jonathan", "rusty_luck": 0},
|
||||
{"name": "Andres", "rusty_luck": 0},
|
||||
{"name":"GorbyPuff"}
|
||||
]
|
||||
}
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open los_tres_amigos.json
|
||||
| get amigos
|
||||
| default rusty_luck 1
|
||||
| where rusty_luck == 1
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "2");
|
||||
});
|
||||
}
|
16
tests/commands/edit.rs
Normal file
16
tests/commands/edit.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn creates_a_new_table_with_the_new_row_given() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open cargo_sample.toml
|
||||
| edit dev-dependencies.pretty_assertions "7"
|
||||
| get dev-dependencies.pretty_assertions
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "7");
|
||||
}
|
86
tests/commands/enter.rs
Normal file
86
tests/commands/enter.rs
Normal file
@ -0,0 +1,86 @@
|
||||
use std::path::Path;
|
||||
use test_support::fs::{files_exist_at, Stub::EmptyFile};
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, nu_error};
|
||||
|
||||
#[test]
|
||||
fn knows_the_filesystems_entered() {
|
||||
Playground::setup("enter_test_1", |dirs, sandbox| {
|
||||
sandbox
|
||||
.within("red_pill")
|
||||
.with_files(vec![
|
||||
EmptyFile("andres.nu"),
|
||||
EmptyFile("jonathan.nu"),
|
||||
EmptyFile("yehuda.nu"),
|
||||
])
|
||||
.within("blue_pill")
|
||||
.with_files(vec![
|
||||
EmptyFile("bash.nxt"),
|
||||
EmptyFile("korn.nxt"),
|
||||
EmptyFile("powedsh.nxt"),
|
||||
])
|
||||
.mkdir("expected");
|
||||
|
||||
let red_pill_dir = dirs.test().join("red_pill");
|
||||
let blue_pill_dir = dirs.test().join("blue_pill");
|
||||
let expected = dirs.test().join("expected");
|
||||
let expected_recycled = expected.join("recycled");
|
||||
|
||||
nu!(
|
||||
cwd: dirs.test(),
|
||||
r#"
|
||||
enter expected
|
||||
mkdir recycled
|
||||
enter ../red_pill
|
||||
mv jonathan.nu ../expected
|
||||
enter ../blue_pill
|
||||
cp *.nxt ../expected/recycled
|
||||
p
|
||||
p
|
||||
mv ../red_pill/yehuda.nu .
|
||||
n
|
||||
mv andres.nu ../expected/andres.nu
|
||||
exit
|
||||
cd ..
|
||||
rm red_pill --recursive
|
||||
exit
|
||||
n
|
||||
rm blue_pill --recursive
|
||||
exit
|
||||
"#
|
||||
);
|
||||
|
||||
assert!(!red_pill_dir.exists());
|
||||
assert!(files_exist_at(
|
||||
vec![
|
||||
Path::new("andres.nu"),
|
||||
Path::new("jonathan.nu"),
|
||||
Path::new("yehuda.nu"),
|
||||
],
|
||||
expected
|
||||
));
|
||||
|
||||
assert!(!blue_pill_dir.exists());
|
||||
assert!(files_exist_at(
|
||||
vec![
|
||||
Path::new("bash.nxt"),
|
||||
Path::new("korn.nxt"),
|
||||
Path::new("powedsh.nxt"),
|
||||
],
|
||||
expected_recycled
|
||||
));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_if_file_not_found() {
|
||||
Playground::setup("enter_test_2", |dirs, _| {
|
||||
let actual = nu_error!(
|
||||
cwd: dirs.test(),
|
||||
"enter i_dont_exist.csv"
|
||||
);
|
||||
|
||||
assert!(actual.contains("File could not be opened"));
|
||||
assert!(actual.contains("file not found"));
|
||||
})
|
||||
}
|
70
tests/commands/first.rs
Normal file
70
tests/commands/first.rs
Normal file
@ -0,0 +1,70 @@
|
||||
use test_support::fs::Stub::EmptyFile;
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn gets_first_rows_by_amount() {
|
||||
Playground::setup("first_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![
|
||||
EmptyFile("los.txt"),
|
||||
EmptyFile("tres.txt"),
|
||||
EmptyFile("amigos.txt"),
|
||||
EmptyFile("arepas.clu"),
|
||||
]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls
|
||||
| first 3
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "3");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gets_all_rows_if_amount_higher_than_all_rows() {
|
||||
Playground::setup("first_test_2", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![
|
||||
EmptyFile("los.txt"),
|
||||
EmptyFile("tres.txt"),
|
||||
EmptyFile("amigos.txt"),
|
||||
EmptyFile("arepas.clu"),
|
||||
]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls
|
||||
| first 99
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "4");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gets_first_row_when_no_amount_given() {
|
||||
Playground::setup("first_test_3", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![EmptyFile("caballeros.txt"), EmptyFile("arepas.clu")]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls
|
||||
| first
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "1");
|
||||
})
|
||||
}
|
16
tests/commands/format.rs
Normal file
16
tests/commands/format.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn creates_the_resulting_string_from_the_given_fields() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open cargo_sample.toml
|
||||
| get package
|
||||
| format "{name} has license {license}"
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "nu has license ISC");
|
||||
}
|
218
tests/commands/get.rs
Normal file
218
tests/commands/get.rs
Normal file
@ -0,0 +1,218 @@
|
||||
use test_support::fs::Stub::FileWithContent;
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, nu_error, pipeline};
|
||||
|
||||
#[test]
|
||||
fn fetches_a_row() {
|
||||
Playground::setup("get_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"sample.toml",
|
||||
r#"
|
||||
nu_party_venue = "zion"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open sample.toml
|
||||
| get nu_party_venue
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "zion");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fetches_by_index() {
|
||||
Playground::setup("get_test_2", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"sample.toml",
|
||||
r#"
|
||||
[package]
|
||||
name = "nu"
|
||||
version = "0.4.1"
|
||||
authors = ["Yehuda Katz <wycats@gmail.com>", "Jonathan Turner <jonathan.d.turner@gmail.com>", "Andrés N. Robalino <andres@androbtech.com>"]
|
||||
description = "When arepas shells are tasty and fun."
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open sample.toml
|
||||
| get package.authors.2
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "Andrés N. Robalino <andres@androbtech.com>");
|
||||
})
|
||||
}
|
||||
#[test]
|
||||
fn fetches_by_column_path() {
|
||||
Playground::setup("get_test_3", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"sample.toml",
|
||||
r#"
|
||||
[package]
|
||||
name = "nu"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open sample.toml
|
||||
| get package.name
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "nu");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn column_paths_are_either_double_quoted_or_regular_unquoted_words_separated_by_dot() {
|
||||
Playground::setup("get_test_4", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"sample.toml",
|
||||
r#"
|
||||
[package]
|
||||
9999 = ["Yehuda Katz <wycats@gmail.com>", "Jonathan Turner <jonathan.d.turner@gmail.com>", "Andrés N. Robalino <andres@androbtech.com>"]
|
||||
description = "When arepas shells are tasty and fun."
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open sample.toml
|
||||
| get package."9999"
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "3");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fetches_more_than_one_column_path() {
|
||||
Playground::setup("get_test_5", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"sample.toml",
|
||||
r#"
|
||||
[[fortune_tellers]]
|
||||
name = "Andrés N. Robalino"
|
||||
arepas = 1
|
||||
|
||||
[[fortune_tellers]]
|
||||
name = "Jonathan Turner"
|
||||
arepas = 1
|
||||
|
||||
[[fortune_tellers]]
|
||||
name = "Yehuda Katz"
|
||||
arepas = 1
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open sample.toml
|
||||
| get fortune_tellers.2.name fortune_tellers.0.name fortune_tellers.1.name
|
||||
| nth 2
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "Jonathan Turner");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_fetching_by_column_not_present() {
|
||||
Playground::setup("get_test_6", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"sample.toml",
|
||||
r#"
|
||||
[taconushell]
|
||||
sentence_words = ["Yo", "quiero", "taconushell"]
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu_error!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open sample.toml
|
||||
| get taco
|
||||
"#
|
||||
));
|
||||
|
||||
assert!(actual.contains("Unknown column"));
|
||||
assert!(actual.contains("did you mean 'taconushell'?"));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn errors_fetching_by_column_using_a_number() {
|
||||
Playground::setup("get_test_7", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"sample.toml",
|
||||
r#"
|
||||
[spanish_lesson]
|
||||
0 = "can only be fetched with 0 double quoted."
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu_error!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open sample.toml
|
||||
| get spanish_lesson.9
|
||||
"#
|
||||
));
|
||||
|
||||
assert!(actual.contains("No rows available"));
|
||||
assert!(actual.contains(r#"Not a table. Perhaps you meant to get the column "0" instead?"#))
|
||||
})
|
||||
}
|
||||
#[test]
|
||||
fn errors_fetching_by_index_out_of_bounds() {
|
||||
Playground::setup("get_test_8", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"sample.toml",
|
||||
r#"
|
||||
[spanish_lesson]
|
||||
sentence_words = ["Yo", "quiero", "taconushell"]
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu_error!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open sample.toml
|
||||
| get spanish_lesson.sentence_words.3
|
||||
"#
|
||||
));
|
||||
|
||||
assert!(
|
||||
actual.contains("Row not found"),
|
||||
format!("actual: {:?}", actual)
|
||||
);
|
||||
assert!(
|
||||
actual.contains("There isn't a row indexed at 3"),
|
||||
format!("actual: {:?}", actual)
|
||||
);
|
||||
assert!(
|
||||
actual.contains("The table only has 3 rows (0 to 2)"),
|
||||
format!("actual: {:?}", actual)
|
||||
)
|
||||
})
|
||||
}
|
56
tests/commands/group_by.rs
Normal file
56
tests/commands/group_by.rs
Normal file
@ -0,0 +1,56 @@
|
||||
use test_support::fs::Stub::FileWithContentToBeTrimmed;
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, nu_error, pipeline};
|
||||
|
||||
#[test]
|
||||
fn groups() {
|
||||
Playground::setup("group_by_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"los_tres_caballeros.csv",
|
||||
r#"
|
||||
first_name,last_name,rusty_at,type
|
||||
Andrés,Robalino,10/11/2013,A
|
||||
Jonathan,Turner,10/12/2013,B
|
||||
Yehuda,Katz,10/11/2013,A
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open los_tres_caballeros.csv
|
||||
| group-by rusty_at
|
||||
| get "10/11/2013"
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "2");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_if_given_unknown_column_name_is_missing() {
|
||||
Playground::setup("group_by_test_2", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"los_tres_caballeros.csv",
|
||||
r#"
|
||||
first_name,last_name,rusty_at,type
|
||||
Andrés,Robalino,10/11/2013,A
|
||||
Jonathan,Turner,10/12/2013,B
|
||||
Yehuda,Katz,10/11/2013,A
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu_error!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open los_tres_caballeros.csv
|
||||
| group-by ttype
|
||||
"#
|
||||
));
|
||||
|
||||
assert!(actual.contains("Unknown column"));
|
||||
})
|
||||
}
|
32
tests/commands/histogram.rs
Normal file
32
tests/commands/histogram.rs
Normal file
@ -0,0 +1,32 @@
|
||||
use test_support::fs::Stub::FileWithContentToBeTrimmed;
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn summarizes() {
|
||||
Playground::setup("histogram_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"los_tres_caballeros.csv",
|
||||
r#"
|
||||
first_name,last_name,rusty_at
|
||||
Andrés,Robalino,Ecuador
|
||||
Jonathan,Turner,Estados Unidos
|
||||
Yehuda,Katz,Estados Unidos
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open los_tres_caballeros.csv
|
||||
| histogram rusty_at countries
|
||||
| where rusty_at == "Ecuador"
|
||||
| get countries
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "**************************************************");
|
||||
// 50%
|
||||
})
|
||||
}
|
16
tests/commands/insert.rs
Normal file
16
tests/commands/insert.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn insert_plugin() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open cargo_sample.toml
|
||||
| insert dev-dependencies.newdep "1"
|
||||
| get dev-dependencies.newdep
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "1");
|
||||
}
|
56
tests/commands/last.rs
Normal file
56
tests/commands/last.rs
Normal file
@ -0,0 +1,56 @@
|
||||
use test_support::fs::Stub::EmptyFile;
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn gets_the_last_row() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats",
|
||||
"ls | sort-by name | last 1 | get name | trim | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(actual, "utf16.ini");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gets_last_rows_by_amount() {
|
||||
Playground::setup("last_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![
|
||||
EmptyFile("los.txt"),
|
||||
EmptyFile("tres.txt"),
|
||||
EmptyFile("amigos.txt"),
|
||||
EmptyFile("arepas.clu"),
|
||||
]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls
|
||||
| last 3
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "3");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gets_last_row_when_no_amount_given() {
|
||||
Playground::setup("last_test_2", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![EmptyFile("caballeros.txt"), EmptyFile("arepas.clu")]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls
|
||||
| last
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "1");
|
||||
})
|
||||
}
|
21
tests/commands/lines.rs
Normal file
21
tests/commands/lines.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn lines() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open cargo_sample.toml --raw
|
||||
| lines
|
||||
| skip-while $it != "[dependencies]"
|
||||
| skip 1
|
||||
| first 1
|
||||
| split-column "="
|
||||
| get Column1
|
||||
| trim
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "rustyline");
|
||||
}
|
71
tests/commands/ls.rs
Normal file
71
tests/commands/ls.rs
Normal file
@ -0,0 +1,71 @@
|
||||
use test_support::fs::Stub::EmptyFile;
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn lists_regular_files() {
|
||||
Playground::setup("ls_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![
|
||||
EmptyFile("yehuda.txt"),
|
||||
EmptyFile("jonathan.txt"),
|
||||
EmptyFile("andres.txt"),
|
||||
]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "3");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lists_regular_files_using_asterisk_wildcard() {
|
||||
Playground::setup("ls_test_2", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![
|
||||
EmptyFile("los.txt"),
|
||||
EmptyFile("tres.txt"),
|
||||
EmptyFile("amigos.txt"),
|
||||
EmptyFile("arepas.clu"),
|
||||
]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls *.txt
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "3");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lists_regular_files_using_question_mark_wildcard() {
|
||||
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"),
|
||||
]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls *.??.txt
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "3");
|
||||
})
|
||||
}
|
47
tests/commands/mkdir.rs
Normal file
47
tests/commands/mkdir.rs
Normal file
@ -0,0 +1,47 @@
|
||||
use std::path::Path;
|
||||
use test_support::fs::files_exist_at;
|
||||
use test_support::nu;
|
||||
use test_support::playground::Playground;
|
||||
|
||||
#[test]
|
||||
fn creates_directory() {
|
||||
Playground::setup("mkdir_test_1", |dirs, _| {
|
||||
nu!(
|
||||
cwd: dirs.test(),
|
||||
"mkdir my_new_directory"
|
||||
);
|
||||
|
||||
let expected = dirs.test().join("my_new_directory");
|
||||
|
||||
assert!(expected.exists());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepts_and_creates_directories() {
|
||||
Playground::setup("mkdir_test_2", |dirs, _| {
|
||||
nu!(
|
||||
cwd: dirs.test(),
|
||||
"mkdir dir_1 dir_2 dir_3"
|
||||
);
|
||||
|
||||
assert!(files_exist_at(
|
||||
vec![Path::new("dir_1"), Path::new("dir_2"), Path::new("dir_3")],
|
||||
dirs.test()
|
||||
));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn creates_intermediary_directories() {
|
||||
Playground::setup("mkdir_test_3", |dirs, _| {
|
||||
nu!(
|
||||
cwd: dirs.test(),
|
||||
"mkdir some_folder/another/deeper_one"
|
||||
);
|
||||
|
||||
let expected = dirs.test().join("some_folder/another/deeper_one");
|
||||
|
||||
assert!(expected.exists());
|
||||
})
|
||||
}
|
30
tests/commands/mod.rs
Normal file
30
tests/commands/mod.rs
Normal file
@ -0,0 +1,30 @@
|
||||
mod append;
|
||||
mod cd;
|
||||
mod compact;
|
||||
mod cp;
|
||||
mod default;
|
||||
mod edit;
|
||||
mod enter;
|
||||
mod first;
|
||||
mod format;
|
||||
mod get;
|
||||
mod group_by;
|
||||
mod histogram;
|
||||
mod insert;
|
||||
mod last;
|
||||
mod lines;
|
||||
mod ls;
|
||||
mod mkdir;
|
||||
mod mv;
|
||||
mod open;
|
||||
mod parse;
|
||||
mod prepend;
|
||||
mod range;
|
||||
mod reverse;
|
||||
mod rm;
|
||||
mod save;
|
||||
mod sort_by;
|
||||
mod split_by;
|
||||
mod split_column;
|
||||
mod where_;
|
||||
mod wrap;
|
182
tests/commands/mv.rs
Normal file
182
tests/commands/mv.rs
Normal file
@ -0,0 +1,182 @@
|
||||
use test_support::fs::{files_exist_at, Stub::EmptyFile};
|
||||
use test_support::nu;
|
||||
use test_support::playground::Playground;
|
||||
|
||||
#[test]
|
||||
fn moves_a_file() {
|
||||
Playground::setup("mv_test_1", |dirs, sandbox| {
|
||||
sandbox
|
||||
.with_files(vec![EmptyFile("andres.txt")])
|
||||
.mkdir("expected");
|
||||
|
||||
let original = dirs.test().join("andres.txt");
|
||||
let expected = dirs.test().join("expected/yehuda.txt");
|
||||
|
||||
nu!(
|
||||
cwd: dirs.test(),
|
||||
"mv andres.txt expected/yehuda.txt"
|
||||
);
|
||||
|
||||
assert!(!original.exists());
|
||||
assert!(expected.exists());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overwrites_if_moving_to_existing_file() {
|
||||
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!(
|
||||
cwd: dirs.test(),
|
||||
"mv andres.txt jonathan.txt"
|
||||
);
|
||||
|
||||
assert!(!original.exists());
|
||||
assert!(expected.exists());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn moves_a_directory() {
|
||||
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!(
|
||||
cwd: dirs.test(),
|
||||
"mv empty_dir renamed_dir"
|
||||
);
|
||||
|
||||
assert!(!original_dir.exists());
|
||||
assert!(expected.exists());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn moves_the_file_inside_directory_if_path_to_move_is_existing_directory() {
|
||||
Playground::setup("mv_test_4", |dirs, sandbox| {
|
||||
sandbox
|
||||
.with_files(vec![EmptyFile("jonathan.txt")])
|
||||
.mkdir("expected");
|
||||
|
||||
let original_dir = dirs.test().join("jonathan.txt");
|
||||
let expected = dirs.test().join("expected/jonathan.txt");
|
||||
|
||||
nu!(
|
||||
cwd: dirs.test(),
|
||||
"mv jonathan.txt expected"
|
||||
);
|
||||
|
||||
assert!(!original_dir.exists());
|
||||
assert!(expected.exists());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn moves_the_directory_inside_directory_if_path_to_move_is_existing_directory() {
|
||||
Playground::setup("mv_test_5", |dirs, sandbox| {
|
||||
sandbox
|
||||
.within("contributors")
|
||||
.with_files(vec![EmptyFile("jonathan.txt")])
|
||||
.mkdir("expected");
|
||||
|
||||
let original_dir = dirs.test().join("contributors");
|
||||
let expected = dirs.test().join("expected/contributors");
|
||||
|
||||
nu!(
|
||||
cwd: dirs.test(),
|
||||
"mv contributors expected"
|
||||
);
|
||||
|
||||
assert!(!original_dir.exists());
|
||||
assert!(expected.exists());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn moves_the_directory_inside_directory_if_path_to_move_is_nonexistent_directory() {
|
||||
Playground::setup("mv_test_6", |dirs, sandbox| {
|
||||
sandbox
|
||||
.within("contributors")
|
||||
.with_files(vec![EmptyFile("jonathan.txt")])
|
||||
.mkdir("expected");
|
||||
|
||||
let original_dir = dirs.test().join("contributors");
|
||||
|
||||
nu!(
|
||||
cwd: dirs.test(),
|
||||
"mv contributors expected/this_dir_exists_now/los_tres_amigos"
|
||||
);
|
||||
|
||||
let expected = dirs
|
||||
.test()
|
||||
.join("expected/this_dir_exists_now/los_tres_amigos");
|
||||
|
||||
assert!(!original_dir.exists());
|
||||
assert!(expected.exists());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn moves_using_path_with_wildcard() {
|
||||
Playground::setup("mv_test_7", |dirs, sandbox| {
|
||||
sandbox
|
||||
.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");
|
||||
|
||||
let work_dir = dirs.test().join("work_dir");
|
||||
let expected = dirs.test().join("expected");
|
||||
|
||||
nu!(cwd: work_dir, "mv ../originals/*.ini ../expected");
|
||||
|
||||
assert!(files_exist_at(
|
||||
vec!["yehuda.ini", "jonathan.ini", "sample.ini", "andres.ini",],
|
||||
expected
|
||||
));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn moves_using_a_glob() {
|
||||
Playground::setup("mv_test_8", |dirs, sandbox| {
|
||||
sandbox
|
||||
.within("meals")
|
||||
.with_files(vec![
|
||||
EmptyFile("arepa.txt"),
|
||||
EmptyFile("empanada.txt"),
|
||||
EmptyFile("taquiza.txt"),
|
||||
])
|
||||
.mkdir("work_dir")
|
||||
.mkdir("expected");
|
||||
|
||||
let meal_dir = dirs.test().join("meals");
|
||||
let work_dir = dirs.test().join("work_dir");
|
||||
let expected = dirs.test().join("expected");
|
||||
|
||||
nu!(cwd: work_dir, "mv ../meals/* ../expected");
|
||||
|
||||
assert!(meal_dir.exists());
|
||||
assert!(files_exist_at(
|
||||
vec!["arepa.txt", "empanada.txt", "taquiza.txt",],
|
||||
expected
|
||||
));
|
||||
})
|
||||
}
|
39
tests/commands/nth.rs
Normal file
39
tests/commands/nth.rs
Normal file
@ -0,0 +1,39 @@
|
||||
#[test]
|
||||
fn selects_a_row() {
|
||||
Playground::setup("nth_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![EmptyFile("notes.txt"), EmptyFile("arepas.txt")]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls
|
||||
| sort-by name
|
||||
| nth 0
|
||||
| get name
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "arepas.txt");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selects_many_rows() {
|
||||
Playground::setup("nth_test_2", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![EmptyFile("notes.txt"), EmptyFile("arepas.txt")]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls
|
||||
| get name
|
||||
| nth 1 0
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "2");
|
||||
});
|
||||
}
|
229
tests/commands/open.rs
Normal file
229
tests/commands/open.rs
Normal file
@ -0,0 +1,229 @@
|
||||
use test_support::fs::Stub::FileWithContentToBeTrimmed;
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, nu_error, pipeline};
|
||||
|
||||
#[test]
|
||||
fn parses_csv() {
|
||||
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 actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open nu.zion.csv
|
||||
| where author == "Andres N. Robalino"
|
||||
| get source
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "Ecuador");
|
||||
})
|
||||
}
|
||||
|
||||
// sample.bson has the following format:
|
||||
// ━━━━━━━━━━┯━━━━━━━━━━━
|
||||
// _id │ root
|
||||
// ──────────┼───────────
|
||||
// [object] │ [9 items]
|
||||
// ━━━━━━━━━━┷━━━━━━━━━━━
|
||||
//
|
||||
// the root value is:
|
||||
// ━━━┯━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━
|
||||
// # │ _id │ a │ b │ c
|
||||
// ───┼───────────────────┼─────────────────────────┼──────────┼──────────
|
||||
// 0 │ [object] │ 1.000000000000000 │ hello │ [2 items]
|
||||
// 1 │ [object] │ 42.00000000000000 │ whel │ hello
|
||||
// 2 │ [object] │ [object] │ │
|
||||
// 3 │ [object] │ │ [object] │
|
||||
// 4 │ [object] │ │ │ [object]
|
||||
// 5 │ [object] │ │ │ [object]
|
||||
// 6 │ [object] │ [object] │ [object] │
|
||||
// 7 │ [object] │ <date value> │ [object] │
|
||||
// 8 │ 1.000000 │ <decimal value> │ [object] │
|
||||
//
|
||||
// The decimal value is supposed to be π, but is currently wrong due to
|
||||
// what appears to be an issue in the bson library that is under investigation.
|
||||
//
|
||||
|
||||
#[test]
|
||||
fn parses_bson() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats",
|
||||
"open sample.bson | get root | nth 0 | get b | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(actual, "hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_more_bson_complexity() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open sample.bson
|
||||
| get root
|
||||
| nth 6
|
||||
| get b
|
||||
| get '$binary_subtype'
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "function");
|
||||
}
|
||||
|
||||
// sample.db has the following format:
|
||||
//
|
||||
// ━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━
|
||||
// # │ table_name │ table_values
|
||||
// ───┼────────────┼──────────────
|
||||
// 0 │ strings │ [6 items]
|
||||
// 1 │ ints │ [5 items]
|
||||
// 2 │ floats │ [4 items]
|
||||
// ━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━
|
||||
//
|
||||
// In this case, this represents a sqlite database
|
||||
// with three tables named `strings`, `ints`, and `floats`.
|
||||
// The table_values represent the values for the tables:
|
||||
//
|
||||
// ━━━━┯━━━━━━━┯━━━━━━━━━━┯━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
// # │ x │ y │ z │ f
|
||||
// ────┼───────┼──────────┼──────┼──────────────────────────────────────────────────────────────────────
|
||||
// 0 │ hello │ <binary> │ │
|
||||
// 1 │ hello │ <binary> │ │
|
||||
// 2 │ hello │ <binary> │ │
|
||||
// 3 │ hello │ <binary> │ │
|
||||
// 4 │ world │ <binary> │ │
|
||||
// 5 │ world │ <binary> │ │
|
||||
// 6 │ │ │ 1 │
|
||||
// 7 │ │ │ 42 │
|
||||
// 8 │ │ │ 425 │
|
||||
// 9 │ │ │ 4253 │
|
||||
// 10 │ │ │ │
|
||||
// 11 │ │ │ │ 3.400000000000000
|
||||
// 12 │ │ │ │ 3.141592650000000
|
||||
// 13 │ │ │ │ 23.00000000000000
|
||||
// 14 │ │ │ │ this string that doesn't really belong here but sqlite is what it is
|
||||
// ━━━━┷━━━━━━━┷━━━━━━━━━━┷━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
//
|
||||
// We can see here that each table has different columns. `strings` has `x` and `y`, while
|
||||
// `ints` has just `z`, and `floats` has only the column `f`. This means, in general, when working
|
||||
// with sqlite, one will want to select a single table, e.g.:
|
||||
//
|
||||
// open sample.db | nth 1 | get table_values
|
||||
// ━━━┯━━━━━━
|
||||
// # │ z
|
||||
// ───┼──────
|
||||
// 0 │ 1
|
||||
// 1 │ 42
|
||||
// 2 │ 425
|
||||
// 3 │ 4253
|
||||
// 4 │
|
||||
// ━━━┷━━━━━━
|
||||
|
||||
#[test]
|
||||
fn parses_sqlite() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open sample.db
|
||||
| get table_values
|
||||
| nth 2
|
||||
| get x
|
||||
| echo $it"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_toml() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats",
|
||||
"open cargo_sample.toml | get package.edition | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(actual, "2018");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_tsv() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open caco3_plastics.tsv
|
||||
| first 1
|
||||
| get origin
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "SPAIN")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_json() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open sgml_description.json
|
||||
| get glossary.GlossDiv.GlossList.GlossEntry.GlossSee
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "markup")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_xml() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats",
|
||||
"open jonathan.xml | get rss.channel | get item | get link | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
actual,
|
||||
"http://www.jonathanturner.org/2015/10/off-to-new-adventures.html"
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_ini() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats",
|
||||
"open sample.ini | get SectionOne.integer | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(actual, "1234")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_utf16_ini() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats",
|
||||
"open utf16.ini | get '.ShellClassInfo' | get IconIndex | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(actual, "-236")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_if_file_not_found() {
|
||||
let actual = nu_error!(
|
||||
cwd: "tests/fixtures/formats",
|
||||
"open i_dont_exist.txt"
|
||||
);
|
||||
|
||||
assert!(actual.contains("File could not be opened"));
|
||||
assert!(actual.contains("file not found"));
|
||||
}
|
17
tests/commands/parse.rs
Normal file
17
tests/commands/parse.rs
Normal file
@ -0,0 +1,17 @@
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn extracts_fields_from_the_given_the_pattern() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open fileA.txt
|
||||
| parse "{Name}={Value}"
|
||||
| nth 1
|
||||
| get Value
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "StupidLongName");
|
||||
}
|
17
tests/commands/prepend.rs
Normal file
17
tests/commands/prepend.rs
Normal file
@ -0,0 +1,17 @@
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn adds_a_row_to_the_beginning() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open fileA.txt
|
||||
| lines
|
||||
| prepend "testme"
|
||||
| nth 0
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "testme");
|
||||
}
|
47
tests/commands/range.rs
Normal file
47
tests/commands/range.rs
Normal file
@ -0,0 +1,47 @@
|
||||
use test_support::fs::Stub::EmptyFile;
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn selects_a_row() {
|
||||
Playground::setup("range_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![EmptyFile("notes.txt"), EmptyFile("tests.txt")]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls
|
||||
| sort-by name
|
||||
| range 0..0
|
||||
| get name
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "notes.txt");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selects_some_rows() {
|
||||
Playground::setup("range_test_2", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![
|
||||
EmptyFile("notes.txt"),
|
||||
EmptyFile("tests.txt"),
|
||||
EmptyFile("persons.txt"),
|
||||
]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls
|
||||
| get name
|
||||
| range 1..2
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "2");
|
||||
});
|
||||
}
|
11
tests/commands/reverse.rs
Normal file
11
tests/commands/reverse.rs
Normal file
@ -0,0 +1,11 @@
|
||||
use test_support::nu;
|
||||
|
||||
#[test]
|
||||
fn can_get_reverse_first() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats",
|
||||
"ls | sort-by name | reverse | first 1 | get name | trim | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(actual, "utf16.ini");
|
||||
}
|
161
tests/commands/rm.rs
Normal file
161
tests/commands/rm.rs
Normal file
@ -0,0 +1,161 @@
|
||||
use test_support::fs::{files_exist_at, Stub::EmptyFile};
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, nu_error};
|
||||
|
||||
#[test]
|
||||
fn removes_a_file() {
|
||||
Playground::setup("rm_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![EmptyFile("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");
|
||||
|
||||
assert!(!path.exists());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removes_files_with_wildcard() {
|
||||
Playground::setup("rm_test_2", |dirs, sandbox| {
|
||||
sandbox
|
||||
.within("src")
|
||||
.with_files(vec![
|
||||
EmptyFile("cli.rs"),
|
||||
EmptyFile("lib.rs"),
|
||||
EmptyFile("prelude.rs"),
|
||||
])
|
||||
.within("src/parser")
|
||||
.with_files(vec![EmptyFile("parse.rs"), EmptyFile("parser.rs")])
|
||||
.within("src/parser/parse")
|
||||
.with_files(vec![EmptyFile("token_tree.rs")])
|
||||
.within("src/parser/hir")
|
||||
.with_files(vec![
|
||||
EmptyFile("baseline_parse.rs"),
|
||||
EmptyFile("baseline_parse_tokens.rs"),
|
||||
]);
|
||||
|
||||
nu!(
|
||||
cwd: dirs.test(),
|
||||
r#"rm "src/*/*/*.rs""#
|
||||
);
|
||||
|
||||
assert!(!files_exist_at(
|
||||
vec![
|
||||
"src/parser/parse/token_tree.rs",
|
||||
"src/parser/hir/baseline_parse.rs",
|
||||
"src/parser/hir/baseline_parse_tokens.rs"
|
||||
],
|
||||
dirs.test()
|
||||
));
|
||||
|
||||
assert_eq!(
|
||||
Playground::glob_vec(&format!("{}/src/*/*/*.rs", dirs.test().display())),
|
||||
Vec::<std::path::PathBuf>::new()
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removes_deeply_nested_directories_with_wildcard_and_recursive_flag() {
|
||||
Playground::setup("rm_test_3", |dirs, sandbox| {
|
||||
sandbox
|
||||
.within("src")
|
||||
.with_files(vec![
|
||||
EmptyFile("cli.rs"),
|
||||
EmptyFile("lib.rs"),
|
||||
EmptyFile("prelude.rs"),
|
||||
])
|
||||
.within("src/parser")
|
||||
.with_files(vec![EmptyFile("parse.rs"), EmptyFile("parser.rs")])
|
||||
.within("src/parser/parse")
|
||||
.with_files(vec![EmptyFile("token_tree.rs")])
|
||||
.within("src/parser/hir")
|
||||
.with_files(vec![
|
||||
EmptyFile("baseline_parse.rs"),
|
||||
EmptyFile("baseline_parse_tokens.rs"),
|
||||
]);
|
||||
|
||||
nu!(
|
||||
cwd: dirs.test(),
|
||||
"rm src/* --recursive"
|
||||
);
|
||||
|
||||
assert!(!files_exist_at(
|
||||
vec!["src/parser/parse", "src/parser/hir"],
|
||||
dirs.test()
|
||||
));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removes_directory_contents_without_recursive_flag_if_empty() {
|
||||
Playground::setup("rm_test_4", |dirs, _| {
|
||||
nu!(
|
||||
cwd: dirs.root(),
|
||||
"rm rm_test_4"
|
||||
);
|
||||
|
||||
assert!(!dirs.test().exists());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removes_directory_contents_with_recursive_flag() {
|
||||
Playground::setup("rm_test_5", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![
|
||||
EmptyFile("yehuda.txt"),
|
||||
EmptyFile("jonathan.txt"),
|
||||
EmptyFile("andres.txt"),
|
||||
]);
|
||||
|
||||
nu!(
|
||||
cwd: dirs.root(),
|
||||
"rm rm_test_5 --recursive"
|
||||
);
|
||||
|
||||
assert!(!dirs.test().exists());
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_if_attempting_to_delete_a_directory_with_content_without_recursive_flag() {
|
||||
Playground::setup("rm_test_6", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![EmptyFile("some_empty_file.txt")]);
|
||||
|
||||
let actual = nu_error!(
|
||||
cwd: dirs.root(),
|
||||
"rm rm_test_6"
|
||||
);
|
||||
|
||||
assert!(dirs.test().exists());
|
||||
assert!(actual.contains("is a directory"));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_if_attempting_to_delete_single_dot_as_argument() {
|
||||
Playground::setup("rm_test_7", |dirs, _| {
|
||||
let actual = nu_error!(
|
||||
cwd: dirs.root(),
|
||||
"rm ."
|
||||
);
|
||||
|
||||
assert!(actual.contains("may not be removed"));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_if_attempting_to_delete_two_dot_as_argument() {
|
||||
Playground::setup("rm_test_8", |dirs, _| {
|
||||
let actual = nu_error!(
|
||||
cwd: dirs.root(),
|
||||
"rm .."
|
||||
);
|
||||
|
||||
assert!(actual.contains("may not be removed"));
|
||||
})
|
||||
}
|
47
tests/commands/save.rs
Normal file
47
tests/commands/save.rs
Normal file
@ -0,0 +1,47 @@
|
||||
use test_support::fs::{file_contents, Stub::FileWithContent};
|
||||
use test_support::nu;
|
||||
use test_support::playground::Playground;
|
||||
|
||||
#[test]
|
||||
fn figures_out_intelligently_where_to_write_out_with_metadata() {
|
||||
Playground::setup("save_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContent(
|
||||
"cargo_sample.toml",
|
||||
r#"
|
||||
[package]
|
||||
name = "nu"
|
||||
version = "0.1.1"
|
||||
authors = ["Yehuda Katz <wycats@gmail.com>"]
|
||||
description = "A shell for the GitHub era"
|
||||
license = "ISC"
|
||||
edition = "2018"
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let subject_file = dirs.test().join("cargo_sample.toml");
|
||||
|
||||
nu!(
|
||||
cwd: dirs.root(),
|
||||
"open save_test_1/cargo_sample.toml | save"
|
||||
);
|
||||
|
||||
let actual = file_contents(&subject_file);
|
||||
assert!(actual.contains("0.1.1"));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writes_out_csv() {
|
||||
Playground::setup("save_test_2", |dirs, _| {
|
||||
let expected_file = dirs.test().join("cargo_sample.csv");
|
||||
|
||||
nu!(
|
||||
cwd: dirs.root(),
|
||||
"open {}/cargo_sample.toml | get package | save save_test_2/cargo_sample.csv",
|
||||
dirs.formats()
|
||||
);
|
||||
|
||||
let actual = file_contents(expected_file);
|
||||
assert!(actual.contains("[Table],A shell for the GitHub era,2018,ISC,nu,0.1.1"));
|
||||
})
|
||||
}
|
23
tests/commands/sort_by.rs
Normal file
23
tests/commands/sort_by.rs
Normal file
@ -0,0 +1,23 @@
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn by_column() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", 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!(actual, "description");
|
||||
}
|
55
tests/commands/split_by.rs
Normal file
55
tests/commands/split_by.rs
Normal file
@ -0,0 +1,55 @@
|
||||
use test_support::fs::Stub::{EmptyFile, FileWithContentToBeTrimmed};
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, nu_error, pipeline};
|
||||
|
||||
#[test]
|
||||
fn splits() {
|
||||
Playground::setup("split_by_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"los_tres_caballeros.csv",
|
||||
r#"
|
||||
first_name,last_name,rusty_at,type
|
||||
Andrés,Robalino,10/11/2013,A
|
||||
Jonathan,Turner,10/12/2013,B
|
||||
Yehuda,Katz,10/11/2013,A
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open los_tres_caballeros.csv
|
||||
| group-by rusty_at
|
||||
| split-by type
|
||||
| get A."10/11/2013"
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "2");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_if_no_table_given_as_input() {
|
||||
Playground::setup("split_by_test_2", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![
|
||||
EmptyFile("los.txt"),
|
||||
EmptyFile("tres.txt"),
|
||||
EmptyFile("amigos.txt"),
|
||||
EmptyFile("arepas.clu"),
|
||||
]);
|
||||
|
||||
let actual = nu_error!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls
|
||||
| get name
|
||||
| split-by type
|
||||
"#
|
||||
));
|
||||
|
||||
assert!(actual.contains("Expected table from pipeline"));
|
||||
})
|
||||
}
|
20
tests/commands/split_column.rs
Normal file
20
tests/commands/split_column.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn by_column() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open cargo_sample.toml --raw
|
||||
| lines
|
||||
| skip 1
|
||||
| first 1
|
||||
| split-column "="
|
||||
| get Column1
|
||||
| trim
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "name");
|
||||
}
|
120
tests/commands/where_.rs
Normal file
120
tests/commands/where_.rs
Normal file
@ -0,0 +1,120 @@
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn filters_by_unit_size_comparison() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats",
|
||||
"ls | where size > 1kb | sort-by size | get name | first 1 | trim | echo $it"
|
||||
);
|
||||
|
||||
assert_eq!(actual, "cargo_sample.toml");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn binary_operator_comparisons() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open sample.db
|
||||
| where table_name == ints
|
||||
| get table_values
|
||||
| first 4
|
||||
| where z > 4200
|
||||
| get z
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "4253");
|
||||
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open sample.db
|
||||
| where table_name == ints
|
||||
| get table_values
|
||||
| first 4
|
||||
| where z >= 4253
|
||||
| get z
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "4253");
|
||||
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open sample.db
|
||||
| where table_name == ints
|
||||
| get table_values
|
||||
| first 4
|
||||
| where z < 10
|
||||
| get z
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "1");
|
||||
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open sample.db
|
||||
| where table_name == ints
|
||||
| get table_values
|
||||
| first 4
|
||||
| where z <= 1
|
||||
| get z
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "1");
|
||||
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open sample.db
|
||||
| where table_name == ints
|
||||
| get table_values
|
||||
| where z != 1
|
||||
| first 1
|
||||
| get z
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "42");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn contains_operator() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open sample.db
|
||||
| where table_name == strings
|
||||
| get table_values
|
||||
| where x =~ ell
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "4");
|
||||
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open sample.db
|
||||
| where table_name == strings
|
||||
| get table_values
|
||||
| where x !~ ell
|
||||
| count
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "2");
|
||||
}
|
63
tests/commands/wrap.rs
Normal file
63
tests/commands/wrap.rs
Normal file
@ -0,0 +1,63 @@
|
||||
use test_support::fs::Stub::FileWithContentToBeTrimmed;
|
||||
use test_support::playground::Playground;
|
||||
use test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn wrap_rows_into_a_row() {
|
||||
Playground::setup("embed_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"los_tres_caballeros.txt",
|
||||
r#"
|
||||
first_name,last_name
|
||||
Andrés,Robalino
|
||||
Jonathan,Turner
|
||||
Yehuda,Katz
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open los_tres_caballeros.txt
|
||||
| from-csv
|
||||
| wrap caballeros
|
||||
| get caballeros
|
||||
| nth 0
|
||||
| get last_name
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "Robalino");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrap_rows_into_a_table() {
|
||||
Playground::setup("embed_test_2", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"los_tres_caballeros.txt",
|
||||
r#"
|
||||
first_name,last_name
|
||||
Andrés,Robalino
|
||||
Jonathan,Turner
|
||||
Yehuda,Katz
|
||||
"#,
|
||||
)]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open los_tres_caballeros.txt
|
||||
| from-csv
|
||||
| get last_name
|
||||
| wrap caballero
|
||||
| nth 2
|
||||
| get caballero
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "Katz");
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user