Skip to content

Commit 0a16c6f

Browse files
committed
fix(run): preserve typed test runner launch args, build before test, and support JUnit 6
1 parent 1d91edb commit 0a16c6f

3 files changed

Lines changed: 296 additions & 98 deletions

File tree

‎backend/src/run.rs‎

Lines changed: 184 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
use crate::launch::{self, TEST_RUNNER};
3737
use crate::settings_session::{SettingsSession, EXECUTE_COMMAND, REQUEST_METHOD, SERVER_ID};
3838
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
39-
use lithe_java_domain::debug::java_test_launch;
39+
use lithe_java_domain::debug::{java_test_process_launch, JavaTestProcessLaunch};
4040
use lithe_java_domain::execution::{
4141
plan_manifest_classpath_launch, ClasspathJarPlan, ClasspathJarRequest, ClasspathStyle,
4242
};
@@ -55,7 +55,7 @@ use lithe_plugin_sdk::{
5555
BackendHandle, Context, FunctionError, Host, PluginError, SdkError, Subscription, Value,
5656
WorkspaceContext,
5757
};
58-
use serde_json::{json, Map};
58+
use serde_json::json;
5959
use std::collections::{HashMap, HashSet, VecDeque};
6060
use std::io::{BufRead, BufReader, Cursor, Write};
6161
use std::net::TcpListener;
@@ -117,9 +117,9 @@ const BUILD_SUCCEEDED: i64 = 1;
117117
/// a while on a first run.
118118
const LAUNCH_REQUEST_TIMEOUT_MILLISECONDS: u64 = 120_000;
119119

120-
/// Timeout of the JDT LS requests collection makes; the host bounds the whole
121-
/// collection to eight seconds.
122-
const COLLECT_REQUEST_TIMEOUT_MILLISECONDS: u64 = 6_000;
120+
/// Timeout of each JDT LS request collection makes; main-method and test
121+
/// discovery run sequentially inside the host's eight-second budget.
122+
const COLLECT_REQUEST_TIMEOUT_MILLISECONDS: u64 = 3_500;
123123

124124
/// Most actions one answer carries; the contract refuses more.
125125
const MAX_ACTIONS: usize = 1000;
@@ -686,34 +686,22 @@ pub fn collect_actions_function(
686686
let uri = file_uri(&root.join(path))?;
687687
let host = context.host();
688688
let ticket = session.query_started(reference);
689-
let main_methods = match execute_command(
689+
let main_outcome = execute_command(
690690
host,
691691
reference,
692692
java_main_methods_command(&uri),
693693
COLLECT_REQUEST_TIMEOUT_MILLISECONDS,
694-
) {
695-
Ok(value) => value,
696-
Err(error) if is_transient(&error) => {
697-
session.query_finished(reference, ticket, true);
698-
return Ok(json!({"actions": []}));
699-
}
700-
Err(error) => return Err(language_server_failed(error)),
701-
};
694+
);
702695
context.check_cancelled()?;
703-
let test_items = match execute_command(
696+
let test_outcome = execute_command(
704697
host,
705698
reference,
706699
java_test_items_command(&uri),
707700
COLLECT_REQUEST_TIMEOUT_MILLISECONDS,
708-
) {
709-
Ok(value) => value,
710-
Err(error) if is_transient(&error) => {
711-
session.query_finished(reference, ticket, true);
712-
return Ok(json!({"actions": []}));
713-
}
714-
Err(error) => return Err(language_server_failed(error)),
715-
};
716-
session.query_finished(reference, ticket, false);
701+
);
702+
let (main_methods, test_items, deferred) =
703+
combine_collection_outcomes(main_outcome, test_outcome)?;
704+
session.query_finished(reference, ticket, deferred);
717705
let methods = normalize_java_main_methods(&main_methods)
718706
.map(|methods| methods.methods)
719707
.unwrap_or_default();
@@ -972,24 +960,22 @@ fn manifest_jar(manifest: &str) -> Result<Vec<u8>, zip::result::ZipError> {
972960
Ok(archive.finish()?.into_inner())
973961
}
974962

975-
/// Builds the workspace and resolves the classpath of a main class.
976-
fn main_process(
963+
/// Builds the project before resolving main-class or test runner classpaths.
964+
fn build_workspace(
977965
host: &Host,
978966
reference: &WorkspaceContext,
979-
root: &Path,
980-
target: &Target,
981-
) -> Result<Process, FunctionError> {
982-
let main_class = target
983-
.main_class
984-
.as_deref()
985-
.filter(|class| !class.is_empty())
986-
.ok_or_else(|| failure(RUN_INVALID_PARAMS, "The action names no main class."))?;
967+
file_path: &Path,
968+
project_name: Option<&str>,
969+
main_class: Option<&str>,
970+
) -> Result<(), FunctionError> {
987971
let mut build_request = json!({
988-
"mainClass": main_class,
989-
"filePath": root.join(&target.path).to_string_lossy(),
972+
"filePath": file_path.to_string_lossy(),
990973
"isFullBuild": false,
991974
});
992-
if let Some(project) = &target.project_name {
975+
if let Some(main_class) = main_class.filter(|class| !class.is_empty()) {
976+
build_request["mainClass"] = json!(main_class);
977+
}
978+
if let Some(project) = project_name.filter(|project| !project.is_empty()) {
993979
build_request["projectName"] = json!(project);
994980
}
995981
let build = execute_command(
@@ -999,7 +985,29 @@ fn main_process(
999985
LAUNCH_REQUEST_TIMEOUT_MILLISECONDS,
1000986
)
1001987
.map_err(language_server_failed)?;
1002-
require_successful_build(&build)?;
988+
require_successful_build(&build)
989+
}
990+
991+
/// Builds the workspace and resolves the classpath of a main class.
992+
fn main_process(
993+
host: &Host,
994+
reference: &WorkspaceContext,
995+
root: &Path,
996+
target: &Target,
997+
) -> Result<Process, FunctionError> {
998+
let main_class = target
999+
.main_class
1000+
.as_deref()
1001+
.filter(|class| !class.is_empty())
1002+
.ok_or_else(|| failure(RUN_INVALID_PARAMS, "The action names no main class."))?;
1003+
let file_path = root.join(&target.path);
1004+
build_workspace(
1005+
host,
1006+
reference,
1007+
&file_path,
1008+
target.project_name.as_deref(),
1009+
Some(main_class),
1010+
)?;
10031011
let classpath = execute_command(
10041012
host,
10051013
reference,
@@ -1081,6 +1089,13 @@ fn test_process(
10811089
let plan =
10821090
jdt_test_items::plan_debug_launch(&items, &file_name, target.test_identifier.as_deref())
10831091
.map_err(|error| failure(RUN_NO_TESTS, error.message))?;
1092+
build_workspace(
1093+
host,
1094+
reference,
1095+
&file_path,
1096+
Some(plan.project_name.as_str()),
1097+
None,
1098+
)?;
10841099
let launch = execute_command(
10851100
host,
10861101
reference,
@@ -1105,9 +1120,9 @@ fn test_process(
11051120
reference.clone(),
11061121
target.label.clone(),
11071122
)?;
1108-
let configuration = java_test_launch(debug_target.launch_request(listener.port))
1123+
let launch = java_test_process_launch(debug_target.launch_request(listener.port))
11091124
.map_err(|error| failure(RUN_INVALID_PARAMS, error.message))?;
1110-
let arguments = launch_arguments(&configuration.arguments);
1125+
let arguments = process_launch_arguments(&launch);
11111126
Ok((
11121127
Process {
11131128
arguments,
@@ -1117,39 +1132,21 @@ fn test_process(
11171132
))
11181133
}
11191134

1120-
/// Turns the DAP-style launch arguments the domain produces into `java`
1135+
/// Turns the typed launch parameters the domain produces into `java`
11211136
/// arguments: VM arguments, module path, classpath, main class, program
11221137
/// arguments.
1123-
fn launch_arguments(configuration: &Map<String, Value>) -> Vec<String> {
1124-
let strings = |key: &str| -> Vec<String> {
1125-
configuration
1126-
.get(key)
1127-
.and_then(Value::as_array)
1128-
.map(|values| {
1129-
values
1130-
.iter()
1131-
.filter_map(Value::as_str)
1132-
.filter(|value| !value.is_empty())
1133-
.map(str::to_owned)
1134-
.collect()
1135-
})
1136-
.unwrap_or_default()
1137-
};
1138-
let mut arguments = strings("vmArgs");
1139-
let module_paths = strings("modulePaths");
1140-
if !module_paths.is_empty() {
1138+
fn process_launch_arguments(launch: &JavaTestProcessLaunch) -> Vec<String> {
1139+
let mut arguments = launch.vm_arguments.clone();
1140+
if !launch.module_paths.is_empty() {
11411141
arguments.push("--module-path".to_owned());
1142-
arguments.push(module_paths.join(PATH_SEPARATOR));
1142+
arguments.push(launch.module_paths.join(PATH_SEPARATOR));
11431143
}
1144-
let class_paths = strings("classPaths");
1145-
if !class_paths.is_empty() {
1144+
if !launch.class_paths.is_empty() {
11461145
arguments.push("-cp".to_owned());
1147-
arguments.push(class_paths.join(PATH_SEPARATOR));
1146+
arguments.push(launch.class_paths.join(PATH_SEPARATOR));
11481147
}
1149-
if let Some(main_class) = configuration.get("mainClass").and_then(Value::as_str) {
1150-
arguments.push(main_class.to_owned());
1151-
}
1152-
arguments.extend(strings("args"));
1148+
arguments.push(launch.main_class.clone());
1149+
arguments.extend(launch.program_arguments.iter().cloned());
11531150
arguments
11541151
}
11551152

@@ -1362,9 +1359,9 @@ fn execute_command(
13621359

13631360
/// A host failure collection should answer with no actions rather than fail:
13641361
/// the server is not running yet, is busy, or the request timed out.
1365-
fn is_transient(error: &SdkError) -> bool {
1362+
fn is_transient_code(code: &str) -> bool {
13661363
matches!(
1367-
error.code(),
1364+
code,
13681365
"plugin.host.languageServerUnavailable"
13691366
| "plugin.host.busy"
13701367
| "plugin.host.timeout"
@@ -1373,6 +1370,67 @@ fn is_transient(error: &SdkError) -> bool {
13731370
)
13741371
}
13751372

1373+
/// Combines independent `resolveMainMethod` and `findTestTypesAndMethods`
1374+
/// outcomes so a transient warm-up error on one query does not suppress markers
1375+
/// already discovered by the other.
1376+
fn combine_collection_outcomes(
1377+
main_outcome: Result<Value, SdkError>,
1378+
test_outcome: Result<Value, SdkError>,
1379+
) -> Result<(Value, Value, bool), FunctionError> {
1380+
combine_collection_outcomes_with(
1381+
main_outcome,
1382+
test_outcome,
1383+
SdkError::code,
1384+
language_server_failed,
1385+
)
1386+
}
1387+
1388+
fn combine_collection_outcomes_with<E>(
1389+
main_outcome: Result<Value, E>,
1390+
test_outcome: Result<Value, E>,
1391+
code_of: impl Fn(&E) -> &str,
1392+
map_error: impl FnOnce(E) -> FunctionError,
1393+
) -> Result<(Value, Value, bool), FunctionError> {
1394+
let mut deferred = false;
1395+
let mut first_error = None;
1396+
let mut main_ok = false;
1397+
let mut test_ok = false;
1398+
let main_value = match main_outcome {
1399+
Ok(value) => {
1400+
main_ok = true;
1401+
value
1402+
}
1403+
Err(error) if is_transient_code(code_of(&error)) => {
1404+
deferred = true;
1405+
Value::Null
1406+
}
1407+
Err(error) => {
1408+
first_error.get_or_insert(error);
1409+
Value::Null
1410+
}
1411+
};
1412+
let test_value = match test_outcome {
1413+
Ok(value) => {
1414+
test_ok = true;
1415+
value
1416+
}
1417+
Err(error) if is_transient_code(code_of(&error)) => {
1418+
deferred = true;
1419+
Value::Null
1420+
}
1421+
Err(error) => {
1422+
first_error.get_or_insert(error);
1423+
Value::Null
1424+
}
1425+
};
1426+
if !main_ok && !test_ok {
1427+
if let Some(error) = first_error {
1428+
return Err(map_error(error));
1429+
}
1430+
}
1431+
Ok((main_value, test_value, deferred))
1432+
}
1433+
13761434
fn language_server_failed(error: SdkError) -> FunctionError {
13771435
failure(RUN_LANGUAGE_SERVER_FAILED, error.message().to_owned())
13781436
}
@@ -1710,29 +1768,77 @@ mod tests {
17101768
}
17111769

17121770
#[test]
1713-
fn launch_arguments_follow_the_java_command_line_order() {
1714-
let configuration = json!({
1715-
"vmArgs": ["-ea"],
1716-
"modulePaths": [],
1717-
"classPaths": ["/a.jar", "/classes"],
1718-
"mainClass": "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner",
1719-
"args": ["-port", "5000"],
1720-
});
1721-
let arguments = launch_arguments(configuration.as_object().unwrap());
1771+
fn process_launch_arguments_keep_domain_vm_and_runner_arguments() {
1772+
use lithe_java_domain::debug::{JavaTestFramework, JavaTestLaunchRequest};
1773+
1774+
let launch = java_test_process_launch(JavaTestLaunchRequest {
1775+
name: "UserServiceTest".to_owned(),
1776+
framework: JavaTestFramework::Junit,
1777+
working_directory: "/work".to_owned(),
1778+
main_class: "org.eclipse.jdt.internal.junit.runner.RemoteTestRunner".to_owned(),
1779+
project_name: Some("shop".to_owned()),
1780+
class_paths: vec!["/a.jar".to_owned(), "/classes".to_owned()],
1781+
module_paths: vec!["/mods".to_owned()],
1782+
vm_arguments: vec!["-ea".to_owned(), "-Dmode=test".to_owned()],
1783+
program_arguments: vec![
1784+
"-version".to_owned(),
1785+
"3".to_owned(),
1786+
"-port".to_owned(),
1787+
"-1".to_owned(),
1788+
"-classNames".to_owned(),
1789+
"example.UserServiceTest".to_owned(),
1790+
],
1791+
result_port: 5000,
1792+
testng_runner_path: None,
1793+
testng_test_names: Vec::new(),
1794+
})
1795+
.expect("test process launch should resolve");
1796+
let arguments = process_launch_arguments(&launch);
17221797
let expected_classpath = format!("/a.jar{PATH_SEPARATOR}/classes");
17231798
assert_eq!(
17241799
arguments,
17251800
vec![
17261801
"-ea",
1802+
"-Dmode=test",
1803+
"--module-path",
1804+
"/mods",
17271805
"-cp",
17281806
expected_classpath.as_str(),
17291807
"org.eclipse.jdt.internal.junit.runner.RemoteTestRunner",
1808+
"-version",
1809+
"3",
17301810
"-port",
17311811
"5000",
1812+
"-classNames",
1813+
"example.UserServiceTest",
17321814
]
17331815
);
17341816
}
17351817

1818+
#[test]
1819+
fn transient_main_method_failure_does_not_suppress_discovered_tests() {
1820+
let test_payload = json!([{"id": "t1"}]);
1821+
let (main, test, deferred) = combine_collection_outcomes_with(
1822+
Err("plugin.host.busy"),
1823+
Ok(test_payload.clone()),
1824+
|code| *code,
1825+
|code| failure(RUN_LANGUAGE_SERVER_FAILED, code),
1826+
)
1827+
.expect("discovered test items should survive transient main failure");
1828+
assert_eq!(main, Value::Null);
1829+
assert_eq!(test, test_payload);
1830+
assert!(deferred);
1831+
1832+
let err = combine_collection_outcomes_with(
1833+
Err("plugin.host.invalidParams"),
1834+
Err("plugin.host.busy"),
1835+
|code| *code,
1836+
|code| failure(RUN_LANGUAGE_SERVER_FAILED, code),
1837+
)
1838+
.expect_err("non-transient failure should surface when nothing succeeded");
1839+
assert!(!err.to_string().is_empty());
1840+
}
1841+
17361842
#[test]
17371843
fn result_counts_follow_the_eclipse_runner_protocol() {
17381844
let mut counts = ResultCounts::default();

0 commit comments

Comments
 (0)