5024: Simplify r=matklad a=matklad



bors r+
🤖

5026: Disable file watching when running slow tests r=matklad a=matklad

This should rid us of the intermittent test failure

https://github.com/rust-analyzer/rust-analyzer/pull/5017#issuecomment-648717983



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-06-24 10:31:20 +00:00 committed by GitHub
commit 414b731e7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 117 deletions

View File

@ -573,10 +573,9 @@ mod tests {
impl Expr { impl Expr {
fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr { fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
Expr::Bin { <|> } Expr::Bin { }
} }
} }
"; ";
let after = r" let after = r"
enum Expr { enum Expr {
@ -585,10 +584,9 @@ mod tests {
impl Expr { impl Expr {
fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr { fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
Expr::Bin { lhs: (), rhs: () <|> } Expr::Bin { lhs: (), rhs: () }
} }
} }
"; ";
check_apply_diagnostic_fix(before, after); check_apply_diagnostic_fix(before, after);
} }

View File

@ -1,81 +1,19 @@
//! FIXME: write short doc here //! FIXME: write short doc here
use std::{str::FromStr, sync::Arc}; use std::sync::Arc;
use ra_cfg::CfgOptions; use ra_cfg::CfgOptions;
use ra_db::{CrateName, Env, FileSet, SourceRoot, VfsPath}; use ra_db::{CrateName, Env, FileSet, SourceRoot, VfsPath};
use test_utils::{extract_offset, extract_range, Fixture, CURSOR_MARKER}; use test_utils::{extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER};
use crate::{ use crate::{
Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange, Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange,
}; };
#[derive(Debug)]
enum MockFileData {
Plain { path: String, content: String },
Fixture(Fixture),
}
impl MockFileData {
fn new(path: String, content: String) -> Self {
// `Self::Plain` causes a false warning: 'variant is never constructed: `Plain` '
// see https://github.com/rust-lang/rust/issues/69018
MockFileData::Plain { path, content }
}
fn path(&self) -> &str {
match self {
MockFileData::Plain { path, .. } => path.as_str(),
MockFileData::Fixture(f) => f.path.as_str(),
}
}
fn content(&self) -> &str {
match self {
MockFileData::Plain { content, .. } => content,
MockFileData::Fixture(f) => f.text.as_str(),
}
}
fn cfg_options(&self) -> CfgOptions {
match self {
MockFileData::Fixture(f) => {
let mut cfg = CfgOptions::default();
f.cfg_atoms.iter().for_each(|it| cfg.insert_atom(it.into()));
f.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into()));
cfg
}
_ => CfgOptions::default(),
}
}
fn edition(&self) -> Edition {
match self {
MockFileData::Fixture(f) => {
f.edition.as_ref().map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap())
}
_ => Edition::Edition2018,
}
}
fn env(&self) -> Env {
match self {
MockFileData::Fixture(f) => Env::from(f.env.iter()),
_ => Env::default(),
}
}
}
impl From<Fixture> for MockFileData {
fn from(fixture: Fixture) -> Self {
Self::Fixture(fixture)
}
}
/// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis /// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis
/// from a set of in-memory files. /// from a set of in-memory files.
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct MockAnalysis { pub struct MockAnalysis {
files: Vec<MockFileData>, files: Vec<Fixture>,
} }
impl MockAnalysis { impl MockAnalysis {
@ -89,52 +27,53 @@ impl MockAnalysis {
/// //- /foo.rs /// //- /foo.rs
/// struct Baz; /// struct Baz;
/// ``` /// ```
pub fn with_files(fixture: &str) -> MockAnalysis { pub fn with_files(ra_fixture: &str) -> MockAnalysis {
let mut res = MockAnalysis::default(); let (res, pos) = MockAnalysis::with_fixture(ra_fixture);
for entry in Fixture::parse(fixture) { assert!(pos.is_none());
res.add_file_fixture(entry);
}
res res
} }
/// Same as `with_files`, but requires that a single file contains a `<|>` marker, /// Same as `with_files`, but requires that a single file contains a `<|>` marker,
/// whose position is also returned. /// whose position is also returned.
pub fn with_files_and_position(fixture: &str) -> (MockAnalysis, FilePosition) { pub fn with_files_and_position(fixture: &str) -> (MockAnalysis, FilePosition) {
let (res, position) = MockAnalysis::with_fixture(fixture);
let (file_id, range_or_offset) = position.expect("expected a marker (<|>)");
let offset = match range_or_offset {
RangeOrOffset::Range(_) => panic!(),
RangeOrOffset::Offset(it) => it,
};
(res, FilePosition { file_id, offset })
}
fn with_fixture(fixture: &str) -> (MockAnalysis, Option<(FileId, RangeOrOffset)>) {
let mut position = None; let mut position = None;
let mut res = MockAnalysis::default(); let mut res = MockAnalysis::default();
for mut entry in Fixture::parse(fixture) { for mut entry in Fixture::parse(fixture) {
if entry.text.contains(CURSOR_MARKER) { if entry.text.contains(CURSOR_MARKER) {
assert!(position.is_none(), "only one marker (<|>) per fixture is allowed"); assert!(position.is_none(), "only one marker (<|>) per fixture is allowed");
let (offset, text) = extract_offset(&entry.text); let (range_or_offset, text) = extract_range_or_offset(&entry.text);
entry.text = text; entry.text = text;
let file_id = res.add_file_fixture(entry); let file_id = res.add_file_fixture(entry);
position = Some(FilePosition { file_id, offset }); position = Some((file_id, range_or_offset));
} else { } else {
res.add_file_fixture(entry); res.add_file_fixture(entry);
} }
} }
let position = position.expect("expected a marker (<|>)");
(res, position) (res, position)
} }
fn add_file_fixture(&mut self, fixture: Fixture) -> FileId { fn add_file_fixture(&mut self, fixture: Fixture) -> FileId {
let file_id = self.next_id(); let file_id = FileId((self.files.len() + 1) as u32);
self.files.push(MockFileData::from(fixture)); self.files.push(fixture);
file_id file_id
} }
fn add_file_with_range(&mut self, path: &str, text: &str) -> FileRange {
let (range, text) = extract_range(text);
let file_id = self.next_id();
self.files.push(MockFileData::new(path.to_string(), text));
FileRange { file_id, range }
}
pub fn id_of(&self, path: &str) -> FileId { pub fn id_of(&self, path: &str) -> FileId {
let (idx, _) = self let (idx, _) = self
.files .files
.iter() .iter()
.enumerate() .enumerate()
.find(|(_, data)| path == data.path()) .find(|(_, data)| path == data.path)
.expect("no file in this mock"); .expect("no file in this mock");
FileId(idx as u32 + 1) FileId(idx as u32 + 1)
} }
@ -145,18 +84,23 @@ impl MockAnalysis {
let mut crate_graph = CrateGraph::default(); let mut crate_graph = CrateGraph::default();
let mut root_crate = None; let mut root_crate = None;
for (i, data) in self.files.into_iter().enumerate() { for (i, data) in self.files.into_iter().enumerate() {
let path = data.path(); let path = data.path;
assert!(path.starts_with('/')); assert!(path.starts_with('/'));
let cfg_options = data.cfg_options();
let mut cfg = CfgOptions::default();
data.cfg_atoms.iter().for_each(|it| cfg.insert_atom(it.into()));
data.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into()));
let edition: Edition =
data.edition.and_then(|it| it.parse().ok()).unwrap_or(Edition::Edition2018);
let file_id = FileId(i as u32 + 1); let file_id = FileId(i as u32 + 1);
let edition = data.edition(); let env = Env::from(data.env.iter());
let env = data.env();
if path == "/lib.rs" || path == "/main.rs" { if path == "/lib.rs" || path == "/main.rs" {
root_crate = Some(crate_graph.add_crate_root( root_crate = Some(crate_graph.add_crate_root(
file_id, file_id,
edition, edition,
None, None,
cfg_options, cfg,
env, env,
Default::default(), Default::default(),
)); ));
@ -167,7 +111,7 @@ impl MockAnalysis {
file_id, file_id,
edition, edition,
Some(CrateName::new(crate_name).unwrap()), Some(CrateName::new(crate_name).unwrap()),
cfg_options, cfg,
env, env,
Default::default(), Default::default(),
); );
@ -179,7 +123,7 @@ impl MockAnalysis {
} }
let path = VfsPath::new_virtual_path(path.to_string()); let path = VfsPath::new_virtual_path(path.to_string());
file_set.insert(file_id, path); file_set.insert(file_id, path);
change.change_file(file_id, Some(Arc::new(data.content().to_owned()))); change.change_file(file_id, Some(Arc::new(data.text).to_owned()));
} }
change.set_crate_graph(crate_graph); change.set_crate_graph(crate_graph);
change.set_roots(vec![SourceRoot::new_local(file_set)]); change.set_roots(vec![SourceRoot::new_local(file_set)]);
@ -189,10 +133,6 @@ impl MockAnalysis {
pub fn analysis(self) -> Analysis { pub fn analysis(self) -> Analysis {
self.analysis_host().analysis() self.analysis_host().analysis()
} }
fn next_id(&self) -> FileId {
FileId((self.files.len() + 1) as u32)
}
} }
/// Creates analysis from a multi-file fixture, returns positions marked with <|>. /// Creates analysis from a multi-file fixture, returns positions marked with <|>.
@ -209,8 +149,12 @@ pub fn single_file(ra_fixture: &str) -> (Analysis, FileId) {
} }
/// Creates analysis for a single file, returns range marked with a pair of <|>. /// Creates analysis for a single file, returns range marked with a pair of <|>.
pub fn single_file_with_range(ra_fixture: &str) -> (Analysis, FileRange) { pub fn analysis_and_range(ra_fixture: &str) -> (Analysis, FileRange) {
let mut mock = MockAnalysis::default(); let (res, position) = MockAnalysis::with_fixture(ra_fixture);
let pos = mock.add_file_with_range("/main.rs", ra_fixture); let (file_id, range_or_offset) = position.expect("expected a marker (<|>)");
(mock.analysis(), pos) let range = match range_or_offset {
RangeOrOffset::Range(it) => it,
RangeOrOffset::Offset(_) => panic!(),
};
(res.analysis(), FileRange { file_id, range })
} }

View File

@ -125,12 +125,12 @@ mod tests {
#[test] #[test]
fn test_resolve_crate_root() { fn test_resolve_crate_root() {
let mock = MockAnalysis::with_files( let mock = MockAnalysis::with_files(
" r#"
//- /bar.rs //- /bar.rs
mod foo; mod foo;
//- /foo.rs //- /foo.rs
// empty <|> // empty
", "#,
); );
let root_file = mock.id_of("/bar.rs"); let root_file = mock.id_of("/bar.rs");
let mod_file = mock.id_of("/foo.rs"); let mod_file = mock.id_of("/foo.rs");

View File

@ -104,7 +104,7 @@ fn syntax_tree_for_token(node: &SyntaxToken, text_range: TextRange) -> Option<St
mod tests { mod tests {
use test_utils::assert_eq_text; use test_utils::assert_eq_text;
use crate::mock_analysis::{single_file, single_file_with_range}; use crate::mock_analysis::{analysis_and_range, single_file};
#[test] #[test]
fn test_syntax_tree_without_range() { fn test_syntax_tree_without_range() {
@ -184,7 +184,7 @@ SOURCE_FILE@0..60
#[test] #[test]
fn test_syntax_tree_with_range() { fn test_syntax_tree_with_range() {
let (analysis, range) = single_file_with_range(r#"<|>fn foo() {}<|>"#.trim()); let (analysis, range) = analysis_and_range(r#"<|>fn foo() {}<|>"#.trim());
let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap(); let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap();
assert_eq_text!( assert_eq_text!(
@ -206,7 +206,7 @@ FN_DEF@0..11
.trim() .trim()
); );
let (analysis, range) = single_file_with_range( let (analysis, range) = analysis_and_range(
r#"fn test() { r#"fn test() {
<|>assert!(" <|>assert!("
fn foo() { fn foo() {
@ -242,7 +242,7 @@ EXPR_STMT@16..58
#[test] #[test]
fn test_syntax_tree_inside_string() { fn test_syntax_tree_inside_string() {
let (analysis, range) = single_file_with_range( let (analysis, range) = analysis_and_range(
r#"fn test() { r#"fn test() {
assert!(" assert!("
<|>fn foo() { <|>fn foo() {
@ -276,7 +276,7 @@ SOURCE_FILE@0..12
); );
// With a raw string // With a raw string
let (analysis, range) = single_file_with_range( let (analysis, range) = analysis_and_range(
r###"fn test() { r###"fn test() {
assert!(r#" assert!(r#"
<|>fn foo() { <|>fn foo() {
@ -310,7 +310,7 @@ SOURCE_FILE@0..12
); );
// With a raw string // With a raw string
let (analysis, range) = single_file_with_range( let (analysis, range) = analysis_and_range(
r###"fn test() { r###"fn test() {
assert!(r<|>#" assert!(r<|>#"
fn foo() { fn foo() {

View File

@ -19,7 +19,7 @@ use test_utils::{find_mismatch, Fixture};
use ra_project_model::ProjectManifest; use ra_project_model::ProjectManifest;
use rust_analyzer::{ use rust_analyzer::{
config::{ClientCapsConfig, Config, LinkedProject}, config::{ClientCapsConfig, Config, FilesConfig, FilesWatcher, LinkedProject},
main_loop, main_loop,
}; };
@ -90,9 +90,9 @@ impl<'a> Project<'a> {
}, },
with_sysroot: self.with_sysroot, with_sysroot: self.with_sysroot,
linked_projects, linked_projects,
files: FilesConfig { watcher: FilesWatcher::Client, exclude: Vec::new() },
..Config::default() ..Config::default()
}; };
if let Some(f) = &self.config { if let Some(f) = &self.config {
f(&mut config) f(&mut config)
} }
@ -173,8 +173,14 @@ impl Server {
self.client.sender.send(r.into()).unwrap(); self.client.sender.send(r.into()).unwrap();
while let Some(msg) = self.recv() { while let Some(msg) = self.recv() {
match msg { match msg {
Message::Request(req) if req.method == "window/workDoneProgress/create" => (), Message::Request(req) => {
Message::Request(req) => panic!("unexpected request: {:?}", req), if req.method != "window/workDoneProgress/create"
&& !(req.method == "client/registerCapability"
&& req.params.to_string().contains("workspace/didChangeWatchedFiles"))
{
panic!("unexpected request: {:?}", req)
}
}
Message::Notification(_) => (), Message::Notification(_) => (),
Message::Response(res) => { Message::Response(res) => {
assert_eq!(res.id, id); assert_eq!(res.id, id);