9 use crossbeam_channel::{after, select, Receiver};
10 use lsp_server::{Connection, Message, Notification, Request};
12 notification::Exit, request::Shutdown, TextDocumentIdentifier, Url, WorkDoneProgress,
14 use lsp_types::{ProgressParams, ProgressParamsValue};
16 use serde_json::{to_string_pretty, Value};
17 use tempfile::TempDir;
18 use test_utils::{find_mismatch, Fixture};
20 use ra_db::AbsPathBuf;
21 use ra_project_model::ProjectManifest;
23 config::{ClientCapsConfig, Config, FilesConfig, FilesWatcher, LinkedProject},
27 pub struct Project<'a> {
30 tmp_dir: Option<TempDir>,
32 config: Option<Box<dyn Fn(&mut Config)>>,
35 impl<'a> Project<'a> {
36 pub fn with_fixture(fixture: &str) -> Project {
37 Project { fixture, tmp_dir: None, roots: vec![], with_sysroot: false, config: None }
40 pub fn tmp_dir(mut self, tmp_dir: TempDir) -> Project<'a> {
41 self.tmp_dir = Some(tmp_dir);
45 pub(crate) fn root(mut self, path: &str) -> Project<'a> {
46 self.roots.push(path.into());
50 pub fn with_sysroot(mut self, sysroot: bool) -> Project<'a> {
51 self.with_sysroot = sysroot;
55 pub fn with_config(mut self, config: impl Fn(&mut Config) + 'static) -> Project<'a> {
56 self.config = Some(Box::new(config));
60 pub fn server(self) -> Server {
61 let tmp_dir = self.tmp_dir.unwrap_or_else(|| TempDir::new().unwrap());
62 static INIT: Once = Once::new();
64 env_logger::builder().is_test(true).try_init().unwrap();
65 ra_prof::init_from(crate::PROFILE);
68 for entry in Fixture::parse(self.fixture) {
69 let path = tmp_dir.path().join(&entry.path['/'.len_utf8()..]);
70 fs::create_dir_all(path.parent().unwrap()).unwrap();
71 fs::write(path.as_path(), entry.text.as_bytes()).unwrap();
74 let tmp_dir_path = AbsPathBuf::assert(tmp_dir.path().to_path_buf());
76 self.roots.into_iter().map(|root| tmp_dir_path.join(root)).collect::<Vec<_>>();
78 roots.push(tmp_dir_path.clone());
80 let linked_projects = roots
82 .map(|it| ProjectManifest::discover_single(&it).unwrap())
83 .map(LinkedProject::from)
86 let mut config = Config {
87 client_caps: ClientCapsConfig {
89 code_action_literals: true,
90 work_done_progress: true,
93 with_sysroot: self.with_sysroot,
95 files: FilesConfig { watcher: FilesWatcher::Client, exclude: Vec::new() },
96 ..Config::new(tmp_dir_path)
98 if let Some(f) = &self.config {
102 Server::new(tmp_dir, config)
106 pub fn project(fixture: &str) -> Server {
107 Project::with_fixture(fixture).server()
112 messages: RefCell<Vec<Message>>,
113 _thread: jod_thread::JoinHandle<()>,
115 /// XXX: remove the tempdir last
120 fn new(dir: TempDir, config: Config) -> Server {
121 let (connection, client) = Connection::memory();
123 let _thread = jod_thread::Builder::new()
124 .name("test server".to_string())
125 .spawn(move || main_loop(config, connection).unwrap())
126 .expect("failed to spawn a thread");
128 Server { req_id: Cell::new(1), dir, messages: Default::default(), client, _thread }
131 pub fn doc_id(&self, rel_path: &str) -> TextDocumentIdentifier {
132 let path = self.dir.path().join(rel_path);
133 TextDocumentIdentifier { uri: Url::from_file_path(path).unwrap() }
136 pub fn notification<N>(&self, params: N::Params)
138 N: lsp_types::notification::Notification,
139 N::Params: Serialize,
141 let r = Notification::new(N::METHOD.to_string(), params);
142 self.send_notification(r)
145 pub fn request<R>(&self, params: R::Params, expected_resp: Value)
147 R: lsp_types::request::Request,
148 R::Params: Serialize,
150 let actual = self.send_request::<R>(params);
151 if let Some((expected_part, actual_part)) = find_mismatch(&expected_resp, &actual) {
153 "JSON mismatch\nExpected:\n{}\nWas:\n{}\nExpected part:\n{}\nActual part:\n{}\n",
154 to_string_pretty(&expected_resp).unwrap(),
155 to_string_pretty(&actual).unwrap(),
156 to_string_pretty(expected_part).unwrap(),
157 to_string_pretty(actual_part).unwrap(),
162 pub fn send_request<R>(&self, params: R::Params) -> Value
164 R: lsp_types::request::Request,
165 R::Params: Serialize,
167 let id = self.req_id.get();
168 self.req_id.set(id + 1);
170 let r = Request::new(id.into(), R::METHOD.to_string(), params);
171 self.send_request_(r)
173 fn send_request_(&self, r: Request) -> Value {
174 let id = r.id.clone();
175 self.client.sender.send(r.into()).unwrap();
176 while let Some(msg) = self.recv() {
178 Message::Request(req) => {
179 if req.method != "window/workDoneProgress/create"
180 && !(req.method == "client/registerCapability"
181 && req.params.to_string().contains("workspace/didChangeWatchedFiles"))
183 panic!("unexpected request: {:?}", req)
186 Message::Notification(_) => (),
187 Message::Response(res) => {
188 assert_eq!(res.id, id);
189 if let Some(err) = res.error {
190 panic!("error response: {:#?}", err);
192 return res.result.unwrap();
196 panic!("no response");
198 pub fn wait_until_workspace_is_loaded(&self) {
199 self.wait_for_message_cond(1, &|msg: &Message| match msg {
200 Message::Notification(n) if n.method == "$/progress" => {
201 match n.clone().extract::<ProgressParams>("$/progress").unwrap() {
203 token: lsp_types::ProgressToken::String(ref token),
204 value: ProgressParamsValue::WorkDone(WorkDoneProgress::End(_)),
205 } if token == "rustAnalyzer/roots scanned" => true,
212 fn wait_for_message_cond(&self, n: usize, cond: &dyn Fn(&Message) -> bool) {
214 for msg in self.messages.borrow().iter() {
220 let msg = self.recv().expect("no response");
226 fn recv(&self) -> Option<Message> {
227 recv_timeout(&self.client.receiver).map(|msg| {
228 self.messages.borrow_mut().push(msg.clone());
232 fn send_notification(&self, not: Notification) {
233 self.client.sender.send(Message::Notification(not)).unwrap();
236 pub fn path(&self) -> &Path {
241 impl Drop for Server {
243 self.request::<Shutdown>((), Value::Null);
244 self.notification::<Exit>(());
248 fn recv_timeout(receiver: &Receiver<Message>) -> Option<Message> {
249 let timeout = Duration::from_secs(120);
251 recv(receiver) -> msg => msg.ok(),
252 recv(after(timeout)) -> _ => panic!("timed out"),