Move diagnostics tests to expect

This commit is contained in:
Aleksey Kladov 2020-07-09 16:04:29 +02:00
parent d70f4f5da5
commit e075e6eef2
14 changed files with 53 additions and 113 deletions

1
Cargo.lock generated
View file

@ -1438,7 +1438,6 @@ dependencies = [
"expect", "expect",
"flycheck", "flycheck",
"globset", "globset",
"insta",
"itertools", "itertools",
"jod-thread", "jod-thread",
"log", "log",

View file

@ -121,6 +121,10 @@ impl ExpectFile {
} }
Runtime::fail_file(self, &expected, actual); Runtime::fail_file(self, &expected, actual);
} }
pub fn assert_debug_eq(&self, actual: &impl fmt::Debug) {
let actual = format!("{:#?}\n", actual);
self.assert_eq(&actual)
}
fn read(&self) -> String { fn read(&self) -> String {
fs::read_to_string(self.abs_path()).unwrap_or_default().replace("\r\n", "\n") fs::read_to_string(self.abs_path()).unwrap_or_default().replace("\r\n", "\n")
} }

View file

@ -58,7 +58,6 @@ winapi = "0.3.8"
[dev-dependencies] [dev-dependencies]
tempfile = "3.1.0" tempfile = "3.1.0"
insta = "0.16.0"
expect = { path = "../expect" } expect = { path = "../expect" }
test_utils = { path = "../test_utils" } test_utils = { path = "../test_utils" }
mbe = { path = "../ra_mbe", package = "ra_mbe" } mbe = { path = "../ra_mbe", package = "ra_mbe" }

View file

@ -256,13 +256,23 @@ pub(crate) fn map_rust_diagnostic_to_lsp(
mod tests { mod tests {
use super::*; use super::*;
fn parse_diagnostic(val: &str) -> flycheck::Diagnostic { use expect::{expect_file, ExpectFile};
serde_json::from_str::<flycheck::Diagnostic>(val).unwrap()
// TODO: inlay hints config order
fn check(diagnostics_json: &str, expect: ExpectFile) {
check_with_config(DiagnosticsConfig::default(), diagnostics_json, expect)
}
fn check_with_config(config: DiagnosticsConfig, diagnostics_json: &str, expect: ExpectFile) {
let diagnostic: flycheck::Diagnostic = serde_json::from_str(diagnostics_json).unwrap();
let workspace_root = Path::new("/test/");
let actual = map_rust_diagnostic_to_lsp(&config, &diagnostic, workspace_root);
expect.assert_debug_eq(&actual)
} }
#[test] #[test]
fn snap_rustc_incompatible_type_for_trait() { fn rustc_incompatible_type_for_trait() {
let diag = parse_diagnostic( check(
r##"{ r##"{
"message": "method `next` has an incompatible type for trait", "message": "method `next` has an incompatible type for trait",
"code": { "code": {
@ -306,16 +316,13 @@ mod tests {
"rendered": "error[E0053]: method `next` has an incompatible type for trait\n --> compiler/ty/list_iter.rs:52:5\n |\n52 | fn next(&self) -> Option<&'list ty::Ref<M>> {\n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ in mutability\n |\n = note: expected type `fn(&mut ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&ty::Ref<M>>`\n found type `fn(&ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&'list ty::Ref<M>>`\n\n" "rendered": "error[E0053]: method `next` has an incompatible type for trait\n --> compiler/ty/list_iter.rs:52:5\n |\n52 | fn next(&self) -> Option<&'list ty::Ref<M>> {\n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ types differ in mutability\n |\n = note: expected type `fn(&mut ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&ty::Ref<M>>`\n found type `fn(&ty::list_iter::ListIterator<'list, M>) -> std::option::Option<&'list ty::Ref<M>>`\n\n"
} }
"##, "##,
expect_file!["crates/rust-analyzer/test_data/rustc_incompatible_type_for_trait.txt"],
); );
let workspace_root = Path::new("/test/");
let diag = map_rust_diagnostic_to_lsp(&DiagnosticsConfig::default(), &diag, workspace_root);
insta::assert_debug_snapshot!(diag);
} }
#[test] #[test]
fn snap_rustc_unused_variable() { fn rustc_unused_variable() {
let diag = parse_diagnostic( check(
r##"{ r##"{
"message": "unused variable: `foo`", "message": "unused variable: `foo`",
"code": { "code": {
@ -388,105 +395,18 @@ mod tests {
], ],
"rendered": "warning: unused variable: `foo`\n --> driver/subcommand/repl.rs:291:9\n |\n291 | let foo = 42;\n | ^^^ help: consider prefixing with an underscore: `_foo`\n |\n = note: #[warn(unused_variables)] on by default\n\n" "rendered": "warning: unused variable: `foo`\n --> driver/subcommand/repl.rs:291:9\n |\n291 | let foo = 42;\n | ^^^ help: consider prefixing with an underscore: `_foo`\n |\n = note: #[warn(unused_variables)] on by default\n\n"
}"##, }"##,
expect_file!["crates/rust-analyzer/test_data/rustc_unused_variable.txt"],
); );
let workspace_root = Path::new("/test/");
let diag = map_rust_diagnostic_to_lsp(&DiagnosticsConfig::default(), &diag, workspace_root);
insta::assert_debug_snapshot!(diag);
} }
#[test] #[test]
#[cfg(not(windows))] #[cfg(not(windows))]
fn snap_rustc_unused_variable_as_info() { fn rustc_unused_variable_as_info() {
let diag = parse_diagnostic( check_with_config(
r##"{ DiagnosticsConfig {
"message": "unused variable: `foo`",
"code": {
"code": "unused_variables",
"explanation": null
},
"level": "warning",
"spans": [
{
"file_name": "driver/subcommand/repl.rs",
"byte_start": 9228,
"byte_end": 9231,
"line_start": 291,
"line_end": 291,
"column_start": 9,
"column_end": 12,
"is_primary": true,
"text": [
{
"text": " let foo = 42;",
"highlight_start": 9,
"highlight_end": 12
}
],
"label": null,
"suggested_replacement": null,
"suggestion_applicability": null,
"expansion": null
}
],
"children": [
{
"message": "#[warn(unused_variables)] on by default",
"code": null,
"level": "note",
"spans": [],
"children": [],
"rendered": null
},
{
"message": "consider prefixing with an underscore",
"code": null,
"level": "help",
"spans": [
{
"file_name": "driver/subcommand/repl.rs",
"byte_start": 9228,
"byte_end": 9231,
"line_start": 291,
"line_end": 291,
"column_start": 9,
"column_end": 12,
"is_primary": true,
"text": [
{
"text": " let foo = 42;",
"highlight_start": 9,
"highlight_end": 12
}
],
"label": null,
"suggested_replacement": "_foo",
"suggestion_applicability": "MachineApplicable",
"expansion": null
}
],
"children": [],
"rendered": null
}
],
"rendered": "warning: unused variable: `foo`\n --> driver/subcommand/repl.rs:291:9\n |\n291 | let foo = 42;\n | ^^^ help: consider prefixing with an underscore: `_foo`\n |\n = note: #[warn(unused_variables)] on by default\n\n"
}"##,
);
let config = DiagnosticsConfig {
warnings_as_info: vec!["unused_variables".to_string()], warnings_as_info: vec!["unused_variables".to_string()],
..DiagnosticsConfig::default() ..DiagnosticsConfig::default()
}; },
let workspace_root = Path::new("/test/");
let diag = map_rust_diagnostic_to_lsp(&config, &diag, workspace_root);
insta::assert_debug_snapshot!(diag);
}
#[test]
#[cfg(not(windows))]
fn snap_rustc_unused_variable_as_hint() {
let diag = parse_diagnostic(
r##"{ r##"{
"message": "unused variable: `foo`", "message": "unused variable: `foo`",
"code": { "code": {
@ -559,21 +479,97 @@ mod tests {
], ],
"rendered": "warning: unused variable: `foo`\n --> driver/subcommand/repl.rs:291:9\n |\n291 | let foo = 42;\n | ^^^ help: consider prefixing with an underscore: `_foo`\n |\n = note: #[warn(unused_variables)] on by default\n\n" "rendered": "warning: unused variable: `foo`\n --> driver/subcommand/repl.rs:291:9\n |\n291 | let foo = 42;\n | ^^^ help: consider prefixing with an underscore: `_foo`\n |\n = note: #[warn(unused_variables)] on by default\n\n"
}"##, }"##,
expect_file!["crates/rust-analyzer/test_data/rustc_unused_variable_as_info.txt"],
); );
let config = DiagnosticsConfig {
warnings_as_hint: vec!["unused_variables".to_string()],
..DiagnosticsConfig::default()
};
let workspace_root = Path::new("/test/");
let diag = map_rust_diagnostic_to_lsp(&config, &diag, workspace_root);
insta::assert_debug_snapshot!(diag);
} }
#[test] #[test]
fn snap_rustc_wrong_number_of_parameters() { #[cfg(not(windows))]
let diag = parse_diagnostic( fn rustc_unused_variable_as_hint() {
check_with_config(
DiagnosticsConfig {
warnings_as_hint: vec!["unused_variables".to_string()],
..DiagnosticsConfig::default()
},
r##"{
"message": "unused variable: `foo`",
"code": {
"code": "unused_variables",
"explanation": null
},
"level": "warning",
"spans": [
{
"file_name": "driver/subcommand/repl.rs",
"byte_start": 9228,
"byte_end": 9231,
"line_start": 291,
"line_end": 291,
"column_start": 9,
"column_end": 12,
"is_primary": true,
"text": [
{
"text": " let foo = 42;",
"highlight_start": 9,
"highlight_end": 12
}
],
"label": null,
"suggested_replacement": null,
"suggestion_applicability": null,
"expansion": null
}
],
"children": [
{
"message": "#[warn(unused_variables)] on by default",
"code": null,
"level": "note",
"spans": [],
"children": [],
"rendered": null
},
{
"message": "consider prefixing with an underscore",
"code": null,
"level": "help",
"spans": [
{
"file_name": "driver/subcommand/repl.rs",
"byte_start": 9228,
"byte_end": 9231,
"line_start": 291,
"line_end": 291,
"column_start": 9,
"column_end": 12,
"is_primary": true,
"text": [
{
"text": " let foo = 42;",
"highlight_start": 9,
"highlight_end": 12
}
],
"label": null,
"suggested_replacement": "_foo",
"suggestion_applicability": "MachineApplicable",
"expansion": null
}
],
"children": [],
"rendered": null
}
],
"rendered": "warning: unused variable: `foo`\n --> driver/subcommand/repl.rs:291:9\n |\n291 | let foo = 42;\n | ^^^ help: consider prefixing with an underscore: `_foo`\n |\n = note: #[warn(unused_variables)] on by default\n\n"
}"##,
expect_file!["crates/rust-analyzer/test_data/rustc_unused_variable_as_hint.txt"],
);
}
#[test]
fn rustc_wrong_number_of_parameters() {
check(
r##"{ r##"{
"message": "this function takes 2 parameters but 3 parameters were supplied", "message": "this function takes 2 parameters but 3 parameters were supplied",
"code": { "code": {
@ -688,16 +684,13 @@ mod tests {
"children": [], "children": [],
"rendered": "error[E0061]: this function takes 2 parameters but 3 parameters were supplied\n --> compiler/ty/select.rs:104:18\n |\n104 | self.add_evidence(target_fixed, evidence_fixed, false);\n | ^^^^^^^^^^^^ expected 2 parameters\n...\n219 | / pub fn add_evidence(\n220 | | &mut self,\n221 | | target_poly: &ty::Ref<ty::Poly>,\n222 | | evidence_poly: &ty::Ref<ty::Poly>,\n... |\n230 | | }\n231 | | }\n | |_____- defined here\n\n" "rendered": "error[E0061]: this function takes 2 parameters but 3 parameters were supplied\n --> compiler/ty/select.rs:104:18\n |\n104 | self.add_evidence(target_fixed, evidence_fixed, false);\n | ^^^^^^^^^^^^ expected 2 parameters\n...\n219 | / pub fn add_evidence(\n220 | | &mut self,\n221 | | target_poly: &ty::Ref<ty::Poly>,\n222 | | evidence_poly: &ty::Ref<ty::Poly>,\n... |\n230 | | }\n231 | | }\n | |_____- defined here\n\n"
}"##, }"##,
expect_file!["crates/rust-analyzer/test_data/rustc_wrong_number_of_parameters.txt"],
); );
let workspace_root = Path::new("/test/");
let diag = map_rust_diagnostic_to_lsp(&DiagnosticsConfig::default(), &diag, workspace_root);
insta::assert_debug_snapshot!(diag);
} }
#[test] #[test]
fn snap_clippy_pass_by_ref() { fn clippy_pass_by_ref() {
let diag = parse_diagnostic( check(
r##"{ r##"{
"message": "this argument is passed by reference, but would be more efficient if passed by value", "message": "this argument is passed by reference, but would be more efficient if passed by value",
"code": { "code": {
@ -808,16 +801,13 @@ mod tests {
], ],
"rendered": "warning: this argument is passed by reference, but would be more efficient if passed by value\n --> compiler/mir/tagset.rs:42:24\n |\n42 | pub fn is_disjoint(&self, other: Self) -> bool {\n | ^^^^^ help: consider passing by value instead: `self`\n |\nnote: lint level defined here\n --> compiler/lib.rs:1:9\n |\n1 | #![warn(clippy::all)]\n | ^^^^^^^^^^^\n = note: #[warn(clippy::trivially_copy_pass_by_ref)] implied by #[warn(clippy::all)]\n = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref\n\n" "rendered": "warning: this argument is passed by reference, but would be more efficient if passed by value\n --> compiler/mir/tagset.rs:42:24\n |\n42 | pub fn is_disjoint(&self, other: Self) -> bool {\n | ^^^^^ help: consider passing by value instead: `self`\n |\nnote: lint level defined here\n --> compiler/lib.rs:1:9\n |\n1 | #![warn(clippy::all)]\n | ^^^^^^^^^^^\n = note: #[warn(clippy::trivially_copy_pass_by_ref)] implied by #[warn(clippy::all)]\n = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref\n\n"
}"##, }"##,
expect_file!["crates/rust-analyzer/test_data/clippy_pass_by_ref.txt"],
); );
let workspace_root = Path::new("/test/");
let diag = map_rust_diagnostic_to_lsp(&DiagnosticsConfig::default(), &diag, workspace_root);
insta::assert_debug_snapshot!(diag);
} }
#[test] #[test]
fn snap_rustc_mismatched_type() { fn rustc_mismatched_type() {
let diag = parse_diagnostic( check(
r##"{ r##"{
"message": "mismatched types", "message": "mismatched types",
"code": { "code": {
@ -851,16 +841,13 @@ mod tests {
"children": [], "children": [],
"rendered": "error[E0308]: mismatched types\n --> runtime/compiler_support.rs:48:65\n |\n48 | let layout = alloc::Layout::from_size_align_unchecked(size, align);\n | ^^^^^ expected usize, found u32\n\n" "rendered": "error[E0308]: mismatched types\n --> runtime/compiler_support.rs:48:65\n |\n48 | let layout = alloc::Layout::from_size_align_unchecked(size, align);\n | ^^^^^ expected usize, found u32\n\n"
}"##, }"##,
expect_file!["crates/rust-analyzer/test_data/rustc_mismatched_type.txt"],
); );
let workspace_root = Path::new("/test/");
let diag = map_rust_diagnostic_to_lsp(&DiagnosticsConfig::default(), &diag, workspace_root);
insta::assert_debug_snapshot!(diag);
} }
#[test] #[test]
fn snap_handles_macro_location() { fn handles_macro_location() {
let diag = parse_diagnostic( check(
r##"{ r##"{
"rendered": "error[E0277]: can't compare `{integer}` with `&str`\n --> src/main.rs:2:5\n |\n2 | assert_eq!(1, \"love\");\n | ^^^^^^^^^^^^^^^^^^^^^^ no implementation for `{integer} == &str`\n |\n = help: the trait `std::cmp::PartialEq<&str>` is not implemented for `{integer}`\n = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)\n\n", "rendered": "error[E0277]: can't compare `{integer}` with `&str`\n --> src/main.rs:2:5\n |\n2 | assert_eq!(1, \"love\");\n | ^^^^^^^^^^^^^^^^^^^^^^ no implementation for `{integer} == &str`\n |\n = help: the trait `std::cmp::PartialEq<&str>` is not implemented for `{integer}`\n = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)\n\n",
"children": [ "children": [
@ -1122,16 +1109,13 @@ mod tests {
} }
] ]
}"##, }"##,
expect_file!["crates/rust-analyzer/test_data/handles_macro_location.txt"],
); );
let workspace_root = Path::new("/test/");
let diag = map_rust_diagnostic_to_lsp(&DiagnosticsConfig::default(), &diag, workspace_root);
insta::assert_debug_snapshot!(diag);
} }
#[test] #[test]
fn snap_macro_compiler_error() { fn macro_compiler_error() {
let diag = parse_diagnostic( check(
r##"{ r##"{
"rendered": "error: Please register your known path in the path module\n --> crates/ra_hir_def/src/path.rs:265:9\n |\n265 | compile_error!(\"Please register your known path in the path module\")\n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n | \n ::: crates/ra_hir_def/src/data.rs:80:16\n |\n80 | let path = path![std::future::Future];\n | -------------------------- in this macro invocation\n\n", "rendered": "error: Please register your known path in the path module\n --> crates/ra_hir_def/src/path.rs:265:9\n |\n265 | compile_error!(\"Please register your known path in the path module\")\n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n | \n ::: crates/ra_hir_def/src/data.rs:80:16\n |\n80 | let path = path![std::future::Future];\n | -------------------------- in this macro invocation\n\n",
"children": [], "children": [],
@ -1351,16 +1335,13 @@ mod tests {
] ]
} }
"##, "##,
expect_file!["crates/rust-analyzer/test_data/macro_compiler_error.txt"],
); );
let workspace_root = Path::new("/test/");
let diag = map_rust_diagnostic_to_lsp(&DiagnosticsConfig::default(), &diag, workspace_root);
insta::assert_debug_snapshot!(diag);
} }
#[test] #[test]
fn snap_multi_line_fix() { fn snap_multi_line_fix() {
let diag = parse_diagnostic( check(
r##"{ r##"{
"rendered": "warning: returning the result of a let binding from a block\n --> src/main.rs:4:5\n |\n3 | let a = (0..10).collect();\n | -------------------------- unnecessary let binding\n4 | a\n | ^\n |\n = note: `#[warn(clippy::let_and_return)]` on by default\n = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return\nhelp: return the expression directly\n |\n3 | \n4 | (0..10).collect()\n |\n\n", "rendered": "warning: returning the result of a let binding from a block\n --> src/main.rs:4:5\n |\n3 | let a = (0..10).collect();\n | -------------------------- unnecessary let binding\n4 | a\n | ^\n |\n = note: `#[warn(clippy::let_and_return)]` on by default\n = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return\nhelp: return the expression directly\n |\n3 | \n4 | (0..10).collect()\n |\n\n",
"children": [ "children": [
@ -1484,10 +1465,7 @@ mod tests {
] ]
} }
"##, "##,
expect_file!["crates/rust-analyzer/test_data/snap_multi_line_fix.txt"],
); );
let workspace_root = Path::new("/test/");
let diag = map_rust_diagnostic_to_lsp(&DiagnosticsConfig::default(), &diag, workspace_root);
insta::assert_debug_snapshot!(diag);
} }
} }

View file

@ -1,7 +1,3 @@
---
source: crates/rust-analyzer/src/diagnostics/to_proto.rs
expression: diag
---
[ [
MappedRustDiagnostic { MappedRustDiagnostic {
url: "file:///test/compiler/mir/tagset.rs", url: "file:///test/compiler/mir/tagset.rs",

View file

@ -1,7 +1,3 @@
---
source: crates/rust-analyzer/src/diagnostics/to_proto.rs
expression: diag
---
[ [
MappedRustDiagnostic { MappedRustDiagnostic {
url: "file:///test/src/main.rs", url: "file:///test/src/main.rs",

View file

@ -1,7 +1,3 @@
---
source: crates/rust-analyzer/src/diagnostics/to_proto.rs
expression: diag
---
[ [
MappedRustDiagnostic { MappedRustDiagnostic {
url: "file:///test/crates/ra_hir_def/src/data.rs", url: "file:///test/crates/ra_hir_def/src/data.rs",

View file

@ -1,7 +1,3 @@
---
source: crates/rust-analyzer/src/diagnostics/to_proto.rs
expression: diag
---
[ [
MappedRustDiagnostic { MappedRustDiagnostic {
url: "file:///test/compiler/ty/list_iter.rs", url: "file:///test/compiler/ty/list_iter.rs",

View file

@ -1,7 +1,3 @@
---
source: crates/rust-analyzer/src/diagnostics/to_proto.rs
expression: diag
---
[ [
MappedRustDiagnostic { MappedRustDiagnostic {
url: "file:///test/runtime/compiler_support.rs", url: "file:///test/runtime/compiler_support.rs",

View file

@ -1,7 +1,3 @@
---
source: crates/rust-analyzer/src/diagnostics/to_proto.rs
expression: diag
---
[ [
MappedRustDiagnostic { MappedRustDiagnostic {
url: "file:///test/driver/subcommand/repl.rs", url: "file:///test/driver/subcommand/repl.rs",

View file

@ -1,7 +1,3 @@
---
source: crates/rust-analyzer/src/diagnostics/to_proto.rs
expression: diag
---
[ [
MappedRustDiagnostic { MappedRustDiagnostic {
url: "file:///test/driver/subcommand/repl.rs", url: "file:///test/driver/subcommand/repl.rs",

View file

@ -1,7 +1,3 @@
---
source: crates/rust-analyzer/src/diagnostics/to_proto.rs
expression: diag
---
[ [
MappedRustDiagnostic { MappedRustDiagnostic {
url: "file:///test/driver/subcommand/repl.rs", url: "file:///test/driver/subcommand/repl.rs",

View file

@ -1,7 +1,3 @@
---
source: crates/rust-analyzer/src/diagnostics/to_proto.rs
expression: diag
---
[ [
MappedRustDiagnostic { MappedRustDiagnostic {
url: "file:///test/compiler/ty/select.rs", url: "file:///test/compiler/ty/select.rs",

View file

@ -1,7 +1,3 @@
---
source: crates/rust-analyzer/src/diagnostics/to_proto.rs
expression: diag
---
[ [
MappedRustDiagnostic { MappedRustDiagnostic {
url: "file:///test/src/main.rs", url: "file:///test/src/main.rs",