Pretty print json in ui tests

This commit is contained in:
Oliver Schneider 2017-11-03 13:38:26 +01:00
parent 65c899edfe
commit c7cb2cf8b5
No known key found for this signature in database
GPG Key ID: A69F8D225B3AD7D9
8 changed files with 418 additions and 21 deletions

View File

@ -155,7 +155,7 @@ impl OutputType {
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ErrorOutputType { pub enum ErrorOutputType {
HumanReadable(ColorConfig), HumanReadable(ColorConfig),
Json, Json(bool),
Short(ColorConfig), Short(ColorConfig),
} }
@ -1104,6 +1104,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"enable ThinLTO when possible"), "enable ThinLTO when possible"),
inline_in_all_cgus: Option<bool> = (None, parse_opt_bool, [TRACKED], inline_in_all_cgus: Option<bool> = (None, parse_opt_bool, [TRACKED],
"control whether #[inline] functions are in all cgus"), "control whether #[inline] functions are in all cgus"),
pretty_json_error_format: bool = (false, parse_bool, [UNTRACKED],
"allow `--error-format=pretty-json` (used for compiletest)"),
} }
pub fn default_lib_output() -> CrateType { pub fn default_lib_output() -> CrateType {
@ -1433,7 +1435,8 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
let error_format = if matches.opts_present(&["error-format".to_owned()]) { let error_format = if matches.opts_present(&["error-format".to_owned()]) {
match matches.opt_str("error-format").as_ref().map(|s| &s[..]) { match matches.opt_str("error-format").as_ref().map(|s| &s[..]) {
Some("human") => ErrorOutputType::HumanReadable(color), Some("human") => ErrorOutputType::HumanReadable(color),
Some("json") => ErrorOutputType::Json, Some("json") => ErrorOutputType::Json(false),
Some("pretty-json") => ErrorOutputType::Json(true),
Some("short") => ErrorOutputType::Short(color), Some("short") => ErrorOutputType::Short(color),
None => ErrorOutputType::HumanReadable(color), None => ErrorOutputType::HumanReadable(color),
@ -1474,6 +1477,11 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
let debugging_opts = build_debugging_options(matches, error_format); let debugging_opts = build_debugging_options(matches, error_format);
if !debugging_opts.pretty_json_error_format && error_format == ErrorOutputType::Json(true) {
early_error(ErrorOutputType::Json(false), "--error-format=pretty-json is unstable \
(use -Zpretty-json-error-format)");
}
let mut output_types = BTreeMap::new(); let mut output_types = BTreeMap::new();
if !debugging_opts.parse_only { if !debugging_opts.parse_only {
for list in matches.opt_strs("emit") { for list in matches.opt_strs("emit") {

View File

@ -372,7 +372,7 @@ impl Session {
match self.opts.error_format { match self.opts.error_format {
// when outputting JSON for tool consumption, the tool might want // when outputting JSON for tool consumption, the tool might want
// the duplicates // the duplicates
config::ErrorOutputType::Json => { config::ErrorOutputType::Json(_) => {
do_method() do_method()
}, },
_ => { _ => {
@ -736,11 +736,11 @@ pub fn build_session_with_codemap(sopts: config::Options,
(config::ErrorOutputType::HumanReadable(_), Some(dst)) => { (config::ErrorOutputType::HumanReadable(_), Some(dst)) => {
Box::new(EmitterWriter::new(dst, Some(codemap.clone()), false)) Box::new(EmitterWriter::new(dst, Some(codemap.clone()), false))
} }
(config::ErrorOutputType::Json, None) => { (config::ErrorOutputType::Json(pretty), None) => {
Box::new(JsonEmitter::stderr(Some(registry), codemap.clone())) Box::new(JsonEmitter::stderr(Some(registry), codemap.clone(), pretty))
} }
(config::ErrorOutputType::Json, Some(dst)) => { (config::ErrorOutputType::Json(pretty), Some(dst)) => {
Box::new(JsonEmitter::new(dst, Some(registry), codemap.clone())) Box::new(JsonEmitter::new(dst, Some(registry), codemap.clone(), pretty))
} }
(config::ErrorOutputType::Short(color_config), None) => { (config::ErrorOutputType::Short(color_config), None) => {
Box::new(EmitterWriter::stderr(color_config, Some(codemap.clone()), true)) Box::new(EmitterWriter::stderr(color_config, Some(codemap.clone()), true))
@ -918,7 +918,7 @@ pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! {
config::ErrorOutputType::HumanReadable(color_config) => { config::ErrorOutputType::HumanReadable(color_config) => {
Box::new(EmitterWriter::stderr(color_config, None, false)) Box::new(EmitterWriter::stderr(color_config, None, false))
} }
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()), config::ErrorOutputType::Json(pretty) => Box::new(JsonEmitter::basic(pretty)),
config::ErrorOutputType::Short(color_config) => { config::ErrorOutputType::Short(color_config) => {
Box::new(EmitterWriter::stderr(color_config, None, true)) Box::new(EmitterWriter::stderr(color_config, None, true))
} }
@ -933,7 +933,7 @@ pub fn early_warn(output: config::ErrorOutputType, msg: &str) {
config::ErrorOutputType::HumanReadable(color_config) => { config::ErrorOutputType::HumanReadable(color_config) => {
Box::new(EmitterWriter::stderr(color_config, None, false)) Box::new(EmitterWriter::stderr(color_config, None, false))
} }
config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()), config::ErrorOutputType::Json(pretty) => Box::new(JsonEmitter::basic(pretty)),
config::ErrorOutputType::Short(color_config) => { config::ErrorOutputType::Short(color_config) => {
Box::new(EmitterWriter::stderr(color_config, None, true)) Box::new(EmitterWriter::stderr(color_config, None, true))
} }

View File

@ -30,36 +30,41 @@ use std::rc::Rc;
use std::io::{self, Write}; use std::io::{self, Write};
use std::vec; use std::vec;
use rustc_serialize::json::as_json; use rustc_serialize::json::{as_json, as_pretty_json};
pub struct JsonEmitter { pub struct JsonEmitter {
dst: Box<Write + Send>, dst: Box<Write + Send>,
registry: Option<Registry>, registry: Option<Registry>,
cm: Rc<CodeMapper + 'static>, cm: Rc<CodeMapper + 'static>,
pretty: bool,
} }
impl JsonEmitter { impl JsonEmitter {
pub fn stderr(registry: Option<Registry>, pub fn stderr(registry: Option<Registry>,
code_map: Rc<CodeMap>) -> JsonEmitter { code_map: Rc<CodeMap>,
pretty: bool) -> JsonEmitter {
JsonEmitter { JsonEmitter {
dst: Box::new(io::stderr()), dst: Box::new(io::stderr()),
registry, registry,
cm: code_map, cm: code_map,
pretty,
} }
} }
pub fn basic() -> JsonEmitter { pub fn basic(pretty: bool) -> JsonEmitter {
let file_path_mapping = FilePathMapping::empty(); let file_path_mapping = FilePathMapping::empty();
JsonEmitter::stderr(None, Rc::new(CodeMap::new(file_path_mapping))) JsonEmitter::stderr(None, Rc::new(CodeMap::new(file_path_mapping)), pretty)
} }
pub fn new(dst: Box<Write + Send>, pub fn new(dst: Box<Write + Send>,
registry: Option<Registry>, registry: Option<Registry>,
code_map: Rc<CodeMap>) -> JsonEmitter { code_map: Rc<CodeMap>,
pretty: bool) -> JsonEmitter {
JsonEmitter { JsonEmitter {
dst, dst,
registry, registry,
cm: code_map, cm: code_map,
pretty,
} }
} }
} }
@ -67,7 +72,12 @@ impl JsonEmitter {
impl Emitter for JsonEmitter { impl Emitter for JsonEmitter {
fn emit(&mut self, db: &DiagnosticBuilder) { fn emit(&mut self, db: &DiagnosticBuilder) {
let data = Diagnostic::from_diagnostic_builder(db, self); let data = Diagnostic::from_diagnostic_builder(db, self);
if let Err(e) = writeln!(&mut self.dst, "{}", as_json(&data)) { let result = if self.pretty {
writeln!(&mut self.dst, "{}", as_pretty_json(&data))
} else {
writeln!(&mut self.dst, "{}", as_json(&data))
};
if let Err(e) = result {
panic!("failed to print diagnostics: {:?}", e); panic!("failed to print diagnostics: {:?}", e);
} }
} }

View File

@ -8,7 +8,7 @@
// 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.
// compile-flags: --error-format json // compile-flags: --error-format pretty-json -Zpretty_json_error_format
// The output for humans should just highlight the whole span without showing // The output for humans should just highlight the whole span without showing
// the suggested replacement, but we also want to test that suggested // the suggested replacement, but we also want to test that suggested

View File

@ -1 +1,91 @@
{"message":"unnecessary parentheses around assigned value","code":{"code":"unused_parens","explanation":null},"level":"warning","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":1001,"byte_end":1014,"line_start":24,"line_end":24,"column_start":14,"column_end":27,"is_primary":true,"text":[{"text":" let _a = (1 / (2 + 3));","highlight_start":14,"highlight_end":27}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[{"message":"lint level defined here","code":null,"level":"note","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":847,"byte_end":860,"line_start":19,"line_end":19,"column_start":9,"column_end":22,"is_primary":true,"text":[{"text":"#![warn(unused_parens)]","highlight_start":9,"highlight_end":22}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[],"rendered":null},{"message":"remove these parentheses","code":null,"level":"help","spans":[{"file_name":"$DIR/unused_parens_json_suggestion.rs","byte_start":1001,"byte_end":1014,"line_start":24,"line_end":24,"column_start":14,"column_end":27,"is_primary":true,"text":[{"text":" let _a = (1 / (2 + 3));","highlight_start":14,"highlight_end":27}],"label":null,"suggested_replacement":"1 / (2 + 3)","expansion":null}],"children":[],"rendered":null}],"rendered":null} {
"message": "unnecessary parentheses around assigned value",
"code": {
"code": "unused_parens",
"explanation": null
},
"level": "warning",
"spans": [
{
"file_name": "$DIR/unused_parens_json_suggestion.rs",
"byte_start": 1035,
"byte_end": 1048,
"line_start": 24,
"line_end": 24,
"column_start": 14,
"column_end": 27,
"is_primary": true,
"text": [
{
"text": " let _a = (1 / (2 + 3));",
"highlight_start": 14,
"highlight_end": 27
}
],
"label": null,
"suggested_replacement": null,
"expansion": null
}
],
"children": [
{
"message": "lint level defined here",
"code": null,
"level": "note",
"spans": [
{
"file_name": "$DIR/unused_parens_json_suggestion.rs",
"byte_start": 881,
"byte_end": 894,
"line_start": 19,
"line_end": 19,
"column_start": 9,
"column_end": 22,
"is_primary": true,
"text": [
{
"text": "#![warn(unused_parens)]",
"highlight_start": 9,
"highlight_end": 22
}
],
"label": null,
"suggested_replacement": null,
"expansion": null
}
],
"children": [],
"rendered": null
},
{
"message": "remove these parentheses",
"code": null,
"level": "help",
"spans": [
{
"file_name": "$DIR/unused_parens_json_suggestion.rs",
"byte_start": 1035,
"byte_end": 1048,
"line_start": 24,
"line_end": 24,
"column_start": 14,
"column_end": 27,
"is_primary": true,
"text": [
{
"text": " let _a = (1 / (2 + 3));",
"highlight_start": 14,
"highlight_end": 27
}
],
"label": null,
"suggested_replacement": "1 / (2 + 3)",
"expansion": null
}
],
"children": [],
"rendered": null
}
],
"rendered": null
}

View File

@ -8,7 +8,7 @@
// 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.
// compile-flags: --error-format json // compile-flags: --error-format pretty-json -Zpretty_json_error_format
// The output for humans should just highlight the whole span without showing // The output for humans should just highlight the whole span without showing
// the suggested replacement, but we also want to test that suggested // the suggested replacement, but we also want to test that suggested

File diff suppressed because one or more lines are too long

View File

@ -2378,7 +2378,8 @@ actual:\n\
fn normalize_output(&self, output: &str, custom_rules: &[(String, String)]) -> String { fn normalize_output(&self, output: &str, custom_rules: &[(String, String)]) -> String {
let parent_dir = self.testpaths.file.parent().unwrap(); let parent_dir = self.testpaths.file.parent().unwrap();
let cflags = self.props.compile_flags.join(" "); let cflags = self.props.compile_flags.join(" ");
let parent_dir_str = if cflags.contains("--error-format json") { let parent_dir_str = if cflags.contains("--error-format json")
|| cflags.contains("--error-format pretty-json") {
parent_dir.display().to_string().replace("\\", "\\\\") parent_dir.display().to_string().replace("\\", "\\\\")
} else { } else {
parent_dir.display().to_string() parent_dir.display().to_string()