From b7f30090cd84ca70460ab15e51a1fe1a184fa064 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Wed, 13 Feb 2019 16:39:47 +0800 Subject: [PATCH 1/2] Support in all the fields in the test configurations --- src/runners/baseRunner/BaseRunner.ts | 3 +- src/runners/runnerExecutor.ts | 8 +++-- src/utils/settingUtils.ts | 54 +++++++++++++++++++++++----- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/runners/baseRunner/BaseRunner.ts b/src/runners/baseRunner/BaseRunner.ts index cfee2df0..9c43d420 100644 --- a/src/runners/baseRunner/BaseRunner.ts +++ b/src/runners/baseRunner/BaseRunner.ts @@ -14,7 +14,6 @@ import { IExecutionConfig } from '../../runConfigs'; import * as classpathUtils from '../../utils/classpathUtils'; import { resolveRuntimeClassPath } from '../../utils/commandUtils'; import { killProcess } from '../../utils/cpUtils'; -import { resolveWorkingDirectory } from '../../utils/settingUtils'; import { ITestRunner } from '../ITestRunner'; import { ITestResult } from '../models'; import { BaseRunnerResultAnalyzer } from './BaseRunnerResultAnalyzer'; @@ -57,7 +56,7 @@ export abstract class BaseRunner implements ITestRunner { public async run(): Promise { const commandParams: string[] = await this.constructCommandParams(); - const options: cp.SpawnOptions = { cwd: this.config ? resolveWorkingDirectory(this.tests[0].uri, this.config.workingDirectory) : undefined, env: process.env }; + const options: cp.SpawnOptions = { cwd: this.config ? this.config.workingDirectory : undefined, env: process.env }; if (this.config && this.config.env) { options.env = {...this.config.env, ...options.env}; } diff --git a/src/runners/runnerExecutor.ts b/src/runners/runnerExecutor.ts index 94ceb68f..0e9f48e2 100644 --- a/src/runners/runnerExecutor.ts +++ b/src/runners/runnerExecutor.ts @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. -import { CancellationToken, ExtensionContext, Progress, ProgressLocation, window } from 'vscode'; +import { CancellationToken, ExtensionContext, Progress, ProgressLocation, Uri, window } from 'vscode'; import { testCodeLensProvider } from '../codeLensProvider'; import { logger } from '../logger/logger'; import { ITestItem, TestKind } from '../protocols'; @@ -10,6 +10,7 @@ import { testConfigManager } from '../testConfigManager'; import { testReportProvider } from '../testReportProvider'; import { testResultManager } from '../testResultManager'; import { testStatusBarProvider } from '../testStatusBarProvider'; +import { resolve } from '../utils/settingUtils'; import { ITestRunner } from './ITestRunner'; import { JUnit4Runner } from './junit4Runner/Junit4Runner'; import { JUnit5Runner } from './junit5Runner/JUnit5Runner'; @@ -43,13 +44,16 @@ class RunnerExecutor { logger.info('Test job is canceled.'); continue; } + + const resolvedConfig: IExecutionConfig = resolve(config, Uri.parse(tests[0].uri)); + await window.withProgress( { location: ProgressLocation.Notification, cancellable: true }, async (progress: Progress, token: CancellationToken): Promise => { token.onCancellationRequested(() => { this.cleanUp(true /* isCancel */); }); - await runner.setup(tests, isDebug, config); + await runner.setup(tests, isDebug, resolvedConfig); testStatusBarProvider.showRunningTest(); progress.report({ message: 'Running tests...'}); await runner.execPreLaunchTaskIfExist(); diff --git a/src/utils/settingUtils.ts b/src/utils/settingUtils.ts index d13cc01f..832e9af6 100644 --- a/src/utils/settingUtils.ts +++ b/src/utils/settingUtils.ts @@ -1,9 +1,11 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. +import * as _ from 'lodash'; import { Uri, ViewColumn, workspace, WorkspaceConfiguration, WorkspaceFolder } from 'vscode'; import { DEFAULT_LOG_LEVEL, DEFAULT_REPORT_POSITION, LOG_LEVEL_SETTING_KEY, REPORT_POSITION_SETTING_KEY } from '../constants/configs'; import { logger } from '../logger/logger'; +import { IExecutionConfig } from '../runConfigs'; export function getReportPosition(): ViewColumn { const config: WorkspaceConfiguration = workspace.getConfiguration(); @@ -16,14 +18,50 @@ export function getLogLevel(): string { } const workspaceRegexp: RegExp = /\$\{workspacefolder\}/i; -export function resolveWorkingDirectory(testUriString: string, cwd: string | undefined): string | undefined { - if (cwd && workspaceRegexp.test(cwd)) { - const workspaceFolder: WorkspaceFolder | undefined = workspace.getWorkspaceFolder(Uri.parse(testUriString)); - if (workspaceFolder) { - return cwd.replace(workspaceRegexp, workspaceFolder.uri.fsPath); +export function resolve(config: IExecutionConfig, uri: Uri): IExecutionConfig { + const resolvedConfig: IExecutionConfig = {}; + const workspaceFolder: WorkspaceFolder | undefined = workspace.getWorkspaceFolder(uri); + if (!workspaceFolder) { + logger.error(`Failed to parse the working directory for test: ${uri}`); + return config; + } + + if (config.workingDirectory) { + if (workspaceRegexp.test(config.workingDirectory)) { + resolvedConfig.workingDirectory = config.workingDirectory.replace(workspaceRegexp, workspaceFolder.uri.fsPath); + } + } + + if (config.args) { + resolvedConfig.args = []; + for (const arg of config.args) { + if (needResolve(arg)) { + resolvedConfig.args.push(arg.replace(workspaceRegexp, workspaceFolder.uri.fsPath)); + } + } + } + + if (config.vmargs) { + resolvedConfig.vmargs = []; + for (const vmarg of config.vmargs as string[]) { + if (needResolve(vmarg)) { + resolvedConfig.vmargs.push(vmarg.replace(workspaceRegexp, workspaceFolder.uri.fsPath)); + } + } + } + + if (config.env) { + resolvedConfig.env = {}; + for (const key of Object.keys(config.env)) { + if (needResolve(config.env[key])) { + resolvedConfig.env[key] = config.env[key].replace(workspaceRegexp, workspaceFolder.uri.fsPath); + } } - logger.error(`Failed to parse the working directory for test: ${testUriString}`); - return undefined; } - return cwd; + + return resolvedConfig; +} + +function needResolve(value: any): boolean { + return _.isString(value) && workspaceRegexp.test(value); } From f5d94cccad70aea96406b41f4963e7252b82b16a Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Wed, 13 Feb 2019 16:43:11 +0800 Subject: [PATCH 2/2] Simplify the code --- src/runners/runnerExecutor.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/runners/runnerExecutor.ts b/src/runners/runnerExecutor.ts index 0e9f48e2..11767dec 100644 --- a/src/runners/runnerExecutor.ts +++ b/src/runners/runnerExecutor.ts @@ -45,15 +45,13 @@ class RunnerExecutor { continue; } - const resolvedConfig: IExecutionConfig = resolve(config, Uri.parse(tests[0].uri)); - await window.withProgress( { location: ProgressLocation.Notification, cancellable: true }, async (progress: Progress, token: CancellationToken): Promise => { token.onCancellationRequested(() => { this.cleanUp(true /* isCancel */); }); - await runner.setup(tests, isDebug, resolvedConfig); + await runner.setup(tests, isDebug, resolve(config, Uri.parse(tests[0].uri))); testStatusBarProvider.showRunningTest(); progress.report({ message: 'Running tests...'}); await runner.execPreLaunchTaskIfExist();