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.
This commit is contained in:
Yehuda Katz
2019-08-28 10:01:16 -07:00
parent 8e95508353
commit 21ad06b1e1
21 changed files with 823 additions and 732 deletions

View File

@ -1,51 +1,40 @@
mod helpers;
use h::{in_directory as cwd, Playground};
use h::Playground;
use helpers as h;
use std::path::{Path, PathBuf};
#[test]
fn creates_directory() {
let sandbox = Playground::setup_for("mkdir_test_1").test_dir_name();
Playground::setup("mkdir_test_1", |dirs, _| {
nu!(dirs.test(), "mkdir my_new_directory");
let full_path = format!("{}/{}", Playground::root(), sandbox);
let expected = dirs.test().join("my_new_directory");
nu!(_output, cwd(&full_path), "mkdir my_new_directory");
let mut expected = PathBuf::from(full_path);
expected.push("my_new_directory");
assert!(h::dir_exists_at(expected));
assert!(h::dir_exists_at(expected));
})
}
#[test]
fn accepts_and_creates_directories() {
let sandbox = Playground::setup_for("mkdir_test_2").test_dir_name();
Playground::setup("mkdir_test_2", |dirs, _| {
nu!(dirs.test(), "mkdir dir_1 dir_2 dir_3");
let full_path = format!("{}/{}", Playground::root(), sandbox);
nu!(_output, cwd(&full_path), "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")],
PathBuf::from(&full_path)
));
assert!(h::files_exist_at(
vec![Path::new("dir_1"), Path::new("dir_2"), Path::new("dir_3")],
dirs.test()
));
})
}
#[test]
fn creates_intermediary_directories() {
let sandbox = Playground::setup_for("mkdir_test_3").test_dir_name();
Playground::setup("mkdir_test_3", |dirs, _| {
nu!(dirs.test(), "mkdir some_folder/another/deeper_one");
let full_path = format!("{}/{}", Playground::root(), sandbox);
let mut expected = PathBuf::from(dirs.test());
expected.push("some_folder/another/deeper_one");
nu!(
_output,
cwd(&full_path),
"mkdir some_folder/another/deeper_one"
);
let mut expected = PathBuf::from(full_path);
expected.push("some_folder/another/deeper_one");
assert!(h::dir_exists_at(expected));
assert!(h::dir_exists_at(expected));
})
}