Adds coverage and updates todo

Squashes several bug and inconsistencies, mostly on test tho
This commit is contained in:
Dandaedre 2023-05-23 00:03:01 +02:00
parent 56fdb06116
commit cca0390896
8 changed files with 224 additions and 747 deletions

3
.gitignore vendored
View File

@ -20,4 +20,5 @@ Cargo.lock
/target
/coverage
/llvm
/llvm
tarpaulin-report.html

View File

@ -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"
]

26
TODO.md
View File

@ -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 ?

View File

@ -1,6 +1,6 @@
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" width="96" height="20" role="img" aria-label="coverage: 79%">
<title>coverage: 79%</title>
xmlns:xlink="http://www.w3.org/1999/xlink" width="96" height="20" role="img" aria-label="coverage: 99%">
<title>coverage: 99%</title>
<linearGradient id="s" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
@ -10,14 +10,14 @@
</clipPath>
<g clip-path="url(#r)">
<rect width="61" height="20" fill="#555"/>
<rect x="61" width="35" height="20" fill="#dfb317"/>
<rect x="61" width="35" height="20" fill="#97ca00"/>
<rect width="96" height="20" fill="url(#s)"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110">
<text aria-hidden="true" x="315" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">coverage</text>
<text x="315" y="140" transform="scale(.1)" fill="#fff" textLength="510">coverage</text>
<text aria-hidden="true" x="775" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="250">79%</text>
<text x="775" y="140" transform="scale(.1)" fill="#fff" textLength="250">79%</text>
<text aria-hidden="true" x="775" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="250">99%</text>
<text x="775" y="140" transform="scale(.1)" fill="#fff" textLength="250">99%</text>
</g>
<script xmlns=""/>
</svg>

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

145
src/actions.rs Normal file
View File

@ -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<Actions> {
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());
}
}
}

View File

@ -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<Actions> {
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)
}

View File

@ -223,7 +223,7 @@ pub fn config_folder() -> Result<PathBuf, SnippetError> {
}
#[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(_)
));
};
}
}

File diff suppressed because one or more lines are too long