Skip to content

Commit

Permalink
Fixed test executable detection using wildcards (#63)
Browse files Browse the repository at this point in the history
* 1.3.4

* Refactored Settings to be testable

* Updated npm packages

* Wrote tests and fixed issue #55

* Fixed merge conflicts
  • Loading branch information
bneumann authored Oct 2, 2024
1 parent 797d9a8 commit 77fb2fe
Show file tree
Hide file tree
Showing 15 changed files with 1,110 additions and 186 deletions.
839 changes: 778 additions & 61 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"icon": "img/icon.png",
"author": "Benjamin Giesinger <benjamin.giesinger@gmail.com>",
"publisher": "bneumann",
"version": "1.3.1",
"version": "1.3.4",
"license": "MIT",
"homepage": "https://github.com/bneumann/CppUTest-Test-Adapter",
"repository": {
Expand Down Expand Up @@ -35,19 +35,20 @@
"publish:pre": "npm run rebuild && vsce publish --pre-release"
},
"dependencies": {
"@types/xml2js": "^0.4.9",
"glob": "^7.2.0",
"glob": "^11.0.0",
"tslib": "^2.3.1",
"vscode-test-adapter-api": "^1.9.0",
"vscode-test-adapter-util": "^0.7.1",
"xml2js": "^0.5.0"
},
"devDependencies": {
"@types/xml2js": "^0.4.9",
"@types/chai": "^4.2.22",
"@types/chai-as-promised": "^7.1.4",
"@types/glob": "^7.1.4",
"@types/mocha": "^9.1.1",
"@types/vscode": "~1.61.0",
"@types/node": "^22.7.4",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"mocha": "^9.2.2",
Expand All @@ -60,9 +61,6 @@
"engines": {
"vscode": "^1.63.0"
},
"extensionDependencies": [
"hbenl.vscode-test-explorer"
],
"activationEvents": [
"onStartupFinished"
],
Expand Down
16 changes: 9 additions & 7 deletions src/Domain/CppUTestContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { CppUTest } from "./CppUTest";
import { TestState } from "./TestState";
import { ResultParser } from "./ResultParser";
import ExecutableRunner from "../Infrastructure/ExecutableRunner";
import { SettingsProvider, TestLocationFetchMode } from "../Infrastructure/SettingsProvider";
import { SettingsProvider } from "../Infrastructure/SettingsProvider";
import { TestLocationFetchMode } from '../Infrastructure/TestLocationFetchMode';
import { VscodeAdapter } from "../Infrastructure/VscodeAdapter";

export default class CppUTestContainer {
Expand Down Expand Up @@ -99,7 +100,7 @@ export default class CppUTestContainer {
public async DebugTest(...testId: string[]): Promise<void> {
const config = this.settingsProvider.GetDebugConfiguration();
const workspaceFolders = this.settingsProvider.GetWorkspaceFolders();
if (config === "") {
if (config === undefined) {
throw new Error("No debug configuration found. Not able to debug!");
}
if (!workspaceFolders) {
Expand All @@ -112,11 +113,12 @@ export default class CppUTestContainer {
const isTest = testOrGroup instanceof CppUTest;
const testRunName = isTest ? `${testOrGroup.group}.${testOrGroup.label}` : `${testOrGroup.label}`;
const testRunArg = isTest ? "-t" : "-sg";
(config as DebugConfiguration).name = testRunName;
(config as DebugConfiguration).args = [testRunArg, testRunName];
(config as DebugConfiguration).program = runner.Command;
(config as DebugConfiguration).target = runner.Command;
await this.vscodeAdapter.StartDebugger((workspaceFolders as WorkspaceFolder[]), config);
const debugConfig = config as unknown as DebugConfiguration;
debugConfig.name = testRunName;
debugConfig.args = [testRunArg, testRunName];
debugConfig.program = runner.Command;
debugConfig.target = runner.Command;
await this.vscodeAdapter.StartDebugger((workspaceFolders as WorkspaceFolder[]), debugConfig);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/ExecutableRunner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ExecException } from "child_process";
import { basename, dirname } from "path";
import { ProcessExecuter } from "../Application/ProcessExecuter";
import { TestLocationFetchMode } from "./SettingsProvider";
import { TestLocationFetchMode } from './TestLocationFetchMode';
import { Log } from "vscode-test-adapter-util";

export enum RunResultStatus {
Expand Down
7 changes: 7 additions & 0 deletions src/Infrastructure/IDebugConfiguration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

export interface IDebugConfiguration {
target?: string;
program?: string;
args?: string[];
name: string;
}
11 changes: 11 additions & 0 deletions src/Infrastructure/IWorkspaceConfiguration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

export interface IWorkspaceConfiguration {
testExecutable: string | undefined;
testExecutablePath: string | undefined;
testLocationFetchMode: any;
logfile: string | undefined;
logpanel: boolean;
debugLaunchConfigurationName: string | undefined;
objDumpExecutable: string | undefined;
preLaunchTask: string;
}
6 changes: 6 additions & 0 deletions src/Infrastructure/IWorkspaceFolder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export interface IWorkspaceFolder {
readonly uri: any;
readonly name: string;
readonly index: number;
}
13 changes: 13 additions & 0 deletions src/Infrastructure/Infrastructure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IDebugConfiguration } from "./IDebugConfiguration";
import { IWorkspaceConfiguration } from "./IWorkspaceConfiguration";
import { IWorkspaceFolder } from "./IWorkspaceFolder";
import { SettingsProvider } from "./SettingsProvider";
import { TestLocationFetchMode } from "./TestLocationFetchMode";
import { VscodeAdapter } from "./VscodeAdapter";

export { IDebugConfiguration };
export { TestLocationFetchMode };
export { SettingsProvider };
export { IWorkspaceConfiguration };
export { IWorkspaceFolder };
export { VscodeAdapter };
102 changes: 87 additions & 15 deletions src/Infrastructure/SettingsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,90 @@
import { DebugConfiguration, WorkspaceFolder } from 'vscode';
import { Log } from 'vscode-test-adapter-util';
import { IWorkspaceFolder } from './IWorkspaceFolder';
import { IDebugConfiguration } from './IDebugConfiguration';
import { IWorkspaceConfiguration } from './IWorkspaceConfiguration';
import { TestLocationFetchMode } from './TestLocationFetchMode';

export enum TestLocationFetchMode {
Auto,
TestQuery,
DebugDump,
Disabled
}
export abstract class SettingsProvider {
protected log: Log;
protected config: IWorkspaceConfiguration;

constructor(log: Log) {
const configSection = "cpputestTestAdapter";

this.log = log;
this.config = this.GetConfig(configSection);
}

protected abstract GetConfig(section: string): IWorkspaceConfiguration;
public abstract GetObjDumpPath(): string;
public abstract GetTestRunners(): string[];
public abstract GetTestPath(): string;
public abstract GetPreLaunchTask(): string;
public abstract GetDebugConfiguration(): (IDebugConfiguration | undefined);
public abstract GetWorkspaceFolders(): readonly IWorkspaceFolder[] | undefined
protected abstract GetCurrentFilename(): string
protected abstract GetCurrentWorkspaceFolder(): string
protected abstract GlobFiles(wildcardString: string): string[]

public get TestLocationFetchMode(): TestLocationFetchMode {
switch (this.config.testLocationFetchMode) {
case 'test query':
return TestLocationFetchMode.TestQuery;
case 'debug dump':
return TestLocationFetchMode.DebugDump;
case 'auto':
return TestLocationFetchMode.Auto;
case 'disabled':
default:
return TestLocationFetchMode.Disabled;
}
}

protected IsCCppDebugger(config: any) {
const isWin = process.platform === "win32";
// This is my way of saying: If we are using windows check for a config that has an .exe program.
const executionExtension: boolean = isWin ? config.program.endsWith(".exe") : true;
return config.request == 'launch' &&
typeof config.type == 'string' &&
executionExtension &&
(config.type.startsWith('cpp') ||
config.type.startsWith('lldb') ||
config.type.startsWith('gdb'));
}

protected SplitRunners(executablesString: string | undefined): string[] {
if (executablesString) {
if (executablesString.indexOf(";") === -1) {
return this.GlobFiles(this.ResolveSettingsVariable(executablesString));
}
return executablesString
.split(";")
.map(r => this.ResolveSettingsVariable(r))
.map(r => this.GlobFiles(r))
.reduce((flatten, arr) => [...flatten, ...arr]);
} else {
return [];
}
}

export interface SettingsProvider {
GetObjDumpPath(): string;
GetTestRunners(): string[];
GetTestPath(): string;
GetPreLaunchTask(): string;
get TestLocationFetchMode(): TestLocationFetchMode;
GetDebugConfiguration(): (DebugConfiguration | string);
GetWorkspaceFolders(): readonly WorkspaceFolder[] | undefined
/**
* This function converts some of the VSCode variables like workspaceFolder
* into their correspoing values. This is a workaround for https://github.com/microsoft/vscode/issues/46471
* @param input Input string from settings.json
*/
protected ResolveSettingsVariable(input: string | undefined): string {
if (input) {
const result: string[] | null = input.match(/\$\{(.*)\}/gmi);
if (result && result.length > 0) {
this.log.info(`replacing config variabe "${input}"`);
input = input.replace(/(\$\{file\})/gmi, this.GetCurrentFilename());
input = input.replace(/(\$\{workspaceFolder\})/gmi, this.GetCurrentWorkspaceFolder());
this.log.info(`replaced variable is now "${input}"`);
}
return input;
}
else {
return "";
}
}
}
7 changes: 7 additions & 0 deletions src/Infrastructure/TestLocationFetchMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

export enum TestLocationFetchMode {
Auto,
TestQuery,
DebugDump,
Disabled
}
Loading

0 comments on commit 77fb2fe

Please sign in to comment.