2018-09-01 20:21:11 +03:00
|
|
|
use std::{
|
2018-09-02 12:34:06 +03:00
|
|
|
cell::{Cell, RefCell},
|
2018-10-15 17:44:23 -04:00
|
|
|
fs,
|
2019-04-13 19:45:21 +02:00
|
|
|
path::{Path, PathBuf},
|
2018-09-02 14:46:15 +03:00
|
|
|
sync::Once,
|
2018-10-15 17:44:23 -04:00
|
|
|
time::Duration,
|
2018-09-01 20:21:11 +03:00
|
|
|
};
|
|
|
|
|
2018-12-06 21:03:39 +03:00
|
|
|
use crossbeam_channel::{after, select, Receiver};
|
2019-08-30 20:18:57 +03:00
|
|
|
use lsp_server::{Connection, Message, Notification, Request};
|
2021-01-28 19:10:48 +03:00
|
|
|
use lsp_types::{notification::Exit, request::Shutdown, TextDocumentIdentifier, Url};
|
2021-01-06 13:54:28 +03:00
|
|
|
use project_model::ProjectManifest;
|
2021-01-28 19:10:48 +03:00
|
|
|
use rust_analyzer::{config::Config, lsp_ext, main_loop};
|
2020-07-08 18:22:57 +02:00
|
|
|
use serde::Serialize;
|
2021-01-28 19:10:48 +03:00
|
|
|
use serde_json::{json, to_string_pretty, Value};
|
2020-07-08 18:22:57 +02:00
|
|
|
use test_utils::{find_mismatch, Fixture};
|
|
|
|
use vfs::AbsPathBuf;
|
2018-09-01 20:21:11 +03:00
|
|
|
|
2020-07-23 22:26:25 +02:00
|
|
|
use crate::testdir::TestDir;
|
|
|
|
|
2020-11-02 16:31:38 +01:00
|
|
|
pub(crate) struct Project<'a> {
|
2019-04-13 20:33:49 +02:00
|
|
|
fixture: &'a str,
|
2019-08-19 15:41:18 +03:00
|
|
|
with_sysroot: bool,
|
2020-07-23 22:26:25 +02:00
|
|
|
tmp_dir: Option<TestDir>,
|
2019-04-13 20:33:49 +02:00
|
|
|
roots: Vec<PathBuf>,
|
2021-01-06 13:54:28 +03:00
|
|
|
config: serde_json::Value,
|
2019-03-05 22:29:23 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 20:33:49 +02:00
|
|
|
impl<'a> Project<'a> {
|
2020-11-02 16:31:38 +01:00
|
|
|
pub(crate) fn with_fixture(fixture: &str) -> Project {
|
2021-01-06 13:54:28 +03:00
|
|
|
Project {
|
|
|
|
fixture,
|
|
|
|
tmp_dir: None,
|
|
|
|
roots: vec![],
|
|
|
|
with_sysroot: false,
|
|
|
|
config: serde_json::Value::Null,
|
|
|
|
}
|
2019-04-13 20:33:49 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:31:38 +01:00
|
|
|
pub(crate) fn tmp_dir(mut self, tmp_dir: TestDir) -> Project<'a> {
|
2019-04-13 20:33:49 +02:00
|
|
|
self.tmp_dir = Some(tmp_dir);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:22:01 +02:00
|
|
|
pub(crate) fn root(mut self, path: &str) -> Project<'a> {
|
2019-04-13 20:33:49 +02:00
|
|
|
self.roots.push(path.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-11-13 17:38:26 +01:00
|
|
|
pub(crate) fn with_sysroot(mut self, yes: bool) -> Project<'a> {
|
|
|
|
self.with_sysroot = yes;
|
2019-08-19 15:41:18 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-01-06 13:54:28 +03:00
|
|
|
pub(crate) fn with_config(mut self, config: serde_json::Value) -> Project<'a> {
|
|
|
|
self.config = config;
|
2020-03-31 19:16:25 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-11-02 16:31:38 +01:00
|
|
|
pub(crate) fn server(self) -> Server {
|
2020-07-23 22:26:25 +02:00
|
|
|
let tmp_dir = self.tmp_dir.unwrap_or_else(|| TestDir::new());
|
2019-04-13 20:33:49 +02:00
|
|
|
static INIT: Once = Once::new();
|
|
|
|
INIT.call_once(|| {
|
2021-01-28 16:49:07 +03:00
|
|
|
env_logger::builder().is_test(true).parse_env("RA_LOG").try_init().unwrap();
|
2020-08-12 16:32:36 +02:00
|
|
|
profile::init_from(crate::PROFILE);
|
2019-04-13 20:33:49 +02:00
|
|
|
});
|
2018-09-01 20:21:11 +03:00
|
|
|
|
2020-06-23 18:58:45 +02:00
|
|
|
for entry in Fixture::parse(self.fixture) {
|
2020-06-23 18:34:50 +02:00
|
|
|
let path = tmp_dir.path().join(&entry.path['/'.len_utf8()..]);
|
2019-04-13 20:33:49 +02:00
|
|
|
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
|
|
|
fs::write(path.as_path(), entry.text.as_bytes()).unwrap();
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:34:24 +02:00
|
|
|
let tmp_dir_path = AbsPathBuf::assert(tmp_dir.path().to_path_buf());
|
2020-06-03 12:22:01 +02:00
|
|
|
let mut roots =
|
2020-06-24 13:34:24 +02:00
|
|
|
self.roots.into_iter().map(|root| tmp_dir_path.join(root)).collect::<Vec<_>>();
|
2020-06-03 12:22:01 +02:00
|
|
|
if roots.is_empty() {
|
2020-06-24 13:34:24 +02:00
|
|
|
roots.push(tmp_dir_path.clone());
|
2020-06-03 12:22:01 +02:00
|
|
|
}
|
2021-01-06 13:54:28 +03:00
|
|
|
let discovered_projects = roots
|
2020-06-03 12:22:01 +02:00
|
|
|
.into_iter()
|
|
|
|
.map(|it| ProjectManifest::discover_single(&it).unwrap())
|
|
|
|
.collect::<Vec<_>>();
|
2019-04-13 20:33:49 +02:00
|
|
|
|
2021-01-06 13:54:28 +03:00
|
|
|
let mut config = Config::new(
|
|
|
|
tmp_dir_path,
|
|
|
|
lsp_types::ClientCapabilities {
|
2021-01-05 16:57:05 +03:00
|
|
|
text_document: Some(lsp_types::TextDocumentClientCapabilities {
|
|
|
|
definition: Some(lsp_types::GotoCapability {
|
|
|
|
link_support: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
code_action: Some(lsp_types::CodeActionClientCapabilities {
|
|
|
|
code_action_literal_support: Some(
|
|
|
|
lsp_types::CodeActionLiteralSupport::default(),
|
|
|
|
),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-01-06 13:54:28 +03:00
|
|
|
hover: Some(lsp_types::HoverClientCapabilities {
|
|
|
|
content_format: Some(vec![lsp_types::MarkupKind::Markdown]),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-01-05 16:57:05 +03:00
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
window: Some(lsp_types::WindowClientCapabilities {
|
2021-01-28 19:10:48 +03:00
|
|
|
work_done_progress: Some(false),
|
2021-01-05 16:57:05 +03:00
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-01-28 19:10:48 +03:00
|
|
|
experimental: Some(json!({
|
|
|
|
"statusNotification": true,
|
|
|
|
})),
|
2020-04-26 18:54:05 -04:00
|
|
|
..Default::default()
|
|
|
|
},
|
2021-01-06 13:54:28 +03:00
|
|
|
);
|
|
|
|
config.discovered_projects = Some(discovered_projects);
|
|
|
|
config.update(self.config);
|
2020-03-31 19:16:25 +02:00
|
|
|
|
2020-06-23 13:20:53 +02:00
|
|
|
Server::new(tmp_dir, config)
|
2018-09-01 20:21:11 +03:00
|
|
|
}
|
2019-04-13 20:33:49 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:31:38 +01:00
|
|
|
pub(crate) fn project(fixture: &str) -> Server {
|
2019-04-13 20:33:49 +02:00
|
|
|
Project::with_fixture(fixture).server()
|
2018-09-01 20:21:11 +03:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:31:38 +01:00
|
|
|
pub(crate) struct Server {
|
2020-11-16 15:10:13 -05:00
|
|
|
req_id: Cell<i32>,
|
2019-08-30 17:24:11 +03:00
|
|
|
messages: RefCell<Vec<Message>>,
|
2019-08-30 20:18:57 +03:00
|
|
|
_thread: jod_thread::JoinHandle<()>,
|
|
|
|
client: Connection,
|
2020-03-28 13:19:05 +01:00
|
|
|
/// XXX: remove the tempdir last
|
2020-07-23 22:26:25 +02:00
|
|
|
dir: TestDir,
|
2018-09-01 20:21:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Server {
|
2020-07-23 22:26:25 +02:00
|
|
|
fn new(dir: TestDir, config: Config) -> Server {
|
2019-08-30 20:18:57 +03:00
|
|
|
let (connection, client) = Connection::memory();
|
2019-04-13 20:33:49 +02:00
|
|
|
|
2019-08-30 20:18:57 +03:00
|
|
|
let _thread = jod_thread::Builder::new()
|
|
|
|
.name("test server".to_string())
|
2020-06-03 12:22:01 +02:00
|
|
|
.spawn(move || main_loop(config, connection).unwrap())
|
2019-08-30 20:18:57 +03:00
|
|
|
.expect("failed to spawn a thread");
|
|
|
|
|
2020-06-23 13:20:53 +02:00
|
|
|
Server { req_id: Cell::new(1), dir, messages: Default::default(), client, _thread }
|
2018-09-01 20:21:11 +03:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:31:38 +01:00
|
|
|
pub(crate) fn doc_id(&self, rel_path: &str) -> TextDocumentIdentifier {
|
2018-09-01 20:21:11 +03:00
|
|
|
let path = self.dir.path().join(rel_path);
|
2019-02-08 14:49:43 +03:00
|
|
|
TextDocumentIdentifier { uri: Url::from_file_path(path).unwrap() }
|
2018-09-01 20:21:11 +03:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:31:38 +01:00
|
|
|
pub(crate) fn notification<N>(&self, params: N::Params)
|
2019-05-27 14:20:11 +03:00
|
|
|
where
|
2019-08-30 17:24:11 +03:00
|
|
|
N: lsp_types::notification::Notification,
|
2019-05-27 14:20:11 +03:00
|
|
|
N::Params: Serialize,
|
|
|
|
{
|
2019-08-30 17:24:11 +03:00
|
|
|
let r = Notification::new(N::METHOD.to_string(), params);
|
2019-05-27 14:20:11 +03:00
|
|
|
self.send_notification(r)
|
|
|
|
}
|
|
|
|
|
2020-11-02 16:31:38 +01:00
|
|
|
pub(crate) fn request<R>(&self, params: R::Params, expected_resp: Value)
|
2018-09-01 20:21:11 +03:00
|
|
|
where
|
2019-08-30 17:24:11 +03:00
|
|
|
R: lsp_types::request::Request,
|
2018-09-01 20:21:11 +03:00
|
|
|
R::Params: Serialize,
|
|
|
|
{
|
2019-01-11 00:37:10 +03:00
|
|
|
let actual = self.send_request::<R>(params);
|
2019-06-03 10:01:10 -04:00
|
|
|
if let Some((expected_part, actual_part)) = find_mismatch(&expected_resp, &actual) {
|
|
|
|
panic!(
|
2018-12-06 21:07:31 +01:00
|
|
|
"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(),
|
2019-06-03 10:01:10 -04:00
|
|
|
);
|
2018-12-06 21:07:31 +01:00
|
|
|
}
|
2018-09-01 20:21:11 +03:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:31:38 +01:00
|
|
|
pub(crate) fn send_request<R>(&self, params: R::Params) -> Value
|
2018-09-01 20:21:11 +03:00
|
|
|
where
|
2019-08-30 17:24:11 +03:00
|
|
|
R: lsp_types::request::Request,
|
2018-09-01 20:21:11 +03:00
|
|
|
R::Params: Serialize,
|
|
|
|
{
|
2019-01-11 00:37:10 +03:00
|
|
|
let id = self.req_id.get();
|
2020-11-16 15:10:13 -05:00
|
|
|
self.req_id.set(id.wrapping_add(1));
|
2019-01-11 00:37:10 +03:00
|
|
|
|
2019-08-30 17:24:11 +03:00
|
|
|
let r = Request::new(id.into(), R::METHOD.to_string(), params);
|
2018-09-02 16:36:03 +03:00
|
|
|
self.send_request_(r)
|
|
|
|
}
|
2019-08-30 17:24:11 +03:00
|
|
|
fn send_request_(&self, r: Request) -> Value {
|
|
|
|
let id = r.id.clone();
|
2021-01-28 16:00:33 +03:00
|
|
|
self.client.sender.send(r.clone().into()).unwrap();
|
|
|
|
while let Some(msg) = self.recv().unwrap_or_else(|Timeout| panic!("timeout: {:?}", r)) {
|
2018-09-01 20:21:11 +03:00
|
|
|
match msg {
|
2020-06-24 12:27:13 +02:00
|
|
|
Message::Request(req) => {
|
2020-07-02 15:32:56 +02:00
|
|
|
if req.method == "client/registerCapability" {
|
|
|
|
let params = req.params.to_string();
|
|
|
|
if ["workspace/didChangeWatchedFiles", "textDocument/didSave"]
|
|
|
|
.iter()
|
|
|
|
.any(|&it| params.contains(it))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic!("unexpected request: {:?}", req)
|
2020-06-24 12:27:13 +02:00
|
|
|
}
|
2019-08-30 17:24:11 +03:00
|
|
|
Message::Notification(_) => (),
|
|
|
|
Message::Response(res) => {
|
2018-09-01 20:21:11 +03:00
|
|
|
assert_eq!(res.id, id);
|
|
|
|
if let Some(err) = res.error {
|
|
|
|
panic!("error response: {:#?}", err);
|
|
|
|
}
|
|
|
|
return res.result.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-28 16:00:33 +03:00
|
|
|
panic!("no response for {:?}", r);
|
2018-09-01 20:21:11 +03:00
|
|
|
}
|
2020-11-02 16:31:38 +01:00
|
|
|
pub(crate) fn wait_until_workspace_is_loaded(self) -> Server {
|
2019-08-30 17:24:11 +03:00
|
|
|
self.wait_for_message_cond(1, &|msg: &Message| match msg {
|
2021-01-28 19:10:48 +03:00
|
|
|
Message::Notification(n) if n.method == "rust-analyzer/status" => {
|
|
|
|
let status = n
|
|
|
|
.clone()
|
|
|
|
.extract::<lsp_ext::StatusParams>("rust-analyzer/status")
|
|
|
|
.unwrap()
|
|
|
|
.status;
|
|
|
|
matches!(status, lsp_ext::Status::Ready)
|
2018-10-15 17:44:23 -04:00
|
|
|
}
|
|
|
|
_ => false,
|
2021-01-28 16:00:33 +03:00
|
|
|
})
|
|
|
|
.unwrap_or_else(|Timeout| panic!("timeout while waiting for ws to load"));
|
2020-08-25 19:02:28 +02:00
|
|
|
self
|
2019-03-07 17:46:17 +03:00
|
|
|
}
|
2021-01-28 16:00:33 +03:00
|
|
|
fn wait_for_message_cond(
|
|
|
|
&self,
|
|
|
|
n: usize,
|
|
|
|
cond: &dyn Fn(&Message) -> bool,
|
|
|
|
) -> Result<(), Timeout> {
|
2018-09-04 00:49:21 +03:00
|
|
|
let mut total = 0;
|
2018-09-02 14:46:15 +03:00
|
|
|
for msg in self.messages.borrow().iter() {
|
2019-03-07 17:46:17 +03:00
|
|
|
if cond(msg) {
|
2018-09-04 00:49:21 +03:00
|
|
|
total += 1
|
2018-09-02 14:46:15 +03:00
|
|
|
}
|
|
|
|
}
|
2018-09-04 00:49:21 +03:00
|
|
|
while total < n {
|
2021-01-28 16:00:33 +03:00
|
|
|
let msg = self.recv()?.expect("no response");
|
2019-03-07 17:46:17 +03:00
|
|
|
if cond(&msg) {
|
2018-09-04 00:49:21 +03:00
|
|
|
total += 1;
|
2018-09-02 14:46:15 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-28 16:00:33 +03:00
|
|
|
Ok(())
|
2018-09-02 14:46:15 +03:00
|
|
|
}
|
2021-01-28 16:00:33 +03:00
|
|
|
fn recv(&self) -> Result<Option<Message>, Timeout> {
|
|
|
|
let msg = recv_timeout(&self.client.receiver)?;
|
|
|
|
let msg = msg.map(|msg| {
|
2018-10-15 17:44:23 -04:00
|
|
|
self.messages.borrow_mut().push(msg.clone());
|
|
|
|
msg
|
2021-01-28 16:00:33 +03:00
|
|
|
});
|
|
|
|
Ok(msg)
|
2018-09-02 12:34:06 +03:00
|
|
|
}
|
2019-08-30 17:24:11 +03:00
|
|
|
fn send_notification(&self, not: Notification) {
|
2019-08-30 20:18:57 +03:00
|
|
|
self.client.sender.send(Message::Notification(not)).unwrap();
|
2018-09-01 20:21:11 +03:00
|
|
|
}
|
2019-04-13 19:45:21 +02:00
|
|
|
|
2020-11-02 16:31:38 +01:00
|
|
|
pub(crate) fn path(&self) -> &Path {
|
2019-04-13 19:45:21 +02:00
|
|
|
self.dir.path()
|
|
|
|
}
|
2018-09-01 20:21:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Server {
|
|
|
|
fn drop(&mut self) {
|
2019-08-30 20:18:57 +03:00
|
|
|
self.request::<Shutdown>((), Value::Null);
|
|
|
|
self.notification::<Exit>(());
|
2018-09-01 20:21:11 +03:00
|
|
|
}
|
|
|
|
}
|
2018-09-08 12:08:46 +03:00
|
|
|
|
2021-01-28 16:00:33 +03:00
|
|
|
struct Timeout;
|
|
|
|
|
|
|
|
fn recv_timeout(receiver: &Receiver<Message>) -> Result<Option<Message>, Timeout> {
|
2020-07-28 10:24:33 +02:00
|
|
|
let timeout =
|
|
|
|
if cfg!(target_os = "macos") { Duration::from_secs(300) } else { Duration::from_secs(120) };
|
2018-09-08 12:08:46 +03:00
|
|
|
select! {
|
2021-01-28 16:00:33 +03:00
|
|
|
recv(receiver) -> msg => Ok(msg.ok()),
|
|
|
|
recv(after(timeout)) -> _ => Err(Timeout),
|
2018-09-08 12:08:46 +03:00
|
|
|
}
|
|
|
|
}
|