Add unit test to ensure changing enable_language_server works

This commit is contained in:
Antonio Scandurra 2022-06-09 10:48:06 +02:00
parent 36a1a7a819
commit 69170fc33a
3 changed files with 131 additions and 5 deletions

View File

@ -9089,7 +9089,6 @@ mod tests {
#[gpui::test] #[gpui::test]
async fn test_document_format_during_save(cx: &mut gpui::TestAppContext) { async fn test_document_format_during_save(cx: &mut gpui::TestAppContext) {
cx.foreground().forbid_parking(); cx.foreground().forbid_parking();
cx.update(|cx| cx.set_global(Settings::test(cx)));
let mut language = Language::new( let mut language = Language::new(
LanguageConfig { LanguageConfig {
@ -9202,7 +9201,6 @@ mod tests {
#[gpui::test] #[gpui::test]
async fn test_range_format_during_save(cx: &mut gpui::TestAppContext) { async fn test_range_format_during_save(cx: &mut gpui::TestAppContext) {
cx.foreground().forbid_parking(); cx.foreground().forbid_parking();
cx.update(|cx| cx.set_global(Settings::test(cx)));
let mut language = Language::new( let mut language = Language::new(
LanguageConfig { LanguageConfig {
@ -9316,8 +9314,6 @@ mod tests {
#[gpui::test] #[gpui::test]
async fn test_completion(cx: &mut gpui::TestAppContext) { async fn test_completion(cx: &mut gpui::TestAppContext) {
cx.update(|cx| cx.set_global(Settings::test(cx)));
let mut language = Language::new( let mut language = Language::new(
LanguageConfig { LanguageConfig {
name: "Rust".into(), name: "Rust".into(),

View File

@ -11,6 +11,7 @@ doctest = false
test-support = [ test-support = [
"client/test-support", "client/test-support",
"language/test-support", "language/test-support",
"settings/test-support",
"text/test-support", "text/test-support",
] ]
@ -56,6 +57,7 @@ collections = { path = "../collections", features = ["test-support"] }
gpui = { path = "../gpui", features = ["test-support"] } gpui = { path = "../gpui", features = ["test-support"] }
language = { path = "../language", features = ["test-support"] } language = { path = "../language", features = ["test-support"] }
lsp = { path = "../lsp", features = ["test-support"] } lsp = { path = "../lsp", features = ["test-support"] }
settings = { path = "../settings", features = ["test-support"] }
util = { path = "../util", features = ["test-support"] } util = { path = "../util", features = ["test-support"] }
rpc = { path = "../rpc", features = ["test-support"] } rpc = { path = "../rpc", features = ["test-support"] }
tempdir = { version = "0.3.7" } tempdir = { version = "0.3.7" }

View File

@ -585,6 +585,10 @@ impl Project {
root_paths: impl IntoIterator<Item = &Path>, root_paths: impl IntoIterator<Item = &Path>,
cx: &mut gpui::TestAppContext, cx: &mut gpui::TestAppContext,
) -> ModelHandle<Project> { ) -> ModelHandle<Project> {
if !cx.read(|cx| cx.has_global::<Settings>()) {
cx.update(|cx| cx.set_global(Settings::test(cx)));
}
let languages = Arc::new(LanguageRegistry::test()); let languages = Arc::new(LanguageRegistry::test());
let http_client = client::test::FakeHttpClient::with_404_response(); let http_client = client::test::FakeHttpClient::with_404_response();
let client = client::Client::new(http_client.clone()); let client = client::Client::new(http_client.clone());
@ -5751,7 +5755,7 @@ mod tests {
use super::{Event, *}; use super::{Event, *};
use fs::RealFs; use fs::RealFs;
use futures::{future, StreamExt}; use futures::{future, StreamExt};
use gpui::test::subscribe; use gpui::{executor::Deterministic, test::subscribe};
use language::{ use language::{
tree_sitter_rust, tree_sitter_typescript, Diagnostic, FakeLspAdapter, LanguageConfig, tree_sitter_rust, tree_sitter_typescript, Diagnostic, FakeLspAdapter, LanguageConfig,
OffsetRangeExt, Point, ToPoint, OffsetRangeExt, Point, ToPoint,
@ -6503,6 +6507,130 @@ mod tests {
}); });
} }
#[gpui::test]
async fn test_toggling_enable_language_server(
deterministic: Arc<Deterministic>,
cx: &mut gpui::TestAppContext,
) {
deterministic.forbid_parking();
let mut rust = Language::new(
LanguageConfig {
name: Arc::from("Rust"),
path_suffixes: vec!["rs".to_string()],
..Default::default()
},
None,
);
let mut fake_rust_servers = rust.set_fake_lsp_adapter(FakeLspAdapter {
name: "rust-lsp",
..Default::default()
});
let mut js = Language::new(
LanguageConfig {
name: Arc::from("JavaScript"),
path_suffixes: vec!["js".to_string()],
..Default::default()
},
None,
);
let mut fake_js_servers = js.set_fake_lsp_adapter(FakeLspAdapter {
name: "js-lsp",
..Default::default()
});
let fs = FakeFs::new(cx.background());
fs.insert_tree("/dir", json!({ "a.rs": "", "b.js": "" }))
.await;
let project = Project::test(fs, ["/dir".as_ref()], cx).await;
project.update(cx, |project, _| {
project.languages.add(Arc::new(rust));
project.languages.add(Arc::new(js));
});
let _rs_buffer = project
.update(cx, |project, cx| project.open_local_buffer("/dir/a.rs", cx))
.await
.unwrap();
let _js_buffer = project
.update(cx, |project, cx| project.open_local_buffer("/dir/b.js", cx))
.await
.unwrap();
let mut fake_rust_server_1 = fake_rust_servers.next().await.unwrap();
assert_eq!(
fake_rust_server_1
.receive_notification::<lsp::notification::DidOpenTextDocument>()
.await
.text_document
.uri
.as_str(),
"file:///dir/a.rs"
);
let mut fake_js_server = fake_js_servers.next().await.unwrap();
assert_eq!(
fake_js_server
.receive_notification::<lsp::notification::DidOpenTextDocument>()
.await
.text_document
.uri
.as_str(),
"file:///dir/b.js"
);
// Disable Rust language server, ensuring only that server gets stopped.
cx.update(|cx| {
cx.update_global(|settings: &mut Settings, _| {
settings.language_overrides.insert(
Arc::from("Rust"),
settings::LanguageOverride {
enable_language_server: Some(false),
..Default::default()
},
);
})
});
fake_rust_server_1
.receive_notification::<lsp::notification::Exit>()
.await;
// Enable Rust and disable JavaScript language servers, ensuring that the
// former gets started again and that the latter stops.
cx.update(|cx| {
cx.update_global(|settings: &mut Settings, _| {
settings.language_overrides.insert(
Arc::from("Rust"),
settings::LanguageOverride {
enable_language_server: Some(true),
..Default::default()
},
);
settings.language_overrides.insert(
Arc::from("JavaScript"),
settings::LanguageOverride {
enable_language_server: Some(false),
..Default::default()
},
);
})
});
let mut fake_rust_server_2 = fake_rust_servers.next().await.unwrap();
assert_eq!(
fake_rust_server_2
.receive_notification::<lsp::notification::DidOpenTextDocument>()
.await
.text_document
.uri
.as_str(),
"file:///dir/a.rs"
);
fake_js_server
.receive_notification::<lsp::notification::Exit>()
.await;
}
#[gpui::test] #[gpui::test]
async fn test_transforming_diagnostics(cx: &mut gpui::TestAppContext) { async fn test_transforming_diagnostics(cx: &mut gpui::TestAppContext) {
cx.foreground().forbid_parking(); cx.foreground().forbid_parking();