Test helpers and unit tests (#26)
* Implement a few utility functions for tests Most tests would require a test file hierarchy instead of operating on a live system. * Add unit tests for `tab::scan_path` Tests: * Works on a valid path * Returns an empty Vec on an invalid directory * Returns an empty Vec for an empty directory I also implemented a few test helpers that may be useful for other unit tests. * Less spammy test logs and placate Clippy.
This commit is contained in:
parent
60eeb724d1
commit
d6c58991c0
4 changed files with 228 additions and 0 deletions
70
src/tab.rs
70
src/tab.rs
|
|
@ -1013,3 +1013,73 @@ impl Tab {
|
|||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io;
|
||||
|
||||
use log::debug;
|
||||
use test_log::test;
|
||||
|
||||
use super::scan_path;
|
||||
use crate::test_utils::{
|
||||
empty_fs, eq_path_item, simple_fs, sort_files, NAME_LEN, NUM_DIRS, NUM_FILES, NUM_NESTED,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn scan_path_succeeds_on_valid_path() -> io::Result<()> {
|
||||
let fs = simple_fs(NUM_FILES, NUM_DIRS, NUM_NESTED, NAME_LEN)?;
|
||||
let path = fs.path();
|
||||
|
||||
let mut entries: Vec<_> = path
|
||||
.read_dir()?
|
||||
.map(|maybe_entry| maybe_entry.map(|entry| entry.path()))
|
||||
.collect::<io::Result<_>>()?;
|
||||
entries.sort_by(|a, b| sort_files(a, b));
|
||||
|
||||
debug!("Calling scan_path(\"{}\")", path.display());
|
||||
let actual = scan_path(&path.to_owned());
|
||||
|
||||
// scan_path shouldn't skip any entries
|
||||
assert_eq!(entries.len(), actual.len());
|
||||
|
||||
// Correct files should be scanned
|
||||
assert!(entries
|
||||
.into_iter()
|
||||
.zip(actual.into_iter())
|
||||
.all(|(path, item)| eq_path_item(&path, &item)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scan_path_returns_empty_vec_for_invalid_path() -> io::Result<()> {
|
||||
let fs = simple_fs(NUM_FILES, NUM_DIRS, NUM_NESTED, NAME_LEN)?;
|
||||
let path = fs.path();
|
||||
|
||||
// A nonexisting path within the temp dir
|
||||
let invalid_path = path.join("ferris");
|
||||
assert!(!invalid_path.exists());
|
||||
|
||||
debug!("Calling scan_path(\"{}\")", invalid_path.display());
|
||||
let actual = scan_path(&invalid_path);
|
||||
|
||||
assert!(actual.is_empty());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scan_path_empty_dir_returns_empty_vec() -> io::Result<()> {
|
||||
let fs = empty_fs()?;
|
||||
let path = fs.path();
|
||||
|
||||
debug!("Calling scan_path(\"{}\")", path.display());
|
||||
let actual = scan_path(&path.to_owned());
|
||||
|
||||
assert_eq!(0, path.read_dir()?.count());
|
||||
assert_eq!(0, actual.len());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue