Clean up lint errors

This commit is contained in:
Yehuda Katz
2019-07-23 21:10:48 -07:00
parent 5a8e041a48
commit 73deeb69db
24 changed files with 253 additions and 124 deletions

View File

@ -3,6 +3,8 @@ mod helpers;
use h::in_directory as cwd;
use helpers as h;
use nu::AbsolutePath;
#[test]
fn lines() {
nu!(output,
@ -80,73 +82,102 @@ fn open_error_if_file_not_found() {
}
#[test]
fn save_can_write_out_csv() {
let (playground_path, tests_dir) = h::setup_playground_for("save_test");
fn save_can_write_out_csv() -> Result<(), std::io::Error> {
let (playground, tmp, dir) = h::setup_playground_for("save_test")?;
let full_path = format!("{}/{}", playground_path, tests_dir );
let expected_file = format!("{}/{}", full_path , "cargo_sample.csv");
let tmp = AbsolutePath::new(tmp);
nu!(
_output,
cwd(&playground_path),
"open ../formats/cargo_sample.toml | inc package.version --minor | get package | save save_test/cargo_sample.csv"
let expected_file = tmp / "cargo_sample.csv";
let root = AbsolutePath::new(std::env::current_dir()?);
let path = root / "tests" / "fixtures" / "formats" / "cargo_sample.toml";
let command = format!(
"open {} | inc package.version --minor | get package | save {}/cargo_sample.csv",
path.as_ref().display(),
dir
);
nu!(_output, playground.path().display(), command);
let actual = h::file_contents(&expected_file);
assert!(actual.contains("[list list],A shell for the GitHub era,2018,ISC,nu,0.2.0"));
Ok(())
}
#[test]
fn rm_can_remove_a_file() {
let directory = "tests/fixtures/nuplayground";
let file = format!("{}/rm_test.txt", directory);
fn rm_can_remove_a_file() -> Result<(), std::io::Error> {
let _ = pretty_env_logger::try_init();
h::create_file_at(&file);
let (_playground, tmp, _) = h::setup_playground_for("remove_file")?;
nu!(_output, cwd(directory), "rm rm_test.txt");
let file = AbsolutePath::new(&tmp) / "rm_test.txt";
// let file = tmp.path().join("rm_test.txt");
assert!(!h::file_exists_at(&file));
h::create_file_at(&file)?;
nu!(_output, tmp.path().display(), "rm rm_test.txt");
assert!(!file.as_ref().exists());
Ok(())
}
#[test]
fn rm_can_remove_directory_contents_with_recursive_flag() {
let (playground_path, tests_dir) = h::setup_playground_for("rm_test");
fn rm_can_remove_directory_contents_with_recursive_flag() -> Result<(), std::io::Error> {
let _ = pretty_env_logger::try_init();
let (playground, tmp, dir) = h::setup_playground_for("rm_test")?;
for f in ["yehuda.txt", "jonathan.txt", "andres.txt"].iter() {
h::create_file_at(&format!("{}/{}/{}", playground_path, tests_dir, f));
h::create_file_at(&tmp.path().join(f))?;
}
nu!(
_output,
cwd("tests/fixtures/nuplayground"),
"rm rm_test --recursive"
playground.path().display(),
format!("rm {} --recursive", dir)
);
assert!(!h::file_exists_at(&format!("{}/{}", playground_path, tests_dir)));
assert!(!tmp.path().exists());
Ok(())
}
#[test]
fn rm_error_if_attempting_to_delete_a_directory_without_recursive_flag() {
let (playground_path, tests_dir) = h::setup_playground_for("rm_test_2");
let full_path = format!("{}/{}", playground_path, tests_dir);
fn rm_error_if_attempting_to_delete_a_directory_without_recursive_flag(
) -> Result<(), std::io::Error> {
let (playground, tmp, dir) = h::setup_playground_for("rm_test_2")?;
nu_error!(output, cwd("tests/fixtures/nuplayground"), "rm rm_test_2");
nu_error!(output, playground.path().display(), format!("rm {}", dir));
assert!(h::file_exists_at(&full_path));
assert!(tmp.path().exists());
assert!(output.contains("is a directory"));
h::delete_directory_at(&full_path);
h::delete_directory_at(tmp.path());
Ok(())
}
#[test]
fn rm_error_if_attempting_to_delete_single_dot_as_argument() {
nu_error!(output, cwd("tests/fixtures/nuplayground"), "rm .");
fn rm_error_if_attempting_to_delete_single_dot_as_argument() -> Result<(), std::io::Error> {
let (_playground, tmp, _) = h::setup_playground_for("rm_test_2")?;
nu_error!(output, tmp.path().display(), "rm .");
assert!(output.contains("may not be removed"));
Ok(())
}
#[test]
fn rm_error_if_attempting_to_delete_two_dot_as_argument() {
nu_error!(output, cwd("tests/fixtures/nuplayground"), "rm ..");
fn rm_error_if_attempting_to_delete_two_dot_as_argument() -> Result<(), std::io::Error> {
let (_playground, tmp, _) = h::setup_playground_for("rm_test_2")?;
nu_error!(output, tmp.path().display(), "rm ..");
assert!(output.contains("may not be removed"));
Ok(())
}

View File

@ -1,8 +1,10 @@
#![allow(dead_code)]
pub use std::path::PathBuf;
pub use std::path::{Path, PathBuf};
use log::trace;
use std::io::Read;
use tempdir::TempDir;
#[macro_export]
macro_rules! nu {
@ -79,20 +81,30 @@ macro_rules! nu_error {
};
}
pub fn setup_playground_for(topic: &str) -> (String, String) {
let home = "tests/fixtures/nuplayground";
let full_path = format!("{}/{}", home, topic);
pub fn setup_playground_for(topic: &str) -> Result<(TempDir, TempDir, String), std::io::Error> {
let home = TempDir::new("nuplayground")?;
let child = TempDir::new_in(home.path(), topic)?;
let relative = child
.path()
.file_name()
.unwrap()
.to_str()
.expect(&format!(
"file name {} was not valid",
child.path().display()
))
.to_string();
if file_exists_at(&full_path) {
delete_directory_at(&full_path);
}
trace!(
"created {:?} dir={}",
child.path().display(),
child.path().is_dir()
);
create_directory_at(&full_path);
(home.to_string(), topic.to_string())
Ok((home, child, relative))
}
pub fn file_contents(full_path: &str) -> String {
pub fn file_contents(full_path: impl AsRef<Path>) -> String {
let mut file = std::fs::File::open(full_path).expect("can not open file");
let mut contents = String::new();
file.read_to_string(&mut contents)
@ -100,19 +112,26 @@ pub fn file_contents(full_path: &str) -> String {
contents
}
pub fn create_file_at(full_path: &str) {
std::fs::write(PathBuf::from(full_path), "fake data".as_bytes()).expect("can not create file");
pub fn create_file_at(full_path: impl AsRef<Path>) -> Result<(), std::io::Error> {
let full_path = full_path.as_ref();
assert!(
full_path.parent().unwrap().is_dir(),
"{:?} exists",
full_path.parent().unwrap().display(),
);
std::fs::write(full_path, "fake data".as_bytes())
}
pub fn file_exists_at(full_path: &str) -> bool {
PathBuf::from(full_path).exists()
}
pub fn delete_directory_at(full_path: &str) {
pub fn delete_directory_at(full_path: &Path) {
std::fs::remove_dir_all(PathBuf::from(full_path)).expect("can not remove directory");
}
pub fn create_directory_at(full_path: &str) {
pub fn create_directory_at(full_path: &Path) {
let path = PathBuf::from(full_path);
println!("{:?} - is_dir: {:?}", path, path.is_dir());