Skip to content

Commit 7553eb3

Browse files
committed
add integration tests for create project command
1 parent de0e180 commit 7553eb3

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//! integration tests for ink! Language Server create project command.
2+
3+
mod utils;
4+
5+
#[test]
6+
fn create_project_command_works() {
7+
// Creates an in-memory connection to an initialized LSP server.
8+
let client_connection = utils::create_initialized_lsp_server();
9+
10+
// Creates test project.
11+
let project_name = "hello_ink";
12+
let project_uri = lsp_types::Url::parse("file:///tmp/hello_ink/").unwrap();
13+
14+
// Creates LSP execute command request.
15+
use lsp_types::request::Request;
16+
let req_id = lsp_server::RequestId::from("create-project::hello_ink".to_string());
17+
let req = lsp_server::Request {
18+
id: req_id.clone(),
19+
method: lsp_types::request::ExecuteCommand::METHOD.to_string(),
20+
params: serde_json::to_value(&lsp_types::ExecuteCommandParams {
21+
command: "createProject".to_string(),
22+
arguments: vec![serde_json::json!({
23+
"name": project_name,
24+
"root": project_uri
25+
})],
26+
work_done_progress_params: Default::default(),
27+
})
28+
.unwrap(),
29+
};
30+
// Sends LSP execute command request from client to server.
31+
client_connection.sender.send(req.into()).unwrap();
32+
33+
// Retrieves the LSP execute command response (from the server) on the client.
34+
let resp = client_connection
35+
.receiver
36+
.iter()
37+
.find_map(|message| match message {
38+
lsp_server::Message::Response(it) => Some(it),
39+
_ => None,
40+
})
41+
.unwrap();
42+
// Verifies that the execute command response is for the current request
43+
// and includes the expected results (i.e. successful but with an empty result).
44+
assert_eq!(resp.id, req_id);
45+
assert_eq!(resp.result, Some(serde_json::Value::Null));
46+
47+
// Retrieves the LSP `workspace/applyEdit` request (from the server) on the client.
48+
let workspace_edit_req = client_connection
49+
.receiver
50+
.iter()
51+
.find_map(|message| match message {
52+
lsp_server::Message::Request(it) => {
53+
(it.method == lsp_types::request::ApplyWorkspaceEdit::METHOD).then_some(it)
54+
}
55+
_ => None,
56+
})
57+
.unwrap();
58+
// Verifies the expected workspace edit/ document changes.
59+
let params: lsp_types::ApplyWorkspaceEditParams =
60+
serde_json::from_value(workspace_edit_req.params).unwrap();
61+
assert_eq!(params.label.as_deref(), Some("new ink! project"));
62+
let document_changes = match params.edit.document_changes.unwrap() {
63+
lsp_types::DocumentChanges::Edits(_) => None,
64+
lsp_types::DocumentChanges::Operations(it) => Some(it),
65+
}
66+
.unwrap();
67+
// Verifies that `lib.rs` and `Cargo.toml` files are created and contain expected content.
68+
let lib_uri = project_uri.clone().join("lib.rs").unwrap();
69+
let cargo_uri = project_uri.clone().join("Cargo.toml").unwrap();
70+
let contains_file_create = |uri: &lsp_types::Url| {
71+
document_changes.iter().any(|change| match change {
72+
lsp_types::DocumentChangeOperation::Op(lsp_types::ResourceOp::Create(it)) => {
73+
&it.uri == uri
74+
}
75+
_ => false,
76+
})
77+
};
78+
let contains_file_content = |uri: &lsp_types::Url, pat: &str| {
79+
document_changes.iter().any(|change| match change {
80+
lsp_types::DocumentChangeOperation::Edit(it) => {
81+
let edit_text = match &it.edits[0] {
82+
lsp_types::OneOf::Left(it) => &it.new_text,
83+
lsp_types::OneOf::Right(it) => &it.text_edit.new_text,
84+
};
85+
&it.text_document.uri == uri && edit_text.contains(pat)
86+
}
87+
_ => false,
88+
})
89+
};
90+
assert!(contains_file_create(&lib_uri));
91+
assert!(contains_file_create(&cargo_uri));
92+
assert!(contains_file_content(
93+
&lib_uri,
94+
"#[ink::contract]\npub mod hello_ink {"
95+
));
96+
assert!(contains_file_content(&cargo_uri, r#"name = "hello_ink""#));
97+
}

crates/lsp-server/tests/utils/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
33
use std::thread;
44

5-
/// Creates an LSP server, starts its message dispatch loop and returns a in memory connection to it to simulate a connected client.
5+
/// Creates an LSP server, starts its message dispatch loop and
6+
/// returns a in memory connection to it to simulate a connected client.
67
pub fn create_initialized_lsp_server() -> lsp_server::Connection {
78
// Creates pair of in-memory connections to simulate an LSP client and server.
89
let (server_connection, client_connection) = lsp_server::Connection::memory();
910

1011
// Creates client capabilities.
1112
let client_capabilities = test_utils::simple_client_config();
1213

13-
// Runs the message dispatch loop on a separate thread (because `ink_lsp_server::main_loop` function is blocking).
14+
// Runs the message dispatch loop on a separate thread
15+
// (because `ink_lsp_server::main_loop` function is blocking).
1416
thread::spawn(|| ink_lsp_server::main_loop(server_connection, client_capabilities));
1517

1618
// Returns client connection.

0 commit comments

Comments
 (0)