2018-09-01 12:21:11 -05:00
|
|
|
use std::{
|
2018-09-02 04:34:06 -05:00
|
|
|
cell::{Cell, RefCell},
|
2018-10-15 16:44:23 -05:00
|
|
|
fs,
|
2018-09-01 12:21:11 -05:00
|
|
|
path::PathBuf,
|
2018-09-02 06:46:15 -05:00
|
|
|
sync::Once,
|
2018-10-15 16:44:23 -05:00
|
|
|
time::Duration,
|
2018-09-01 12:21:11 -05:00
|
|
|
};
|
|
|
|
|
2018-12-06 12:03:39 -06:00
|
|
|
use crossbeam_channel::{after, select, Receiver};
|
2018-09-01 12:21:11 -05:00
|
|
|
use flexi_logger::Logger;
|
2018-10-15 16:44:23 -05:00
|
|
|
use gen_lsp_server::{RawMessage, RawNotification, RawRequest};
|
2019-01-14 04:55:56 -06:00
|
|
|
use lsp_types::{
|
2018-09-03 15:32:42 -05:00
|
|
|
notification::DidOpenTextDocument,
|
2018-10-15 16:44:23 -05:00
|
|
|
request::{Request, Shutdown},
|
|
|
|
DidOpenTextDocumentParams, TextDocumentIdentifier, TextDocumentItem, Url,
|
2019-03-05 13:59:01 -06:00
|
|
|
notification::{Notification, ShowMessage},
|
2018-09-01 12:21:11 -05:00
|
|
|
};
|
|
|
|
use serde::Serialize;
|
2018-12-06 14:07:31 -06:00
|
|
|
use serde_json::{to_string_pretty, Value};
|
2018-12-30 14:33:04 -06:00
|
|
|
use tempfile::TempDir;
|
2019-02-14 11:43:45 -06:00
|
|
|
use thread_worker::Worker;
|
2018-12-06 14:07:31 -06:00
|
|
|
use test_utils::{parse_fixture, find_mismatch};
|
2018-09-01 12:21:11 -05:00
|
|
|
|
2018-10-15 16:44:23 -05:00
|
|
|
use ra_lsp_server::{
|
|
|
|
main_loop, req,
|
2019-03-06 03:34:38 -06:00
|
|
|
InitializationOptions,
|
2018-10-15 16:44:23 -05:00
|
|
|
};
|
2018-09-01 12:21:11 -05:00
|
|
|
|
|
|
|
pub fn project(fixture: &str) -> Server {
|
2019-03-05 15:29:23 -06:00
|
|
|
let tmp_dir = TempDir::new().unwrap();
|
|
|
|
project_with_tmpdir(tmp_dir, fixture)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn project_with_tmpdir(tmp_dir: TempDir, fixture: &str) -> Server {
|
2018-09-02 06:46:15 -05:00
|
|
|
static INIT: Once = Once::new();
|
2019-02-27 08:35:04 -06:00
|
|
|
INIT.call_once(|| {
|
|
|
|
let _ = Logger::with_env_or_str(crate::LOG).start().unwrap();
|
|
|
|
});
|
2018-09-01 12:21:11 -05:00
|
|
|
|
|
|
|
let mut paths = vec![];
|
2018-10-31 13:37:32 -05:00
|
|
|
|
|
|
|
for entry in parse_fixture(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));
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
Server::new(tmp_dir, paths)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Server {
|
|
|
|
req_id: Cell<u64>,
|
2018-09-02 04:34:06 -05:00
|
|
|
messages: RefCell<Vec<RawMessage>>,
|
2018-09-01 12:21:11 -05:00
|
|
|
dir: TempDir,
|
2018-09-08 05:15:01 -05:00
|
|
|
worker: Option<Worker<RawMessage, RawMessage>>,
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Server {
|
|
|
|
fn new(dir: TempDir, files: Vec<(PathBuf, String)>) -> Server {
|
|
|
|
let path = dir.path().to_path_buf();
|
2019-02-14 11:43:45 -06:00
|
|
|
let worker = Worker::<RawMessage, RawMessage>::spawn(
|
2018-09-08 05:15:01 -05:00
|
|
|
"test server",
|
|
|
|
128,
|
|
|
|
move |mut msg_receiver, mut msg_sender| {
|
2019-03-06 03:34:38 -06:00
|
|
|
main_loop(
|
|
|
|
path,
|
|
|
|
InitializationOptions::default(),
|
|
|
|
&mut msg_receiver,
|
|
|
|
&mut msg_sender,
|
|
|
|
)
|
|
|
|
.unwrap()
|
2018-10-15 16:44:23 -05:00
|
|
|
},
|
2018-09-08 05:15:01 -05:00
|
|
|
);
|
2018-09-01 12:21:11 -05:00
|
|
|
let res = Server {
|
|
|
|
req_id: Cell::new(1),
|
|
|
|
dir,
|
2018-09-02 04:34:06 -05:00
|
|
|
messages: Default::default(),
|
2018-09-08 05:15:01 -05:00
|
|
|
worker: Some(worker),
|
2018-09-01 12:21:11 -05:00
|
|
|
};
|
2018-09-08 04:08:46 -05:00
|
|
|
|
2018-09-01 12:21:11 -05:00
|
|
|
for (path, text) in files {
|
|
|
|
res.send_notification(RawNotification::new::<DidOpenTextDocument>(
|
2018-09-02 07:18:43 -05:00
|
|
|
&DidOpenTextDocumentParams {
|
2018-09-01 12:21:11 -05:00
|
|
|
text_document: TextDocumentItem {
|
|
|
|
uri: Url::from_file_path(path).unwrap(),
|
|
|
|
language_id: "rust".to_string(),
|
|
|
|
version: 0,
|
|
|
|
text,
|
2018-10-15 16:44:23 -05:00
|
|
|
},
|
|
|
|
},
|
2018-09-01 12:21:11 -05:00
|
|
|
))
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn doc_id(&self, rel_path: &str) -> TextDocumentIdentifier {
|
|
|
|
let path = self.dir.path().join(rel_path);
|
2019-02-08 05:49:43 -06:00
|
|
|
TextDocumentIdentifier { uri: Url::from_file_path(path).unwrap() }
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 14:07:31 -06:00
|
|
|
pub fn request<R>(&self, params: R::Params, expected_resp: Value)
|
2018-09-01 12:21:11 -05:00
|
|
|
where
|
|
|
|
R: Request,
|
|
|
|
R::Params: Serialize,
|
|
|
|
{
|
2019-01-10 15:37:10 -06:00
|
|
|
let actual = self.send_request::<R>(params);
|
2018-12-06 14:07:31 -06:00
|
|
|
match find_mismatch(&expected_resp, &actual) {
|
|
|
|
Some((expected_part, actual_part)) => 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(),
|
|
|
|
),
|
|
|
|
None => {}
|
|
|
|
}
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
|
2019-01-10 15:37:10 -06:00
|
|
|
pub fn send_request<R>(&self, params: R::Params) -> Value
|
2018-09-01 12:21:11 -05:00
|
|
|
where
|
|
|
|
R: Request,
|
|
|
|
R::Params: Serialize,
|
|
|
|
{
|
2019-01-10 15:37:10 -06:00
|
|
|
let id = self.req_id.get();
|
|
|
|
self.req_id.set(id + 1);
|
|
|
|
|
2018-09-02 07:18:43 -05:00
|
|
|
let r = RawRequest::new::<R>(id, ¶ms);
|
2018-09-02 08:36:03 -05:00
|
|
|
self.send_request_(r)
|
|
|
|
}
|
2018-10-15 16:44:23 -05:00
|
|
|
fn send_request_(&self, r: RawRequest) -> Value {
|
2018-09-02 08:36:03 -05:00
|
|
|
let id = r.id;
|
2019-02-14 11:43:45 -06:00
|
|
|
self.worker.as_ref().unwrap().sender().send(RawMessage::Request(r)).unwrap();
|
2018-09-02 04:34:06 -05:00
|
|
|
while let Some(msg) = self.recv() {
|
2018-09-01 12:21:11 -05:00
|
|
|
match msg {
|
|
|
|
RawMessage::Request(req) => panic!("unexpected request: {:?}", req),
|
|
|
|
RawMessage::Notification(_) => (),
|
|
|
|
RawMessage::Response(res) => {
|
|
|
|
assert_eq!(res.id, id);
|
|
|
|
if let Some(err) = res.error {
|
|
|
|
panic!("error response: {:#?}", err);
|
|
|
|
}
|
|
|
|
return res.result.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic!("no response");
|
|
|
|
}
|
2019-03-07 08:46:17 -06:00
|
|
|
pub fn wait_until_workspace_is_loaded(&self) {
|
|
|
|
self.wait_for_message_cond(1, &|msg: &RawMessage| match msg {
|
2019-03-05 13:59:01 -06:00
|
|
|
RawMessage::Notification(n) if n.method == ShowMessage::METHOD => {
|
2019-03-05 14:25:24 -06:00
|
|
|
let msg = n.clone().cast::<req::ShowMessage>().unwrap();
|
2019-03-07 08:46:17 -06:00
|
|
|
msg.message.starts_with("workspace loaded")
|
2018-10-15 16:44:23 -05:00
|
|
|
}
|
|
|
|
_ => false,
|
2019-03-07 08:46:17 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
fn wait_for_message_cond(&self, n: usize, cond: &dyn Fn(&RawMessage) -> bool) {
|
2018-09-03 16:49:21 -05:00
|
|
|
let mut total = 0;
|
2018-09-02 06:46:15 -05:00
|
|
|
for msg in self.messages.borrow().iter() {
|
2019-03-07 08:46:17 -06:00
|
|
|
if cond(msg) {
|
2018-09-03 16:49:21 -05:00
|
|
|
total += 1
|
2018-09-02 06:46:15 -05:00
|
|
|
}
|
|
|
|
}
|
2018-09-03 16:49:21 -05:00
|
|
|
while total < n {
|
|
|
|
let msg = self.recv().expect("no response");
|
2019-03-07 08:46:17 -06:00
|
|
|
if cond(&msg) {
|
2018-09-03 16:49:21 -05:00
|
|
|
total += 1;
|
2018-09-02 06:46:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 04:34:06 -05:00
|
|
|
fn recv(&self) -> Option<RawMessage> {
|
2019-02-14 11:43:45 -06:00
|
|
|
recv_timeout(&self.worker.as_ref().unwrap().receiver()).map(|msg| {
|
2018-10-15 16:44:23 -05:00
|
|
|
self.messages.borrow_mut().push(msg.clone());
|
|
|
|
msg
|
|
|
|
})
|
2018-09-02 04:34:06 -05:00
|
|
|
}
|
2018-09-01 12:21:11 -05:00
|
|
|
fn send_notification(&self, not: RawNotification) {
|
2019-02-14 11:43:45 -06:00
|
|
|
self.worker.as_ref().unwrap().sender().send(RawMessage::Notification(not)).unwrap();
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Server {
|
|
|
|
fn drop(&mut self) {
|
2019-01-10 15:37:10 -06:00
|
|
|
self.send_request::<Shutdown>(());
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
}
|
2018-09-08 04:08:46 -05:00
|
|
|
|
|
|
|
fn recv_timeout(receiver: &Receiver<RawMessage>) -> Option<RawMessage> {
|
2019-01-10 15:37:10 -06:00
|
|
|
let timeout = Duration::from_secs(50);
|
2018-09-08 04:08:46 -05:00
|
|
|
select! {
|
2018-12-30 14:23:31 -06:00
|
|
|
recv(receiver) -> msg => msg.ok(),
|
|
|
|
recv(after(timeout)) -> _ => panic!("timed out"),
|
2018-09-08 04:08:46 -05:00
|
|
|
}
|
|
|
|
}
|