From 003f493a4d359d532aaeeab3e43ff2c31a846565 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Tue, 2 Nov 2021 14:35:40 +0800 Subject: [PATCH] feat: Add 'envFile' in test configuration (#1326) --- .github/workflows/build.yml | 9 +++++ .vscode/launch.json | 2 +- package.json | 5 +++ package.nls.json | 1 + package.nls.zh.json | 1 + src/runConfigs.ts | 35 +++++++++++++++++++- src/utils/launchUtils.ts | 20 ++++------- test/index.ts | 1 + test/suite/JUnitAnalyzer.test.ts | 28 ++-------------- test/suite/config.test.ts | 57 ++++++++++++++++++++++++++++++++ test/suite/utils.ts | 56 +++++++++++++++++++++++++++++++ 11 files changed, 174 insertions(+), 41 deletions(-) create mode 100644 test/suite/config.test.ts create mode 100644 test/suite/utils.ts diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 107d49a..2442f9f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -40,6 +40,9 @@ jobs: - name: Build OSGi bundle run: npm run build-plugin + - name: prepublish + run: npm run vscode:prepublish + - name: Test extension run: DISPLAY=:99 npm test @@ -73,6 +76,9 @@ jobs: - name: Build OSGi bundle run: npm run build-plugin + - name: prepublish + run: npm run vscode:prepublish + - name: Test extension run: npm test @@ -106,6 +112,9 @@ jobs: - name: Build OSGi bundle run: npm run build-plugin + - name: prepublish + run: npm run vscode:prepublish + - name: Test extension run: npm test diff --git a/.vscode/launch.json b/.vscode/launch.json index 627a12b..cb91eea 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -29,7 +29,7 @@ "type": "extensionHost", "request": "launch", "runtimeExecutable": "${execPath}", - "args": ["--extensionDevelopmentPath=${workspaceFolder}", "--extensionTestsPath=${workspaceFolder}/out/test/suite" ], + "args": ["${workspaceFolder}/test/test-projects/junit", "--extensionDevelopmentPath=${workspaceFolder}", "--extensionTestsPath=${workspaceFolder}/out/test/suite" ], "sourceMaps": true, "outFiles": [ "${workspaceFolder}/dist/**/*.js" ], "preLaunchTask": "npm: compile" diff --git a/package.json b/package.json index edc2a55..4ff6a13 100644 --- a/package.json +++ b/package.json @@ -197,6 +197,11 @@ "description": "%configuration.java.test.config.env.description%", "default": {} }, + "envFile": { + "type": "string", + "description": "%configuration.java.test.config.envFile.description%", + "default": "${workspaceFolder}/.env" + }, "sourcePaths": { "type": "array", "items": { diff --git a/package.nls.json b/package.nls.json index f89d5e9..18e6386 100644 --- a/package.nls.json +++ b/package.nls.json @@ -13,6 +13,7 @@ "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", + "configuration.java.test.config.envFile.description": "Specify the absolute path to a file containing environment variable definitions.", "configuration.java.test.config.sourcePaths.description": "Specify extra source paths when debugging the tests", "contributes.viewsWelcome.inLightWeightMode": "No test cases are listed because the Java Language Server is currently running in [LightWeight Mode](https://aka.ms/vscode-java-lightweight). To show test cases, click on the button to switch to Standard Mode.\n[Switch to Standard Mode](command:java.server.mode.switch?%5B%22Standard%22,true%5D)", "contributes.viewsWelcome.noProjectWithProjectManagerInstalled": "No folder opened in Visual Studio Code. You can [open a Java project](command:_java.project.open), or create a new Java project by clicking the button below.\n[Create Java Project](command:java.project.create)", diff --git a/package.nls.zh.json b/package.nls.zh.json index d48f28c..ddeaab3 100644 --- a/package.nls.zh.json +++ b/package.nls.zh.json @@ -13,6 +13,7 @@ "configuration.java.test.config.vmArgs.description": "设定启动 JVM 的额外选项和系统属性", "configuration.java.test.config.args.description": "设定启动 Test Runner 时的命令行参数", "configuration.java.test.config.env.description": "启动应用程序时自定义的环境变量", + "configuration.java.test.config.envFile.description": "环境变量文件绝对路径", "configuration.java.test.config.sourcePaths.description": "设定调试测试用例时的源代码路径", "contributes.viewsWelcome.inLightWeightMode": "由于 Java 语言服务正运行在 [LightWeight 模式](https://aka.ms/vscode-java-lightweight)下,因此测试用例将不会展示在该视图中。如果您需要展示测试用例,可以点击下方按钮将 Java 语言服务切换至 Standard 模式。\n[切换至 Standard 模式](command:java.server.mode.switch?%5B%22Standard%22,true%5D)", "contributes.viewsWelcome.noProjectWithProjectManagerInstalled": "当前没有已打开的文件夹,您可以[打开一个 Java 项目](command:_java.project.open),或点击下方按钮创建一个新的 Java 项目。\n[创建 Java 项目](command:java.project.create)", diff --git a/src/runConfigs.ts b/src/runConfigs.ts index e977b87..abd51ed 100644 --- a/src/runConfigs.ts +++ b/src/runConfigs.ts @@ -4,13 +4,46 @@ import { Configurations } from './constants'; export interface IExecutionConfig { + /** + * The name of the configuration item. + * @since 0.14.0 + */ name?: string; + /** + * The working directory when running the tests. + * @since 0.14.0 + */ workingDirectory?: string; + /** + * The command line arguments which will be passed to the test runner. + * @since 0.14.0 + */ args?: any[]; - // deprecated, we should align with the debug launch configuration, which is 'vmArgs' + /** + * the extra options and system properties for the JVM. + * It's deprecated, we should align with the debug launch configuration, which is 'vmArgs'. + * @since 0.14.0 + */ vmargs?: any[]; + /** + * the extra options and system properties for the JVM. + * @since 0.14.0 + */ vmArgs?: any[]; + /** + * The extra environment variables when running the tests. + * @since 0.25.0 + */ env?: { [key: string]: string; }; + /** + * The absolute path to a file containing environment variable definitions. + * @since 0.33.0 + */ + envFile?: string; + /** + * The extra source paths when debugging the tests + * @since 0.22.4 + */ sourcePaths?: string[]; } diff --git a/src/utils/launchUtils.ts b/src/utils/launchUtils.ts index b0c4c41..4a89426 100644 --- a/src/utils/launchUtils.ts +++ b/src/utils/launchUtils.ts @@ -16,22 +16,12 @@ import { executeJavaLanguageServerCommand } from './commandUtils'; export async function resolveLaunchConfigurationForRunner(runner: BaseRunner, testContext: IRunTestContext, config?: IExecutionConfig): Promise { const launchArguments: IJUnitLaunchArguments = await getLaunchArguments(testContext); - let env: {} = {}; - if (config && config.env) { - env = config.env; - } - if (config && config.vmArgs) { launchArguments.vmArguments.push(...config.vmArgs.filter(Boolean)); } else if (config && config.vmargs) { launchArguments.vmArguments.push(...config.vmargs.filter(Boolean)); } - const moreEntries: {[key: string]: any} = {}; - if (config && config.sourcePaths) { - moreEntries['sourcePaths'] = config.sourcePaths; - } - if (testContext.kind === TestKind.TestNG) { return { name: `Launch Java Tests - ${testContext.testItems[0].label}`, @@ -48,9 +38,10 @@ export async function resolveLaunchConfigurationForRunner(runner: BaseRunner, te modulePaths: launchArguments.modulepath, args: runner.getApplicationArgs(config), vmArgs: launchArguments.vmArguments, - env, + env: config?.env, + envFile: config?.envFile, noDebug: !testContext.isDebug, - ...moreEntries, + sourcePaths: config?.sourcePaths, }; } @@ -65,9 +56,10 @@ export async function resolveLaunchConfigurationForRunner(runner: BaseRunner, te modulePaths: launchArguments.modulepath, args: launchArguments.programArguments, vmArgs: launchArguments.vmArguments, - env, + env: config?.env, + envFile: config?.envFile, noDebug: !testContext.isDebug, - ...moreEntries, + sourcePaths: config?.sourcePaths, }; } diff --git a/test/index.ts b/test/index.ts index 63eb8f9..c889642 100644 --- a/test/index.ts +++ b/test/index.ts @@ -33,6 +33,7 @@ async function main(): Promise { extensionTestsPath: path.resolve(__dirname, 'suite'), launchArgs: [ '--disable-workspace-trust', + path.join(__dirname, '..', '..', 'test', 'test-projects', 'junit'), ], }); diff --git a/test/suite/JUnitAnalyzer.test.ts b/test/suite/JUnitAnalyzer.test.ts index d72c746..0a1b1a1 100644 --- a/test/suite/JUnitAnalyzer.test.ts +++ b/test/suite/JUnitAnalyzer.test.ts @@ -5,10 +5,10 @@ import * as assert from 'assert'; import * as sinon from 'sinon'; -import { IRunTestContext, TestKind, TestLevel } from '../../src/types'; -import { MarkdownString, Range, TestController, TestItem, TestMessage, TestRunRequest, tests, Uri, workspace } from 'vscode'; +import { IRunTestContext, TestKind } from '../../src/types'; +import { MarkdownString, TestController, TestMessage, TestRunRequest, tests, workspace } from 'vscode'; import { JUnitRunnerResultAnalyzer } from '../../src/runners/junitRunner/JUnitRunnerResultAnalyzer'; -import { dataCache } from '../../src/controller/testItemDataCache'; +import { generateTestItem } from './utils'; // tslint:disable: only-arrow-functions // tslint:disable: no-object-literal-type-assertion @@ -126,25 +126,3 @@ java.lang.RuntimeException assert.strictEqual(testMessage.location?.range.start.line, 14); }); }); - -function generateTestItem(testController: TestController, id: string, testKind: TestKind): TestItem { - if (!id) { - throw new Error('id cannot be null'); - } - - const projectName = id.substring(0, id.indexOf('@')); - const fullName = id.substring(id.indexOf('@') + 1); - const label = id.substring(id.indexOf('#') + 1); - - const testItem = testController.createTestItem(id, label, Uri.file('/mock/test/TestAnnotation.java')); - testItem.range = new Range(0, 0, 0, 0); - dataCache.set(testItem, { - jdtHandler: '', - fullName, - projectName, - testLevel: TestLevel.Method, - testKind, - }); - - return testItem; -} diff --git a/test/suite/config.test.ts b/test/suite/config.test.ts new file mode 100644 index 0000000..abdd236 --- /dev/null +++ b/test/suite/config.test.ts @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +'use strict'; + +import assert = require('assert'); +import { TestController, TestRunRequest, tests, workspace } from 'vscode'; +import { JUnitRunner } from '../../src/runners/junitRunner/JunitRunner'; +import { IRunTestContext, TestKind } from '../../src/types'; +import { resolveLaunchConfigurationForRunner } from '../../src/utils/launchUtils'; +import { generateTestItem, setupTestEnv } from './utils'; + +// tslint:disable: only-arrow-functions +// tslint:disable: no-object-literal-type-assertion +suite('JUnit Runner Analyzer Tests', () => { + + let testController: TestController; + + suiteSetup(async function() { + await setupTestEnv(); + }); + + setup(() => { + testController = tests.createTestController('testController', 'Mock Test'); + }); + + teardown(() => { + testController.dispose(); + }); + + test("test launch configuration", async () => { + const testItem = generateTestItem(testController, 'junit@junit4.TestAnnotation#shouldPass', TestKind.JUnit, '=junit/src\\/test\\/java=/optional=/true=/=/maven.pomderived=/true=/=/test=/true=/ { + setTimeout(resolve, ms); + }); +}