From 9efba4311274f5a7e93fdd72ee4c281e72f4b35d Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 23 May 2023 21:08:40 +0200 Subject: [PATCH 1/2] Makes -l as an alternative to . Also changes to optionnal and mandatory --- Cargo.toml | 2 +- coverage/html/badges/flat.svg | 8 ++-- src/actions.rs | 12 +++--- src/main.rs | 39 ++++++++++++----- src/wrapper_functions.rs | 81 +++++++++++++++++++++-------------- 5 files changed, 88 insertions(+), 54 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5b5d1e5..415fde2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "snp" authors = ["Dandaedre"] -version = "1.0.1" +version = "1.0.2" edition = "2021" [features] diff --git a/coverage/html/badges/flat.svg b/coverage/html/badges/flat.svg index a43354b..849843f 100644 --- a/coverage/html/badges/flat.svg +++ b/coverage/html/badges/flat.svg @@ -1,6 +1,6 @@ - coverage: 99% + xmlns:xlink="http://www.w3.org/1999/xlink" width="96" height="20" role="img" aria-label="coverage: 95%"> + coverage: 95% @@ -16,8 +16,8 @@ coverage - - 99% + + 95% \ No newline at end of file diff --git a/src/actions.rs b/src/actions.rs index 6295d5c..9ee4d72 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -94,6 +94,7 @@ mod test { }; } + // #[ignore] #[test] fn test_populate_actions() { // false_cli!("snp", "name", "-p"), Actions::Locate @@ -136,11 +137,8 @@ mod test { false_cli!("snp", "name", "-c", "-r", "new_name", "-f"), vec![Actions::Create] ), - (false_cli!("snp", "name", "-l"), vec![Actions::List]), - ( - false_cli!("snp", "name", "-l", "-c", "-e", "-E"), - vec![Actions::List] - ), + (false_cli!("snp", "-l"), vec![Actions::List]), + // (false_cli!("snp", "-l"), vec![Actions::List]), ); for (cli, res) in group.iter() { println!("Testing {:?}..actions expected {:?}", cli, res); @@ -149,8 +147,10 @@ mod test { assert!(res.iter().zip(fnd).all(|(a, b)| *a == b)); } } + #[test] fn test_execute_parsed_cli() { + // println!("Executing test for cli"); let list_cli = vec![ false_cli!("snp", "parsed_cli", "-e", "-c"), false_cli!("snp", "parsed_cli", "-c", "-f"), @@ -160,7 +160,7 @@ mod test { false_cli!("snp", "parsed_cli", "-c"), false_cli!("snp", "parsed_cli", "-r", "new_name", "-f"), false_cli!("snp", "new_name", "-d"), - false_cli!("snp", "new_name", "-l"), + false_cli!("snp", "-l"), ]; let bad_cli = vec![ diff --git a/src/main.rs b/src/main.rs index 945586e..af8c9cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,43 +20,49 @@ mod wrapper_functions; #[command(author, version, about, verbatim_doc_comment)] pub struct Cli { /// The Snippet name (and therefore his path) - #[arg()] - snippet: String, + #[arg(group = "mandatory", group = "mutex")] + snippet: Option, /// Try to create a Snippet with the given name and default arguments - #[arg(short, long, default_value_t = false)] + #[arg(short, long, default_value_t = false, requires = "mandatory")] create: bool, /// Find the path of the snippet with the given name - #[arg(short, long, default_value_t = false)] + #[arg(short, long, default_value_t = false, requires = "mandatory")] path: bool, /// Launch the editor and edit the Snippet content - #[arg(short, long, default_value_t = false)] + #[arg(short, long, default_value_t = false, requires = "mandatory")] edit: bool, /// Launch the editor and edit the whole snippet - #[arg(short = 'E', long = "Edit", default_value_t = false)] + #[arg( + short = 'E', + long = "Edit", + default_value_t = false, + requires = "mandatory" + )] edit_all: bool, /// The list of arguments to give to the snippet /// if the -c flag is used, it's used to create the default arguments + #[arg(requires = "mandatory")] args: Vec, /// Force the creation/moving/renaming of a Snippet even if it mean overwriting an existing one - #[arg(short, long, default_value_t = false)] + #[arg(short, long, default_value_t = false, requires = "mandatory")] force: bool, /// Delete a snippet, takes precedence over everything - #[arg(short, long, default_value_t = false)] + #[arg(short, long, default_value_t = false, requires = "mandatory")] delete: bool, /// Try to rename the Snippet (Use -f if the destination name already exist) - #[arg(short, long)] + #[arg(short, long, requires = "mandatory")] rename: Option, /// List all snippets in the snippet folder - #[arg(short, long, default_value_t = false)] + #[arg(short, long, default_value_t = false, group = "mutex")] list: bool, } @@ -65,3 +71,16 @@ fn main() -> Result<(), errors::SnippetError> { let cli = Cli::parse(); actions::execute_parsed_cli(cli) } + +#[cfg(test)] +mod test { + use clap::CommandFactory; + + use super::*; + + #[test] + fn test_cli_main() { + let cmd = Cli::command_for_update(); + cmd.debug_assert(); + } +} diff --git a/src/wrapper_functions.rs b/src/wrapper_functions.rs index 90bea6c..f82254e 100644 --- a/src/wrapper_functions.rs +++ b/src/wrapper_functions.rs @@ -18,7 +18,7 @@ use crate::{dynamic_formatter, Cli}; /// /// Will error on snippet not existing pub fn locate_snippet(cli: &Cli) -> Result<(), SnippetError> { - let status = check_dir_and_open(&cli.snippet)?; + let status = check_dir_and_open(cli.snippet.as_ref().unwrap())?; match status { FileStatus::Opened((p, _)) => { println!("{:?}", p.as_os_str()); @@ -31,8 +31,12 @@ pub fn locate_snippet(cli: &Cli) -> Result<(), SnippetError> { /// Create a new snippet with the given cli args pub fn create_snippet(cli: &Cli) -> Result<(), SnippetError> { // Create a snippet with the given data - let snp = Snippet::new(cli.snippet.clone(), cli.args.clone(), String::from("")); - let status = check_dir_and_open(&cli.snippet)?; + let snp = Snippet::new( + cli.snippet.as_ref().unwrap().clone(), + cli.args.clone(), + String::from(""), + ); + let status = check_dir_and_open(cli.snippet.as_ref().unwrap())?; let (f, p) = match status { FileStatus::Opened((p, _)) => { if cli.force { @@ -54,10 +58,10 @@ pub fn create_snippet(cli: &Cli) -> Result<(), SnippetError> { pub fn rename_snippet(cli: &Cli) -> Result<(), SnippetError> { // Check wether the snippet exitsts let new_name = cli.rename.as_ref().unwrap(); - if new_name == &cli.snippet { + if new_name == cli.snippet.as_ref().unwrap() { return Ok(()); // The noop is a success ? } - let (f, oldpath) = match check_dir_and_open(&cli.snippet)? { + let (f, oldpath) = match check_dir_and_open(cli.snippet.as_ref().unwrap())? { FileStatus::Opened((p, f)) => (f, p), FileStatus::NotFound(e) => return Err(SnippetError::NotFound(e)), }; @@ -83,7 +87,8 @@ pub fn rename_snippet(cli: &Cli) -> Result<(), SnippetError> { // Save snippet println!( "The snippet {} was sucessfuly renamed as {}", - cli.snippet, new_name + cli.snippet.as_ref().unwrap(), + new_name ); Ok(()) } @@ -91,13 +96,17 @@ pub fn rename_snippet(cli: &Cli) -> Result<(), SnippetError> { /// Open and load snippet and edit it pub fn edit_snippet(cli: &Cli) -> Result<(), SnippetError> { // Try loading, if not existant check if create or error - let status = check_dir_and_open(&cli.snippet)?; + let status = check_dir_and_open(cli.snippet.as_ref().unwrap())?; let (mut snp, _, p) = match status { FileStatus::Opened((p, f)) => (Snippet::from_file(&f)?, f, p), FileStatus::NotFound(p) => { if cli.create { ( - Snippet::new(cli.snippet.clone(), cli.args.clone(), String::from("")), + Snippet::new( + cli.snippet.as_ref().unwrap().clone(), + cli.args.clone(), + String::from(""), + ), File::create(&p)?, p, ) @@ -124,13 +133,16 @@ pub fn edit_snippet(cli: &Cli) -> Result<(), SnippetError> { /// a creation or an override pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> { // Try loading the snippet - let status = check_dir_and_open(&cli.snippet); + let status = check_dir_and_open(cli.snippet.as_ref().unwrap()); let (buf, mut origin_p) = match status? { FileStatus::Opened((p, f)) => (read_to_string(f)?, p), FileStatus::NotFound(p) => { if cli.create { File::create(&p)?; - (Snippet::empty(cli.snippet.clone(), cli.args.clone()), p) + ( + Snippet::empty(cli.snippet.clone().unwrap(), cli.args.clone()), + p, + ) } else { return Err(SnippetError::NotFoundNoRetryError(p)); } @@ -143,7 +155,7 @@ pub fn edit_whole_snippet(cli: &Cli) -> Result<(), SnippetError> { // Try reparsing, on error no modifications will be made match Snippet::from_string(&buf) { Ok(snp) => { - if cli.snippet == snp.name { + if cli.snippet.as_ref().unwrap() == &snp.name { // No renaming has occured, the file can safely be overriten snp.write_over(&origin_p)?; println!( @@ -213,7 +225,7 @@ pub fn list_snippets(_cli: &Cli) -> Result<(), SnippetError> { /// Print the snippet to stdout pub fn print_snippet(cli: &Cli) -> Result<(), SnippetError> { // Try to load the snippet - let (_, f) = match check_dir_and_open(&cli.snippet)? { + let (_, f) = match check_dir_and_open(cli.snippet.as_ref().unwrap())? { FileStatus::Opened((p, f)) => (p, f), FileStatus::NotFound(p) => return Err(SnippetError::NotFound(p)), }; @@ -233,7 +245,7 @@ pub fn print_snippet(cli: &Cli) -> Result<(), SnippetError> { /// Delete a given snippet, errors if the snippet doesn't exists pub fn delete_snippet(cli: &Cli) -> Result<(), SnippetError> { let mut path = config_folder()?; - path.push(format!("{}.yaml", cli.snippet)); + path.push(format!("{}.yaml", cli.snippet.as_ref().unwrap())); std::fs::remove_file(path)?; Ok(()) } @@ -308,6 +320,7 @@ pub mod test { assert!(delete_snippet(false_cli!("snp", "-d", "delete_snippet")).is_ok()); } + // #[ignore] #[test] fn test_locate() { // Try to locate an unexisting snippet @@ -315,14 +328,14 @@ pub mod test { _ = std::fs::remove_file(&theorical_path); // Locating the snippet should fail - assert!(locate_snippet(false_cli!("-l", "test_locate")).is_err()); + assert!(locate_snippet(false_cli!("snp", "-f", "test_locate")).is_err()); let mut f = File::create(&theorical_path).expect("Couldn't create file"); write!(f, "N").expect("Couldn't write on the new file"); f.sync_data().expect("Can't sync file"); // Locating the snippet should now suceed - assert!(locate_snippet(false_cli!("-l", "test_locate")).is_ok()); + assert!(locate_snippet(false_cli!("-f", "test_locate")).is_ok()); } #[test] @@ -334,57 +347,59 @@ pub mod test { assert!(print_snippet(false_cli!("snp", "test_print")).is_err()); // Create snippet - assert!(create_snippet(false_cli!("-c", "test_print")).is_ok()); + assert!(create_snippet(false_cli!("snp", "-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()); // Test to create empty - assert!(create_snippet(false_cli!("-c", "create_snippet")).is_ok()); + assert!(create_snippet(false_cli!("snp", "-c", "create_snippet")).is_ok()); // Test to create again - assert!(create_snippet(false_cli!("-c", "create_snippet")).is_err()); + assert!(create_snippet(false_cli!("snp", "-c", "create_snippet")).is_err()); // Test to create with force - assert!(create_snippet(false_cli!("-c", "-f", "create_snippet")).is_ok()); + assert!(create_snippet(false_cli!("snp", "-c", "-f", "create_snippet")).is_ok()); } #[test] fn test_edit() { _ = std::fs::remove_file(snippet_path("edit_content").unwrap()); // Open unnexitant - assert!(edit_snippet(false_cli!("-e", "edit_content")).is_err()); + assert!(edit_snippet(false_cli!("snp", "-e", "edit_content")).is_err()); // Force creation - assert!(edit_snippet(false_cli!("-e", "-c", "edit_content")).is_ok()); + assert!(edit_snippet(false_cli!("snp", "-e", "-c", "edit_content")).is_ok()); // Edit when created - println!("{:?}", edit_snippet(false_cli!("-e", "edit_content"))); - assert!(edit_snippet(false_cli!("-e", "edit_content")).is_ok()); + println!( + "{:?}", + edit_snippet(false_cli!("snp", "-e", "edit_content")) + ); + assert!(edit_snippet(false_cli!("snp", "-e", "edit_content")).is_ok()); } #[test] fn test_edit_whole() { _ = std::fs::remove_file(snippet_path("edit_whole").unwrap()); // Open unnexitant - assert!(edit_whole_snippet(false_cli!("-E", "edit_whole")).is_err()); + assert!(edit_whole_snippet(false_cli!("snp", "-E", "edit_whole")).is_err()); // Force creation - assert!(edit_whole_snippet(false_cli!("-E", "-c", "edit_whole")).is_ok()); + assert!(edit_whole_snippet(false_cli!("snp", "-E", "-c", "edit_whole")).is_ok()); // Edit when created - assert!(edit_whole_snippet(false_cli!("-E", "edit_whole")).is_ok()); + assert!(edit_whole_snippet(false_cli!("snp", "-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!(edit_whole_snippet(false_cli!("snp", "-E", "edit_whole")).is_err()); assert!(matches!( check_dir_and_open("edit_whole.bad").unwrap(), FileStatus::Opened(_) @@ -400,7 +415,7 @@ pub mod test { _ = std::fs::remove_file(&oldpath); _ = std::fs::remove_file(&newpath); _ = std::fs::remove_file(filledpath); - assert!(create_snippet(false_cli!("-c", "rename_snippet", "-f")).is_ok()); + assert!(create_snippet(false_cli!("snp", "-c", "rename_snippet", "-f")).is_ok()); assert!( rename_snippet(false_cli!("snp", "-r", "renamed_snippet", "rename_snippet")).is_ok() ); @@ -410,8 +425,8 @@ pub mod test { assert!(rename_snippet(false_cli!("snp", "-r", "non_ex", "non_ex")).is_ok()); assert!(rename_snippet(false_cli!("snp", "-r", "non_ex", "renamed_snippet")).is_err()); - assert!(create_snippet(false_cli!("-c", "filled_destination", "-f")).is_ok()); - assert!(create_snippet(false_cli!("-c", "rename_snippet", "-f")).is_ok()); + assert!(create_snippet(false_cli!("snp", "-c", "filled_destination", "-f")).is_ok()); + assert!(create_snippet(false_cli!("snp", "-c", "rename_snippet", "-f")).is_ok()); assert!(matches!( rename_snippet(false_cli!( "snp", @@ -449,8 +464,8 @@ pub mod test { println!("Trying to resave without destination {:?}", res); assert!(res.is_ok()); - assert!(create_snippet(false_cli!("-c", "new_snippet")).is_err()); - assert!(create_snippet(false_cli!("-c", "resave_snippet")).is_err()); + assert!(create_snippet(false_cli!("snp", "-c", "new_snippet")).is_err()); + assert!(create_snippet(false_cli!("snp", "-c", "resave_snippet")).is_err()); let res = edit_whole_snippet(false_cli!("snp", "resave_snippet", "-f")); println!("Sucessfully erasing {:?}", res); -- 2.45.1 From 5cd297b1da44a1aaca0dedfa7f41b3e16879650e Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 23 May 2023 21:11:55 +0200 Subject: [PATCH 2/2] Updates coverage and version --- Cargo.toml | 2 +- coverage/html/badges/flat.svg | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 415fde2..0819a21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "snp" authors = ["Dandaedre"] -version = "1.0.2" +version = "1.0.4" edition = "2021" [features] diff --git a/coverage/html/badges/flat.svg b/coverage/html/badges/flat.svg index 849843f..a43354b 100644 --- a/coverage/html/badges/flat.svg +++ b/coverage/html/badges/flat.svg @@ -1,6 +1,6 @@ - coverage: 95% + xmlns:xlink="http://www.w3.org/1999/xlink" width="96" height="20" role="img" aria-label="coverage: 99%"> + coverage: 99% @@ -16,8 +16,8 @@ coverage - - 95% + + 99% \ No newline at end of file -- 2.45.1