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,
|
2019-04-13 12:45:21 -05:00
|
|
|
path::{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};
|
2019-08-30 12:18:57 -05:00
|
|
|
use lsp_server::{Connection, Message, Notification, Request};
|
2019-01-14 04:55:56 -06:00
|
|
|
use lsp_types::{
|
2020-06-23 06:20:53 -05:00
|
|
|
notification::Exit, request::Shutdown, TextDocumentIdentifier, Url, WorkDoneProgress,
|
2018-09-01 12:21:11 -05:00
|
|
|
};
|
2020-05-10 12:25:37 -05:00
|
|
|
use lsp_types::{ProgressParams, ProgressParamsValue};
|
2020-06-03 05:22:01 -05:00
|
|
|
use ra_project_model::ProjectManifest;
|
2020-04-01 11:46:26 -05:00
|
|
|
use rust_analyzer::{
|
2020-06-24 05:27:13 -05:00
|
|
|
config::{ClientCapsConfig, Config, FilesConfig, FilesWatcher, LinkedProject},
|
2020-05-10 12:25:37 -05:00
|
|
|
main_loop,
|
2020-04-01 11:46:26 -05:00
|
|
|
};
|
2020-07-08 11:22:57 -05:00
|
|
|
use serde::Serialize;
|
|
|
|
use serde_json::{to_string_pretty, Value};
|
|
|
|
use test_utils::{find_mismatch, Fixture};
|
|
|
|
use vfs::AbsPathBuf;
|
2018-09-01 12:21:11 -05:00
|
|
|
|
2020-07-23 15:26:25 -05:00
|
|
|
use crate::testdir::TestDir;
|
|
|
|
|
2019-04-13 13:33:49 -05:00
|
|
|
pub struct Project<'a> {
|
|
|
|
fixture: &'a str,
|
2019-08-19 07:41:18 -05:00
|
|
|
with_sysroot: bool,
|
2020-07-23 15:26:25 -05:00
|
|
|
tmp_dir: Option<TestDir>,
|
2019-04-13 13:33:49 -05:00
|
|
|
roots: Vec<PathBuf>,
|
2020-04-01 11:41:43 -05:00
|
|
|
config: Option<Box<dyn Fn(&mut Config)>>,
|
2019-03-05 15:29:23 -06:00
|
|
|
}
|
|
|
|
|
2019-04-13 13:33:49 -05:00
|
|
|
impl<'a> Project<'a> {
|
|
|
|
pub fn with_fixture(fixture: &str) -> Project {
|
2020-03-31 12:16:25 -05:00
|
|
|
Project { fixture, tmp_dir: None, roots: vec![], with_sysroot: false, config: None }
|
2019-04-13 13:33:49 -05:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:26:25 -05:00
|
|
|
pub fn tmp_dir(mut self, tmp_dir: TestDir) -> Project<'a> {
|
2019-04-13 13:33:49 -05:00
|
|
|
self.tmp_dir = Some(tmp_dir);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-06-03 05:22:01 -05:00
|
|
|
pub(crate) fn root(mut self, path: &str) -> Project<'a> {
|
2019-04-13 13:33:49 -05:00
|
|
|
self.roots.push(path.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-08-19 07:41:18 -05:00
|
|
|
pub fn with_sysroot(mut self, sysroot: bool) -> Project<'a> {
|
|
|
|
self.with_sysroot = sysroot;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-04-01 11:41:43 -05:00
|
|
|
pub fn with_config(mut self, config: impl Fn(&mut Config) + 'static) -> Project<'a> {
|
2020-03-31 12:16:25 -05:00
|
|
|
self.config = Some(Box::new(config));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-04-13 13:33:49 -05:00
|
|
|
pub fn server(self) -> Server {
|
2020-07-23 15:26:25 -05:00
|
|
|
let tmp_dir = self.tmp_dir.unwrap_or_else(|| TestDir::new());
|
2019-04-13 13:33:49 -05:00
|
|
|
static INIT: Once = Once::new();
|
|
|
|
INIT.call_once(|| {
|
2020-02-18 06:53:02 -06:00
|
|
|
env_logger::builder().is_test(true).try_init().unwrap();
|
2020-04-25 07:52:23 -05:00
|
|
|
ra_prof::init_from(crate::PROFILE);
|
2019-04-13 13:33:49 -05:00
|
|
|
});
|
2018-09-01 12:21:11 -05:00
|
|
|
|
2020-06-23 11:58:45 -05:00
|
|
|
for entry in Fixture::parse(self.fixture) {
|
2020-06-23 11:34:50 -05:00
|
|
|
let path = tmp_dir.path().join(&entry.path['/'.len_utf8()..]);
|
2019-04-13 13:33:49 -05:00
|
|
|
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
|
|
|
fs::write(path.as_path(), entry.text.as_bytes()).unwrap();
|
|
|
|
}
|
|
|
|
|
2020-06-24 06:34:24 -05:00
|
|
|
let tmp_dir_path = AbsPathBuf::assert(tmp_dir.path().to_path_buf());
|
2020-06-03 05:22:01 -05:00
|
|
|
let mut roots =
|
2020-06-24 06:34:24 -05:00
|
|
|
self.roots.into_iter().map(|root| tmp_dir_path.join(root)).collect::<Vec<_>>();
|
2020-06-03 05:22:01 -05:00
|
|
|
if roots.is_empty() {
|
2020-06-24 06:34:24 -05:00
|
|
|
roots.push(tmp_dir_path.clone());
|
2020-06-03 05:22:01 -05:00
|
|
|
}
|
|
|
|
let linked_projects = roots
|
|
|
|
.into_iter()
|
|
|
|
.map(|it| ProjectManifest::discover_single(&it).unwrap())
|
|
|
|
.map(LinkedProject::from)
|
|
|
|
.collect::<Vec<_>>();
|
2019-04-13 13:33:49 -05:00
|
|
|
|
2020-04-01 11:41:43 -05:00
|
|
|
let mut config = Config {
|
2020-04-26 17:54:05 -05:00
|
|
|
client_caps: ClientCapsConfig {
|
|
|
|
location_link: true,
|
|
|
|
code_action_literals: true,
|
2020-05-09 16:15:22 -05:00
|
|
|
work_done_progress: true,
|
2020-04-26 17:54:05 -05:00
|
|
|
..Default::default()
|
|
|
|
},
|
2020-04-01 11:41:43 -05:00
|
|
|
with_sysroot: self.with_sysroot,
|
2020-06-03 05:22:01 -05:00
|
|
|
linked_projects,
|
2020-06-24 05:27:13 -05:00
|
|
|
files: FilesConfig { watcher: FilesWatcher::Client, exclude: Vec::new() },
|
2020-06-24 06:34:24 -05:00
|
|
|
..Config::new(tmp_dir_path)
|
2020-04-01 11:41:43 -05:00
|
|
|
};
|
2020-03-31 12:16:25 -05:00
|
|
|
if let Some(f) = &self.config {
|
|
|
|
f(&mut config)
|
|
|
|
}
|
|
|
|
|
2020-06-23 06:20:53 -05:00
|
|
|
Server::new(tmp_dir, config)
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
2019-04-13 13:33:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn project(fixture: &str) -> Server {
|
|
|
|
Project::with_fixture(fixture).server()
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Server {
|
|
|
|
req_id: Cell<u64>,
|
2019-08-30 09:24:11 -05:00
|
|
|
messages: RefCell<Vec<Message>>,
|
2019-08-30 12:18:57 -05:00
|
|
|
_thread: jod_thread::JoinHandle<()>,
|
|
|
|
client: Connection,
|
2020-03-28 07:19:05 -05:00
|
|
|
/// XXX: remove the tempdir last
|
2020-07-23 15:26:25 -05:00
|
|
|
dir: TestDir,
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Server {
|
2020-07-23 15:26:25 -05:00
|
|
|
fn new(dir: TestDir, config: Config) -> Server {
|
2019-08-30 12:18:57 -05:00
|
|
|
let (connection, client) = Connection::memory();
|
2019-04-13 13:33:49 -05:00
|
|
|
|
2019-08-30 12:18:57 -05:00
|
|
|
let _thread = jod_thread::Builder::new()
|
|
|
|
.name("test server".to_string())
|
2020-06-03 05:22:01 -05:00
|
|
|
.spawn(move || main_loop(config, connection).unwrap())
|
2019-08-30 12:18:57 -05:00
|
|
|
.expect("failed to spawn a thread");
|
|
|
|
|
2020-06-23 06:20:53 -05:00
|
|
|
Server { req_id: Cell::new(1), dir, messages: Default::default(), client, _thread }
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-27 06:20:11 -05:00
|
|
|
pub fn notification<N>(&self, params: N::Params)
|
|
|
|
where
|
2019-08-30 09:24:11 -05:00
|
|
|
N: lsp_types::notification::Notification,
|
2019-05-27 06:20:11 -05:00
|
|
|
N::Params: Serialize,
|
|
|
|
{
|
2019-08-30 09:24:11 -05:00
|
|
|
let r = Notification::new(N::METHOD.to_string(), params);
|
2019-05-27 06:20:11 -05:00
|
|
|
self.send_notification(r)
|
|
|
|
}
|
|
|
|
|
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
|
2019-08-30 09:24:11 -05:00
|
|
|
R: lsp_types::request::Request,
|
2018-09-01 12:21:11 -05:00
|
|
|
R::Params: Serialize,
|
|
|
|
{
|
2019-01-10 15:37:10 -06:00
|
|
|
let actual = self.send_request::<R>(params);
|
2019-06-03 09:01:10 -05:00
|
|
|
if let Some((expected_part, actual_part)) = find_mismatch(&expected_resp, &actual) {
|
|
|
|
panic!(
|
2018-12-06 14:07:31 -06: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 09:01:10 -05:00
|
|
|
);
|
2018-12-06 14:07:31 -06:00
|
|
|
}
|
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
|
2019-08-30 09:24:11 -05:00
|
|
|
R: lsp_types::request::Request,
|
2018-09-01 12:21:11 -05:00
|
|
|
R::Params: Serialize,
|
|
|
|
{
|
2019-01-10 15:37:10 -06:00
|
|
|
let id = self.req_id.get();
|
|
|
|
self.req_id.set(id + 1);
|
|
|
|
|
2019-08-30 09:24:11 -05:00
|
|
|
let r = Request::new(id.into(), R::METHOD.to_string(), params);
|
2018-09-02 08:36:03 -05:00
|
|
|
self.send_request_(r)
|
|
|
|
}
|
2019-08-30 09:24:11 -05:00
|
|
|
fn send_request_(&self, r: Request) -> Value {
|
|
|
|
let id = r.id.clone();
|
2019-08-30 12:18:57 -05:00
|
|
|
self.client.sender.send(r.into()).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 {
|
2020-06-24 05:27:13 -05:00
|
|
|
Message::Request(req) => {
|
2020-07-02 08:32:56 -05:00
|
|
|
if req.method == "window/workDoneProgress/create" {
|
|
|
|
continue;
|
2020-06-24 05:27:13 -05:00
|
|
|
}
|
2020-07-02 08:32:56 -05: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 05:27:13 -05:00
|
|
|
}
|
2019-08-30 09:24:11 -05:00
|
|
|
Message::Notification(_) => (),
|
|
|
|
Message::Response(res) => {
|
2018-09-01 12:21:11 -05:00
|
|
|
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) {
|
2019-08-30 09:24:11 -05:00
|
|
|
self.wait_for_message_cond(1, &|msg: &Message| match msg {
|
2020-03-14 00:06:49 -05:00
|
|
|
Message::Notification(n) if n.method == "$/progress" => {
|
|
|
|
match n.clone().extract::<ProgressParams>("$/progress").unwrap() {
|
|
|
|
ProgressParams {
|
2020-05-10 12:25:37 -05:00
|
|
|
token: lsp_types::ProgressToken::String(ref token),
|
2020-03-14 00:06:49 -05:00
|
|
|
value: ProgressParamsValue::WorkDone(WorkDoneProgress::End(_)),
|
2020-06-11 04:04:09 -05:00
|
|
|
} if token == "rustAnalyzer/roots scanned" => true,
|
2020-03-14 00:06:49 -05:00
|
|
|
_ => false,
|
|
|
|
}
|
2018-10-15 16:44:23 -05:00
|
|
|
}
|
|
|
|
_ => false,
|
2019-03-07 08:46:17 -06:00
|
|
|
})
|
|
|
|
}
|
2019-08-30 09:24:11 -05:00
|
|
|
fn wait_for_message_cond(&self, n: usize, cond: &dyn Fn(&Message) -> 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-30 09:24:11 -05:00
|
|
|
fn recv(&self) -> Option<Message> {
|
2019-08-30 12:18:57 -05:00
|
|
|
recv_timeout(&self.client.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
|
|
|
}
|
2019-08-30 09:24:11 -05:00
|
|
|
fn send_notification(&self, not: Notification) {
|
2019-08-30 12:18:57 -05:00
|
|
|
self.client.sender.send(Message::Notification(not)).unwrap();
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
2019-04-13 12:45:21 -05:00
|
|
|
|
|
|
|
pub fn path(&self) -> &Path {
|
|
|
|
self.dir.path()
|
|
|
|
}
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Server {
|
|
|
|
fn drop(&mut self) {
|
2019-08-30 12:18:57 -05:00
|
|
|
self.request::<Shutdown>((), Value::Null);
|
|
|
|
self.notification::<Exit>(());
|
2018-09-01 12:21:11 -05:00
|
|
|
}
|
|
|
|
}
|
2018-09-08 04:08:46 -05:00
|
|
|
|
2019-08-30 09:24:11 -05:00
|
|
|
fn recv_timeout(receiver: &Receiver<Message>) -> Option<Message> {
|
2019-05-29 14:14:06 -05:00
|
|
|
let timeout = Duration::from_secs(120);
|
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
|
|
|
}
|
|
|
|
}
|