mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 02:57:37 +00:00
Return failure exit code on found diffs (fix #906)
This changes rustfmt to return exit code 4 when run with write mode diff and differences between the formatted code and the original code are found. Useful for CI to make sure your contributors actually ran rustfmt.
This commit is contained in:
parent
48a37713f9
commit
8260d277c8
@ -269,6 +269,9 @@ fn main() {
|
||||
2
|
||||
} else if summary.has_formatting_errors() {
|
||||
3
|
||||
} else if summary.has_diff {
|
||||
// should only happen in diff mode
|
||||
4
|
||||
} else {
|
||||
assert!(summary.has_no_errors());
|
||||
0
|
||||
|
@ -82,7 +82,7 @@ pub fn write_file<T>(text: &StringBuffer,
|
||||
filename: &str,
|
||||
out: &mut T,
|
||||
config: &Config)
|
||||
-> Result<Option<String>, io::Error>
|
||||
-> Result<bool, io::Error>
|
||||
where T: Write
|
||||
{
|
||||
|
||||
@ -146,8 +146,10 @@ pub fn write_file<T>(text: &StringBuffer,
|
||||
WriteMode::Diff => {
|
||||
println!("Diff of {}:\n", filename);
|
||||
if let Ok((ori, fmt)) = source_and_formatted_text(text, filename, config) {
|
||||
print_diff(make_diff(&ori, &fmt, 3),
|
||||
|line_num| format!("\nDiff at line {}:", line_num));
|
||||
let mismatch = make_diff(&ori, &fmt, 3);
|
||||
let has_diff = !mismatch.is_empty();
|
||||
print_diff(mismatch, |line_num| format!("\nDiff at line {}:", line_num));
|
||||
return Ok(has_diff);
|
||||
}
|
||||
}
|
||||
WriteMode::Checkstyle => {
|
||||
@ -156,5 +158,6 @@ pub fn write_file<T>(text: &StringBuffer,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
// when we are not in diff mode, don't indicate differing files
|
||||
Ok(false)
|
||||
}
|
||||
|
20
src/lib.rs
20
src/lib.rs
@ -286,10 +286,12 @@ fn format_ast<F>(krate: &ast::Crate,
|
||||
main_file: &Path,
|
||||
config: &Config,
|
||||
mut after_file: F)
|
||||
-> Result<FileMap, io::Error>
|
||||
where F: FnMut(&str, &mut StringBuffer) -> Result<(), io::Error>
|
||||
-> Result<(FileMap, bool), io::Error>
|
||||
where F: FnMut(&str, &mut StringBuffer) -> Result<bool, io::Error>
|
||||
{
|
||||
let mut result = FileMap::new();
|
||||
// diff mode: check if any files are differing
|
||||
let mut has_diff = false;
|
||||
|
||||
// We always skip children for the "Plain" write mode, since there is
|
||||
// nothing to distinguish the nested module contents.
|
||||
@ -305,12 +307,12 @@ fn format_ast<F>(krate: &ast::Crate,
|
||||
let mut visitor = FmtVisitor::from_codemap(parse_session, config);
|
||||
visitor.format_separate_mod(module);
|
||||
|
||||
try!(after_file(path, &mut visitor.buffer));
|
||||
has_diff |= try!(after_file(path, &mut visitor.buffer));
|
||||
|
||||
result.push((path.to_owned(), visitor.buffer));
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
Ok((result, has_diff))
|
||||
}
|
||||
|
||||
// Formatting done on a char by char or line by line basis.
|
||||
@ -458,15 +460,19 @@ pub fn format_input<T: Write>(input: Input,
|
||||
format_lines(file, file_name, config, &mut report);
|
||||
|
||||
if let Some(ref mut out) = out {
|
||||
try!(filemap::write_file(file, file_name, out, config));
|
||||
return filemap::write_file(file, file_name, out, config);
|
||||
}
|
||||
Ok(())
|
||||
Ok(false)
|
||||
}) {
|
||||
Ok(file_map) => {
|
||||
Ok((file_map, has_diff)) => {
|
||||
if report.has_warnings() {
|
||||
summary.add_formatting_error();
|
||||
}
|
||||
|
||||
if has_diff {
|
||||
summary.add_diff();
|
||||
}
|
||||
|
||||
Ok((summary, file_map, report))
|
||||
}
|
||||
Err(e) => Err((e, summary)),
|
||||
|
@ -9,6 +9,9 @@ pub struct Summary {
|
||||
|
||||
// Code is valid, but it is impossible to format it properly.
|
||||
has_formatting_errors: bool,
|
||||
|
||||
// Formatted code differs from existing code (write-mode diff only).
|
||||
pub has_diff: bool,
|
||||
}
|
||||
|
||||
impl Summary {
|
||||
@ -17,6 +20,7 @@ impl Summary {
|
||||
has_operational_errors: false,
|
||||
has_parsing_errors: false,
|
||||
has_formatting_errors: false,
|
||||
has_diff: false,
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,13 +48,19 @@ impl Summary {
|
||||
self.has_formatting_errors = true;
|
||||
}
|
||||
|
||||
pub fn add_diff(&mut self) {
|
||||
self.has_diff = true;
|
||||
}
|
||||
|
||||
pub fn has_no_errors(&self) -> bool {
|
||||
!(self.has_operational_errors || self.has_parsing_errors || self.has_formatting_errors)
|
||||
!(self.has_operational_errors || self.has_parsing_errors || self.has_formatting_errors ||
|
||||
self.has_diff)
|
||||
}
|
||||
|
||||
pub fn add(&mut self, other: Summary) {
|
||||
self.has_operational_errors |= other.has_operational_errors;
|
||||
self.has_formatting_errors |= other.has_formatting_errors;
|
||||
self.has_parsing_errors |= other.has_parsing_errors;
|
||||
self.has_diff |= other.has_diff;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user