Skip to content

Commit

Permalink
adding existing jest.nodeEnv into debug config (#1167)
Browse files Browse the repository at this point in the history
  • Loading branch information
connectdotz authored Aug 13, 2024
1 parent 01191b8 commit 000a6d1
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 139 deletions.
85 changes: 50 additions & 35 deletions src/DebugConfigurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ import {
toAbsoluteRootPath,
} from './helpers';
import { platform } from 'os';
import { PluginResourceSettings } from './Settings';

export const DEBUG_CONFIG_PLATFORMS = ['windows', 'linux', 'osx'];
const testNamePatternRegex = /\$\{jest.testNamePattern\}/g;
const testFileRegex = /\$\{jest.testFile\}/g;
const testFilePatternRegex = /\$\{jest.testFilePattern\}/g;

export type DebugConfigOptions = Partial<
Pick<PluginResourceSettings, 'jestCommandLine' | 'rootPath' | 'nodeEnv'>
>;
type PartialDebugConfig = Partial<vscode.DebugConfiguration>;
export class DebugConfigurationProvider implements vscode.DebugConfigurationProvider {
private fileNameToRun = '';
private testToRun = '';
Expand Down Expand Up @@ -176,7 +182,7 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
}

/** return a config if cmd is a package-manager */
private usePM(cmd: string, args: string[]): Partial<vscode.DebugConfiguration | undefined> {
private usePM(cmd: string, args: string[]): PartialDebugConfig | undefined {
const commonConfig = {
program: undefined,
};
Expand All @@ -191,49 +197,58 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
}

/**
* generate a debug config incorporating commandLine and rootPath. Throw exception if error.
* @param cmdLine
* @param rootPath
* @returns a debug config.
* Creates a debug configuration for a given workspace.
*
* @param {vscode.WorkspaceFolder} workspace - The workspace folder for which the debug configuration is created.
* @param {DebugConfigOptions} [options] - Optional parameters to override the default debug configuration.
* @returns {vscode.DebugConfiguration} The final debug configuration.
*
* @throws {Error} If the provided jestCommandLine is invalid.
*
* This function customizes the default debug configuration with the settings from the options parameter,
* such as `rootPath`, `jestCommandLine`, and `nodeEnv`.
* Please note, the platform-specific settings that were not converted are removed.
*/
withCommandLine(
createDebugConfig(
workspace: vscode.WorkspaceFolder,
cmdLine: string,
rootPath?: string
options?: DebugConfigOptions
): vscode.DebugConfiguration {
const config = this.provideDebugConfigurations(workspace)[0];
const [cmd, ...cmdArgs] = parseCmdLine(cmdLine);
if (!cmd) {
throw new Error(`invalid cmdLine: ${cmdLine}`);
}

const absoluteRootPath = rootPath && toAbsoluteRootPath(workspace, rootPath);

let finalConfig: vscode.DebugConfiguration = { ...config };
let args: string[] = [];
let override: PartialDebugConfig = {};

const absoluteRootPath = options?.rootPath && toAbsoluteRootPath(workspace, options.rootPath);
const cwd = absoluteRootPath ?? config.cwd;

const pmConfig = this.usePM(cmd, cmdArgs);
if (pmConfig) {
const args = [...cmdArgs, ...pmConfig.args, ...config.args];
finalConfig = {
...finalConfig,
...pmConfig,
cwd,
args,
};
} else {
// convert the cmd to absolute path
let program = path.isAbsolute(cmd)
? cmd
: absoluteRootPath
? path.resolve(absoluteRootPath, cmd)
: ['${workspaceFolder}', cmd].join(path.sep);
program = this.adjustProgram(program);
const args = [...cmdArgs, ...config.args];
finalConfig = { ...finalConfig, cwd, program, args };
// handle jestCommandLine related overrides
if (options?.jestCommandLine) {
const [cmd, ...cmdArgs] = parseCmdLine(options.jestCommandLine);
if (!cmd) {
throw new Error(`invalid cmdLine: ${options.jestCommandLine}`);
}
const pmConfig = this.usePM(cmd, cmdArgs);
if (pmConfig) {
args = [...cmdArgs, ...pmConfig.args, ...config.args];
override = { ...pmConfig, args };
} else {
let program = path.isAbsolute(cmd)
? cmd
: absoluteRootPath
? path.resolve(absoluteRootPath, cmd)
: ['${workspaceFolder}', cmd].join(path.sep);
program = this.adjustProgram(program);
args = [...cmdArgs, ...config.args];
override = { program, args };
}
}

//handle nodeEnv
if (options?.nodeEnv) {
override = { env: options.nodeEnv, ...override };
}

const finalConfig: vscode.DebugConfiguration = { ...config, cwd, ...override };

// delete platform specific settings since we did not convert them
DEBUG_CONFIG_PLATFORMS.forEach((p) => delete finalConfig[p]);

Expand Down
16 changes: 5 additions & 11 deletions src/JestExt/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,17 +605,11 @@ export class JestExt {
'debug',
'No debug config named "vscode-jest-tests.v2" or "vscode-jest-tests" found in launch.json, will use a default config.'
);
if (this.extContext.settings.jestCommandLine) {
debugConfig = this.debugConfigurationProvider.withCommandLine(
this.extContext.workspace,
this.extContext.settings.jestCommandLine,
this.extContext.settings.rootPath
);
} else {
debugConfig = this.debugConfigurationProvider.provideDebugConfigurations(
this.extContext.workspace
)[0];
}
debugConfig = this.debugConfigurationProvider.createDebugConfig(this.extContext.workspace, {
jestCommandLine: this.extContext.settings.jestCommandLine,
rootPath: this.extContext.settings.rootPath,
nodeEnv: this.extContext.settings.nodeEnv,
});

this.debugConfig = debugConfig;
this.extContext.output.write('auto config debug config:', 'info');
Expand Down
6 changes: 5 additions & 1 deletion src/setup-wizard/tasks/setup-jest-debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ export const setupJestDebug: SetupTask = async (context: WizardContext): Promise
}
}

const debugConfig = debugConfigProvider.withCommandLine(workspace, jestCommandLine, rootPath);
const debugConfig = debugConfigProvider.createDebugConfig(workspace, {
jestCommandLine,
rootPath,
nodeEnv: settings.nodeEnv,
});
message('generated a debug config with jestCommandLine and rootPath:', 'info');
message(`${JSON.stringify(debugConfig, undefined, ' ')}`, 'new-line');

Expand Down
6 changes: 2 additions & 4 deletions src/setup-wizard/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { DebugConfigurationProvider } from '../DebugConfigurationProvider';
import { DebugConfigurationProvider, DebugConfigOptions } from '../DebugConfigurationProvider';
import { JestExtOutput } from '../JestExt/output-terminal';
import { WorkspaceManager } from '../workspace-manager';

Expand Down Expand Up @@ -57,9 +57,7 @@ export interface ActionInputBoxOptions<T> extends AllowBackButton, Verbose {
export type SetupTask = (context: WizardContext) => Promise<WizardStatus>;

// settings
export interface WizardSettings {
jestCommandLine?: string;
rootPath?: string;
export interface WizardSettings extends DebugConfigOptions {
absoluteRootPath?: string;
configurations?: vscode.DebugConfiguration[];
}
Expand Down
3 changes: 2 additions & 1 deletion src/setup-wizard/wizard-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
isActionableButton,
WizardContext,
} from './types';
import { VirtualFolderSettings, createJestSettingGetter } from '../Settings';
import { NodeEnv, VirtualFolderSettings, createJestSettingGetter } from '../Settings';
import { existsSync } from 'fs';
import { parseCmdLine, removeSurroundingQuote, toAbsoluteRootPath } from '../helpers';
import { VirtualWorkspaceFolder, isVirtualWorkspaceFolder } from '../virtual-workspace-folder';
Expand Down Expand Up @@ -269,6 +269,7 @@ export const getWizardSettings = (workspaceFolder: vscode.WorkspaceFolder): Wiza
const wsSettings: WizardSettings = {
jestCommandLine: getSetting<string>('jestCommandLine')?.trim() || undefined,
rootPath: getSetting<string>('rootPath')?.trim() || undefined,
nodeEnv: getSetting<NodeEnv>('nodeEnv') || undefined,
};

// populate jest settings
Expand Down
Loading

0 comments on commit 000a6d1

Please sign in to comment.