Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix newlines in JSON output #4262

Merged
merged 2 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions src/emitter/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,22 @@ impl JsonEmitter {
let mut expected_end_line = expected_begin_line;
let mut original_line_counter = 0;
let mut expected_line_counter = 0;
let mut original_lines = vec![];
let mut expected_lines = vec![];
let mut original = String::new();
let mut expected = String::new();

for line in mismatch.lines {
match line {
DiffLine::Expected(msg) => {
expected_end_line = expected_begin_line + expected_line_counter;
expected_line_counter += 1;
expected_lines.push(msg)
expected.push_str(&msg);
expected.push('\n');
calebcartwright marked this conversation as resolved.
Show resolved Hide resolved
}
DiffLine::Resulting(msg) => {
original_end_line = original_begin_line + original_line_counter;
original_line_counter += 1;
original_lines.push(msg)
original.push_str(&msg);
original.push('\n');
}
DiffLine::Context(_) => continue,
}
Expand All @@ -91,8 +93,8 @@ impl JsonEmitter {
original_end_line,
expected_begin_line,
expected_end_line,
original: original_lines.join("\n"),
expected: expected_lines.join("\n"),
original,
expected,
});
}
self.mismatched_files.push(MismatchedFile {
Expand Down Expand Up @@ -122,8 +124,8 @@ mod tests {
original_end_line: 79,
expected_begin_line: 79,
expected_end_line: 82,
original: String::from("fn Foo<T>() where T: Bar {"),
expected: String::from("fn Foo<T>()\nwhere\n T: Bar,\n{"),
original: String::from("fn Foo<T>() where T: Bar {\n"),
expected: String::from("fn Foo<T>()\nwhere\n T: Bar,\n{\n"),
}],
};
let mismatch = Mismatch {
Expand Down Expand Up @@ -160,10 +162,10 @@ mod tests {
expected_begin_line: 5,
expected_end_line: 5,
original: String::from(
"fn foo(_x: &u64) -> Option<&(dyn::std::error::Error + 'static)> {",
"fn foo(_x: &u64) -> Option<&(dyn::std::error::Error + 'static)> {\n",
),
expected: String::from(
"fn foo(_x: &u64) -> Option<&(dyn ::std::error::Error + 'static)> {",
"fn foo(_x: &u64) -> Option<&(dyn ::std::error::Error + 'static)> {\n",
),
}],
};
Expand Down Expand Up @@ -262,19 +264,19 @@ mod tests {
original_end_line: 2,
expected_begin_line: 2,
expected_end_line: 2,
original: String::from("println!(\"Hello, world!\");"),
expected: String::from(" println!(\"Hello, world!\");"),
original: String::from("println!(\"Hello, world!\");\n"),
expected: String::from(" println!(\"Hello, world!\");\n"),
},
MismatchedBlock {
original_begin_line: 7,
original_end_line: 10,
expected_begin_line: 7,
expected_end_line: 10,
original: String::from(
"#[test]\nfn it_works() {\n assert_eq!(2 + 2, 4);\n}",
"#[test]\nfn it_works() {\n assert_eq!(2 + 2, 4);\n}\n",
),
expected: String::from(
" #[test]\n fn it_works() {\n assert_eq!(2 + 2, 4);\n }",
" #[test]\n fn it_works() {\n assert_eq!(2 + 2, 4);\n }\n",
),
},
],
Expand Down Expand Up @@ -323,8 +325,8 @@ mod tests {
original_end_line: 2,
expected_begin_line: 2,
expected_end_line: 2,
original: String::from("println!(\"Hello, world!\");"),
expected: String::from(" println!(\"Hello, world!\");"),
original: String::from("println!(\"Hello, world!\");\n"),
expected: String::from(" println!(\"Hello, world!\");\n"),
}],
};

Expand All @@ -335,8 +337,8 @@ mod tests {
original_end_line: 2,
expected_begin_line: 2,
expected_end_line: 2,
original: String::from("println!(\"Greetings!\");"),
expected: String::from(" println!(\"Greetings!\");"),
original: String::from("println!(\"Greetings!\");\n"),
expected: String::from(" println!(\"Greetings!\");\n"),
}],
};

Expand Down
2 changes: 1 addition & 1 deletion tests/writemode/target/output.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"name":"tests/writemode/source/json.rs","mismatches":[{"original_begin_line":5,"original_end_line":7,"expected_begin_line":5,"expected_end_line":5,"original":"fn foo_expr() {\n 1\n}","expected":"fn foo_expr() { 1 }"},{"original_begin_line":9,"original_end_line":11,"expected_begin_line":7,"expected_end_line":7,"original":"fn foo_stmt() {\n foo();\n}","expected":"fn foo_stmt() { foo(); }"},{"original_begin_line":13,"original_end_line":15,"expected_begin_line":9,"expected_end_line":9,"original":"fn foo_decl_local() {\n let z = 5;\n }","expected":"fn foo_decl_local() { let z = 5; }"},{"original_begin_line":17,"original_end_line":19,"expected_begin_line":11,"expected_end_line":11,"original":"fn foo_decl_item(x: &mut i32) {\n x = 3;\n}","expected":"fn foo_decl_item(x: &mut i32) { x = 3; }"},{"original_begin_line":21,"original_end_line":21,"expected_begin_line":13,"expected_end_line":13,"original":" fn empty() {","expected":"fn empty() {}"},{"original_begin_line":23,"original_end_line":23,"expected_begin_line":15,"expected_end_line":15,"original":"}","expected":"fn foo_return() -> String { \"yay\" }"},{"original_begin_line":25,"original_end_line":29,"expected_begin_line":17,"expected_end_line":20,"original":"fn foo_return() -> String {\n \"yay\"\n}\n\nfn foo_where() -> T where T: Sync {","expected":"fn foo_where() -> T\nwhere\n T: Sync,\n{"},{"original_begin_line":33,"original_end_line":37,"expected_begin_line":24,"expected_end_line":24,"original":"fn fooblock() {\n {\n \"inner-block\"\n }\n}","expected":"fn fooblock() { { \"inner-block\" } }"},{"original_begin_line":64,"original_end_line":66,"expected_begin_line":51,"expected_end_line":51,"original":"fn lots_of_space () {\n 1 \n}","expected":"fn lots_of_space() { 1 }"},{"original_begin_line":71,"original_end_line":72,"expected_begin_line":56,"expected_end_line":56,"original":" fn dummy(&self) {\n }","expected":" fn dummy(&self) {}"},{"original_begin_line":75,"original_end_line":75,"expected_begin_line":59,"expected_end_line":60,"original":"trait CoolerTypes { fn dummy(&self) { ","expected":"trait CoolerTypes {\n fn dummy(&self) {}"},{"original_begin_line":77,"original_end_line":77,"expected_begin_line":62,"expected_end_line":62,"original":"}","expected":""},{"original_begin_line":79,"original_end_line":79,"expected_begin_line":63,"expected_end_line":66,"original":"fn Foo<T>() where T: Bar {","expected":"fn Foo<T>()\nwhere\n T: Bar,\n{"}]}]
[{"name":"tests/writemode/source/json.rs","mismatches":[{"original_begin_line":5,"original_end_line":7,"expected_begin_line":5,"expected_end_line":5,"original":"fn foo_expr() {\n 1\n}\n","expected":"fn foo_expr() { 1 }\n"},{"original_begin_line":9,"original_end_line":11,"expected_begin_line":7,"expected_end_line":7,"original":"fn foo_stmt() {\n foo();\n}\n","expected":"fn foo_stmt() { foo(); }\n"},{"original_begin_line":13,"original_end_line":15,"expected_begin_line":9,"expected_end_line":9,"original":"fn foo_decl_local() {\n let z = 5;\n }\n","expected":"fn foo_decl_local() { let z = 5; }\n"},{"original_begin_line":17,"original_end_line":19,"expected_begin_line":11,"expected_end_line":11,"original":"fn foo_decl_item(x: &mut i32) {\n x = 3;\n}\n","expected":"fn foo_decl_item(x: &mut i32) { x = 3; }\n"},{"original_begin_line":21,"original_end_line":21,"expected_begin_line":13,"expected_end_line":13,"original":" fn empty() {\n","expected":"fn empty() {}\n"},{"original_begin_line":23,"original_end_line":23,"expected_begin_line":15,"expected_end_line":15,"original":"}\n","expected":"fn foo_return() -> String { \"yay\" }\n"},{"original_begin_line":25,"original_end_line":29,"expected_begin_line":17,"expected_end_line":20,"original":"fn foo_return() -> String {\n \"yay\"\n}\n\nfn foo_where() -> T where T: Sync {\n","expected":"fn foo_where() -> T\nwhere\n T: Sync,\n{\n"},{"original_begin_line":33,"original_end_line":37,"expected_begin_line":24,"expected_end_line":24,"original":"fn fooblock() {\n {\n \"inner-block\"\n }\n}\n","expected":"fn fooblock() { { \"inner-block\" } }\n"},{"original_begin_line":64,"original_end_line":66,"expected_begin_line":51,"expected_end_line":51,"original":"fn lots_of_space () {\n 1 \n}\n","expected":"fn lots_of_space() { 1 }\n"},{"original_begin_line":71,"original_end_line":72,"expected_begin_line":56,"expected_end_line":56,"original":" fn dummy(&self) {\n }\n","expected":" fn dummy(&self) {}\n"},{"original_begin_line":75,"original_end_line":75,"expected_begin_line":59,"expected_end_line":60,"original":"trait CoolerTypes { fn dummy(&self) { \n","expected":"trait CoolerTypes {\n fn dummy(&self) {}\n"},{"original_begin_line":77,"original_end_line":77,"expected_begin_line":62,"expected_end_line":62,"original":"}\n","expected":""},{"original_begin_line":79,"original_end_line":79,"expected_begin_line":63,"expected_end_line":66,"original":"fn Foo<T>() where T: Bar {\n","expected":"fn Foo<T>()\nwhere\n T: Bar,\n{\n"}]}]
2 changes: 1 addition & 1 deletion tests/writemode/target/stdin.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"name":"stdin","mismatches":[{"original_begin_line":1,"original_end_line":6,"expected_begin_line":1,"expected_end_line":2,"original":"\nfn\n some( )\n{\n}\nfn main () {}","expected":"fn some() {}\nfn main() {}"}]}]
[{"name":"stdin","mismatches":[{"original_begin_line":1,"original_end_line":6,"expected_begin_line":1,"expected_end_line":2,"original":"\nfn\n some( )\n{\n}\nfn main () {}\n","expected":"fn some() {}\nfn main() {}\n"}]}]