-
Notifications
You must be signed in to change notification settings - Fork 935
Implemented rough draft of check
write mode.
#2539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
2b258f7
533df58
b0ece6b
bda78f8
d5944ee
7412ff6
0fb92c6
4860852
a3006b9
257715a
8755d1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -198,7 +198,7 @@ fn make_opts() -> Options { | |
"", | ||
"write-mode", | ||
"How to write output (not usable when piping from stdin)", | ||
"[replace|overwrite|display|plain|diff|coverage|checkstyle]", | ||
"[replace|overwrite|display|plain|diff|check|coverage|checkstyle]", | ||
); | ||
|
||
opts | ||
|
@@ -287,6 +287,10 @@ fn execute(opts: &Options) -> FmtResult<Summary> { | |
} | ||
|
||
let mut error_summary = Summary::default(); | ||
if let Some(mode) = options.write_mode { | ||
error_summary.add_write_mode(mode) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought I suggested a refactoring to avoid saving the write mode in the error summary, but maybe that was on another PR since I can't find any trace of it. I think that by moving some code around in the main function so that we know the write mode (i.e., process the opts outside of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No worries.
I think that's a good idea. I wanted to introduce a minimally invasive change with this PR, but if exit codes are gonna change, so should this. |
||
for file in files { | ||
if !file.exists() { | ||
eprintln!("Error: file `{}` does not exist", file.to_str().unwrap()); | ||
|
@@ -342,6 +346,8 @@ fn main() { | |
Ok(summary) => { | ||
if summary.has_operational_errors() { | ||
1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part is a bit wrong. IIRC in check mode, the exit code should be either 0 or 1, but this change does not seem to respect it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback! I haven't gotten around to implementing #1977 (comment), but when I do later today, this the exit code handling should be far nicer and correct, wheras with my PR, it is not. |
||
} else if summary.has_diff && summary.write_mode == Some(WriteMode::Check) { | ||
1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will probably be easier to change the other exit codes now, rather than later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alrighty! I'll implement the stuff suggested in this comment: #1977 (comment) |
||
} else if summary.has_parsing_errors() { | ||
2 | ||
} else if summary.has_formatting_errors() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,6 +183,9 @@ configuration_option_enum! { WriteMode: | |
Checkstyle, | ||
// Output the changed lines (for internal value only) | ||
Modified, | ||
// Displays how much of the input was processed, but if anything was modified, rustfmt quits | ||
// with exit code 0. This option is intended to be run as part of a CI pipeline. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the exit code is backwards - it should quit with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. D'oh—you're right. I'll fix in subsequent commits. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed/resolved. |
||
Check, | ||
} | ||
|
||
configuration_option_enum! { Color: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
use std::time::{Duration, Instant}; | ||
use std::default::Default; | ||
|
||
use config::options::WriteMode; | ||
|
||
#[must_use] | ||
#[derive(Debug, Default, Clone, Copy)] | ||
pub struct Summary { | ||
|
@@ -26,6 +28,9 @@ pub struct Summary { | |
// Formatted code differs from existing code (write-mode diff only). | ||
pub has_diff: bool, | ||
|
||
// What write mode rustfmt was invoked with, if any. | ||
pub write_mode: Option<WriteMode>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this needs to be optional - even if there is no explicit write mode, then there is a default. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I'll default it to overwrite? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolved, defaults to Overwrite. |
||
|
||
// Keeps track of time spent in parsing and formatting steps. | ||
timer: Timer, | ||
} | ||
|
@@ -89,6 +94,10 @@ impl Summary { | |
self.has_diff = true; | ||
} | ||
|
||
pub fn add_write_mode(&mut self, mode: WriteMode) { | ||
self.write_mode = Some(mode) | ||
} | ||
|
||
pub fn has_no_errors(&self) -> bool { | ||
!(self.has_operational_errors || self.has_parsing_errors || self.has_formatting_errors | ||
|| self.has_diff) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would document check mode in its own right, rather than comparing to diff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point; will do.