Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Overhaul fixture handling in tests #1190

Merged
merged 10 commits into from
Dec 18, 2018
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ rustc-workspace-hack = "1.0.0"

[dev-dependencies]
difference = "2"
tempfile = "3"

[build-dependencies]
rustc_tools_util = { git = "https://github.com/rust-lang/rust-clippy", rev = "a3c77f6ad1c1c185e561e9cd7fdec7db569169d1" }
Expand Down
39 changes: 22 additions & 17 deletions src/actions/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,16 @@ impl IsWithin for Range {

/// Tests for formatted messages from the compilers json output
/// run cargo with `--message-format=json` to generate the json for new tests and add .json
/// message files to '../../test_data/compiler_message/'
/// message files to '$(crate::test::FIXTURES_DIR)/compiler_message/'
#[cfg(test)]
mod diagnostic_message_test {
use super::*;
use languageserver_types::Position;
pub(super) use crate::test::FIXTURES_DIR;

pub(super) fn read_fixture(path: impl AsRef<Path>) -> String {
std::fs::read_to_string(FIXTURES_DIR.join(path.as_ref())).unwrap()
}

pub(super) fn parse_compiler_message(
compiler_message: &str,
Expand Down Expand Up @@ -413,7 +418,7 @@ mod diagnostic_message_test {
#[test]
fn message_use_after_move() {
let diag = parse_compiler_message(
include_str!("../../test_data/compiler_message/use-after-move.json"),
&read_fixture("compiler_message/use-after-move.json"),
true,
);

Expand Down Expand Up @@ -443,7 +448,7 @@ mod diagnostic_message_test {
#[test]
fn message_type_annotations_needed() {
let messages = parse_compiler_message(
include_str!("../../test_data/compiler_message/type-annotations-needed.json"),
&read_fixture("compiler_message/type-annotations-needed.json"),
true,
).to_messages();
assert_eq!(
Expand All @@ -460,7 +465,7 @@ mod diagnostic_message_test {
// Check if we don't emit related information if it's not supported and
// if secondary spans are emitted as separate diagnostics
let messages = parse_compiler_message(
include_str!("../../test_data/compiler_message/type-annotations-needed.json"),
&read_fixture("compiler_message/type-annotations-needed.json"),
false,
);

Expand All @@ -486,7 +491,7 @@ mod diagnostic_message_test {
#[test]
fn message_mismatched_types() {
let messages = parse_compiler_message(
include_str!("../../test_data/compiler_message/mismatched-types.json"),
&read_fixture("compiler_message/mismatched-types.json"),
true,
).to_messages();
assert_eq!(
Expand All @@ -513,7 +518,7 @@ mod diagnostic_message_test {
#[test]
fn message_not_mutable() {
let messages = parse_compiler_message(
include_str!("../../test_data/compiler_message/not-mut.json"),
&read_fixture("compiler_message/not-mut.json"),
true,
).to_messages();
assert_eq!(
Expand Down Expand Up @@ -542,7 +547,7 @@ mod diagnostic_message_test {
#[test]
fn message_consider_borrowing() {
let messages = parse_compiler_message(
include_str!("../../test_data/compiler_message/consider-borrowing.json"),
&read_fixture("compiler_message/consider-borrowing.json"),
true,
).to_messages();
assert_eq!(
Expand Down Expand Up @@ -573,7 +578,7 @@ help: consider borrowing here: `&string`"#,
#[test]
fn message_move_out_of_borrow() {
let messages = parse_compiler_message(
include_str!("../../test_data/compiler_message/move-out-of-borrow.json"),
&read_fixture("compiler_message/move-out-of-borrow.json"),
true,
).to_messages();
assert_eq!(
Expand All @@ -596,7 +601,7 @@ help: consider borrowing here: `&string`"#,
#[test]
fn message_unused_use() {
let messages = parse_compiler_message(
include_str!("../../test_data/compiler_message/unused-use.json"),
&read_fixture("compiler_message/unused-use.json"),
true,
).to_messages();

Expand All @@ -616,7 +621,7 @@ help: consider borrowing here: `&string`"#,
#[test]
fn message_cannot_find_type() {
let messages = parse_compiler_message(
include_str!("../../test_data/compiler_message/cannot-find-type.json"),
&read_fixture("compiler_message/cannot-find-type.json"),
true,
).to_messages();
assert_eq!(
Expand All @@ -634,7 +639,7 @@ help: consider borrowing here: `&string`"#,
#[test]
fn message_clippy_identity_op() {
let diag = parse_compiler_message(
include_str!("../../test_data/compiler_message/clippy-identity-op.json"),
&read_fixture("compiler_message/clippy-identity-op.json"),
true,
);

Expand Down Expand Up @@ -674,7 +679,7 @@ help: consider borrowing here: `&string`"#,
#[test]
fn macro_error_no_trait() {
let diag = parse_compiler_message(
include_str!("../../test_data/compiler_message/macro-error-no-trait.json"),
&read_fixture("compiler_message/macro-error-no-trait.json"),
true,
);
assert_eq!(diag.diagnostics.len(), 1, "{:#?}", diag.diagnostics);
Expand Down Expand Up @@ -717,7 +722,7 @@ help: consider borrowing here: `&string`"#,
#[test]
fn macro_expected_token_nested_expansion() {
let diag = parse_compiler_message(
include_str!("../../test_data/compiler_message/macro-expected-token.json"),
&read_fixture("compiler_message/macro-expected-token.json"),
true,
);
assert_eq!(diag.diagnostics.len(), 1, "{:#?}", diag.diagnostics);
Expand Down Expand Up @@ -756,7 +761,7 @@ mod diagnostic_suggestion_test {
#[test]
fn suggest_use_when_cannot_find_type() {
let diag = parse_compiler_message(
include_str!("../../test_data/compiler_message/cannot-find-type.json"),
&read_fixture("compiler_message/cannot-find-type.json"),
true,
);

Expand Down Expand Up @@ -787,7 +792,7 @@ mod diagnostic_suggestion_test {
#[test]
fn suggest_mut_when_not_mut() {
let diag = parse_compiler_message(
include_str!("../../test_data/compiler_message/not-mut.json"),
&read_fixture("compiler_message/not-mut.json"),
true,
);

Expand Down Expand Up @@ -818,7 +823,7 @@ mod diagnostic_suggestion_test {
#[test]
fn suggest_clippy_const_static() {
let diag = parse_compiler_message(
include_str!("../../test_data/compiler_message/clippy-const-static-lifetime.json"),
&read_fixture("compiler_message/clippy-const-static-lifetime.json"),
true,
);

Expand Down Expand Up @@ -846,7 +851,7 @@ mod diagnostic_suggestion_test {
#[test]
fn suggest_macro_error_no_trait() {
let diag = parse_compiler_message(
include_str!("../../test_data/compiler_message/macro-error-no-trait.json"),
&read_fixture("compiler_message/macro-error-no-trait.json"),
true,
);
let diagnostics = diag.diagnostics.values().nth(0).unwrap();
Expand Down
49 changes: 25 additions & 24 deletions src/actions/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@ pub mod test {
use crate::lsp_data::{ClientCapabilities, InitializationOptions};
use crate::lsp_data::{Position, TextDocumentIdentifier, TextDocumentPositionParams};
use crate::server::{Output, RequestId};
use crate::test::FIXTURES_DIR;
use rls_analysis as analysis;
use serde_derive::{Deserialize, Serialize};
use serde_json as json;
Expand All @@ -991,7 +992,7 @@ pub mod test {

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct Test {
/// Relative to the project _source_ dir (e.g. relative to test_data/hover/src)
/// Relative to the project _source_ dir (e.g. relative to FIXTURES_DIR/hover/src)
pub file: String,
/// One-based line number
pub line: u64,
Expand Down Expand Up @@ -1672,67 +1673,67 @@ pub mod test {
#[test]
fn test_extract_decl() {
let vfs = Vfs::new();
let file = Path::new("test_data/hover/src/test_extract_decl.rs");
let file = FIXTURES_DIR.join("hover/src/test_extract_decl.rs");

let expected = "pub fn foo() -> Foo<u32>";
let row_start = Row::new_zero_indexed(10);
let actual = extract_decl(&vfs, file, row_start)
let actual = extract_decl(&vfs, &file, row_start)
.expect("function declaration")
.join("\n");
assert_eq!(expected, actual);

let expected = "pub struct Foo<T>";
let row_start = Row::new_zero_indexed(15);
let actual = extract_decl(&vfs, file, row_start)
let actual = extract_decl(&vfs, &file, row_start)
.expect("struct declaration")
.join("\n");
assert_eq!(expected, actual);

let expected = "pub enum Bar";
let row_start = Row::new_zero_indexed(20);
let actual = extract_decl(&vfs, file, row_start)
let actual = extract_decl(&vfs, &file, row_start)
.expect("enum declaration")
.join("\n");
assert_eq!(expected, actual);

let expected = "pub struct NewType(pub u32, f32)";
let row_start = Row::new_zero_indexed(25);
let actual = extract_decl(&vfs, file, row_start)
let actual = extract_decl(&vfs, &file, row_start)
.expect("tuple declaration")
.join("\n");
assert_eq!(expected, actual);

let expected = "pub fn new() -> NewType";
let row_start = Row::new_zero_indexed(28);
let actual = extract_decl(&vfs, file, row_start)
let actual = extract_decl(&vfs, &file, row_start)
.expect("struct function declaration")
.join("\n");
assert_eq!(expected, actual);

let expected = "pub fn bar<T: Copy + Add>(&self, the_really_long_name_string: String, the_really_long_name_foo: Foo<T>) -> Vec<(String, Foo<T>)>";
let row_start = Row::new_zero_indexed(32);
let actual = extract_decl(&vfs, file, row_start)
let actual = extract_decl(&vfs, &file, row_start)
.expect("long struct method declaration with generics")
.join("\n");
assert_eq!(expected, actual);

let expected = "pub trait Baz<T> where T: Copy";
let row_start = Row::new_zero_indexed(37);
let actual = extract_decl(&vfs, file, row_start)
let actual = extract_decl(&vfs, &file, row_start)
.expect("enum declaration")
.join("\n");
assert_eq!(expected, actual);

let expected = "fn make_copy(&self) -> Self";
let row_start = Row::new_zero_indexed(38);
let actual = extract_decl(&vfs, file, row_start)
let actual = extract_decl(&vfs, &file, row_start)
.expect("trait method declaration")
.join("\n");
assert_eq!(expected, actual);

let expected = "fn make_copy(&self) -> Self";
let row_start = Row::new_zero_indexed(42);
let actual = extract_decl(&vfs, file, row_start)
let actual = extract_decl(&vfs, &file, row_start)
.expect("trait method implementation")
.join("\n");
assert_eq!(expected, actual);
Expand All @@ -1745,7 +1746,7 @@ pub mod test {
",
);
let row_start = Row::new_zero_indexed(47);
let actual = extract_decl(&vfs, file, row_start)
let actual = extract_decl(&vfs, &file, row_start)
.expect("trait declaration multiline")
.join("\n");
assert_eq!(expected, actual);
Expand All @@ -1759,7 +1760,7 @@ pub mod test {
",
);
let row_start = Row::new_zero_indexed(53);
let actual = extract_decl(&vfs, file, row_start)
let actual = extract_decl(&vfs, &file, row_start)
.expect("function declaration multiline")
.join("\n");
assert_eq!(expected, actual);
Expand Down Expand Up @@ -1837,7 +1838,7 @@ pub mod test {
#[test]
fn test_extract_decl_multiline_empty_function() {
let vfs = Vfs::new();
let file = Path::new("test_data/hover/src/test_extract_decl_multiline_empty_function.rs");
let file = FIXTURES_DIR.join("hover/src/test_extract_decl_multiline_empty_function.rs");

let expected = noindent(
"
Expand All @@ -1850,7 +1851,7 @@ pub mod test {
",
);
let row_start = Row::new_zero_indexed(21);
let actual = extract_decl(&vfs, file, row_start)
let actual = extract_decl(&vfs, &file, row_start)
.expect("the empty body should not be extracted")
.join("\n");
assert_eq!(expected, actual);
Expand All @@ -1859,7 +1860,7 @@ pub mod test {
#[test]
fn test_extract_docs_module_docs_with_attribute() {
let vfs = Vfs::new();
let file = Path::new("test_data/hover/src/test_extract_docs_module_docs_with_attribute.rs");
let file = FIXTURES_DIR.join("hover/src/test_extract_docs_module_docs_with_attribute.rs");
let row_start = Row::new_zero_indexed(0);
let actual = extract_docs(&vfs, &file, row_start)
.expect(&format!("failed to extract docs: {:?}", file))
Expand All @@ -1883,7 +1884,7 @@ pub mod test {
#[test]
fn test_extract_docs_module_docs_no_copyright() {
let vfs = Vfs::new();
let file = Path::new("test_data/hover/src/test_extract_docs_module_docs_no_copyright.rs");
let file = FIXTURES_DIR.join("hover/src/test_extract_docs_module_docs_no_copyright.rs");
let row_start = Row::new_zero_indexed(0);
let actual = extract_docs(&vfs, &file, row_start)
.expect(&format!("failed to extract docs: {:?}", file))
Expand All @@ -1907,7 +1908,7 @@ pub mod test {
#[test]
fn test_extract_docs_comment_block() {
let vfs = Vfs::new();
let file = Path::new("test_data/hover/src/test_extract_docs_comment_block.rs");
let file = FIXTURES_DIR.join("hover/src/test_extract_docs_comment_block.rs");
let row_start = Row::new_zero_indexed(21);
let actual = extract_docs(&vfs, &file, row_start)
.expect(&format!("failed to extract docs: {:?}", file))
Expand All @@ -1931,7 +1932,7 @@ pub mod test {
#[test]
fn test_extract_docs_empty_line_before_decl() {
let vfs = Vfs::new();
let file = Path::new("test_data/hover/src/test_extract_docs_empty_line_before_decl.rs");
let file = FIXTURES_DIR.join("hover/src/test_extract_docs_empty_line_before_decl.rs");
let row_start = Row::new_zero_indexed(18);
let actual = extract_docs(&vfs, &file, row_start)
.expect(&format!("failed to extract docs: {:?}", file))
Expand All @@ -1955,7 +1956,7 @@ pub mod test {
#[test]
fn test_extract_docs_module_docs() {
let vfs = Vfs::new();
let file = Path::new("test_data/hover/src/test_extract_docs_module_docs.rs");
let file = FIXTURES_DIR.join("hover/src/test_extract_docs_module_docs.rs");

let row_start = Row::new_zero_indexed(0);
let actual = extract_docs(&vfs, &file, row_start)
Expand Down Expand Up @@ -1995,7 +1996,7 @@ pub mod test {
#[test]
fn test_extract_docs_attributes() {
let vfs = Vfs::new();
let file = Path::new("test_data/hover/src/test_extract_docs_attributes.rs");
let file = FIXTURES_DIR.join("hover/src/test_extract_docs_attributes.rs");

let row_start = Row::new_zero_indexed(21);
let actual = extract_docs(&vfs, &file, row_start)
Expand Down Expand Up @@ -2039,7 +2040,7 @@ pub mod test {
#[test]
fn test_extract_docs_comment_first_line() {
let vfs = Vfs::new();
let file = Path::new("test_data/hover/src/test_extract_docs_comment_first_line.rs");
let file = FIXTURES_DIR.join("hover/src/test_extract_docs_comment_first_line.rs");

let row_start = Row::new_zero_indexed(1);
let actual = extract_docs(&vfs, &file, row_start)
Expand Down Expand Up @@ -2113,7 +2114,7 @@ pub mod test {

let cwd = env::current_dir()?;
let out = LineOutput::default();
let proj_dir = cwd.join("test_data").join("hover");
let proj_dir = FIXTURES_DIR.join("hover");
let save_dir = cwd
.join("target")
.join("tests")
Expand Down Expand Up @@ -2168,7 +2169,7 @@ pub mod test {

let cwd = env::current_dir()?;
let out = LineOutput::default();
let proj_dir = cwd.join("test_data").join("hover");
let proj_dir = FIXTURES_DIR.join("hover");
let save_dir = cwd
.join("target")
.join("tests")
Expand Down
Loading