From c0fa5e2246457df10e92c2e11c971f2f40921793 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Feb 2020 12:33:16 +0100 Subject: Rename the binary to rust-analyzer --- crates/ra_lsp_server/tests/heavy_tests/main.rs | 2 +- crates/ra_lsp_server/tests/heavy_tests/support.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_lsp_server/tests') diff --git a/crates/ra_lsp_server/tests/heavy_tests/main.rs b/crates/ra_lsp_server/tests/heavy_tests/main.rs index 9ca31cbcc..3af63d9cf 100644 --- a/crates/ra_lsp_server/tests/heavy_tests/main.rs +++ b/crates/ra_lsp_server/tests/heavy_tests/main.rs @@ -7,7 +7,7 @@ use lsp_types::{ PartialResultParams, Position, Range, TextDocumentItem, TextDocumentPositionParams, WorkDoneProgressParams, }; -use ra_lsp_server::req::{ +use rust_analyzer::req::{ CodeActionParams, CodeActionRequest, Completion, CompletionParams, DidOpenTextDocument, Formatting, OnEnter, Runnables, RunnablesParams, }; diff --git a/crates/ra_lsp_server/tests/heavy_tests/support.rs b/crates/ra_lsp_server/tests/heavy_tests/support.rs index d5ea52fa9..5b90b3218 100644 --- a/crates/ra_lsp_server/tests/heavy_tests/support.rs +++ b/crates/ra_lsp_server/tests/heavy_tests/support.rs @@ -19,7 +19,7 @@ use serde_json::{to_string_pretty, Value}; use tempfile::TempDir; use test_utils::{find_mismatch, parse_fixture}; -use ra_lsp_server::{main_loop, req, ServerConfig}; +use rust_analyzer::{main_loop, req, ServerConfig}; pub struct Project<'a> { fixture: &'a str, -- cgit v1.2.3 From 865759925be6b72f7ef39124ed0e4c86c0412a69 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Feb 2020 12:37:45 +0100 Subject: Rename folder --- crates/ra_lsp_server/tests/heavy_tests/main.rs | 582 ---------------------- crates/ra_lsp_server/tests/heavy_tests/support.rs | 254 ---------- 2 files changed, 836 deletions(-) delete mode 100644 crates/ra_lsp_server/tests/heavy_tests/main.rs delete mode 100644 crates/ra_lsp_server/tests/heavy_tests/support.rs (limited to 'crates/ra_lsp_server/tests') diff --git a/crates/ra_lsp_server/tests/heavy_tests/main.rs b/crates/ra_lsp_server/tests/heavy_tests/main.rs deleted file mode 100644 index 3af63d9cf..000000000 --- a/crates/ra_lsp_server/tests/heavy_tests/main.rs +++ /dev/null @@ -1,582 +0,0 @@ -mod support; - -use std::{collections::HashMap, time::Instant}; - -use lsp_types::{ - CodeActionContext, DidOpenTextDocumentParams, DocumentFormattingParams, FormattingOptions, - PartialResultParams, Position, Range, TextDocumentItem, TextDocumentPositionParams, - WorkDoneProgressParams, -}; -use rust_analyzer::req::{ - CodeActionParams, CodeActionRequest, Completion, CompletionParams, DidOpenTextDocument, - Formatting, OnEnter, Runnables, RunnablesParams, -}; -use serde_json::json; -use tempfile::TempDir; -use test_utils::skip_slow_tests; - -use crate::support::{project, Project}; - -const PROFILE: &'static str = ""; -// const PROFILE: &'static str = "*@3>100"; - -#[test] -fn completes_items_from_standard_library() { - if skip_slow_tests() { - return; - } - - let project_start = Instant::now(); - let server = Project::with_fixture( - r#" -//- Cargo.toml -[package] -name = "foo" -version = "0.0.0" - -//- src/lib.rs -use std::collections::Spam; -"#, - ) - .with_sysroot(true) - .server(); - server.wait_until_workspace_is_loaded(); - eprintln!("loading took {:?}", project_start.elapsed()); - let completion_start = Instant::now(); - let res = server.send_request::(CompletionParams { - text_document_position: TextDocumentPositionParams::new( - server.doc_id("src/lib.rs"), - Position::new(0, 23), - ), - context: None, - partial_result_params: PartialResultParams::default(), - work_done_progress_params: WorkDoneProgressParams::default(), - }); - assert!(format!("{}", res).contains("HashMap")); - eprintln!("completion took {:?}", completion_start.elapsed()); -} - -#[test] -fn test_runnables_no_project() { - if skip_slow_tests() { - return; - } - - let server = project( - r" -//- lib.rs -#[test] -fn foo() { -} -", - ); - server.wait_until_workspace_is_loaded(); - server.request::( - RunnablesParams { text_document: server.doc_id("lib.rs"), position: None }, - json!([ - { - "args": [ "test", "--", "foo", "--nocapture" ], - "bin": "cargo", - "env": { "RUST_BACKTRACE": "short" }, - "cwd": null, - "label": "test foo", - "range": { - "end": { "character": 1, "line": 2 }, - "start": { "character": 0, "line": 0 } - } - }, - { - "args": [ - "check", - "--all" - ], - "bin": "cargo", - "env": {}, - "cwd": null, - "label": "cargo check --all", - "range": { - "end": { - "character": 0, - "line": 0 - }, - "start": { - "character": 0, - "line": 0 - } - } - } - ]), - ); -} - -#[test] -fn test_runnables_project() { - if skip_slow_tests() { - return; - } - - let code = r#" -//- foo/Cargo.toml -[package] -name = "foo" -version = "0.0.0" - -//- foo/src/lib.rs -pub fn foo() {} - -//- foo/tests/spam.rs -#[test] -fn test_eggs() {} - -//- bar/Cargo.toml -[package] -name = "bar" -version = "0.0.0" - -//- bar/src/main.rs -fn main() {} -"#; - - let server = Project::with_fixture(code).root("foo").root("bar").server(); - - server.wait_until_workspace_is_loaded(); - server.request::( - RunnablesParams { - text_document: server.doc_id("foo/tests/spam.rs"), - position: None, - }, - json!([ - { - "args": [ "test", "--package", "foo", "--test", "spam", "--", "test_eggs", "--exact", "--nocapture" ], - "bin": "cargo", - "env": { "RUST_BACKTRACE": "short" }, - "label": "test test_eggs", - "range": { - "end": { "character": 17, "line": 1 }, - "start": { "character": 0, "line": 0 } - }, - "cwd": server.path().join("foo") - }, - { - "args": [ - "check", - "--package", - "foo", - "--test", - "spam" - ], - "bin": "cargo", - "env": {}, - "cwd": server.path().join("foo"), - "label": "cargo check -p foo", - "range": { - "end": { - "character": 0, - "line": 0 - }, - "start": { - "character": 0, - "line": 0 - } - } - } - ]) - ); -} - -#[test] -fn test_format_document() { - if skip_slow_tests() { - return; - } - - let server = project( - r#" -//- Cargo.toml -[package] -name = "foo" -version = "0.0.0" - -//- src/lib.rs -mod bar; - -fn main() { -} - -pub use std::collections::HashMap; -"#, - ); - server.wait_until_workspace_is_loaded(); - - server.request::( - DocumentFormattingParams { - text_document: server.doc_id("src/lib.rs"), - options: FormattingOptions { - tab_size: 4, - insert_spaces: false, - insert_final_newline: None, - trim_final_newlines: None, - trim_trailing_whitespace: None, - properties: HashMap::new(), - }, - work_done_progress_params: WorkDoneProgressParams::default(), - }, - json!([ - { - "newText": r#"mod bar; - -fn main() {} - -pub use std::collections::HashMap; -"#, - "range": { - "end": { - "character": 0, - "line": 7 - }, - "start": { - "character": 0, - "line": 0 - } - } - } - ]), - ); -} - -#[test] -fn test_format_document_2018() { - if skip_slow_tests() { - return; - } - - let server = project( - r#" -//- Cargo.toml -[package] -name = "foo" -version = "0.0.0" -edition = "2018" - -//- src/lib.rs -mod bar; - -async fn test() { -} - -fn main() { -} - -pub use std::collections::HashMap; -"#, - ); - server.wait_until_workspace_is_loaded(); - - server.request::( - DocumentFormattingParams { - text_document: server.doc_id("src/lib.rs"), - options: FormattingOptions { - tab_size: 4, - insert_spaces: false, - properties: HashMap::new(), - insert_final_newline: None, - trim_final_newlines: None, - trim_trailing_whitespace: None, - }, - work_done_progress_params: WorkDoneProgressParams::default(), - }, - json!([ - { - "newText": r#"mod bar; - -async fn test() {} - -fn main() {} - -pub use std::collections::HashMap; -"#, - "range": { - "end": { - "character": 0, - "line": 10 - }, - "start": { - "character": 0, - "line": 0 - } - } - } - ]), - ); -} - -#[test] -fn test_missing_module_code_action() { - if skip_slow_tests() { - return; - } - - let server = project( - r#" -//- Cargo.toml -[package] -name = "foo" -version = "0.0.0" - -//- src/lib.rs -mod bar; - -fn main() {} -"#, - ); - server.wait_until_workspace_is_loaded(); - let empty_context = || CodeActionContext { diagnostics: Vec::new(), only: None }; - server.request::( - CodeActionParams { - text_document: server.doc_id("src/lib.rs"), - range: Range::new(Position::new(0, 4), Position::new(0, 7)), - context: empty_context(), - partial_result_params: PartialResultParams::default(), - work_done_progress_params: WorkDoneProgressParams::default(), - }, - json!([ - { - "command": { - "arguments": [ - { - "cursorPosition": null, - "label": "create module", - "workspaceEdit": { - "documentChanges": [ - { - "kind": "create", - "uri": "file:///[..]/src/bar.rs" - } - ] - } - } - ], - "command": "rust-analyzer.applySourceChange", - "title": "create module" - }, - "title": "create module" - } - ]), - ); - - server.request::( - CodeActionParams { - text_document: server.doc_id("src/lib.rs"), - range: Range::new(Position::new(2, 4), Position::new(2, 7)), - context: empty_context(), - partial_result_params: PartialResultParams::default(), - work_done_progress_params: WorkDoneProgressParams::default(), - }, - json!([]), - ); -} - -#[test] -fn test_missing_module_code_action_in_json_project() { - if skip_slow_tests() { - return; - } - - let tmp_dir = TempDir::new().unwrap(); - - let path = tmp_dir.path(); - - let project = json!({ - "roots": [path], - "crates": [ { - "root_module": path.join("src/lib.rs"), - "deps": [], - "edition": "2015", - "atom_cfgs": [], - "key_value_cfgs": {} - } ] - }); - - let code = format!( - r#" -//- rust-project.json -{PROJECT} - -//- src/lib.rs -mod bar; - -fn main() {{}} -"#, - PROJECT = project.to_string(), - ); - - let server = Project::with_fixture(&code).tmp_dir(tmp_dir).server(); - - server.wait_until_workspace_is_loaded(); - let empty_context = || CodeActionContext { diagnostics: Vec::new(), only: None }; - server.request::( - CodeActionParams { - text_document: server.doc_id("src/lib.rs"), - range: Range::new(Position::new(0, 4), Position::new(0, 7)), - context: empty_context(), - partial_result_params: PartialResultParams::default(), - work_done_progress_params: WorkDoneProgressParams::default(), - }, - json!([ - { - "command": { - "arguments": [ - { - "cursorPosition": null, - "label": "create module", - "workspaceEdit": { - "documentChanges": [ - { - "kind": "create", - "uri": "file:///[..]/src/bar.rs" - } - ] - } - } - ], - "command": "rust-analyzer.applySourceChange", - "title": "create module" - }, - "title": "create module" - } - ]), - ); - - server.request::( - CodeActionParams { - text_document: server.doc_id("src/lib.rs"), - range: Range::new(Position::new(2, 4), Position::new(2, 7)), - context: empty_context(), - partial_result_params: PartialResultParams::default(), - work_done_progress_params: WorkDoneProgressParams::default(), - }, - json!([]), - ); -} - -#[test] -fn diagnostics_dont_block_typing() { - if skip_slow_tests() { - return; - } - - let librs: String = (0..10).map(|i| format!("mod m{};", i)).collect(); - let libs: String = (0..10).map(|i| format!("//- src/m{}.rs\nfn foo() {{}}\n\n", i)).collect(); - let server = Project::with_fixture(&format!( - r#" -//- Cargo.toml -[package] -name = "foo" -version = "0.0.0" - -//- src/lib.rs -{} - -{} - -fn main() {{}} -"#, - librs, libs - )) - .with_sysroot(true) - .server(); - - server.wait_until_workspace_is_loaded(); - for i in 0..10 { - server.notification::(DidOpenTextDocumentParams { - text_document: TextDocumentItem { - uri: server.doc_id(&format!("src/m{}.rs", i)).uri, - language_id: "rust".to_string(), - version: 0, - text: "/// Docs\nfn foo() {}".to_string(), - }, - }); - } - let start = std::time::Instant::now(); - server.request::( - TextDocumentPositionParams { - text_document: server.doc_id("src/m0.rs"), - position: Position { line: 0, character: 5 }, - }, - json!({ - "cursorPosition": { - "position": { "character": 4, "line": 1 }, - "textDocument": { "uri": "file:///[..]src/m0.rs" } - }, - "label": "on enter", - "workspaceEdit": { - "documentChanges": [ - { - "edits": [ - { - "newText": "\n/// ", - "range": { - "end": { "character": 5, "line": 0 }, - "start": { "character": 5, "line": 0 } - } - } - ], - "textDocument": { "uri": "file:///[..]src/m0.rs", "version": null } - } - ] - } - }), - ); - let elapsed = start.elapsed(); - assert!(elapsed.as_millis() < 2000, "typing enter took {:?}", elapsed); -} - -#[test] -fn preserves_dos_line_endings() { - if skip_slow_tests() { - return; - } - - let server = Project::with_fixture( - &" -//- Cargo.toml -[package] -name = \"foo\" -version = \"0.0.0\" - -//- src/main.rs -/// Some Docs\r\nfn main() {} -", - ) - .server(); - - server.request::( - TextDocumentPositionParams { - text_document: server.doc_id("src/main.rs"), - position: Position { line: 0, character: 8 }, - }, - json!({ - "cursorPosition": { - "position": { "line": 1, "character": 4 }, - "textDocument": { "uri": "file:///[..]src/main.rs" } - }, - "label": "on enter", - "workspaceEdit": { - "documentChanges": [ - { - "edits": [ - { - "newText": "\r\n/// ", - "range": { - "end": { "line": 0, "character": 8 }, - "start": { "line": 0, "character": 8 } - } - } - ], - "textDocument": { "uri": "file:///[..]src/main.rs", "version": null } - } - ] - } - }), - ); -} diff --git a/crates/ra_lsp_server/tests/heavy_tests/support.rs b/crates/ra_lsp_server/tests/heavy_tests/support.rs deleted file mode 100644 index 5b90b3218..000000000 --- a/crates/ra_lsp_server/tests/heavy_tests/support.rs +++ /dev/null @@ -1,254 +0,0 @@ -use std::{ - cell::{Cell, RefCell}, - fs, - path::{Path, PathBuf}, - sync::Once, - time::Duration, -}; - -use crossbeam_channel::{after, select, Receiver}; -use lsp_server::{Connection, Message, Notification, Request}; -use lsp_types::{ - notification::{DidOpenTextDocument, Exit}, - request::Shutdown, - ClientCapabilities, DidOpenTextDocumentParams, GotoCapability, TextDocumentClientCapabilities, - TextDocumentIdentifier, TextDocumentItem, Url, -}; -use serde::Serialize; -use serde_json::{to_string_pretty, Value}; -use tempfile::TempDir; -use test_utils::{find_mismatch, parse_fixture}; - -use rust_analyzer::{main_loop, req, ServerConfig}; - -pub struct Project<'a> { - fixture: &'a str, - with_sysroot: bool, - tmp_dir: Option, - roots: Vec, -} - -impl<'a> Project<'a> { - pub fn with_fixture(fixture: &str) -> Project { - Project { fixture, tmp_dir: None, roots: vec![], with_sysroot: false } - } - - pub fn tmp_dir(mut self, tmp_dir: TempDir) -> Project<'a> { - self.tmp_dir = Some(tmp_dir); - self - } - - pub fn root(mut self, path: &str) -> Project<'a> { - self.roots.push(path.into()); - self - } - - pub fn with_sysroot(mut self, sysroot: bool) -> Project<'a> { - self.with_sysroot = sysroot; - self - } - - pub fn server(self) -> Server { - let tmp_dir = self.tmp_dir.unwrap_or_else(|| TempDir::new().unwrap()); - static INIT: Once = Once::new(); - INIT.call_once(|| { - let _ = env_logger::builder().is_test(true).try_init().unwrap(); - ra_prof::set_filter(if crate::PROFILE.is_empty() { - ra_prof::Filter::disabled() - } else { - ra_prof::Filter::from_spec(&crate::PROFILE) - }); - }); - - let mut paths = vec![]; - - for entry in parse_fixture(self.fixture) { - let path = tmp_dir.path().join(entry.meta); - fs::create_dir_all(path.parent().unwrap()).unwrap(); - fs::write(path.as_path(), entry.text.as_bytes()).unwrap(); - paths.push((path, entry.text)); - } - - let roots = self.roots.into_iter().map(|root| tmp_dir.path().join(root)).collect(); - - Server::new(tmp_dir, self.with_sysroot, roots, paths) - } -} - -pub fn project(fixture: &str) -> Server { - Project::with_fixture(fixture).server() -} - -pub struct Server { - req_id: Cell, - messages: RefCell>, - dir: TempDir, - _thread: jod_thread::JoinHandle<()>, - client: Connection, -} - -impl Server { - fn new( - dir: TempDir, - with_sysroot: bool, - roots: Vec, - files: Vec<(PathBuf, String)>, - ) -> Server { - let path = dir.path().to_path_buf(); - - let roots = if roots.is_empty() { vec![path] } else { roots }; - let (connection, client) = Connection::memory(); - - let _thread = jod_thread::Builder::new() - .name("test server".to_string()) - .spawn(move || { - main_loop( - roots, - ClientCapabilities { - workspace: None, - text_document: Some(TextDocumentClientCapabilities { - definition: Some(GotoCapability { - dynamic_registration: None, - link_support: Some(true), - }), - ..Default::default() - }), - window: None, - experimental: None, - }, - ServerConfig { with_sysroot, ..ServerConfig::default() }, - connection, - ) - .unwrap() - }) - .expect("failed to spawn a thread"); - - let res = - Server { req_id: Cell::new(1), dir, messages: Default::default(), client, _thread }; - - for (path, text) in files { - res.notification::(DidOpenTextDocumentParams { - text_document: TextDocumentItem { - uri: Url::from_file_path(path).unwrap(), - language_id: "rust".to_string(), - version: 0, - text, - }, - }) - } - res - } - - pub fn doc_id(&self, rel_path: &str) -> TextDocumentIdentifier { - let path = self.dir.path().join(rel_path); - TextDocumentIdentifier { uri: Url::from_file_path(path).unwrap() } - } - - pub fn notification(&self, params: N::Params) - where - N: lsp_types::notification::Notification, - N::Params: Serialize, - { - let r = Notification::new(N::METHOD.to_string(), params); - self.send_notification(r) - } - - pub fn request(&self, params: R::Params, expected_resp: Value) - where - R: lsp_types::request::Request, - R::Params: Serialize, - { - let actual = self.send_request::(params); - if let Some((expected_part, actual_part)) = find_mismatch(&expected_resp, &actual) { - panic!( - "JSON mismatch\nExpected:\n{}\nWas:\n{}\nExpected part:\n{}\nActual part:\n{}\n", - to_string_pretty(&expected_resp).unwrap(), - to_string_pretty(&actual).unwrap(), - to_string_pretty(expected_part).unwrap(), - to_string_pretty(actual_part).unwrap(), - ); - } - } - - pub fn send_request(&self, params: R::Params) -> Value - where - R: lsp_types::request::Request, - R::Params: Serialize, - { - let id = self.req_id.get(); - self.req_id.set(id + 1); - - let r = Request::new(id.into(), R::METHOD.to_string(), params); - self.send_request_(r) - } - fn send_request_(&self, r: Request) -> Value { - let id = r.id.clone(); - self.client.sender.send(r.into()).unwrap(); - while let Some(msg) = self.recv() { - match msg { - Message::Request(req) => panic!("unexpected request: {:?}", req), - Message::Notification(_) => (), - Message::Response(res) => { - assert_eq!(res.id, id); - if let Some(err) = res.error { - panic!("error response: {:#?}", err); - } - return res.result.unwrap(); - } - } - } - panic!("no response"); - } - pub fn wait_until_workspace_is_loaded(&self) { - self.wait_for_message_cond(1, &|msg: &Message| match msg { - Message::Notification(n) if n.method == "window/showMessage" => { - let msg = - n.clone().extract::("window/showMessage").unwrap(); - msg.message.starts_with("workspace loaded") - } - _ => false, - }) - } - fn wait_for_message_cond(&self, n: usize, cond: &dyn Fn(&Message) -> bool) { - let mut total = 0; - for msg in self.messages.borrow().iter() { - if cond(msg) { - total += 1 - } - } - while total < n { - let msg = self.recv().expect("no response"); - if cond(&msg) { - total += 1; - } - } - } - fn recv(&self) -> Option { - recv_timeout(&self.client.receiver).map(|msg| { - self.messages.borrow_mut().push(msg.clone()); - msg - }) - } - fn send_notification(&self, not: Notification) { - self.client.sender.send(Message::Notification(not)).unwrap(); - } - - pub fn path(&self) -> &Path { - self.dir.path() - } -} - -impl Drop for Server { - fn drop(&mut self) { - self.request::((), Value::Null); - self.notification::(()); - } -} - -fn recv_timeout(receiver: &Receiver) -> Option { - let timeout = Duration::from_secs(120); - select! { - recv(receiver) -> msg => msg.ok(), - recv(after(timeout)) -> _ => panic!("timed out"), - } -} -- cgit v1.2.3