Skip to content

Commit 144ae00

Browse files
committed
Add new test for file watcher
Signed-off-by: Siddhi Agrawal <[email protected]>
1 parent 6872a6d commit 144ae00

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

kclvm/tools/src/LSP/src/tests.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,85 @@ fn complete_import_external_file_e2e_test() {
15271527
}
15281528
}
15291529

1530+
#[test]
1531+
fn mod_file_watcher_modify_test() {
1532+
let path = PathBuf::from(".")
1533+
.join("src")
1534+
.join("test_data")
1535+
.join("watcher")
1536+
.join("modify")
1537+
.canonicalize()
1538+
.unwrap();
1539+
1540+
let mod_file_path = path.join("kcl.mod");
1541+
let main_path = path.join("main.k");
1542+
1543+
let mod_src_bac = std::fs::read_to_string(mod_file_path.clone()).unwrap();
1544+
let main_src = std::fs::read_to_string(main_path.clone()).unwrap();
1545+
1546+
let initialize_params = InitializeParams {
1547+
workspace_folders: Some(vec![WorkspaceFolder {
1548+
uri: Url::from_file_path(path.clone()).unwrap(),
1549+
name: "test".to_string(),
1550+
}]),
1551+
..Default::default()
1552+
};
1553+
let server = Project {}.server(initialize_params);
1554+
1555+
// Mock open file
1556+
server.notification::<lsp_types::notification::DidOpenTextDocument>(
1557+
lsp_types::DidOpenTextDocumentParams {
1558+
text_document: TextDocumentItem {
1559+
uri: Url::from_file_path(main_path.clone()).unwrap(),
1560+
language_id: "KCL".to_string(),
1561+
version: 0,
1562+
text: main_src,
1563+
},
1564+
},
1565+
);
1566+
1567+
// Simulate modifying the kcl.mod file
1568+
std::fs::write(&mod_file_path, "[package]\nname = \"add\"\nedition = \"v0.9.0\"\nversion = \"0.0.1\"\n\n[dependencies]\nhelloworld = \"0.1.4\"\n").unwrap();
1569+
1570+
// wait for download dependence
1571+
wait_async!(5000);
1572+
1573+
let id = server.next_request_id.get();
1574+
server.next_request_id.set(id.wrapping_add(1));
1575+
1576+
let r: Request = Request::new(
1577+
id.into(),
1578+
"textDocument/hover".to_string(),
1579+
HoverParams {
1580+
text_document_position_params: TextDocumentPositionParams {
1581+
text_document: TextDocumentIdentifier {
1582+
uri: Url::from_file_path(main_path).unwrap(),
1583+
},
1584+
position: Position::new(0, 8),
1585+
},
1586+
work_done_progress_params: Default::default(),
1587+
},
1588+
);
1589+
1590+
// Send request and wait for it's response
1591+
let res = server.send_and_receive(r);
1592+
1593+
std::fs::write(mod_file_path, mod_src_bac).unwrap();
1594+
assert_eq!(
1595+
res.result.unwrap(),
1596+
to_json(Hover {
1597+
contents: HoverContents::Scalar(MarkedString::LanguageString(
1598+
lsp_types::LanguageString {
1599+
language: "KCL".to_owned(),
1600+
value: "helloworld: ".to_string(),
1601+
}
1602+
)),
1603+
range: None
1604+
})
1605+
.unwrap()
1606+
)
1607+
}
1608+
15301609
// TODO: wait for fix `kcl mod metadata` to read only. Otherwise it will lead to an infinite loop
15311610
#[test]
15321611
fn mod_file_watcher_test() {

0 commit comments

Comments
 (0)