From 1eb57d618b29f11367d5ba8fd8189424030ef089 Mon Sep 17 00:00:00 2001 From: Adrian Wilkins Date: Tue, 24 Sep 2024 22:03:07 +0100 Subject: [PATCH 1/6] Set which encoding your test JVM will start with This patch adds a setting to the `java.test.config` object. ```jsonc { "java.test.config": { "encoding": "ISO-8859-1", }, } ``` The aim here is to be able to seamlessly test code which cares about the launch encoding of the JVM. Yes, such code is not good. But sometimes you don't control your dependencies enough to fix this. Since the debug launcher already cares about the `encoding` member of the config, the only thing required is to add the member, which is missing from the launch configs generated by the test plugin. ```typescript // In `configurationProvider.ts:` (vscode-java-debug) // VS Code internal console uses UTF-8 to display output by default. if (config.console === "internalConsole" && !config.encoding) { config.encoding = "UTF-8"; } ``` Fixes #1641 --- package.nls.json | 1 + src/java-test-runner.api.ts | 8 ++- src/utils/launchUtils.ts | 64 ++++++++----------- test/suite/config.test.ts | 37 +++++++++++ .../src/test/java/junit4/TestEncoding.java | 20 ++++++ 5 files changed, 90 insertions(+), 40 deletions(-) create mode 100644 test/test-projects/junit/src/test/java/junit4/TestEncoding.java diff --git a/package.nls.json b/package.nls.json index 3bd0593f..4828c20f 100644 --- a/package.nls.json +++ b/package.nls.json @@ -21,6 +21,7 @@ "configuration.java.test.config.modulePaths.runtime.description": "The modulepaths within 'runtime' scope of current project.", "configuration.java.test.config.modulePaths.test.description": "The modulepaths within 'test' scope of current project.", "configuration.java.test.config.modulePaths.exclude.description": "The path after '!' will be excluded from the modulePaths.", + "configuration.java.test.config.encoding.description": "The 'file.encoding' setting for the test JVM. Only set this if you have to test code that relies on the default encoding being something other than UTF-8.", "configuration.java.test.config.vmArgs.description": "Specify the extra options and system properties for the JVM.", "configuration.java.test.config.args.description": "Specify the command line arguments which will be passed to the test runner.", "configuration.java.test.config.env.description": "Specify the extra environment variables when running the tests.", diff --git a/src/java-test-runner.api.ts b/src/java-test-runner.api.ts index c47a4219..df35283b 100644 --- a/src/java-test-runner.api.ts +++ b/src/java-test-runner.api.ts @@ -279,6 +279,12 @@ export interface IExecutionConfig { */ javaExec?: string; + /** + * the default encoding the JVM will start with. If undefined, this will be UTF-8. + * @since 0.43.0 + */ + encoding?: string; + /** * the extra options and system properties for the JVM. * @since 0.14.0 @@ -353,4 +359,4 @@ export interface IExecutionConfig { * @since 0.41.0 */ when?: string -} \ No newline at end of file +} diff --git a/src/utils/launchUtils.ts b/src/utils/launchUtils.ts index b59c49b8..ede7938e 100644 --- a/src/utils/launchUtils.ts +++ b/src/utils/launchUtils.ts @@ -23,63 +23,49 @@ export async function resolveLaunchConfigurationForRunner(runner: BaseRunner, te sendInfo('', {'deprecatedPropertyUsed': 'vmargs'}); } - let debugConfiguration: DebugConfiguration; + let debugConfiguration: DebugConfiguration = { + name: `Launch Java Tests - ${testContext.testItems[0].label}`, + type: 'java', + request: 'launch', + projectName: launchArguments.projectName, + cwd: config && config.workingDirectory ? config.workingDirectory : launchArguments.workingDirectory, + modulePaths: [ + ...config?.modulePaths || [], + ...launchArguments.modulepath || [], + ], + encoding: config?.encoding, + vmArgs: launchArguments.vmArguments, + env: config?.env, + envFile: config?.envFile, + noDebug: !testContext.isDebug, + sourcePaths: config?.sourcePaths, + preLaunchTask: config?.preLaunchTask, + postDebugTask: config?.postDebugTask, + javaExec: config?.javaExec, + }; + if (testContext.kind === TestKind.TestNG) { - debugConfiguration = { - name: `Launch Java Tests - ${testContext.testItems[0].label}`, - type: 'java', - request: 'launch', + debugConfiguration = Object.assign(debugConfiguration, { mainClass: 'com.microsoft.java.test.runner.Launcher', - projectName: launchArguments.projectName, - cwd: config && config.workingDirectory ? config.workingDirectory : launchArguments.workingDirectory, classPaths: [ ...config?.classPaths || [], ...launchArguments.classpath || [], path.join(extensionContext.extensionPath, 'server', 'com.microsoft.java.test.runner-jar-with-dependencies.jar'), ], - modulePaths: [ - ...config?.modulePaths || [], - ...launchArguments.modulepath || [], - ], args: runner.getApplicationArgs(config), - vmArgs: launchArguments.vmArguments, - env: config?.env, - envFile: config?.envFile, - noDebug: !testContext.isDebug, - sourcePaths: config?.sourcePaths, - preLaunchTask: config?.preLaunchTask, - postDebugTask: config?.postDebugTask, - javaExec: config?.javaExec, - }; + }); } else { - debugConfiguration = { - name: `Launch Java Tests - ${testContext.testItems[0].label}`, - type: 'java', - request: 'launch', + debugConfiguration = Object.assign(debugConfiguration, { mainClass: launchArguments.mainClass, - projectName: launchArguments.projectName, - cwd: config && config.workingDirectory ? config.workingDirectory : launchArguments.workingDirectory, classPaths: [ ...config?.classPaths || [], ...launchArguments.classpath || [], ], - modulePaths: [ - ...config?.modulePaths || [], - ...launchArguments.modulepath || [], - ], args: [ ...launchArguments.programArguments, ...(testContext.kind === TestKind.JUnit5 ? parseTags(config) : []) ], - vmArgs: launchArguments.vmArguments, - env: config?.env, - envFile: config?.envFile, - noDebug: !testContext.isDebug, - sourcePaths: config?.sourcePaths, - preLaunchTask: config?.preLaunchTask, - postDebugTask: config?.postDebugTask, - javaExec: config?.javaExec, - }; + }); } if (testContext.profile?.kind === TestRunProfileKind.Coverage) { diff --git a/test/suite/config.test.ts b/test/suite/config.test.ts index c2d61fdc..f9e71d72 100644 --- a/test/suite/config.test.ts +++ b/test/suite/config.test.ts @@ -4,6 +4,7 @@ 'use strict'; import * as assert from 'assert'; + import { TestController, TestRunRequest, tests, workspace } from 'vscode'; import { JUnitRunner } from '../../src/runners/junitRunner/JunitRunner'; import { resolveLaunchConfigurationForRunner } from '../../src/utils/launchUtils'; @@ -70,4 +71,40 @@ suite('JUnit Runner Analyzer Tests', () => { assert.strictEqual(configuration.classPaths[1], "/foo/bar.jar"); assert.strictEqual(configuration.javaExec, "/usr/bin/java"); }); + + test("test launch encoding", async() => { + const testItem = generateTestItem(testController, 'junit@junit4.TestEncoding#latin1IsSet', TestKind.JUnit, undefined, '=junit/src\\/test\\/java=/optional=/true=/=/maven.pomderived=/true=/=/test=/true=/ Date: Thu, 26 Sep 2024 22:00:31 +0100 Subject: [PATCH 2/6] Added config description to package.json --- package.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/package.json b/package.json index 4a5d9804..bd95e9fc 100644 --- a/package.json +++ b/package.json @@ -254,6 +254,11 @@ "markdownDescription": "%configuration.java.test.config.javaExec.description%", "default": "" }, + "encoding": { + "type": "string", + "description": "%configuration.java.test.config.encoding.description%", + "default": "" + }, "vmArgs": { "type": "array", "items": { From 007ac6b15c4bb894d3237736a8ab0fd2e9d2029f Mon Sep 17 00:00:00 2001 From: Adrian Wilkins Date: Thu, 3 Oct 2024 10:58:24 +0100 Subject: [PATCH 3/6] Update the parts of package.json that the Maven build updates --- package.json | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index bd95e9fc..8f5f557b 100644 --- a/package.json +++ b/package.json @@ -55,18 +55,18 @@ "main": "./main.js", "contributes": { "javaExtensions": [ - "./server/junit-jupiter-api_5.10.3.jar", - "./server/junit-jupiter-engine_5.10.3.jar", - "./server/junit-jupiter-migrationsupport_5.10.3.jar", - "./server/junit-jupiter-params_5.10.3.jar", - "./server/junit-platform-commons_1.10.3.jar", - "./server/junit-platform-engine_1.10.3.jar", - "./server/junit-platform-launcher_1.10.3.jar", - "./server/junit-platform-runner_1.10.3.jar", - "./server/junit-platform-suite-api_1.10.3.jar", - "./server/junit-platform-suite-commons_1.10.3.jar", - "./server/junit-platform-suite-engine_1.10.3.jar", - "./server/junit-vintage-engine_5.10.3.jar", + "./server/junit-jupiter-api_5.11.0.jar", + "./server/junit-jupiter-engine_5.11.0.jar", + "./server/junit-jupiter-migrationsupport_5.11.0.jar", + "./server/junit-jupiter-params_5.11.0.jar", + "./server/junit-platform-commons_1.11.0.jar", + "./server/junit-platform-engine_1.11.0.jar", + "./server/junit-platform-launcher_1.11.0.jar", + "./server/junit-platform-runner_1.11.0.jar", + "./server/junit-platform-suite-api_1.11.0.jar", + "./server/junit-platform-suite-commons_1.11.0.jar", + "./server/junit-platform-suite-engine_1.11.0.jar", + "./server/junit-vintage-engine_5.11.0.jar", "./server/org.apiguardian.api_1.1.2.jar", "./server/org.eclipse.jdt.junit4.runtime_1.3.100.v20231214-1952.jar", "./server/org.eclipse.jdt.junit5.runtime_1.1.300.v20231214-1952.jar", From 87f239e0ebb00144c13d23d66e5f07a608164216 Mon Sep 17 00:00:00 2001 From: Adrian Wilkins Date: Fri, 18 Oct 2024 10:59:04 +0100 Subject: [PATCH 4/6] Change the case of the vmArgs property in the array type of config to match the singular type --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8f5f557b..f3ab5cb5 100644 --- a/package.json +++ b/package.json @@ -414,7 +414,7 @@ "markdownDescription": "%configuration.java.test.config.javaExec.description%", "default": "" }, - "vmargs": { + "vmArgs": { "type": "array", "items": { "type": "string" From cb7577107c5a3b954a582690e20a4add1db0c961 Mon Sep 17 00:00:00 2001 From: Adrian Wilkins Date: Fri, 18 Oct 2024 10:59:33 +0100 Subject: [PATCH 5/6] Add encoding to the array type of config as well --- package.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/package.json b/package.json index f3ab5cb5..40bbb856 100644 --- a/package.json +++ b/package.json @@ -414,6 +414,11 @@ "markdownDescription": "%configuration.java.test.config.javaExec.description%", "default": "" }, + "encoding": { + "type": "string", + "description": "%configuration.java.test.config.encoding.description%", + "default": "" + }, "vmArgs": { "type": "array", "items": { From f2674b948e75d60efaf0585de199c7d67777df3e Mon Sep 17 00:00:00 2001 From: Adrian Wilkins Date: Fri, 18 Oct 2024 11:05:30 +0100 Subject: [PATCH 6/6] Remove handling of deprecated `vmargs` config item. --- src/utils/launchUtils.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/utils/launchUtils.ts b/src/utils/launchUtils.ts index ede7938e..53500e20 100644 --- a/src/utils/launchUtils.ts +++ b/src/utils/launchUtils.ts @@ -18,9 +18,6 @@ export async function resolveLaunchConfigurationForRunner(runner: BaseRunner, te if (config && config.vmArgs) { launchArguments.vmArguments.push(...config.vmArgs.filter(Boolean)); - } else if (config && (config as any).vmargs) { // to support the deprecated property name. - launchArguments.vmArguments.push(...(config as any).vmargs.filter(Boolean)); - sendInfo('', {'deprecatedPropertyUsed': 'vmargs'}); } let debugConfiguration: DebugConfiguration = {