diff --git a/.gitignore b/.gitignore index ecaefb4..d9773dc 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,5 @@ Cargo.lock /target /coverage -/llvm \ No newline at end of file +/llvm +tarpaulin-report.html \ No newline at end of file diff --git a/Makefile.toml b/Makefile.toml index a6f5dc6..ae00fbc 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -6,7 +6,7 @@ script = [ "mkdir -p coverage/html", "mkdir -p coverage/", "CARGO_INCREMENTAL=0 RUSTFLAGS='-Cinstrument-coverage -Ccodegen-units=1 -Copt-level=0 -Coverflow-checks=off' LLVM_PROFILE_FILE='llvm/cargo-test-%p-%m.profraw' cargo test --features test", -"grcov . --binary-path ./target/debug/deps/ -s . -t html --branch --ignore-not-existing --ignore '../*' --ignore '/*' --ignore '*/errors.rs' -o coverage/html/", +"grcov . --binary-path ./target/debug/deps/ -s . -t html --branch --ignore-not-existing --ignore '../*' --ignore '/*' --ignore '*/main.rs' --ignore '*/errors.rs' -o coverage/html/", ] [tasks.testing] @@ -15,3 +15,18 @@ ignore_errors = true script = [ "cargo test --features \"test\"", ] + +[tasks.win_build] +script_runner = "@shell" +ignore_errors = true +script = [ + "CROSS_CONTAINER_ENGINE=podman cross build --target x86_64-pc-windows-gnu" +] + +[tasks.win_test] +script_runner = "@shell" +ignore_errors = true +script = [ + "CROSS_CONTAINER_ENGINE=podman cross test --features test --target x86_64-pc-windows-gnu" +] + diff --git a/TODO.md b/TODO.md index 4b8cb86..03757a7 100644 --- a/TODO.md +++ b/TODO.md @@ -1,21 +1,11 @@ ## TODO [ ] Benchmarks [ ] Better use of file modes - [ ] Test populate_actions [ ] Document non coverage [ ] Have the the EditWhole save under a different path if name was changed (and edit too) - [ ] Add an option to delete a snippet - + [ ] Find a solution for the .badpocalypse + ### Currently working on - [-] Coverage - [ ] Proceed one by one and catch errors - [-] Finish tests on - [*] locate_snippet - [*] create_snippet - [*] edit_snippet - [*] edit_whole_snippet - [*] print_snippet - [ ] delete snippet ## Finished [*] Make custom errors @@ -27,7 +17,17 @@ [*] edit_snippet [*] edit_whole_snippet [*] print_snippet - [-] Have identical attributes with {0} {1} {0} (^^^^^) + [*] Finish tests on + [*] locate_snippet + [*] create_snippet + [*] edit_snippet + [*] edit_whole_snippet + [*] print_snippet + [*] delete snippet + [*] Have identical attributes with {0} {1} {0} (^^^^^) + [*] Add an option to delete a snippet + [*] Coverage + [*] Test populate_actions ## Posponed or rendered useless [x] Chain the methods when possible with result ? diff --git a/coverage/html/badges/flat.svg b/coverage/html/badges/flat.svg index 1ed2da4..a43354b 100644 --- a/coverage/html/badges/flat.svg +++ b/coverage/html/badges/flat.svg @@ -1,6 +1,6 @@ - coverage: 79% + xmlns:xlink="http://www.w3.org/1999/xlink" width="96" height="20" role="img" aria-label="coverage: 99%"> + coverage: 99% @@ -10,14 +10,14 @@ - + coverage - - 79% + + 99% \ No newline at end of file diff --git a/src/actions.rs b/src/actions.rs new file mode 100644 index 0000000..0a734b8 --- /dev/null +++ b/src/actions.rs @@ -0,0 +1,145 @@ +//! The action module, including all the logic to jump to a specific action +//! depending on the CLI arguments + +use std::error::Error; + +use crate::{errors, wrapper_functions, Cli}; + +/// List of all possible actions, some are mutually exclusive +#[derive(Debug)] +#[cfg_attr(test, derive(PartialEq))] +pub enum Actions { + /// Edit the content of the snippet only + Edit, + /// Edit the whole snippet + EditAll, + /// Locate the snippet and show the position + Locate, + /// Show the snippet (with replacement done) + Show, + /// Create the snippet with the given name and arguments + Create, + /// Deletes the snippet + Delete, +} + +/// The main worker, execute all commands given as Args +pub fn execute_parsed_cli(cli: Cli) -> Result<(), errors::SnippetError> { + let list_actions = populate_actions(&cli); + for action in list_actions.iter() { + if let Err(e) = match action { + Actions::Locate => wrapper_functions::locate_snippet(&cli), + Actions::Create => wrapper_functions::create_snippet(&cli), + Actions::Show => wrapper_functions::print_snippet(&cli), + Actions::Edit => wrapper_functions::edit_snippet(&cli), + Actions::EditAll => wrapper_functions::edit_whole_snippet(&cli), + Actions::Delete => wrapper_functions::delete_snippet(&cli), + } { + println!("An error occured during runtime:\n\t{}", e); + if let Some(e) = e.source() { + println!("This error was caused by: \n\t{}", e); + } + return Err(e); + }; + } + Ok(()) +} + +/// Populate a logical vec of succesive actions to perform depending on arguments +/// even when the arguments don't make sense +fn populate_actions(cli: &Cli) -> Vec { + let mut actions = Vec::new(); + match (cli.locate, cli.create, cli.edit, cli.edit_all, cli.delete) { + (true, _, _, _, false) => actions.push(Actions::Locate), + (_, false, false, true, false) => actions.push(Actions::EditAll), + (_, true, _, true, false) => actions.push(Actions::EditAll), + (_, true, false, false, false) => actions.push(Actions::Create), + (_, false, true, _, false) => actions.push(Actions::Edit), + (_, true, true, false, false) => actions.push(Actions::Edit), + (false, false, false, false, false) => actions.push(Actions::Show), + (_, _, _, _, true) => actions.push(Actions::Delete), + } + actions +} + +#[cfg(test)] +mod test { + use super::*; + use clap::Parser; + + macro_rules! false_cli { + ($($arg:expr),+) => { + Cli::parse_from(vec![$($arg),+]) + }; + } + + macro_rules! group_actions{ + ($(($cli:expr, $expect:expr),)*) => { + vec![$(($cli,$expect)),+] + }; + } + + #[test] + fn test_populate_actions() { + // false_cli!("snp", "name", "-l"), Actions::Locate + let group = group_actions!( + ( + false_cli!("snp", "name", "-l", "-c", "-e", "-E"), + vec![Actions::Locate] + ), + (false_cli!("snp", "name", "-l"), vec![Actions::Locate]), + (false_cli!("snp", "name", "-d"), vec![Actions::Delete]), + ( + false_cli!("snp", "name", "-d", "-e", "e", "-E", "-l"), + vec![Actions::Delete] + ), + (false_cli!("snp", "name", "-E"), vec![Actions::EditAll]), + (false_cli!("snp", "name", "-E", "-e"), vec![Actions::Edit]), + ( + false_cli!("snp", "name", "-E", "-e", "-c"), + vec![Actions::EditAll] + ), + (false_cli!("snp", "name", "-e"), vec![Actions::Edit]), + (false_cli!("snp", "name", "-e", "-c"), vec![Actions::Edit]), + (false_cli!("snp", "name", "-l"), vec![Actions::Locate]), + (false_cli!("snp", "name", "-c"), vec![Actions::Create]), + (false_cli!("snp", "name"), vec![Actions::Show]), + (false_cli!("snp", "name", "-d"), vec![Actions::Delete]), + ( + false_cli!("snp", "name", "-d", "-c", "-E", "-e", "-l"), + vec![Actions::Delete] + ), + ); + for (cli, res) in group.iter() { + println!("Testing {:?}..actions expected {:?}", cli, res); + let fnd = populate_actions(cli); + println!("... found {:?}", fnd); + assert!(res.iter().zip(fnd).all(|(a, b)| *a == b)); + } + } + #[test] + fn test_execute_parsed_cli() { + let list_cli = vec![ + false_cli!("snp", "parsed_cli", "-e", "-c"), + false_cli!("snp", "parsed_cli", "-c", "-f"), + false_cli!("snp", "parsed_cli"), + false_cli!("snp", "parsed_cli", "-E", "-f"), + false_cli!("snp", "parsed_cli", "-d"), + ]; + + let bad_cli = vec![ + false_cli!("snp", "parsed_cli_bad", "-l"), + false_cli!("snp", "-d", "parsed_cli_bad"), + ]; + + for cl in list_cli { + println!("Testing {:?} expecting success", cl); + assert!(execute_parsed_cli(cl).is_ok()); + } + + for cl in bad_cli { + println!("Testing {:?} expecting failure", cl); + assert!(execute_parsed_cli(cl).is_err()); + } + } +} diff --git a/src/main.rs b/src/main.rs index e98749c..63a7e9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,9 +3,8 @@ #![warn(clippy::missing_docs_in_private_items, missing_docs)] #![allow(clippy::result_unit_err)] -use std::error::Error; - use clap::Parser; +mod actions; mod dynamic_formatter; mod errors; mod snippet; @@ -50,64 +49,8 @@ pub struct Cli { delete: bool, } -/// List of all possible actions, some are mutually exclusive -#[derive(Debug)] -pub enum Actions { - /// Edit the content of the snippet only - Edit, - /// Edit the whole snippet - EditAll, - /// Locate the snippet and show the position - Locate, - /// Show the snippet (with replacement done) - Show, - /// Create the snippet with the given name and arguments - Create, - /// Deletes the snippet - Delete, -} - /// Main entry point, everything is defered for testing and coverage fn main() -> Result<(), errors::SnippetError> { let cli = Cli::parse(); - execute_parsed_cli(cli) -} - -/// The main worker, execute all commands given as Args -fn execute_parsed_cli(cli: Cli) -> Result<(), errors::SnippetError> { - let list_actions = populate_actions(&cli); - for action in list_actions.iter() { - if let Err(e) = match action { - Actions::Locate => wrapper_functions::locate_snippet(&cli), - Actions::Create => wrapper_functions::create_snippet(&cli), - Actions::Show => wrapper_functions::print_snippet(&cli), - Actions::Edit => wrapper_functions::edit_snippet(&cli), - Actions::EditAll => wrapper_functions::edit_whole_snippet(&cli), - Actions::Delete => wrapper_functions::delete_snippet(&cli), - } { - println!("An error occured during runtime:\n\t{}", e); - if let Some(e) = e.source() { - println!("This error was caused by: \n\t{}", e); - } - return Err(e); - }; - } - Ok(()) -} - -/// Populate a logical vec of succesive actions to perform depending on arguments -/// even when the arguments don't make sense -fn populate_actions(cli: &Cli) -> Vec { - let mut actions = Vec::new(); - match (cli.locate, cli.create, cli.edit, cli.edit_all, cli.delete) { - (true, _, _, _, false) => actions.push(Actions::Locate), - (_, true, _, true, false) => actions.push(Actions::EditAll), - (_, true, true, false, false) => actions.push(Actions::Edit), - (_, true, false, false, false) => actions.push(Actions::Create), - (_, false, true, _, false) => actions.push(Actions::Edit), - (_, false, false, true, false) => actions.push(Actions::EditAll), - (false, false, false, false, false) => actions.push(Actions::Show), - (_, _, _, _, true) => actions.push(Actions::Delete), - } - actions + actions::execute_parsed_cli(cli) } diff --git a/src/wrapper_functions.rs b/src/wrapper_functions.rs index 5b33454..f42480d 100644 --- a/src/wrapper_functions.rs +++ b/src/wrapper_functions.rs @@ -223,7 +223,7 @@ pub fn config_folder() -> Result { } #[cfg(test)] -mod test { +pub mod test { use super::*; use clap::Parser; use std::io::Write; @@ -245,19 +245,26 @@ mod test { #[test] fn test_config_creation() { // Try to locate where to set the config - let _ = match check_dir_and_open("test_config_creation").expect("Can't open config") { - FileStatus::Opened((_, f)) => f, - FileStatus::NotFound(p) => File::create(p).expect("Can't create path"), - }; + let path: PathBuf = snippet_path("test_config_creation").unwrap(); + create_dir_all(path.parent().unwrap()).unwrap(); + let _f = File::create(path).unwrap(); + assert!(matches!( + check_dir_and_open("test_config_creation").expect("Can't open config"), + FileStatus::Opened(_) + )); + } + + #[test] + fn test_delete_snippet() { + assert!(create_snippet(false_cli!("-c", "delete_snippet")).is_ok()); + assert!(delete_snippet(false_cli!("-d", "delete_snippet")).is_ok()); } #[test] fn test_locate() { // Try to locate an unexisting snippet let theorical_path = snippet_path("test_locate").expect("Can't find config path"); - if let Err(e) = std::fs::remove_file(&theorical_path) { - println!("[Info] File didn't exist {:?}", e); - } + _ = std::fs::remove_file(&theorical_path); // Locating the snippet should fail assert!(locate_snippet(false_cli!("-l", "test_locate")).is_err()); @@ -270,6 +277,20 @@ mod test { assert!(locate_snippet(false_cli!("-l", "test_locate")).is_ok()); } + #[test] + fn test_print_snippet() { + let theorical_path = snippet_path("test_print").expect("Can't find config path"); + _ = std::fs::remove_file(theorical_path); + + // Print non existing snippet + assert!(print_snippet(false_cli!("snp", "test_print")).is_err()); + + // Create snippet + assert!(create_snippet(false_cli!("-c", "test_print")).is_ok()); + assert!(print_snippet(false_cli!("snp", "test_print")).is_ok()); + // assert!(print_snippet(false_cli!("-l", "test_print")).is_ok()); + } + #[test] fn test_create() { _ = std::fs::remove_file(snippet_path("create_snippet").unwrap()); @@ -309,5 +330,17 @@ mod test { // Edit when created assert!(edit_whole_snippet(false_cli!("-E", "edit_whole")).is_ok()); + + // Create a badly formatted file + if let FileStatus::Opened((p, _)) = check_dir_and_open("edit_whole").unwrap() { + let mut f = File::create(p).unwrap(); + write!(f, "badly formatter yaml").unwrap(); + + assert!(edit_whole_snippet(false_cli!("-E", "edit_whole")).is_err()); + assert!(matches!( + check_dir_and_open("edit_whole.bad").unwrap(), + FileStatus::Opened(_) + )); + }; } } diff --git a/tarpaulin-report.html b/tarpaulin-report.html deleted file mode 100644 index 470d8e2..0000000 --- a/tarpaulin-report.html +++ /dev/null @@ -1,660 +0,0 @@ - - - - - - - -
- - - - - - \ No newline at end of file