Skip to content

Commit f669d4a

Browse files
committed
test(lsp): add test for initialize request with multiple workspace folders & options (#16613)
1 parent dd35730 commit f669d4a

File tree

1 file changed

+94
-10
lines changed

1 file changed

+94
-10
lines changed

crates/oxc_language_server/src/tests.rs

Lines changed: 94 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub const FAKE_COMMAND: &str = "fake.command";
2424

2525
const WORKSPACE: &str = "file:///path/to/workspace";
2626

27+
const WORKSPACE_2: &str = "file:///path/to/another_workspace";
28+
2729
impl Tool for FakeTool {
2830
fn name(&self) -> &'static str {
2931
"FakeTool"
@@ -295,17 +297,15 @@ impl TestServer {
295297
}
296298
}
297299

298-
fn initialize_request(
300+
fn initialize_request_workspace_folders(
299301
workspace_configuration: bool,
300302
dynamic_watchers: bool,
301303
workspace_edit: bool,
302304
initialization_options: Option<Value>,
305+
workspace_folders: Vec<WorkspaceFolder>,
303306
) -> Request {
304307
let params = InitializeParams {
305-
workspace_folders: Some(vec![WorkspaceFolder {
306-
uri: WORKSPACE.parse().unwrap(),
307-
name: "workspace".to_string(),
308-
}]),
308+
workspace_folders: Some(workspace_folders),
309309
capabilities: ClientCapabilities {
310310
workspace: Some(WorkspaceClientCapabilities {
311311
apply_edit: Some(workspace_edit),
@@ -327,6 +327,21 @@ fn initialize_request(
327327
Request::build("initialize").params(json!(params)).id(1).finish()
328328
}
329329

330+
fn initialize_request(
331+
workspace_configuration: bool,
332+
dynamic_watchers: bool,
333+
workspace_edit: bool,
334+
initialization_options: Option<Value>,
335+
) -> Request {
336+
initialize_request_workspace_folders(
337+
workspace_configuration,
338+
dynamic_watchers,
339+
workspace_edit,
340+
initialization_options,
341+
vec![WorkspaceFolder { uri: WORKSPACE.parse().unwrap(), name: "workspace".to_string() }],
342+
)
343+
}
344+
330345
fn initialized_notification() -> Request {
331346
let params = InitializedParams {};
332347

@@ -481,11 +496,12 @@ mod test_suite {
481496
use crate::{
482497
backend::Backend,
483498
tests::{
484-
FAKE_COMMAND, FakeToolBuilder, TestServer, WORKSPACE, acknowledge_registrations,
485-
acknowledge_unregistrations, code_action, did_change, did_change_configuration,
486-
did_change_watched_files, did_close, did_open, did_save, execute_command_request,
487-
initialize_request, initialized_notification, response_to_configuration,
488-
shutdown_request, test_configuration_request, workspace_folders_changed,
499+
FAKE_COMMAND, FakeToolBuilder, TestServer, WORKSPACE, WORKSPACE_2,
500+
acknowledge_registrations, acknowledge_unregistrations, code_action, did_change,
501+
did_change_configuration, did_change_watched_files, did_close, did_open, did_save,
502+
execute_command_request, initialize_request, initialize_request_workspace_folders,
503+
initialized_notification, response_to_configuration, shutdown_request,
504+
test_configuration_request, workspace_folders_changed,
489505
},
490506
};
491507

@@ -741,6 +757,74 @@ mod test_suite {
741757
server.shutdown(4).await;
742758
}
743759

760+
#[tokio::test]
761+
async fn test_initialize_with_options_and_multiple_workspace_folders() {
762+
let init_options = json!([
763+
// correctly matches options to workspace folders regardless of order
764+
{
765+
"workspaceUri": WORKSPACE_2,
766+
"options": {
767+
"run": false,
768+
"configPath": "./another_custom.json",
769+
"fmt.experimental": false
770+
}
771+
},
772+
{
773+
"workspaceUri": WORKSPACE,
774+
"options": {
775+
"run": true,
776+
"configPath": "./custom.json",
777+
"fmt.experimental": true
778+
}
779+
},
780+
]);
781+
782+
let mut server = TestServer::new_initialized(
783+
|client| Backend::new(client, server_info(), vec![]),
784+
initialize_request_workspace_folders(
785+
false,
786+
false,
787+
false,
788+
Some(init_options.clone()),
789+
vec![
790+
WorkspaceFolder {
791+
uri: WORKSPACE.parse().unwrap(),
792+
name: "workspace".to_string(),
793+
},
794+
WorkspaceFolder {
795+
uri: WORKSPACE_2.parse().unwrap(),
796+
name: "workspace_2".to_string(),
797+
},
798+
],
799+
),
800+
)
801+
.await;
802+
803+
server.send_request(test_configuration_request(2)).await;
804+
let config_response = server.recv_response().await;
805+
806+
assert!(config_response.is_ok());
807+
assert_eq!(config_response.id(), &Id::Number(2));
808+
assert_eq!(
809+
*config_response.result().unwrap(),
810+
json!([
811+
{
812+
"run": true,
813+
"configPath": "./custom.json",
814+
"fmt.experimental": true
815+
},
816+
{
817+
"run": false,
818+
"configPath": "./another_custom.json",
819+
"fmt.experimental": false
820+
}
821+
])
822+
);
823+
824+
// shutdown request
825+
server.shutdown(3).await;
826+
}
827+
744828
#[tokio::test]
745829
async fn test_execute_workspace_command_with_no_edit() {
746830
let mut server = TestServer::new_initialized(

0 commit comments

Comments
 (0)