Skip to content

Commit

Permalink
refactor(oxlint): move ConfigFileInit output outside CliRunResult, ex…
Browse files Browse the repository at this point in the history
…it code 1 when it fails
  • Loading branch information
Sysix committed Jan 29, 2025
1 parent f952b61 commit 36acc12
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
46 changes: 24 additions & 22 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,23 @@ impl Runner for LintRunner {
} else {
config_file
};
match fs::write(Self::DEFAULT_OXLINTRC, configuration) {
Ok(()) => {
return CliRunResult::ConfigFileInitResult {
message: "Configuration file created".to_string(),
}
}
Err(_) => {
return CliRunResult::ConfigFileInitResult {
message: "Failed to create configuration file".to_string(),
}
}

if fs::write(Self::DEFAULT_OXLINTRC, configuration).is_ok() {
stdout
.write_all("Configuration file created".as_bytes())
.or_else(Self::check_for_writer_error)
.unwrap();
stdout.flush().unwrap();
return CliRunResult::ConfigFileInitSucceeded;
}

// failed case
stdout
.write_all("Failed to create configuration file".as_bytes())
.or_else(Self::check_for_writer_error)
.unwrap();
stdout.flush().unwrap();
return CliRunResult::ConfigFileInitFailed;
}
}

Expand Down Expand Up @@ -224,6 +229,7 @@ impl Runner for LintRunner {
start_time: now.elapsed(),
}) {
stdout.write_all(end.as_bytes()).or_else(Self::check_for_writer_error).unwrap();
stdout.flush().unwrap();
};

CliRunResult::LintResult(ExitCode::from(u8::from(diagnostic_failed)))
Expand Down Expand Up @@ -328,10 +334,7 @@ mod test {
use std::fs;

use super::LintRunner;
use crate::{
cli::{lint_command, CliRunResult, Runner},
tester::Tester,
};
use crate::tester::Tester;

// lints the full directory of fixtures,
// so do not snapshot it, test only
Expand Down Expand Up @@ -644,14 +647,13 @@ mod test {

#[test]
fn test_init_config() {
assert!(!fs::exists(LintRunner::DEFAULT_OXLINTRC).unwrap());

let args = &["--init"];
let options = lint_command().run_inner(args).unwrap();
let mut output = Vec::new();
let ret = LintRunner::new(options).run(&mut output);
let CliRunResult::ConfigFileInitResult { message } = ret else {
panic!("Expected configuration file to be created, got {ret:?}")
};
assert_eq!(message, "Configuration file created");
Tester::new().test(args);

assert!(fs::exists(LintRunner::DEFAULT_OXLINTRC).unwrap());

fs::remove_file(LintRunner::DEFAULT_OXLINTRC).unwrap();
}

Expand Down
16 changes: 7 additions & 9 deletions apps/oxlint/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@ pub enum CliRunResult {
/// The exit unix code for, in general 0 or 1 (from `--deny-warnings` or `--max-warnings` for example)
LintResult(ExitCode),
PrintConfigResult,
ConfigFileInitResult {
message: String,
},
ConfigFileInitFailed,
ConfigFileInitSucceeded,
}

impl Termination for CliRunResult {
#[allow(clippy::print_stdout, clippy::print_stderr)]
fn report(self) -> ExitCode {
match self {
Self::None | Self::PrintConfigResult => ExitCode::from(0),
Self::None | Self::PrintConfigResult | Self::ConfigFileInitSucceeded => {
ExitCode::SUCCESS
}
Self::ConfigFileInitFailed => ExitCode::FAILURE,
Self::InvalidOptions { message } => {
println!("Invalid Options: {message}");
ExitCode::from(1)
ExitCode::FAILURE
}
Self::LintResult(exit_code) => exit_code,
Self::ConfigFileInitResult { message } => {
println!("{message}");
ExitCode::from(0)
}
}
}
}

0 comments on commit 36acc12

Please sign in to comment.