Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit f7d5604

Browse files
committed
Auto merge of #1251 - Xanewok:translate-deglob-test, r=Xanewok
Translate test_deglob This fails often so I attempted to translate this to see if that helps. One thing that I noticed is that we return an empty response to 'execute command' request, but we request from the client to apply a text edit using a side-channel and IIRC there are no guarantees which message arrives quicker. Since LSP 3.8 supports returning pre-calculated text edits we should probably use that instead.
2 parents 361b9f2 + d6843c8 commit f7d5604

File tree

2 files changed

+135
-219
lines changed

2 files changed

+135
-219
lines changed

tests/client.rs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::path::Path;
22

33
use futures::future::Future;
44
use lsp_types::{*, request::*, notification::*};
5+
use serde::de::Deserialize;
56
use serde_json::json;
67

78
use crate::support::{basic_bin_manifest, fixtures_dir};
@@ -1164,3 +1165,137 @@ fn client_find_definitions() {
11641165
}
11651166
}
11661167
}
1168+
1169+
#[test]
1170+
fn client_deglob() {
1171+
let p = ProjectBuilder::try_from_fixture(fixtures_dir().join("deglob"))
1172+
.unwrap()
1173+
.build();
1174+
let root_path = p.root();
1175+
let mut rls = p.spawn_rls_async();
1176+
1177+
rls.request::<Initialize>(0, initialize_params(root_path));
1178+
1179+
rls.wait_for_indexing();
1180+
1181+
// Test a single swglob
1182+
let commands = rls.request::<CodeActionRequest>(100, CodeActionParams {
1183+
text_document: TextDocumentIdentifier {
1184+
uri: Url::from_file_path(p.root().join("src/main.rs")).unwrap(),
1185+
},
1186+
range: Range {
1187+
start: Position::new(12, 0),
1188+
end: Position::new(12, 0),
1189+
},
1190+
context: CodeActionContext {
1191+
diagnostics: vec![],
1192+
only: None,
1193+
}
1194+
}).expect("No code actions returned for line 12");
1195+
1196+
// Right now we only support deglobbing via commands. Please update this
1197+
// test if we move to making text edits via CodeAction (which we should for
1198+
// deglobbing);
1199+
let Command { title, command, arguments, .. }= match commands {
1200+
CodeActionResponse::Commands(commands) => commands,
1201+
CodeActionResponse::Actions(_) => unimplemented!(),
1202+
}.into_iter().nth(0).unwrap();
1203+
1204+
let arguments = arguments.expect("Missing command arguments");
1205+
1206+
assert_eq!(title, "Deglob import".to_string());
1207+
assert!(command.starts_with("rls.deglobImports-"));
1208+
1209+
assert!(arguments[0]["new_text"].as_str() == Some("{Stdin, Stdout}"));
1210+
assert_eq!(
1211+
serde_json::from_value::<Location>(arguments[0]["location"].clone()).unwrap(),
1212+
Location {
1213+
range: Range {
1214+
start: Position::new(12, 13),
1215+
end: Position::new(12, 14),
1216+
},
1217+
uri: Url::from_file_path(p.root().join("src/main.rs")).unwrap(),
1218+
}
1219+
);
1220+
1221+
rls.request::<ExecuteCommand>(200, ExecuteCommandParams { command, arguments });
1222+
// Right now the execute command returns an empty response and sends
1223+
// appropriate apply edit request via a side-channel
1224+
let result = rls.messages().iter().rfind(|msg| msg["method"] == ApplyWorkspaceEdit::METHOD).unwrap().clone();
1225+
let params = <ApplyWorkspaceEdit as Request>::Params::deserialize(&result["params"])
1226+
.expect("Couldn't deserialize params");
1227+
1228+
let (url, edits) = params.edit.changes.unwrap().drain().nth(0).unwrap();
1229+
assert_eq!(url, Url::from_file_path(p.root().join("src/main.rs")).unwrap());
1230+
assert_eq!(edits, vec![TextEdit {
1231+
range: Range {
1232+
start: Position::new(12, 13),
1233+
end: Position::new(12, 14),
1234+
},
1235+
new_text: "{Stdin, Stdout}".to_string(),
1236+
}]);
1237+
1238+
// Test a deglob for double wildcard
1239+
let commands = rls.request::<CodeActionRequest>(1100, CodeActionParams {
1240+
text_document: TextDocumentIdentifier {
1241+
uri: Url::from_file_path(p.root().join("src/main.rs")).unwrap(),
1242+
},
1243+
range: Range {
1244+
start: Position::new(15, 0),
1245+
end: Position::new(15, 0),
1246+
},
1247+
context: CodeActionContext {
1248+
diagnostics: vec![],
1249+
only: None,
1250+
}
1251+
}).expect("No code actions returned for line 12");
1252+
1253+
// Right now we only support deglobbing via commands. Please update this
1254+
// test if we move to making text edits via CodeAction (which we should for
1255+
// deglobbing);
1256+
let Command { title, command, arguments, .. }= match commands {
1257+
CodeActionResponse::Commands(commands) => commands,
1258+
CodeActionResponse::Actions(_) => unimplemented!(),
1259+
}.into_iter().nth(0).unwrap();
1260+
1261+
let arguments = arguments.expect("Missing command arguments");
1262+
1263+
assert_eq!(title, "Deglob imports".to_string());
1264+
assert!(command.starts_with("rls.deglobImports-"));
1265+
let expected = [(14, 15, "size_of"), (31, 32, "max")];
1266+
for i in 0..2 {
1267+
assert!(arguments[i]["new_text"].as_str() == Some(expected[i].2));
1268+
assert_eq!(
1269+
serde_json::from_value::<Location>(arguments[i]["location"].clone()).unwrap(),
1270+
Location {
1271+
range: Range {
1272+
start: Position::new(15, expected[i].0),
1273+
end: Position::new(15, expected[i].1),
1274+
},
1275+
uri: Url::from_file_path(p.root().join("src/main.rs")).unwrap(),
1276+
}
1277+
);
1278+
}
1279+
1280+
rls.request::<ExecuteCommand>(1200, ExecuteCommandParams { command, arguments });
1281+
// Right now the execute command returns an empty response and sends
1282+
// appropriate apply edit request via a side-channel
1283+
let result = rls.messages().iter().rfind(|msg| msg["method"] == ApplyWorkspaceEdit::METHOD).unwrap().clone();
1284+
let params = <ApplyWorkspaceEdit as Request>::Params::deserialize(&result["params"])
1285+
.expect("Couldn't deserialize params");
1286+
1287+
let (url, edits) = params.edit.changes.unwrap().drain().nth(0).unwrap();
1288+
assert_eq!(url, Url::from_file_path(p.root().join("src/main.rs")).unwrap());
1289+
assert_eq!(
1290+
edits,
1291+
expected.iter().map(|e| TextEdit {
1292+
range: Range {
1293+
start: Position::new(15, e.0),
1294+
end: Position::new(15, e.1)
1295+
},
1296+
new_text: e.2.to_string()
1297+
}).collect::<Vec<_>>()
1298+
);
1299+
1300+
rls.shutdown();
1301+
}

tests/tests_old.rs

Lines changed: 0 additions & 219 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,225 +1406,6 @@ fn test_no_default_features() {
14061406
// ExpectedMessage::new(None).expect_contains("progress").expect_contains(r#""done":true"#)]);
14071407
// }
14081408

1409-
#[test]
1410-
fn test_deglob() {
1411-
let mut env = Environment::generate_from_fixture("deglob");
1412-
1413-
let source_file_path = Path::new("src").join("main.rs");
1414-
1415-
let root_path = env.cache.abs_path(Path::new("."));
1416-
let url = Url::from_file_path(env.cache.abs_path(&source_file_path))
1417-
.expect("couldn't convert file path to URL");
1418-
let text_doc = TextDocumentIdentifier::new(url.clone());
1419-
let messages = vec![
1420-
initialize(0, root_path.as_os_str().to_str().map(|x| x.to_owned())).to_string(),
1421-
// request deglob for single wildcard
1422-
request::<requests::CodeAction>(
1423-
100,
1424-
CodeActionParams {
1425-
text_document: text_doc.clone(),
1426-
range: env.cache.mk_ls_range_from_line(12),
1427-
context: CodeActionContext {
1428-
diagnostics: vec![],
1429-
only: None,
1430-
},
1431-
},
1432-
).to_string(),
1433-
// deglob single
1434-
request::<requests::ExecuteCommand>(
1435-
200,
1436-
ExecuteCommandParams {
1437-
command: format!("rls.deglobImports-{}", ::std::process::id()),
1438-
arguments: vec![
1439-
serde_json::to_value(&requests::DeglobResult {
1440-
location: Location {
1441-
uri: url.clone(),
1442-
range: Range::new(Position::new(12, 13), Position::new(12, 14)),
1443-
},
1444-
new_text: "{Stdout, Stdin}".into(),
1445-
}).unwrap(),
1446-
],
1447-
},
1448-
).to_string(),
1449-
// request deglob for double wildcard
1450-
request::<requests::CodeAction>(
1451-
1100,
1452-
CodeActionParams {
1453-
text_document: text_doc,
1454-
range: env.cache.mk_ls_range_from_line(15),
1455-
context: CodeActionContext {
1456-
diagnostics: vec![],
1457-
only: None,
1458-
},
1459-
},
1460-
).to_string(),
1461-
// deglob two wildcards
1462-
request::<requests::ExecuteCommand>(
1463-
1200,
1464-
ExecuteCommandParams {
1465-
command: format!("rls.deglobImports-{}", ::std::process::id()),
1466-
arguments: vec![
1467-
serde_json::to_value(&requests::DeglobResult {
1468-
location: Location {
1469-
uri: url.clone(),
1470-
range: Range::new(Position::new(15, 14), Position::new(15, 15)),
1471-
},
1472-
new_text: "size_of".into(),
1473-
}).unwrap(),
1474-
serde_json::to_value(&requests::DeglobResult {
1475-
location: Location {
1476-
uri: url,
1477-
range: Range::new(Position::new(15, 31), Position::new(15, 32)),
1478-
},
1479-
new_text: "max".into(),
1480-
}).unwrap(),
1481-
],
1482-
},
1483-
).to_string(),
1484-
];
1485-
1486-
let (mut server, results, ..) = env.mock_server(messages);
1487-
// Initialize and build.
1488-
assert_eq!(
1489-
ls_server::LsService::handle_message(&mut server),
1490-
ls_server::ServerStateChange::Continue
1491-
);
1492-
expect_message(
1493-
&mut server,
1494-
results.clone(),
1495-
ExpectedMessage::new(Some(0)).expect_contains("rls.deglobImports-"),
1496-
);
1497-
1498-
expect_series(&mut server, results.clone(), vec!["progress"]);
1499-
1500-
assert_eq!(
1501-
ls_server::LsService::handle_message(&mut server),
1502-
ls_server::ServerStateChange::Continue
1503-
);
1504-
{
1505-
server.wait_for_concurrent_jobs();
1506-
let response: Value = serde_json::from_str(&results.lock().unwrap().remove(0)).unwrap();
1507-
assert_eq!(response["id"], 100);
1508-
assert_eq!(response["result"][0]["title"], "Deglob import");
1509-
assert_eq!(
1510-
response["result"][0]["command"],
1511-
&*format!("rls.deglobImports-{}", ::std::process::id())
1512-
);
1513-
let deglob = &response["result"][0]["arguments"][0];
1514-
assert!(
1515-
deglob["location"]["uri"]
1516-
.as_str()
1517-
.unwrap()
1518-
.ends_with("deglob/src/main.rs")
1519-
);
1520-
let deglob_loc = &deglob["location"]["range"];
1521-
assert_eq!(deglob_loc["start"]["line"], 12);
1522-
assert_eq!(deglob_loc["start"]["character"], 13);
1523-
assert_eq!(deglob_loc["end"]["line"], 12);
1524-
assert_eq!(deglob_loc["end"]["character"], 14);
1525-
let mut imports: Vec<_> = deglob["new_text"]
1526-
.as_str()
1527-
.unwrap()
1528-
.trim_matches('{')
1529-
.trim_matches('}')
1530-
.split(", ")
1531-
.collect();
1532-
imports.sort();
1533-
assert_eq!(imports, vec!["Stdin", "Stdout"]);
1534-
}
1535-
1536-
assert_eq!(
1537-
ls_server::LsService::handle_message(&mut server),
1538-
ls_server::ServerStateChange::Continue
1539-
);
1540-
{
1541-
server.wait_for_concurrent_jobs();
1542-
let response: Value = serde_json::from_str(&results.lock().unwrap().remove(0)).unwrap();
1543-
assert_eq!(response["id"], 0x0100_0001);
1544-
assert_eq!(response["method"], "workspace/applyEdit");
1545-
let (key, changes) = response["params"]["edit"]["changes"]
1546-
.as_object()
1547-
.unwrap()
1548-
.iter()
1549-
.next()
1550-
.unwrap();
1551-
assert!(key.ends_with("deglob/src/main.rs"));
1552-
let change = &changes[0];
1553-
assert_eq!(change["range"]["start"]["line"], 12);
1554-
assert_eq!(change["range"]["start"]["character"], 13);
1555-
assert_eq!(change["range"]["end"]["line"], 12);
1556-
assert_eq!(change["range"]["end"]["character"], 14);
1557-
let mut imports: Vec<_> = change["newText"]
1558-
.as_str()
1559-
.expect("newText missing")
1560-
.trim_matches('{')
1561-
.trim_matches('}')
1562-
.split(", ")
1563-
.collect();
1564-
imports.sort();
1565-
assert_eq!(imports, vec!["Stdin", "Stdout"]);
1566-
1567-
let response: Value = serde_json::from_str(&results.lock().unwrap().remove(0)).unwrap();
1568-
assert_eq!(response["id"], 200);
1569-
assert!(response["result"].is_null());
1570-
}
1571-
1572-
assert_eq!(
1573-
ls_server::LsService::handle_message(&mut server),
1574-
ls_server::ServerStateChange::Continue
1575-
);
1576-
expect_message(
1577-
&mut server,
1578-
results.clone(),
1579-
ExpectedMessage::new(Some(1100))
1580-
.expect_contains(r#""title":"Deglob imports""#)
1581-
.expect_contains(r#""command":"rls.deglobImports-"#)
1582-
.expect_contains(r#"{"location":{"range":{"end":{"character":15,"line":15},"start":{"character":14,"line":15}},"uri":"#)
1583-
.expect_contains(r#"deglob/src/main.rs"}"#)
1584-
.expect_contains(r#""new_text":"size_of""#)
1585-
.expect_contains(r#"{"location":{"range":{"end":{"character":32,"line":15},"start":{"character":31,"line":15}},"uri":"#)
1586-
.expect_contains(r#"deglob/src/main.rs"}"#)
1587-
.expect_contains(r#""new_text":"max""#)
1588-
);
1589-
1590-
assert_eq!(
1591-
ls_server::LsService::handle_message(&mut server),
1592-
ls_server::ServerStateChange::Continue
1593-
);
1594-
1595-
{
1596-
server.wait_for_concurrent_jobs();
1597-
let response: Value = serde_json::from_str(&results.lock().unwrap().remove(0)).unwrap();
1598-
assert_eq!(response["id"], 0x0100_0002);
1599-
assert_eq!(response["method"], "workspace/applyEdit");
1600-
let (key, changes) = response["params"]["edit"]["changes"]
1601-
.as_object()
1602-
.unwrap()
1603-
.iter()
1604-
.next()
1605-
.unwrap();
1606-
assert!(key.ends_with("deglob/src/main.rs"));
1607-
let change = &changes[0];
1608-
assert_eq!(change["range"]["start"]["line"], 15);
1609-
assert_eq!(change["range"]["start"]["character"], 14);
1610-
assert_eq!(change["range"]["end"]["line"], 15);
1611-
assert_eq!(change["range"]["end"]["character"], 15);
1612-
assert_eq!(change["newText"], "size_of");
1613-
let change = &changes[1];
1614-
assert_eq!(change["range"]["start"]["line"], 15);
1615-
assert_eq!(change["range"]["start"]["character"], 31);
1616-
assert_eq!(change["range"]["end"]["line"], 15);
1617-
assert_eq!(change["range"]["end"]["character"], 32);
1618-
assert_eq!(change["newText"], "max");
1619-
}
1620-
1621-
expect_message(
1622-
&mut server,
1623-
results,
1624-
ExpectedMessage::new(Some(1200)).expect_contains(r#"null"#),
1625-
);
1626-
}
1627-
16281409
#[test]
16291410
fn test_all_targets() {
16301411
let mut env = Environment::generate_from_fixture("bin_lib");

0 commit comments

Comments
 (0)