From 8260d277c88f18c708ed9ebb319ae59f6c0c109f Mon Sep 17 00:00:00 2001 From: Johann Hofmann Date: Mon, 20 Jun 2016 20:42:29 +0200 Subject: [PATCH] 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. --- src/bin/rustfmt.rs | 3 +++ src/filemap.rs | 11 +++++++---- src/lib.rs | 20 +++++++++++++------- src/summary.rs | 12 +++++++++++- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index 30894b9d03c..bf78bb39fe4 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -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 diff --git a/src/filemap.rs b/src/filemap.rs index 17abc6f76e7..1fca0d5b012 100644 --- a/src/filemap.rs +++ b/src/filemap.rs @@ -82,7 +82,7 @@ pub fn write_file(text: &StringBuffer, filename: &str, out: &mut T, config: &Config) - -> Result, io::Error> + -> Result where T: Write { @@ -146,8 +146,10 @@ pub fn write_file(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(text: &StringBuffer, } } - Ok(None) + // when we are not in diff mode, don't indicate differing files + Ok(false) } diff --git a/src/lib.rs b/src/lib.rs index 322eed374de..17af40d9b37 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -286,10 +286,12 @@ fn format_ast(krate: &ast::Crate, main_file: &Path, config: &Config, mut after_file: F) - -> Result - where F: FnMut(&str, &mut StringBuffer) -> Result<(), io::Error> + -> Result<(FileMap, bool), io::Error> + where F: FnMut(&str, &mut StringBuffer) -> Result { 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(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(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)), diff --git a/src/summary.rs b/src/summary.rs index 87d20899a58..5846afde7fc 100644 --- a/src/summary.rs +++ b/src/summary.rs @@ -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; } }