nushell/tests/command_ls_tests.rs
Yehuda Katz 21ad06b1e1 Remove unwraps and clean up playground
The original intent of this patch was to remove more unwraps to reduce
panics. I then lost a ton of time to the fact that the playground isn't
in a temp directory (because of permissions issues on Windows).

This commit improves the test facilities to:

- use a tempdir for the playground
- change the playground API so you instantiate it with a block that
  encloses the lifetime of the tempdir
- the block is called with a `dirs` argument that has `dirs.test()` and
  other important directories that we were computing by hand all the time
- the block is also called with a `playground` argument that you can use
  to construct files (it's the same `Playground` as before)
- change the nu! and nu_error! macros to produce output instead of
  taking a variable binding
- change the nu! and nu_error! macros to do the cwd() transformation
  internally
- change the nu! and nu_error! macros to take varargs at the end that
  get interpolated into the running command

I didn't manage to finish porting all of the tests, so a bunch of tests
are currently commented out. That will need to change before we land
this patch.
2019-08-28 10:01:16 -07:00

67 lines
1.8 KiB
Rust

mod helpers;
use h::{in_directory as cwd, Playground, Stub::*};
use helpers as h;
#[test]
fn ls_lists_regular_files() {
Playground::setup("ls_lists_files_test", |dirs, playground| {
playground
.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"#
);
assert_eq!(output, "30");
})
}
#[test]
fn ls_lists_regular_files_using_asterisk_wildcard() {
Playground::setup("ls_asterisk_wildcard_test", |dirs, playground| {
playground
.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"#
);
assert_eq!(output, "3");
})
}
#[test]
fn ls_lists_regular_files_using_question_mark_wildcard() {
Playground::setup("ls_question_mark_wildcard_test", |dirs, playground| {
playground
.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"#
);
assert_eq!(output, "30");
})
}