diff --git a/Changes.md b/Changes.md index 75a360d..4f4d12a 100644 --- a/Changes.md +++ b/Changes.md @@ -19,6 +19,9 @@ will continue to work. I do not intend to ever deprecate the underscore version. They simply will not be used in the docs and examples. +- Fixed cases where `precious` would exit with an exit code of `1` on errors that were _not_ linting + failures. Going forward, an exit code of `1` should only be used for linting failures. + - `precious` will now emit a warning if your config file uses any of the deprecated config keys, `run_mode` and `chdir`. Support for these options will be removed entirely in a future release. diff --git a/README.md b/README.md index 4008f51..4d00109 100644 --- a/README.md +++ b/README.md @@ -451,7 +451,7 @@ When running in `--lint` mode, precious will exit with `0` when all files pass l commands fail it will exit with `1`. In both modes, if any commands fail, either by returning exit codes that aren't listed as ok or by -printing to stderr unexpectedly, then precious will exit with a non-`0` exit code. +printing to stderr unexpectedly, then the exit code will not be `0` or `1`. ## Common Scenarios diff --git a/precious-core/src/command.rs b/precious-core/src/command.rs index 41d71f2..76fd3b1 100644 --- a/precious-core/src/command.rs +++ b/precious-core/src/command.rs @@ -144,7 +144,7 @@ impl fmt::Display for PathArgs { #[derive(Debug, Error, PartialEq, Eq)] enum CommandError { #[error( - "You cannot create a Command which lints and tidies without lint-flags and/or tidy-flags" + "You cannot define a command which lints and tidies without lint-flags and/or tidy-flags" )] CommandWhichIsBothRequiresLintOrTidyFlags, diff --git a/precious-core/src/precious.rs b/precious-core/src/precious.rs index be6af87..24ca7af 100644 --- a/precious-core/src/precious.rs +++ b/precious-core/src/precious.rs @@ -607,7 +607,7 @@ impl LintOrTidyRunner { } Err(e) => { error!("Failed to run precious: {}", e); - 1 + 42 } } } diff --git a/precious-integration/src/lint_tidy.rs b/precious-integration/src/lint_tidy.rs index dae6dda..85d0668 100644 --- a/precious-integration/src/lint_tidy.rs +++ b/precious-integration/src/lint_tidy.rs @@ -300,6 +300,91 @@ fn foo() -> u8 { Ok(()) } +#[test] +#[serial] +fn exit_codes() -> Result<()> { + let helper = set_up_for_tests()?; + + let all_codes = Vec::from_iter(0..=255); + let match_all_re = Regex::new(".*")?; + + let precious = precious_path()?; + let env = HashMap::new(); + let out = exec::run( + &precious, + &["lint", "--all"], + &env, + &all_codes, + Some(&[match_all_re.clone()]), + Some(&helper.precious_root()), + )?; + assert_eq!(out.exit_code, 0); + + helper.write_file("src/good.rs", "this is not valid rust")?; + + let out = exec::run( + &precious, + &["lint", "--all"], + &env, + &all_codes, + Some(&[match_all_re.clone()]), + Some(&helper.precious_root()), + )?; + assert_eq!(out.exit_code, 1); + + let out = exec::run( + &precious, + &["foo", "--all"], + &env, + &all_codes, + Some(&[match_all_re.clone()]), + Some(&helper.precious_root()), + )?; + assert_eq!(out.exit_code, 2); + + let out = exec::run( + &precious, + &["lint", "--foo"], + &env, + &all_codes, + Some(&[match_all_re.clone()]), + Some(&helper.precious_root()), + )?; + assert_eq!(out.exit_code, 2); + + helper.write_file("precious.toml", "this is not valid config")?; + let out = exec::run( + &precious, + &["lint", "--all"], + &env, + &all_codes, + Some(&[match_all_re.clone()]), + Some(&helper.precious_root()), + )?; + assert_eq!(out.exit_code, 42); + + let config_missing_key = r#" +[commands.rustfmt] +type = "both" +include = "**/*.rs" +cmd = [ "rustfmt", "--edition", "2021" ] +ok-exit-codes = 0 +lint-failure-exit-codes = 1 +"#; + helper.write_file("precious.toml", config_missing_key)?; + let out = exec::run( + &precious, + &["lint", "--all"], + &env, + &all_codes, + Some(&[match_all_re.clone()]), + Some(&helper.precious_root()), + )?; + assert_eq!(out.exit_code, 42); + + Ok(()) +} + #[test] #[serial] fn all_invocation_options() -> Result<()> { diff --git a/precious/src/main.rs b/precious/src/main.rs index f4f531f..c8027af 100644 --- a/precious/src/main.rs +++ b/precious/src/main.rs @@ -7,13 +7,13 @@ fn main() { let app = precious::app(); if let Err(e) = app.init_logger() { eprintln!("Error creating logger: {e}"); - std::process::exit(1); + std::process::exit(42); } let status = match app.run() { Ok(s) => s, Err(e) => { error!("{e}"); - 1 + 42 } }; std::process::exit(status as i32);