Skip to content

Commit

Permalink
Set which encoding your test JVM will start with
Browse files Browse the repository at this point in the history
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
  • Loading branch information
awilkins committed Sep 24, 2024
1 parent 3c08f3e commit 1eb57d6
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 40 deletions.
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
8 changes: 7 additions & 1 deletion src/java-test-runner.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -353,4 +359,4 @@ export interface IExecutionConfig {
* @since 0.41.0
*/
when?: string
}
}
64 changes: 25 additions & 39 deletions src/utils/launchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
37 changes: 37 additions & 0 deletions test/suite/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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, '[email protected]#latin1IsSet', TestKind.JUnit, undefined, '=junit/src\\/test\\/java=/optional=/true=/=/maven.pomderived=/true=/=/test=/true=/<junit4{TestAnnotation.java[TestEncoding~latin1IsSet');
const testRunRequest = new TestRunRequest([testItem], []);
const testRun = testController.createTestRun(testRunRequest);
const runnerContext: IRunTestContext = {
isDebug: false,
kind: TestKind.JUnit,
projectName: 'junit',
testItems: [testItem],
testRun: testRun,
workspaceFolder: workspace.workspaceFolders?.[0]!,
};
const junitRunner = new JUnitRunner(runnerContext);
const configuration = await resolveLaunchConfigurationForRunner(junitRunner, runnerContext, {
classPaths: [
"/a/b/c.jar",
"/foo/bar.jar"
],
env: {
test: "test",
},
envFile: "${workspaceFolder}/.env",
encoding: "ISO-8859-1",
javaExec: "/usr/bin/java",
modulePaths: [
"/test/module.jar",
],
postDebugTask: "test",
preLaunchTask: "test",
sourcePaths: [
"/a/b/c.jar"
]
});
assert.strictEqual(configuration.encoding, "ISO-8859-1");
});
});
20 changes: 20 additions & 0 deletions test/test-projects/junit/src/test/java/junit4/TestEncoding.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package junit4;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

import java.nio.charset.Charset;

/**
* The test case in this class will fail unless you set `java.test.config : encoding` to ISO-8859-1
*/
public class TestEncoding {
@Test
public void latin1IsSet() {
assertEquals(
"ISO-8859-1",
Charset.defaultCharset().name()
);
}
}

0 comments on commit 1eb57d6

Please sign in to comment.