Update checkstyle write mode to take Write arguments.

By accepting Write traits we can write tests using StringBuffer.
This commit is contained in:
Mark Story 2016-01-19 00:02:21 -05:00
parent 66d4faf53f
commit d8c6f5954a
3 changed files with 33 additions and 28 deletions

View file

@ -8,30 +8,30 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use rustfmt_diff::{Mismatch, DiffLine}; use rustfmt_diff::{Mismatch, DiffLine};
use std::io::{self, Write, Read, stdout}; use std::io::{self, Write, Read};
use config::WriteMode; use config::WriteMode;
pub fn output_header(mode: WriteMode) -> Result<(), io::Error> { pub fn output_header<T>(out: &mut T, mode: WriteMode) -> Result<(), io::Error>
let stdout = stdout(); where T: Write
let mut stdout = stdout.lock(); {
if mode == WriteMode::Checkstyle { if mode == WriteMode::Checkstyle {
let mut xml_heading = String::new(); let mut xml_heading = String::new();
xml_heading.push_str("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); xml_heading.push_str("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
xml_heading.push_str("\n"); xml_heading.push_str("\n");
xml_heading.push_str("<checkstyle version=\"4.3\">"); xml_heading.push_str("<checkstyle version=\"4.3\">");
try!(write!(stdout, "{}", xml_heading)); try!(write!(out, "{}", xml_heading));
} }
Ok(()) Ok(())
} }
pub fn output_footer(mode: WriteMode) -> Result<(), io::Error> { pub fn output_footer<T>(out: &mut T, mode: WriteMode) -> Result<(), io::Error>
let stdout = stdout(); where T: Write
let mut stdout = stdout.lock(); {
if mode == WriteMode::Checkstyle { if mode == WriteMode::Checkstyle {
let mut xml_tail = String::new(); let mut xml_tail = String::new();
xml_tail.push_str("</checkstyle>"); xml_tail.push_str("</checkstyle>");
try!(write!(stdout, "{}", xml_tail)); try!(write!(out, "{}", xml_tail));
} }
Ok(()) Ok(())
} }

View file

@ -31,15 +31,18 @@ pub fn append_newlines(file_map: &mut FileMap) {
} }
} }
pub fn write_all_files(file_map: &FileMap, pub fn write_all_files<T>(file_map: &FileMap,
mode: WriteMode, mut out: T,
config: &Config) mode: WriteMode,
-> Result<(), io::Error> { config: &Config)
output_header(mode).ok(); -> Result<(), io::Error>
where T: Write
{
output_header(&mut out, mode).ok();
for filename in file_map.keys() { for filename in file_map.keys() {
try!(write_file(&file_map[filename], filename, mode, config)); try!(write_file(&file_map[filename], filename, &mut out, mode, config));
} }
output_footer(mode).ok(); output_footer(&mut out, mode).ok();
Ok(()) Ok(())
} }
@ -81,11 +84,14 @@ pub fn write_system_newlines<T>(writer: T,
} }
} }
pub fn write_file(text: &StringBuffer, pub fn write_file<T>(text: &StringBuffer,
filename: &str, filename: &str,
mode: WriteMode, out: &mut T,
config: &Config) mode: WriteMode,
-> Result<Option<String>, io::Error> { config: &Config)
-> Result<Option<String>, io::Error>
where T: Write
{
fn source_and_formatted_text(text: &StringBuffer, fn source_and_formatted_text(text: &StringBuffer,
filename: &str, filename: &str,
@ -155,11 +161,8 @@ pub fn write_file(text: &StringBuffer,
unreachable!("The WriteMode should NEVER Be default at this point!"); unreachable!("The WriteMode should NEVER Be default at this point!");
} }
WriteMode::Checkstyle => { WriteMode::Checkstyle => {
let stdout = stdout();
let stdout = stdout.lock();
let diff = try!(create_diff(filename, text, config)); let diff = try!(create_diff(filename, text, config));
// Output the XML tags for the lines that are different. try!(output_checkstyle_file(out, filename, diff));
try!(output_checkstyle_file(stdout, filename, diff));
} }
} }

View file

@ -30,6 +30,7 @@ use syntax::codemap::{mk_sp, Span};
use syntax::diagnostic::{EmitterWriter, Handler}; use syntax::diagnostic::{EmitterWriter, Handler};
use syntax::parse::{self, ParseSess}; use syntax::parse::{self, ParseSess};
use std::io::stdout;
use std::ops::{Add, Sub}; use std::ops::{Add, Sub};
use std::path::Path; use std::path::Path;
use std::collections::HashMap; use std::collections::HashMap;
@ -428,8 +429,8 @@ pub fn run(file: &Path, write_mode: WriteMode, config: &Config) {
let mut result = format(file, config, mode); let mut result = format(file, config, mode);
print!("{}", fmt_lines(&mut result, config)); print!("{}", fmt_lines(&mut result, config));
let out = stdout();
let write_result = filemap::write_all_files(&result, mode, config); let write_result = filemap::write_all_files(&result, out, mode, config);
if let Err(msg) = write_result { if let Err(msg) = write_result {
println!("Error writing files: {}", msg); println!("Error writing files: {}", msg);
@ -442,7 +443,8 @@ pub fn run_from_stdin(input: String, write_mode: WriteMode, config: &Config) {
let mut result = format_string(input, config, mode); let mut result = format_string(input, config, mode);
fmt_lines(&mut result, config); fmt_lines(&mut result, config);
let write_result = filemap::write_file(&result["stdin"], "stdin", mode, config); let mut out = stdout();
let write_result = filemap::write_file(&result["stdin"], "stdin", &mut out, mode, config);
if let Err(msg) = write_result { if let Err(msg) = write_result {
panic!("Error writing to stdout: {}", msg); panic!("Error writing to stdout: {}", msg);