From 1a34bc319841b3bc78a21b0f3a986f7eed09124d Mon Sep 17 00:00:00 2001 From: bootandy Date: Fri, 6 Apr 2018 20:44:02 +0100 Subject: [PATCH] Integration tests Several tests for recursive dirs, hard & soft links. Tests use the tempfile project. Personally I find the tests hard to read. Am considering adding a '--no-color' output option as this will make the tests much more readable. (Currently we have to call format_string to get the matching colors and if a test fails the diff is very hard to read). --- Cargo.toml | 2 + src/tests.rs | 122 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 119 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f2410c4..ad11a1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,5 @@ authors = ["bootandy ", "nebkor "] ansi_term = "0.11" clap = "2.31" assert_cli = "0.5" +tempfile = "3" + diff --git a/src/tests.rs b/src/tests.rs index 3af44b9..667e9b2 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,11 +1,13 @@ extern crate ansi_term; +extern crate tempfile; +use self::tempfile::Builder; +use self::tempfile::TempDir; use super::*; use display::format_string; - -// TESTS TODO: -// handle recursive dirs -// handle soft links -// handle hard links +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; +use std::process::Command; #[test] pub fn test_main() { @@ -26,3 +28,113 @@ pub fn test_main() { .is(r) .unwrap(); } + +#[test] +pub fn test_apparent_size() { + let r = format!( + "{}", + format_string("src/test_dir/many/hello_file", true, " 6B", " ├──",), + ); + + assert_cli::Assert::main_binary() + .with_args(&["-s", "src/test_dir"]) + .stdout() + .contains(r) + .unwrap(); +} + +fn build_temp_file(dir: &TempDir) -> (PathBuf) { + let file_path = dir.path().join("notes.txt"); + let mut file = File::create(&file_path).unwrap(); + writeln!(file, "I am a temp file").unwrap(); + file_path +} + +#[test] +pub fn test_soft_sym_link() { + let dir = Builder::new().tempdir().unwrap(); + let file = build_temp_file(&dir); + let dir_s = dir.path().to_str().unwrap(); + let file_path_s = file.to_str().unwrap(); + + let link_name = dir.path().join("the_link"); + let link_name_s = link_name.to_str().unwrap(); + let c = Command::new("ln") + .arg("-s") + .arg(file_path_s) + .arg(link_name_s) + .output(); + assert!(c.is_ok()); + + let r = format!( + "{} +{} +{}", + format_string(dir_s, true, " 8.0K", ""), + format_string(file_path_s, true, " 4.0K", "├──",), + format_string(link_name_s, false, " 4.0K", "└──",), + ); + + assert_cli::Assert::main_binary() + .with_args(&[dir_s]) + .stdout() + .contains(r) + .unwrap(); +} + +// Hard links are ignored as the inode is the same as the file +#[test] +pub fn test_hard_sym_link() { + let dir = Builder::new().tempdir().unwrap(); + let file = build_temp_file(&dir); + let dir_s = dir.path().to_str().unwrap(); + let file_path_s = file.to_str().unwrap(); + + let link_name = dir.path().join("the_link"); + let link_name_s = link_name.to_str().unwrap(); + let c = Command::new("ln") + .arg(file_path_s) + .arg(link_name_s) + .output(); + assert!(c.is_ok()); + + let r = format!( + "{} +{}", + format_string(dir_s, true, " 4.0K", ""), + format_string(file_path_s, true, " 4.0K", "└──") + ); + + assert_cli::Assert::main_binary() + .with_args(&[dir_s]) + .stdout() + .contains(r) + .unwrap(); +} + +//Check we don't recurse down an infinite symlink tree +#[test] +pub fn test_recursive_sym_link() { + let dir = Builder::new().tempdir().unwrap(); + let dir_s = dir.path().to_str().unwrap(); + + let link_name = dir.path().join("the_link"); + let link_name_s = link_name.to_str().unwrap(); + + let c = Command::new("cd").arg(dir_s).output(); + assert!(c.is_ok()); + let c = Command::new("ln") + .arg("-s") + .arg(".") + .arg(link_name_s) + .output(); + assert!(c.is_ok()); + + let r = format!("{}", format_string(dir_s, true, " 4.0K", "")); + + assert_cli::Assert::main_binary() + .with_args(&[dir_s]) + .stdout() + .contains(r) + .unwrap(); +}