Skip to content

Commit

Permalink
Only exit with code = 1 when linting fails
Browse files Browse the repository at this point in the history
  • Loading branch information
autarch committed Mar 30, 2024
1 parent aa60b55 commit f665c32
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion precious-core/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
2 changes: 1 addition & 1 deletion precious-core/src/precious.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ impl LintOrTidyRunner {
}
Err(e) => {
error!("Failed to run precious: {}", e);
1
42
}
}
}
Expand Down
85 changes: 85 additions & 0 deletions precious-integration/src/lint_tidy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down
4 changes: 2 additions & 2 deletions precious/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f665c32

Please sign in to comment.