From 777654b5746c5e94f6383fa94800d89e6ef9f8f0 Mon Sep 17 00:00:00 2001 From: Ayrton Chilibeck Date: Sun, 14 Jan 2024 12:31:51 -0700 Subject: [PATCH] Chaged tests, Added stubs --- Cargo.lock | 229 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/validate.rs | 93 +++++++++++--------- 3 files changed, 283 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a646b96..308cc5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "0.6.7" @@ -50,6 +59,18 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "clap" version = "4.4.16" @@ -102,6 +123,107 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "futures" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-timer" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "hashbrown" version = "0.14.3" @@ -130,6 +252,24 @@ version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +[[package]] +name = "memchr" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" + +[[package]] +name = "pin-project-lite" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "proc-macro2" version = "1.0.76" @@ -148,12 +288,91 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "relative-path" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e898588f33fdd5b9420719948f9f2a32c922a246964576f71ba7f24f80610fbc" + +[[package]] +name = "rstest" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97eeab2f3c0a199bc4be135c36c924b6590b88c377d416494288c14f2db30199" +dependencies = [ + "futures", + "futures-timer", + "rstest_macros", + "rustc_version", +] + +[[package]] +name = "rstest_macros" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d428f8247852f894ee1be110b375111b586d4fa431f6c46e64ba5a0dcccbe605" +dependencies = [ + "cfg-if", + "glob", + "proc-macro2", + "quote", + "regex", + "relative-path", + "rustc_version", + "syn", + "unicode-ident", +] + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + [[package]] name = "ryu" version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c" +[[package]] +name = "semver" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97ed7a9823b74f99c7742f5336af7be5ecd3eeafcb1507d1fa93347b1d589b0" + [[package]] name = "serde" version = "1.0.195" @@ -187,6 +406,15 @@ dependencies = [ "unsafe-libyaml", ] +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + [[package]] name = "strsim" version = "0.10.0" @@ -209,6 +437,7 @@ name = "tester" version = "0.1.0" dependencies = [ "clap", + "rstest", "serde", "serde_yaml", ] diff --git a/Cargo.toml b/Cargo.toml index b78d78d..79402b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ edition = "2021" [dependencies] clap = { version = "4.4.16", features = ["derive"] } +rstest = "0.18.2" serde = { version = "1.0.195", features = ["derive"] } serde_yaml = "0.9.30" diff --git a/src/validate.rs b/src/validate.rs index dcddc5d..8064ae6 100644 --- a/src/validate.rs +++ b/src/validate.rs @@ -1,4 +1,4 @@ -use std::{path::PathBuf, fs}; +use std::{path::PathBuf, fs, collections::HashMap}; use crate::util; @@ -27,8 +27,26 @@ pub fn validate(verbosity: u8, config: util::config::Config) { } // make sure each has a match + // - check the file stems + // - compare the file stems + // - check if either file is in the invalid list + // - add to the notification queue + let mut in_to_out: (Vec<(PathBuf, PathBuf)>, Vec) = map_files(in_files, out_files); + let invalid: Vec<(PathBuf, PathBuf)> = check_files(in_to_out[0]); + + print_invalid_mappings(in_to_out[1]); + print_invalid_pairs(invalid); + } +/** + * @brief Collects paths with invalid suffixes + * + * @param files: A vector of PathBuf containing file names + * @param suf: The expected suffix for the files + * + * @return A vector of paths with invalid suffixes +*/ fn collect_invalid(files: &Vec, suf: &str) -> Vec { let mut res: Vec = vec![]; @@ -41,7 +59,13 @@ fn collect_invalid(files: &Vec, suf: &str) -> Vec { return res } - +/** + * @brief Get all file names in a directory + * + * @param path: the path to the target directory + * + * @returns Result with a Vector of PathBuf or error +*/ fn get_dir_files(path: PathBuf) -> Result, &'static str> { let mut res: Vec = vec![]; @@ -63,32 +87,12 @@ fn get_dir_files(path: PathBuf) -> Result, &'static str> { return Ok(res); } -fn compare_files(in_files: Vec, out_files: Vec) -> Vec { - let mut res: Vec = vec![]; - - res.append(&mut compare_list(&in_files, &out_files)); - res.append(&mut compare_list(&out_files, &in_files)); - - return res; -} - -fn compare_list(from: &Vec, against: &Vec) -> Vec { - let mut res: Vec = vec![]; - - // Compare each input file to each output file - for file in from { - let beg = file.file_stem().unwrap(); // ignore suffix - for out in against { - if beg == out.file_stem().unwrap() { - break; - } - } - res.push(file.clone()); // if the file does not have a counterpart, then push to the lost files - } - - return res; -} - +/** + * @brief Print utility for the invalid paths + * + * @param files: A vector containing the invalid path names + * @param exp: The expected suffix +*/ fn print_invalid_suffixes(files: Vec, exp: &str) { println!("Invalid suffixes detected 😱:" ); println!("Current => Suggested"); @@ -108,14 +112,15 @@ fn print_invalid_suffixes(files: Vec, exp: &str) { #[cfg(test)] mod tests { + use rstest::{fixture, rstest}; + use super::*; - #[test] - fn test_collect_invalid() { - let suffix = "suf"; - let valid_path1 = PathBuf::from("path/file.suf"); - let valid_path2 = PathBuf::from("path/file_stupid.suf"); - let valid_path3 = PathBuf::from("path/file/dead.suf"); + #[fixture] + fn files() -> Vec { + let valid_path1 = PathBuf::from("path/file.suf"); + let valid_path2 = PathBuf::from("path/file_stupid.suf"); + let valid_path3 = PathBuf::from("path/file/dead.suf"); let invalid_path1 = PathBuf::from("path/file.sub"); let invalid_path2 = PathBuf::from("path/file_stupid.sur"); let invalid_path3 = PathBuf::from("path/file.bub"); @@ -123,17 +128,25 @@ mod tests { let files: Vec = vec![valid_path1.clone(), valid_path2.clone(), valid_path3.clone(), invalid_path1.clone(), invalid_path2.clone(), invalid_path3.clone()]; + return files; + } + + #[rstest] + fn test_collect_invalid(files: Vec) { + let suffix = "suf"; let invalid = collect_invalid(&files, suffix); println!("{:?}", invalid); + + // visual testing for the print formatting print_invalid_suffixes(invalid.clone(), suffix); - assert!(invalid.contains(&invalid_path1)); - assert!(invalid.contains(&invalid_path2)); - assert!(invalid.contains(&invalid_path3)); + assert!(!invalid.contains(&files[0])); + assert!(!invalid.contains(&files[1])); + assert!(!invalid.contains(&files[2])); - assert!(!invalid.contains(&valid_path1)); - assert!(!invalid.contains(&valid_path2)); - assert!(!invalid.contains(&valid_path3)); + assert!(invalid.contains(&files[3])); + assert!(invalid.contains(&files[4])); + assert!(invalid.contains(&files[5])); } }