diff --git a/.gitignore b/.gitignore index 193d30e..1a35cee 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ Cargo.lock # Added by cargo /target +*.html diff --git a/Cargo.toml b/Cargo.toml index 94eabf9..03a5591 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,8 @@ [package] name = "rust_autobadges" version = "0.1.0" -edition = "2021" +edition = "2024" +resolver = "2" [dependencies] anyhow = "1.0.92" @@ -11,4 +12,5 @@ indicatif = "0.17.11" regex = "1.11.1" xshell = "0.2.6" -[lints.rust] +[workspace.package] +rust-version = "1.85" diff --git a/README.md b/README.md index 91d331f..b239e9d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,42 @@ # rust_autobadges ![code coverage](badges/coverage.svg) ![version](badges/version.svg) ![code coverage](badges/test.svg) -Automatically create a folder badges/ and add the coverage.svg, test.svg et version.svg \ No newline at end of file +Automatically create a folder badges/ with the generated coverage.svg, test.svg and version.svg + +# Usage + +``` +Usage: rust_autobadges [OPTIONS] + +Options: + -s, --skip-clean + Skip the cargo clean that tarpaulin would run, see tarpaulin --skip-clean for more documentation + -t, --no-test + Dont generate the test:failing/passing badge + -c, --no-coverage + Don't run the coverage and dont generate the coverage:(percent) badge + -v, --no-version + Don't run the version tag check and dont generage the version:(number) badge + --version-color + The color used to use on the version badge [default: orange] + --failing-color + The color used to indicate failure on the test badges [default: red] + --success-color + The color used to indicate success on the badges [default: green] + --high-coverage-color + The color used on a high coverage [default: green] + --mid-coverage-color + The color used on a mid coverage [default: orange] + --low-coverage-color + The color used on a low coverage [default: red] + --high-coverage-percent + The percent indicating a high coverage [default: 1] + --mid-coverage-percent + The percent indicating a mid coverage [default: 0.8] + -o, --output + The output directory where all the badges will be saved [default: badges] + -h, --help + Print help + -V, --version + Print version +``` diff --git a/src/main.rs b/src/main.rs index 91fa7f2..86cfbd4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ //! Basic badge generation using badge-maker and cargo-tarpaulin +// TODO: Testing and helper structs/methods +// TODO: Better error handling use console::style; use indicatif::ProgressBar; use std::time::{Duration, Instant}; @@ -13,15 +15,29 @@ use xshell::Shell; #[command(version, about, long_about = None)] struct CLIArgs { #[arg(short, long)] + /// Skip the cargo clean that tarpaulin would run, see tarpaulin --skip-clean documentation skip_clean: bool, - #[arg(short, long, default_value_t = String::from("orange"))] + // #[arg(short, long)] + // /// Don't delete the old badges, they may be overwritten + // keep_badges: bool, + #[arg(short = 't', long)] + /// Dont generate the test:failing/passing badge + no_test: bool, + #[arg(short = 'c', long)] + /// Don't run the coverage and dont generate the coverage:(percent) badge + no_coverage: bool, + #[arg(short = 'v', long)] + /// Don't run the version tag check and dont generage the version:(number) badge + no_version: bool, + + #[arg(long, default_value_t = String::from("orange"))] /// The color used to use on the version badge version_color: String, - #[arg(short, long, default_value_t = String::from("red"))] + #[arg(long, default_value_t = String::from("red"))] /// The color used to indicate failure on the test badges failing_color: String, - #[arg(short='S', long, default_value_t = String::from("green"))] + #[arg(long, default_value_t = String::from("green"))] /// The color used to indicate success on the badges success_color: String, @@ -50,48 +66,79 @@ struct CLIArgs { // TODO: No anyhow result, first print a conprehensive error, then return an error // : To avoid code reuse, a custom pb with a behaviour on error would be nice // TODO: Better than manual alignment -#[cfg(not(tarpaulin_include))] +// TODO: The cargo test usage is conditionnal of tarpaulin selection, +// if tarpaulin & test => only run tarp & generate both +// if tarpaulin & no test => only run tarp & generate tarp only +// if no tarp & test => only run test & generate test only +// if no tarp & no test => no generation +// #[cfg(not(tarpaulin_include))] fn main() -> anyhow::Result<()> { let args = CLIArgs::parse(); + let new_progress_bar = || { + ProgressBar::new_spinner() + // .with_style( + // ProgressStyle::default_spinner() + // .tick_strings(&["⢎⡰", "⢎⡡", "⢎⡑", "⢎⠱", "⠎⡱", "⢊⡱", "⢌⡱", "⢆⡱", " "]), + // ) + }; let sh = Shell::new()?; - sh.create_dir("badges")?; - // run_test(&sh)?; + // TODO: Cleanup badges beforehand + if !args.skip_clean { + // sh.delete_dir(args.output)?; + } + sh.create_dir(&args.output)?; - run_version(&sh, ProgressBar::new_spinner(), &args)?; - run_coverage(&sh, ProgressBar::new_spinner(), &args)?; + if !args.no_version { + run_version(&sh, new_progress_bar(), &args)?; + } + // TODO: Modular testing + match (args.no_coverage, args.no_test) { + (true, true) => (), + (true, false) => { + // run_tests_only(&sh, pb_model.clone(), &args)?; + } + (false, true) => run_coverage(&sh, new_progress_bar(), &args)?, // The coverage know wether to write the badge test with the cli args + (false, false) => run_coverage(&sh, new_progress_bar(), &args)?, + } Ok(()) } -fn run_coverage(sh: &Shell, pb: ProgressBar, args: &CLIArgs) -> Result<(), anyhow::Error> { - let _ = sh; +/// Run the coverage functions +// #[coverage(off)] // No real coverage can be done as it would recurse indefinitely +fn run_coverage(_sh: &Shell, pb: ProgressBar, args: &CLIArgs) -> Result<(), anyhow::Error> { let started = Instant::now(); - // 84.78% coverage, 78/92 lines covered, -15.22% change in coverage + // FORMAT: 84.78% coverage, 78/92 lines covered, -15.22% change in coverage let pb = pb.with_message(format!( "{:>10} Mesuring coverage...", style("[Coverage]").green().bold() )); pb.enable_steady_tick(Duration::from_millis(100)); + // TODO: Update the status with basic parsing from the output (compiling (), linking (), running tests (), etc..) // cmd!(sh, "cargo tarpaulin").quiet().ignore_stdout().run()?; - let output = Command::new("cargo") - .args(vec!["tarpaulin", "-o", "html", "--skip-clean"]) - .output()?; + let mut args_tarpaulin = vec!["tarpaulin", "-o", "html"]; + if args.skip_clean { + args_tarpaulin.push("--skip-clean"); + } + + let output = Command::new("cargo").args(args_tarpaulin).output()?; + if !args.no_test { + // Only write the test badge if it was asked for + // TODO: isolate in it's own command or use the wrapper struct to avoid multiple write badge + match &output.status.success() { + true => write_badge("test", "passing", &args.success_color, "test", args)?, + false => write_badge("test", "failing", &args.failing_color, "test", args)?, + }; + } - match &output.status.success() { - true => write_badge("test", "passing", &args.success_color, "test", args)?, - false => write_badge("test", "failing", &args.failing_color, "test", args)?, - }; let test = String::from_utf8(output.stdout)?; - - // println!("{}", test); let re = Regex::new(r"(\d+.\d+)% coverage, ").unwrap(); let coverage = re.captures(&test); if let Some(c) = coverage { - let v = c.get(1).unwrap().as_str(); - // println!("{}", v); - let coverage_value: f64 = v.parse()?; + let coverage_value = c.get(1).unwrap().as_str(); + let coverage_value: f64 = coverage_value.parse()?; let color = if coverage_value >= args.high_coverage_percent { &args.high_coverage_color } else if coverage_value > args.mid_coverage_percent { @@ -123,6 +170,7 @@ fn run_coverage(sh: &Shell, pb: ProgressBar, args: &CLIArgs) -> Result<(), anyho Ok(()) } +/// Extract version and write it as a badge fn run_version(sh: &Shell, pb: ProgressBar, args: &CLIArgs) -> Result<(), anyhow::Error> { let started = Instant::now(); let pb = pb.with_message(format!( @@ -178,3 +226,22 @@ fn write_badge( f.write_all(&output.stdout)?; Ok(()) } + +#[cfg(test)] +mod test { + #[ignore] + #[test] + fn test_badge_writing() {} + + #[ignore] + #[test] + fn test_structs() {} + + #[ignore] + #[test] + fn test_version_parsing() {} + + #[ignore] + #[test] + fn test_coverage_parsing() {} +}