Skip to content

Commit

Permalink
Wrote tests and fixed issue #55
Browse files Browse the repository at this point in the history
  • Loading branch information
bneumann committed Oct 2, 2024
1 parent a758236 commit d4330df
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/Infrastructure/SettingsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import glob = require('glob');
import { Log } from 'vscode-test-adapter-util';
import { IWorkspaceFolder } from './IWorkspaceFolder';
import { IDebugConfiguration } from './IDebugConfiguration';
Expand All @@ -25,6 +24,7 @@ export abstract class SettingsProvider {
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) {
Expand Down Expand Up @@ -55,12 +55,12 @@ export abstract class SettingsProvider {
protected SplitRunners(executablesString: string | undefined): string[] {
if (executablesString) {
if (executablesString.indexOf(";") === -1) {
return [this.ResolveSettingsVariable(executablesString)];
return this.GlobFiles(this.ResolveSettingsVariable(executablesString));
}
return executablesString
.split(";")
.map(r => this.ResolveSettingsVariable(r))
.map(r => glob.sync(r))
.map(r => this.GlobFiles(r))
.reduce((flatten, arr) => [...flatten, ...arr]);
} else {
return [];
Expand Down
27 changes: 21 additions & 6 deletions src/Infrastructure/VscodeSettingsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as vscode from 'vscode';
import glob = require('glob');
import { SettingsProvider } from './SettingsProvider';
import { IWorkspaceConfiguration } from './IWorkspaceConfiguration';
import { IDebugConfiguration } from './IDebugConfiguration';
Expand All @@ -7,15 +8,25 @@ import { IWorkspaceFolder } from './IWorkspaceFolder';


export default class VscodeSettingsProvider extends SettingsProvider {
private configSection: string = "cpputestTestAdapter";

constructor(log: Log) {
super(log)

// vscode.workspace.onDidChangeConfiguration(event => {
// if (event.affectsConfiguration(configSection)) {
// this.config = vscode.workspace.getConfiguration(configSection);
// }
// })
vscode.workspace.onDidChangeConfiguration(event => {
if (event.affectsConfiguration(this.configSection)) {
const wsConfig = vscode.workspace.getConfiguration(this.configSection);
this.config = {
debugLaunchConfigurationName: wsConfig["debugLaunchConfigurationName"],
logfile: wsConfig["logfile"],
logpanel: wsConfig["logpanel"],
objDumpExecutable: wsConfig["objDumpExecutable"],
testExecutable: wsConfig["testExecutable"],
testExecutablePath: wsConfig["testExecutablePath"],
testLocationFetchMode: wsConfig["testLocationFetchMode"]
}
}
})
}

protected override GetConfig(configSection: string): IWorkspaceConfiguration {
Expand Down Expand Up @@ -50,7 +61,7 @@ export default class VscodeSettingsProvider extends SettingsProvider {
const hasConfiguredLaunchProfiles: boolean = this.config.debugLaunchConfigurationName !== undefined;

if (wpLaunchConfigs && Array.isArray(wpLaunchConfigs) && wpLaunchConfigs.length > 0) {
if(hasConfiguredLaunchProfiles) {
if (hasConfiguredLaunchProfiles) {
// try and match the config by name
for (let i = 0; i < wpLaunchConfigs.length; ++i) {
if (wpLaunchConfigs[i].name == this.config.debugLaunchConfigurationName) {
Expand Down Expand Up @@ -79,6 +90,10 @@ export default class VscodeSettingsProvider extends SettingsProvider {
return undefined;
}

protected GlobFiles(wildcardString: string): string[] {
return glob.sync(wildcardString)
}

protected override GetCurrentFilename(): string {
return vscode.window.activeTextEditor ? vscode.window.activeTextEditor.document.uri.fsPath : "";
}
Expand Down
91 changes: 76 additions & 15 deletions tests/SettingsProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { Log } from 'vscode-test-adapter-util';
import { expect } from "chai";


class TestSettingsProvider extends SettingsProvider{
class TestSettingsProvider extends SettingsProvider {
protected config: IWorkspaceConfiguration;
private filesToFind: string[];

constructor(logger: Log, vscodeSettings: IWorkspaceConfiguration) {
constructor(logger: Log, vscodeSettings: IWorkspaceConfiguration, filesToFind: string[]) {
super(logger);

this.config = vscodeSettings;
this.filesToFind = filesToFind;
}
protected GetConfig(section: string): IWorkspaceConfiguration {
return this.config;
Expand All @@ -27,7 +29,7 @@ class TestSettingsProvider extends SettingsProvider{
}
GetDebugConfiguration(): (IDebugConfiguration | undefined) {
return {
name :"",
name: "",
args: undefined,
program: undefined,
target: undefined
Expand All @@ -46,27 +48,86 @@ class TestSettingsProvider extends SettingsProvider{
protected GetCurrentWorkspaceFolder(): string {
return "myFolder";
}

protected GlobFiles(wildcardString: string): string[] {
if(wildcardString.indexOf("*") === -1)
return [wildcardString];
const splitPath = wildcardString.split("/");
const testPrefix = splitPath.slice(-1)[0].slice(0, -1);
const binFolder = splitPath.slice(0, -1).join("/")
return this.filesToFind
.filter(f => f.indexOf(testPrefix) != -1)
.map(file => `${binFolder}/${file}`);
}
}

describe("SettingsProvider should", () => {
it("return all executables", () => {
const logger = mock<Log>();
const expectedPath = "/mnt/myPath";

const config = {
debugLaunchConfigurationName: "",
objDumpExecutable : "",
objDumpExecutable: "",
logfile: "",
logpanel: false,
testExecutable: "",
testExecutablePath: expectedPath,
testExecutablePath: "",
testLocationFetchMode: ""
}
};
let filesToFind = [""];

it("return all executables", () => {
const logger = mock<Log>();
const expectedPath = "/mnt/myPath/myExecutable";

config.testExecutable = "/mnt/myPath/myExecutable";

const settingsProvider = new TestSettingsProvider(logger, config, filesToFind);

const actualPath = settingsProvider.GetTestRunners()
expect(actualPath).to.have.members([expectedPath]);
})

it("return all executables with wildcard", () => {
const logger = mock<Log>();
const expectedPath = [
"myFolder/unittest/build/HAL_UnitTests1",
"myFolder/unittest/build/HAL_UnitTests2",
"myFolder/unittest/build/HAL_UnitTests3"];

config.testExecutable = "${workspaceFolder}/unittest/build/HAL_UnitTests*";
filesToFind = [
"HAL_UnitTests1",
"HAL_UnitTests2",
"HAL_UnitTests3",
]
const settingsProvider = new TestSettingsProvider(logger, config, filesToFind);

const actualPath = settingsProvider.GetTestRunners()
expect(actualPath).to.be.deep.eq(expectedPath);
})

it("return all executables in different paths with wildcard", () => {
const logger = mock<Log>();
const expectedPath = [
"myFolder/app/build/App_UnitTests1",
"myFolder/app/build/App_UnitTests2",
"myFolder/hal/build/HAL_UnitTests1",
"myFolder/hal/build/HAL_UnitTests2",
"myFolder/hal/build/HAL_UnitTests3",
"myFolder/pal/build/PAL_UnitTests1",
];

const settingsProvider = new TestSettingsProvider(logger, config);
config.testExecutable = "${workspaceFolder}/app/build/App_UnitTests*;" +
"${workspaceFolder}/pal/build/PAL_UnitTests*;" +
"${workspaceFolder}/hal/build/HAL_UnitTests*";
filesToFind = [
"App_UnitTests1",
"App_UnitTests2",
"HAL_UnitTests1",
"HAL_UnitTests2",
"HAL_UnitTests3",
"PAL_UnitTests1"
]
const settingsProvider = new TestSettingsProvider(logger, config, filesToFind);

const actualPath = settingsProvider.GetTestPath()
expect(actualPath).to.be.eq(expectedPath);
})
const actualPath = settingsProvider.GetTestRunners()
expect(actualPath).to.have.members(expectedPath);
})
});

0 comments on commit d4330df

Please sign in to comment.