diff --git a/src/emitter/checkstyle.rs b/src/emitter/checkstyle.rs index e8f8ec86cf177..4448214f3ff2c 100644 --- a/src/emitter/checkstyle.rs +++ b/src/emitter/checkstyle.rs @@ -29,7 +29,7 @@ impl Emitter for CheckstyleEmitter { formatted_text, }: FormattedFile<'_>, ) -> Result { - const CONTEXT_SIZE: usize = 3; + const CONTEXT_SIZE: usize = 0; let filename = ensure_real_path(filename); let diff = make_diff(original_text, formatted_text, CONTEXT_SIZE); output_checkstyle_file(output, filename, diff)?; @@ -47,13 +47,18 @@ where { write!(writer, r#""#, filename.display())?; for mismatch in diff { + let begin_line = mismatch.line_number; + let mut current_line; + let mut line_counter = 0; for line in mismatch.lines { // Do nothing with `DiffLine::Context` and `DiffLine::Resulting`. if let DiffLine::Expected(message) = line { + current_line = begin_line + line_counter; + line_counter += 1; write!( writer, r#""#, - mismatch.line_number, + current_line, XmlEscaped(&message) )?; } @@ -62,3 +67,82 @@ where write!(writer, "")?; Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + use std::path::PathBuf; + + #[test] + fn emits_empty_record_on_file_with_no_mismatches() { + let file_name = "src/well_formatted.rs"; + let mut writer = Vec::new(); + let _ = output_checkstyle_file(&mut writer, &PathBuf::from(file_name), vec![]); + assert_eq!( + &writer[..], + format!(r#""#, file_name).as_bytes() + ); + } + + // https://github.com/rust-lang/rustfmt/issues/1636 + #[test] + fn emits_single_xml_tree_containing_all_files() { + let bin_file = "src/bin.rs"; + let bin_original = vec!["fn main() {", "println!(\"Hello, world!\");", "}"]; + let bin_formatted = vec!["fn main() {", " println!(\"Hello, world!\");", "}"]; + let lib_file = "src/lib.rs"; + let lib_original = vec!["fn greet() {", "println!(\"Greetings!\");", "}"]; + let lib_formatted = vec!["fn greet() {", " println!(\"Greetings!\");", "}"]; + let mut writer = Vec::new(); + let mut emitter = CheckstyleEmitter::default(); + let _ = emitter.emit_header(&mut writer); + let _ = emitter + .emit_formatted_file( + &mut writer, + FormattedFile { + filename: &FileName::Real(PathBuf::from(bin_file)), + original_text: &bin_original.join("\n"), + formatted_text: &bin_formatted.join("\n"), + }, + ) + .unwrap(); + let _ = emitter + .emit_formatted_file( + &mut writer, + FormattedFile { + filename: &FileName::Real(PathBuf::from(lib_file)), + original_text: &lib_original.join("\n"), + formatted_text: &lib_formatted.join("\n"), + }, + ) + .unwrap(); + let _ = emitter.emit_footer(&mut writer); + let exp_bin_xml = vec![ + format!(r#""#, bin_file), + format!( + r#""#, + XmlEscaped(&r#" println!("Hello, world!");"#), + ), + String::from(""), + ]; + let exp_lib_xml = vec![ + format!(r#""#, lib_file), + format!( + r#""#, + XmlEscaped(&r#" println!("Greetings!");"#), + ), + String::from(""), + ]; + assert_eq!( + String::from_utf8(writer).unwrap(), + vec![ + r#""#, + "\n", + r#""#, + &format!("{}{}", exp_bin_xml.join(""), exp_lib_xml.join("")), + "\n", + ] + .join(""), + ); + } +} diff --git a/tests/writemode/target/checkstyle.xml b/tests/writemode/target/checkstyle.xml index 30ab5ad757971..05bc3a2525d54 100644 --- a/tests/writemode/target/checkstyle.xml +++ b/tests/writemode/target/checkstyle.xml @@ -1,2 +1,2 @@ - +