Skip to content

Commit 2378fef

Browse files
committedJan 30, 2025·
refactor(oxlint): move ConfigFileInit output outside CliRunResult, exit code 1 when it fails (#8776)
1 parent f4cecb5 commit 2378fef

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed
 

‎apps/oxlint/src/lint.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,23 @@ impl Runner for LintRunner {
151151
} else {
152152
config_file
153153
};
154-
match fs::write(Self::DEFAULT_OXLINTRC, configuration) {
155-
Ok(()) => {
156-
return CliRunResult::ConfigFileInitResult {
157-
message: "Configuration file created".to_string(),
158-
}
159-
}
160-
Err(_) => {
161-
return CliRunResult::ConfigFileInitResult {
162-
message: "Failed to create configuration file".to_string(),
163-
}
164-
}
154+
155+
if fs::write(Self::DEFAULT_OXLINTRC, configuration).is_ok() {
156+
stdout
157+
.write_all("Configuration file created\n".as_bytes())
158+
.or_else(Self::check_for_writer_error)
159+
.unwrap();
160+
stdout.flush().unwrap();
161+
return CliRunResult::ConfigFileInitSucceeded;
165162
}
163+
164+
// failed case
165+
stdout
166+
.write_all("Failed to create configuration file\n".as_bytes())
167+
.or_else(Self::check_for_writer_error)
168+
.unwrap();
169+
stdout.flush().unwrap();
170+
return CliRunResult::ConfigFileInitFailed;
166171
}
167172
}
168173

@@ -224,6 +229,7 @@ impl Runner for LintRunner {
224229
start_time: now.elapsed(),
225230
}) {
226231
stdout.write_all(end.as_bytes()).or_else(Self::check_for_writer_error).unwrap();
232+
stdout.flush().unwrap();
227233
};
228234

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

330336
use super::LintRunner;
331-
use crate::{
332-
cli::{lint_command, CliRunResult, Runner},
333-
tester::Tester,
334-
};
337+
use crate::tester::Tester;
335338

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

645648
#[test]
646649
fn test_init_config() {
650+
assert!(!fs::exists(LintRunner::DEFAULT_OXLINTRC).unwrap());
651+
647652
let args = &["--init"];
648-
let options = lint_command().run_inner(args).unwrap();
649-
let mut output = Vec::new();
650-
let ret = LintRunner::new(options).run(&mut output);
651-
let CliRunResult::ConfigFileInitResult { message } = ret else {
652-
panic!("Expected configuration file to be created, got {ret:?}")
653-
};
654-
assert_eq!(message, "Configuration file created");
653+
Tester::new().test(args);
654+
655+
assert!(fs::exists(LintRunner::DEFAULT_OXLINTRC).unwrap());
656+
655657
fs::remove_file(LintRunner::DEFAULT_OXLINTRC).unwrap();
656658
}
657659

‎apps/oxlint/src/result.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,23 @@ pub enum CliRunResult {
99
/// The exit unix code for, in general 0 or 1 (from `--deny-warnings` or `--max-warnings` for example)
1010
LintResult(ExitCode),
1111
PrintConfigResult,
12-
ConfigFileInitResult {
13-
message: String,
14-
},
12+
ConfigFileInitFailed,
13+
ConfigFileInitSucceeded,
1514
}
1615

1716
impl Termination for CliRunResult {
1817
#[allow(clippy::print_stdout, clippy::print_stderr)]
1918
fn report(self) -> ExitCode {
2019
match self {
21-
Self::None | Self::PrintConfigResult => ExitCode::from(0),
20+
Self::None | Self::PrintConfigResult | Self::ConfigFileInitSucceeded => {
21+
ExitCode::SUCCESS
22+
}
23+
Self::ConfigFileInitFailed => ExitCode::FAILURE,
2224
Self::InvalidOptions { message } => {
2325
println!("Invalid Options: {message}");
24-
ExitCode::from(1)
26+
ExitCode::FAILURE
2527
}
2628
Self::LintResult(exit_code) => exit_code,
27-
Self::ConfigFileInitResult { message } => {
28-
println!("{message}");
29-
ExitCode::from(0)
30-
}
3129
}
3230
}
3331
}

0 commit comments

Comments
 (0)
Please sign in to comment.