rust/crates/ra_lsp_server/tests/heavy_tests/support.rs

218 lines
6.3 KiB
Rust
Raw Normal View History

2018-09-01 12:21:11 -05:00
use std::{
fs,
2018-09-02 04:34:06 -05:00
cell::{Cell, RefCell},
2018-09-01 12:21:11 -05:00
path::PathBuf,
2018-09-02 06:46:15 -05:00
time::Duration,
sync::Once,
2018-09-01 12:21:11 -05:00
};
use tempdir::TempDir;
2018-09-08 05:15:01 -05:00
use crossbeam_channel::{after, Receiver};
2018-09-01 12:21:11 -05:00
use flexi_logger::Logger;
use languageserver_types::{
Url,
TextDocumentIdentifier,
request::{Request, Shutdown},
2018-09-03 15:32:42 -05:00
notification::DidOpenTextDocument,
2018-09-01 12:21:11 -05:00
DidOpenTextDocumentParams,
TextDocumentItem,
};
use serde::Serialize;
use serde_json::{Value, from_str, to_string_pretty};
use gen_lsp_server::{RawMessage, RawRequest, RawNotification};
2018-09-16 04:54:24 -05:00
use ra_lsp_server::{main_loop, req, thread_watcher::{ThreadWatcher, Worker}};
2018-09-01 12:21:11 -05:00
pub fn project(fixture: &str) -> Server {
2018-09-02 06:46:15 -05:00
static INIT: Once = Once::new();
INIT.call_once(|| Logger::with_env_or_str(::LOG).start().unwrap());
2018-09-01 12:21:11 -05:00
let tmp_dir = TempDir::new("test-project")
.unwrap();
let mut buf = String::new();
let mut file_name = None;
let mut paths = vec![];
macro_rules! flush {
() => {
if let Some(file_name) = file_name {
let path = tmp_dir.path().join(file_name);
2018-09-02 06:46:15 -05:00
fs::create_dir_all(path.parent().unwrap()).unwrap();
2018-09-01 12:21:11 -05:00
fs::write(path.as_path(), buf.as_bytes()).unwrap();
paths.push((path, buf.clone()));
}
}
};
for line in fixture.lines() {
if line.starts_with("//-") {
flush!();
buf.clear();
file_name = Some(line["//-".len()..].trim());
continue;
}
buf.push_str(line);
buf.push('\n');
}
flush!();
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>>,
watcher: Option<ThreadWatcher>,
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();
2018-09-08 05:15:01 -05:00
let (worker, watcher) = Worker::<RawMessage, RawMessage>::spawn(
"test server",
128,
move |mut msg_receiver, mut msg_sender| {
main_loop(true, path, &mut msg_receiver, &mut msg_sender)
2018-09-08 05:15:01 -05:00
.unwrap()
}
);
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),
watcher: Some(watcher),
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,
}
}
))
}
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 request<R>(
&self,
params: R::Params,
expected_resp: &str,
)
where
R: Request,
R::Params: Serialize,
{
let id = self.req_id.get();
self.req_id.set(id + 1);
let expected_resp: Value = from_str(expected_resp).unwrap();
let actual = self.send_request::<R>(id, params);
assert_eq!(
expected_resp, actual,
"Expected:\n{}\n\
Actual:\n{}\n",
to_string_pretty(&expected_resp).unwrap(),
to_string_pretty(&actual).unwrap(),
);
}
fn send_request<R>(&self, id: u64, params: R::Params) -> Value
where
R: Request,
R::Params: Serialize,
{
2018-09-02 07:18:43 -05:00
let r = RawRequest::new::<R>(id, &params);
2018-09-02 08:36:03 -05:00
self.send_request_(r)
}
fn send_request_(&self, r: RawRequest) -> Value
{
let id = r.id;
2018-09-08 05:15:01 -05:00
self.worker.as_ref()
2018-09-01 12:21:11 -05:00
.unwrap()
.send(RawMessage::Request(r));
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");
}
2018-09-03 15:32:42 -05:00
pub fn wait_for_feedback(&self, feedback: &str) {
2018-09-03 16:49:21 -05:00
self.wait_for_feedback_n(feedback, 1)
}
pub fn wait_for_feedback_n(&self, feedback: &str, n: usize) {
2018-09-02 06:46:15 -05:00
let f = |msg: &RawMessage| match msg {
2018-09-03 15:32:42 -05:00
RawMessage::Notification(n) if n.method == "internalFeedback" => {
return n.clone().cast::<req::InternalFeedback>()
.unwrap() == feedback
2018-09-02 06:46:15 -05:00
}
2018-09-03 15:32:42 -05:00
_ => false,
2018-09-02 06:46:15 -05:00
};
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() {
2018-09-03 15:32:42 -05:00
if f(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");
2018-09-03 15:32:42 -05:00
if f(&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> {
2018-09-08 05:15:01 -05:00
recv_timeout(&self.worker.as_ref().unwrap().out)
2018-09-08 04:08:46 -05:00
.map(|msg| {
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) {
2018-09-08 05:15:01 -05:00
self.worker.as_ref()
2018-09-01 12:21:11 -05:00
.unwrap()
.send(RawMessage::Notification(not));
}
}
impl Drop for Server {
fn drop(&mut self) {
2018-09-08 05:15:01 -05:00
self.send_request::<Shutdown>(666, ());
let receiver = self.worker.take().unwrap().stop();
while let Some(msg) = recv_timeout(&receiver) {
drop(msg);
2018-09-01 12:21:11 -05:00
}
2018-09-08 05:15:01 -05:00
self.watcher.take()
2018-09-01 12:21:11 -05:00
.unwrap()
2018-09-08 05:15:01 -05:00
.stop()
.unwrap();
2018-09-01 12:21:11 -05:00
}
}
2018-09-08 04:08:46 -05:00
fn recv_timeout(receiver: &Receiver<RawMessage>) -> Option<RawMessage> {
let timeout = Duration::from_secs(5);
select! {
recv(receiver, msg) => msg,
recv(after(timeout)) => panic!("timed out"),
}
}