├── .gitignore ├── img ├── icon.png ├── kofi.png ├── tests.png ├── test-explorer.svg └── icon.svg ├── bin ├── testrunner └── testrunner.exe ├── GitVersion.yml ├── src ├── Domain │ ├── TestState.ts │ ├── ResultParser.ts │ ├── uuid.ts │ ├── TestResult.ts │ ├── CppUTest.ts │ ├── CppUTestGroup.ts │ ├── CppUTestSuite.ts │ ├── RegexResultParser.ts │ └── CppUTestContainer.ts ├── Infrastructure │ ├── TestLocationFetchMode.ts │ ├── IWorkspaceFolder.ts │ ├── IDebugConfiguration.ts │ ├── VscodeAdapter.ts │ ├── IWorkspaceConfiguration.ts │ ├── Infrastructure.ts │ ├── SettingsProvider.ts │ ├── VscodeSettingsProvider.ts │ └── ExecutableRunner.ts ├── Application │ ├── ProcessExecuter.ts │ ├── VscodeAdapterImplementation.ts │ └── NodeProcessExecuter.ts ├── main.ts └── adapter.ts ├── .mocharc.json ├── .vscodeignore ├── .vscode └── launch.json ├── tsconfig.json ├── .github └── workflows │ ├── gitversion.yml │ ├── unit_tests.yml │ └── do_release.yml ├── test ├── secondTests.cpp └── basicTests.cpp ├── .devcontainer └── devcontainer.json ├── LICENSE ├── CMakeLists.txt ├── esbuild.js ├── tests ├── CppUTest.spec.ts ├── CppUTestGroup.spec.ts ├── RegexResultParser.spec.ts ├── CppUTestSuite.spec.ts ├── testResults.json ├── ExecutableRunner.spec.ts ├── SettingsProvider.spec.ts └── CppUTestContainer.spec.ts ├── README.md └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | out/ 3 | build* 4 | HowTo.md 5 | *.vsix -------------------------------------------------------------------------------- /img/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bneumann/CppUTest-Test-Adapter/HEAD/img/icon.png -------------------------------------------------------------------------------- /img/kofi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bneumann/CppUTest-Test-Adapter/HEAD/img/kofi.png -------------------------------------------------------------------------------- /img/tests.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bneumann/CppUTest-Test-Adapter/HEAD/img/tests.png -------------------------------------------------------------------------------- /bin/testrunner: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bneumann/CppUTest-Test-Adapter/HEAD/bin/testrunner -------------------------------------------------------------------------------- /bin/testrunner.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bneumann/CppUTest-Test-Adapter/HEAD/bin/testrunner.exe -------------------------------------------------------------------------------- /GitVersion.yml: -------------------------------------------------------------------------------- 1 | mode: Mainline 2 | branches: {} 3 | ignore: 4 | sha: [] 5 | merge-message-formats: {} 6 | -------------------------------------------------------------------------------- /src/Domain/TestState.ts: -------------------------------------------------------------------------------- 1 | export enum TestState { 2 | Unknown, 3 | Passed, 4 | Failed, 5 | Skipped, 6 | Errored 7 | } 8 | -------------------------------------------------------------------------------- /.mocharc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extension": ["ts"], 3 | "package": "./package.json", 4 | "spec": "**/*.spec.ts", 5 | "require": "ts-node/register" 6 | } -------------------------------------------------------------------------------- /src/Infrastructure/TestLocationFetchMode.ts: -------------------------------------------------------------------------------- 1 | 2 | export enum TestLocationFetchMode { 3 | Auto, 4 | TestQuery, 5 | DebugDump, 6 | Disabled 7 | } 8 | -------------------------------------------------------------------------------- /src/Application/ProcessExecuter.ts: -------------------------------------------------------------------------------- 1 | export interface ProcessExecuter { 2 | Exec: Function; 3 | ExecFile: Function; 4 | KillProcess: Function; 5 | } 6 | -------------------------------------------------------------------------------- /src/Infrastructure/IWorkspaceFolder.ts: -------------------------------------------------------------------------------- 1 | 2 | export interface IWorkspaceFolder { 3 | readonly uri: any; 4 | readonly name: string; 5 | readonly index: number; 6 | } 7 | -------------------------------------------------------------------------------- /src/Infrastructure/IDebugConfiguration.ts: -------------------------------------------------------------------------------- 1 | 2 | export interface IDebugConfiguration { 3 | target?: string; 4 | program?: string; 5 | args?: string[]; 6 | name: string; 7 | } 8 | -------------------------------------------------------------------------------- /src/Domain/ResultParser.ts: -------------------------------------------------------------------------------- 1 | import { RunResult } from "../Infrastructure/ExecutableRunner"; 2 | import { TestResult } from "./TestResult"; 3 | 4 | export interface ResultParser { 5 | GetResult(testOutput: RunResult): TestResult; 6 | } 7 | -------------------------------------------------------------------------------- /.vscodeignore: -------------------------------------------------------------------------------- 1 | src/** 2 | **/*.map 3 | package-lock.json 4 | tsconfig.json 5 | .vscode/** 6 | .gitignore 7 | node_modules 8 | .devcontainer 9 | .github 10 | build 11 | build_linux 12 | bin 13 | CMakeLists.txt 14 | test 15 | tests 16 | GitVersion.yml 17 | -------------------------------------------------------------------------------- /src/Infrastructure/VscodeAdapter.ts: -------------------------------------------------------------------------------- 1 | import { DebugConfiguration, WorkspaceFolder } from "vscode"; 2 | 3 | export interface VscodeAdapter { 4 | StartDebugger(workspaceFolders: WorkspaceFolder[], config: string | DebugConfiguration): Promise; 5 | } 6 | -------------------------------------------------------------------------------- /src/Domain/uuid.ts: -------------------------------------------------------------------------------- 1 | const uuid = () => { 2 | function s4() { 3 | return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); 4 | } 5 | return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); 6 | }; 7 | export default uuid; 8 | -------------------------------------------------------------------------------- /src/Domain/TestResult.ts: -------------------------------------------------------------------------------- 1 | import { TestState } from "./TestState"; 2 | 3 | export class TestResult { 4 | public readonly message: string; 5 | public readonly state: TestState; 6 | 7 | constructor(state: TestState, message: string) { 8 | this.state = state; 9 | this.message = message; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/Infrastructure/IWorkspaceConfiguration.ts: -------------------------------------------------------------------------------- 1 | 2 | export interface IWorkspaceConfiguration { 3 | testExecutable: string | undefined; 4 | testExecutablePath: string | undefined; 5 | testLocationFetchMode: any; 6 | logfile: string | undefined; 7 | logpanel: boolean; 8 | debugLaunchConfigurationName: string | undefined; 9 | objDumpExecutable: string | undefined; 10 | preLaunchTask: string; 11 | } 12 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "type": "extensionHost", 6 | "request": "launch", 7 | "name": "CppUTest adapter", 8 | "runtimeExecutable": "${execPath}", 9 | "args": [ 10 | "--extensionDevelopmentPath=${workspaceFolder}" 11 | ], 12 | "preLaunchTask": "npm: build", 13 | "outFiles": [ 14 | "${workspaceFolder}/out/**/*.js" 15 | ] 16 | } 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es2020", 4 | "lib": [ "es2020" ], 5 | "module": "commonjs", 6 | "outDir": "out", 7 | "importHelpers": true, 8 | "sourceMap": true, 9 | "strict": true, 10 | "noImplicitReturns": true, 11 | "noUnusedLocals": true, 12 | "removeComments": true, 13 | "skipLibCheck": true, 14 | }, 15 | "exclude": [ 16 | "node_modules" 17 | ], 18 | "include": [ 19 | "src/main.ts" 20 | ] 21 | } 22 | -------------------------------------------------------------------------------- /src/Application/VscodeAdapterImplementation.ts: -------------------------------------------------------------------------------- 1 | import { DebugConfiguration, WorkspaceFolder } from "vscode"; 2 | import * as vscode from "vscode"; 3 | import { VscodeAdapter } from "../Infrastructure/VscodeAdapter"; 4 | 5 | export class VscodeAdapterImplementation implements VscodeAdapter { 6 | public async StartDebugger(workspaceFolders: WorkspaceFolder[], config: string | DebugConfiguration): Promise { 7 | await vscode.debug.startDebugging(workspaceFolders[0], config); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /.github/workflows/gitversion.yml: -------------------------------------------------------------------------------- 1 | name: Gitversion workflow 2 | 3 | on: 4 | workflow_call: 5 | 6 | jobs: 7 | reusable_workflow_job: 8 | runs-on: ubuntu-latest 9 | steps: 10 | - name: install gitversion tool 11 | uses: gittools/actions/gitversion/setup@v0.9 12 | with: 13 | versionSpec: '5.1.x' 14 | 15 | - name: execute gitversion 16 | id: gitversion # step id used as reference for output values 17 | uses: gittools/actions/gitversion/execute@v0.9 -------------------------------------------------------------------------------- /.github/workflows/unit_tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | on: [push] 3 | jobs: 4 | build: 5 | name: 'Unit Tests' 6 | runs-on: ubuntu-latest 7 | 8 | steps: 9 | - uses: actions/checkout@v2 10 | 11 | - name: Use Node.js 12 | uses: actions/setup-node@v1 13 | with: 14 | node-version: '22.x' 15 | 16 | - name: Install dependencies 17 | run: npm install 18 | 19 | - name: Build assembly 20 | run: npm run build --if-present 21 | 22 | - name: Run Tests 23 | run: npm test 24 | -------------------------------------------------------------------------------- /src/Infrastructure/Infrastructure.ts: -------------------------------------------------------------------------------- 1 | import { IDebugConfiguration } from "./IDebugConfiguration"; 2 | import { IWorkspaceConfiguration } from "./IWorkspaceConfiguration"; 3 | import { IWorkspaceFolder } from "./IWorkspaceFolder"; 4 | import { SettingsProvider } from "./SettingsProvider"; 5 | import { TestLocationFetchMode } from "./TestLocationFetchMode"; 6 | import { VscodeAdapter } from "./VscodeAdapter"; 7 | 8 | export { IDebugConfiguration }; 9 | export { TestLocationFetchMode }; 10 | export { SettingsProvider }; 11 | export { IWorkspaceConfiguration }; 12 | export { IWorkspaceFolder }; 13 | export { VscodeAdapter }; 14 | -------------------------------------------------------------------------------- /test/secondTests.cpp: -------------------------------------------------------------------------------- 1 | #include "CppUTest/TestHarness_c.h" 2 | #include "CppUTest/CommandLineTestRunner.h" 3 | 4 | TEST_GROUP(OtherTests) 5 | { 6 | }; 7 | 8 | TEST(OtherTests, AnotherStuff) 9 | { 10 | CHECK_TEXT(true, "This is failing"); 11 | } 12 | 13 | IGNORE_TEST(OtherTests, ShouldBeIgnored) 14 | { 15 | CHECK_TEXT(false, "This is failing"); 16 | } 17 | 18 | IGNORE_TEST(OtherTests, ShouldAlsoBeIgnored) 19 | { 20 | CHECK_TEXT(false, "This is failing"); 21 | } 22 | 23 | TEST(OtherTests, ShouldFail) 24 | { 25 | CHECK_TEXT(false, "This is failing"); 26 | } 27 | 28 | TEST(OtherTests, ShouldPass) 29 | { 30 | CHECK_TEXT(true, "This is passing"); 31 | } 32 | 33 | int main(int ac, char** av) 34 | { 35 | return CommandLineTestRunner::RunAllTests(ac, av); 36 | } -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | // For format details, see https://aka.ms/devcontainer.json. For config options, see the 2 | // README at: https://github.com/devcontainers/templates/tree/main/src/typescript-node 3 | { 4 | "name": "Node.js & TypeScript", 5 | // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile 6 | "image": "mcr.microsoft.com/devcontainers/typescript-node:1-20-bullseye" 7 | 8 | // Features to add to the dev container. More info: https://containers.dev/features. 9 | // "features": {}, 10 | 11 | // Use 'forwardPorts' to make a list of ports inside the container available locally. 12 | // "forwardPorts": [], 13 | 14 | // Use 'postCreateCommand' to run commands after the container is created. 15 | // "postCreateCommand": "yarn install", 16 | 17 | // Configure tool-specific properties. 18 | // "customizations": {}, 19 | 20 | // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. 21 | // "remoteUser": "root" 22 | } 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /src/Domain/CppUTest.ts: -------------------------------------------------------------------------------- 1 | import { TestInfo } from "vscode-test-adapter-api"; 2 | 3 | export class CppUTest implements TestInfo { 4 | type: "test" = "test"; 5 | id: string; 6 | label: string; 7 | group: string; 8 | description?: string | undefined; 9 | tooltip?: string | undefined; 10 | file?: string | undefined; 11 | line?: number | undefined; 12 | skipped?: boolean | undefined; 13 | 14 | constructor(testString: string, group: string, id: string, file?: string | undefined, line?: number | undefined) { 15 | this.id = id; 16 | this.file = file; 17 | this.line = line; 18 | this.label = testString; 19 | this.group = group; 20 | } 21 | 22 | public AddDebugInformation(testDebugString: string): void { 23 | if(!testDebugString) 24 | return 25 | const symbolInformationLines = testDebugString.split("\n"); 26 | const filePath = symbolInformationLines.filter(si => si.startsWith("/"))[0]; 27 | const debugSymbols: string[] = filePath.split(":"); 28 | const file = debugSymbols[0]; 29 | const line = parseInt(debugSymbols[1], 10); 30 | this.file = file; 31 | this.line = line; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | project(testrunner) 4 | 5 | # CppUTest 6 | include(FetchContent) 7 | FetchContent_Declare( 8 | CppUTest 9 | GIT_REPOSITORY https://github.com/cpputest/cpputest.git 10 | GIT_TAG latest-passing-build # or use release tag, eg. v3.8 11 | ) 12 | # Set this to ON if you want to have the CppUTests in your project as well. 13 | set(TESTS OFF CACHE BOOL "Switch off CppUTest Test build") 14 | FetchContent_MakeAvailable(CppUTest) 15 | 16 | add_executable(testrunner test/basicTests.cpp) 17 | add_dependencies(testrunner CppUTest) 18 | target_link_libraries(testrunner PRIVATE CppUTest) 19 | set_target_properties(testrunner PROPERTIES 20 | RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/bin 21 | RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/bin 22 | ) 23 | 24 | add_executable(testrunner2 test/secondTests.cpp) 25 | add_dependencies(testrunner2 CppUTest) 26 | target_link_libraries(testrunner2 PRIVATE CppUTest) 27 | set_target_properties(testrunner2 PROPERTIES 28 | RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/bin/tests 29 | RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/bin/tests 30 | ) -------------------------------------------------------------------------------- /src/Application/NodeProcessExecuter.ts: -------------------------------------------------------------------------------- 1 | import { ProcessExecuter } from "./ProcessExecuter"; 2 | import { exec, execFile, ChildProcess, ExecFileException } from "child_process"; 3 | 4 | type execCallbackType = (error: ExecFileException | null, stdout: string | Buffer, stderr: string | Buffer) => void; 5 | 6 | export class NodeProcessExecuter implements ProcessExecuter { 7 | public readonly Exec: Function; 8 | public readonly ExecFile: Function; 9 | public readonly KillProcess: Function; 10 | private process: ChildProcess | undefined; 11 | 12 | constructor() { 13 | this.Exec = this.exec; 14 | this.ExecFile = this.execFile; 15 | this.KillProcess = this.killProcess; 16 | } 17 | 18 | private exec(cmd: string, options: any, callback: execCallbackType): ChildProcess { 19 | this.process = exec(cmd, options, callback); 20 | return this.process; 21 | } 22 | 23 | private execFile(cmd: string, args: any, options: any, callback: execCallbackType): ChildProcess { 24 | this.process = execFile(cmd, args, options, callback); 25 | return this.process; 26 | } 27 | 28 | private killProcess(): void { 29 | this.process?.kill("SIGTERM"); 30 | } 31 | } -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import * as vscode from 'vscode'; 2 | import { TestHub, testExplorerExtensionId } from 'vscode-test-adapter-api'; 3 | import { Log, TestAdapterRegistrar } from 'vscode-test-adapter-util'; 4 | import { CppUTestAdapter } from './adapter'; 5 | 6 | export async function activate(context: vscode.ExtensionContext) { 7 | 8 | const workspaceFolder = (vscode.workspace.workspaceFolders || [])[0]; 9 | 10 | // create a simple logger that can be configured with the configuration variables 11 | // `cpputestTestAdapter.logToOutputPanel"` and `cpputestTestAdapter.logToFile` 12 | const log = new Log('cpputestTestAdapter', workspaceFolder, 'CppUTest Test Adapter Log'); 13 | context.subscriptions.push(log); 14 | 15 | // get the Test Explorer extension 16 | const testExplorerExtension = vscode.extensions.getExtension(testExplorerExtensionId); 17 | if (log.enabled) log.info(`Test Explorer ${testExplorerExtension ? '' : 'not '}found`); 18 | 19 | if (testExplorerExtension) { 20 | 21 | const testHub = testExplorerExtension.exports; 22 | // this will register an CppUTestTestAdapter for each WorkspaceFolder 23 | context.subscriptions.push(new TestAdapterRegistrar( 24 | testHub, 25 | workspaceFolder => new CppUTestAdapter(workspaceFolder, log), 26 | log 27 | )); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test/basicTests.cpp: -------------------------------------------------------------------------------- 1 | #include "CppUTest/TestHarness_c.h" 2 | #include "CppUTest/CommandLineTestRunner.h" 3 | 4 | TEST_GROUP(Specials) 5 | { 6 | }; 7 | 8 | TEST_GROUP(SecondClass) 9 | { 10 | }; 11 | 12 | TEST_GROUP(ClassName) 13 | { 14 | }; 15 | 16 | 17 | TEST(Specials, AnotherStuff) 18 | { 19 | CHECK_TEXT(true, "This is failing"); 20 | } 21 | 22 | TEST(Specials, Crash) 23 | { 24 | //*(int*)0=0; 25 | } 26 | 27 | IGNORE_TEST(SecondClass, ShouldBeIgnored) 28 | { 29 | CHECK_TEXT(false, "This is failing"); 30 | } 31 | 32 | IGNORE_TEST(SecondClass, ShouldAlsoBeIgnored) 33 | { 34 | CHECK_TEXT(false, "This is failing"); 35 | } 36 | TEST(SecondClass, ShouldBeNew) 37 | { 38 | CHECK_TEXT(true, "This is passing"); 39 | } 40 | 41 | TEST(SecondClass, ShouldFail) 42 | { 43 | CHECK_TEXT(false, "This is failing"); 44 | } 45 | 46 | TEST(SecondClass, ShouldPass) 47 | { 48 | CHECK_TEXT(true, "This is passing"); 49 | } 50 | 51 | TEST(ClassName, ShouldPass) 52 | { 53 | CHECK_TEXT(true, "passing"); 54 | } 55 | 56 | TEST(ClassName, Create) 57 | { 58 | CHECK_TEXT(false, "This is failing"); 59 | } 60 | 61 | TEST(ClassName, stdOutMessesWithOutputAndFails) 62 | { 63 | printf("This will appear in the output"); 64 | int out = 24 + 74; 65 | CHECK_EQUAL(17, out); 66 | } 67 | 68 | int main(int ac, char** av) 69 | { 70 | return CommandLineTestRunner::RunAllTests(ac, av); 71 | } -------------------------------------------------------------------------------- /esbuild.js: -------------------------------------------------------------------------------- 1 | const esbuild = require('esbuild'); 2 | 3 | const production = process.argv.includes('--production'); 4 | const watch = process.argv.includes('--watch'); 5 | 6 | async function main() { 7 | const ctx = await esbuild.context({ 8 | entryPoints: ['src/main.ts'], 9 | bundle: true, 10 | format: 'cjs', 11 | minify: production, 12 | sourcemap: !production, 13 | sourcesContent: false, 14 | platform: 'node', 15 | outfile: 'out/main.js', 16 | external: ['vscode'], 17 | logLevel: 'silent', 18 | plugins: [ 19 | /* add to the end of plugins array */ 20 | esbuildProblemMatcherPlugin 21 | ] 22 | }); 23 | if (watch) { 24 | await ctx.watch(); 25 | } else { 26 | await ctx.rebuild(); 27 | await ctx.dispose(); 28 | } 29 | } 30 | 31 | /** 32 | * @type {import('esbuild').Plugin} 33 | */ 34 | const esbuildProblemMatcherPlugin = { 35 | name: 'esbuild-problem-matcher', 36 | 37 | setup(build) { 38 | build.onStart(() => { 39 | console.log('[watch] build started'); 40 | }); 41 | build.onEnd(result => { 42 | result.errors.forEach(({ text, location }) => { 43 | console.error(`✘ [ERROR] ${text}`); 44 | console.error(` ${location.file}:${location.line}:${location.column}:`); 45 | }); 46 | console.log('[watch] build finished'); 47 | }); 48 | } 49 | }; 50 | 51 | main().catch(e => { 52 | console.error(e); 53 | process.exit(1); 54 | }); 55 | -------------------------------------------------------------------------------- /img/test-explorer.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/Domain/CppUTestGroup.ts: -------------------------------------------------------------------------------- 1 | import { TestSuiteInfo } from 'vscode-test-adapter-api'; 2 | import { CppUTest } from "./CppUTest"; 3 | 4 | export class CppUTestGroup implements TestSuiteInfo { 5 | type: "suite"; 6 | id: string; 7 | label: string; 8 | description?: string | undefined; 9 | tooltip?: string | undefined; 10 | file?: string | undefined; 11 | line?: number | undefined; 12 | children: (CppUTest | CppUTestGroup)[]; 13 | 14 | constructor(label: string, id: string) { 15 | this.type = "suite"; 16 | this.id = id; 17 | this.label = label; 18 | this.children = new Array(); 19 | } 20 | 21 | AddTest(testName: string, file?: string, line?: number) { 22 | const test: CppUTest = new CppUTest(testName, this.label, this.id + "/" + testName, file, line); 23 | this.children.unshift(test); 24 | } 25 | 26 | AddTestGroup(groupName: string): CppUTestGroup { 27 | const testGroup = new CppUTestGroup(groupName, this.id + "/" + groupName); 28 | this.children.push(testGroup); 29 | return testGroup; 30 | } 31 | 32 | FindTest(id: string): CppUTest[] { 33 | if(this.id === id) { 34 | return this.Tests; 35 | } 36 | return this.Tests.filter(t => t.id === id); 37 | } 38 | 39 | get Tests(): CppUTest[] { 40 | const retVal: CppUTest[] = new Array(); 41 | this.children.forEach(c => { 42 | if (c instanceof CppUTest) { 43 | retVal.push(c); 44 | } 45 | else { 46 | retVal.push(...(c).Tests); 47 | } 48 | }); 49 | return retVal; 50 | } 51 | 52 | get TestGroups(): CppUTestGroup[] { 53 | const retVal: CppUTestGroup[] = new Array(); 54 | this.children.forEach(c => { 55 | if (c instanceof CppUTestGroup) { 56 | retVal.push(c); 57 | } 58 | }); 59 | return retVal; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /tests/CppUTest.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | import { CppUTest } from "../src/Domain/CppUTest"; 3 | 4 | const symbolStrings = [ 5 | { 6 | test: new CppUTest("test1", "group1", "id1"), 7 | value: 8 | "_ZN31TEST_Group1_Test1_TestShellC4Ev():\n" + 9 | "/tmp/myPath/basicTests.cpp:56\n" + 10 | "randomly placed line that confuses the analyzer\n" + 11 | "TEST(Group1, Test1)\n" + 12 | "random information that is not correlated at all" 13 | }, 14 | { 15 | test: new CppUTest("test2", "group1", "id2"), 16 | value: 17 | "_ZN31TEST_Group1_Test1_TestShellC4Ev():\n" + 18 | "randomly placed line that confuses the analyzer\n" + 19 | "/tmp/myPath/basicTests.cpp:56\n" + 20 | "TEST(Group1, Test1)\n" + 21 | "random information that is not correlated at all" 22 | }, 23 | { 24 | test: new CppUTest("test3", "group1", "id3"), 25 | value: 26 | "/tmp/myPath/basicTests.cpp:56" 27 | } 28 | ]; 29 | 30 | describe("CppUTest should", () => { 31 | it("be creatable with all information", () => { 32 | const test = new CppUTest("TestName", "GroupName", "TestName/GroupName", "file.cpp", 53); 33 | expect(test.label).to.be.equal("TestName"); 34 | expect(test.id).to.be.equal("TestName/GroupName"); 35 | expect(test.file).to.be.equal("file.cpp"); 36 | expect(test.line).to.be.equal(53); 37 | }) 38 | 39 | it("be creatable with basic information", () => { 40 | const test = new CppUTest("TestName", "GroupName", "TestName/GroupName"); 41 | expect(test.label).to.be.equal("TestName"); 42 | expect(test.group).to.be.equal("GroupName"); 43 | expect(test.id).to.contain("TestName/GroupName"); 44 | expect(test.file).to.be.equal(undefined); 45 | expect(test.line).to.be.equal(undefined); 46 | }) 47 | 48 | symbolStrings.forEach(symbolString => 49 | it(`create debug information from symbol definition string for ${symbolString.test.label}`, () => { 50 | symbolString.test.AddDebugInformation(symbolString.value); 51 | expect(symbolString.test.file).to.be.equal("/tmp/myPath/basicTests.cpp"); 52 | expect(symbolString.test.line).to.be.equal(56); 53 | }) 54 | ) 55 | }); -------------------------------------------------------------------------------- /tests/CppUTestGroup.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | import { CppUTest } from "../src/Domain/CppUTest"; 3 | import { CppUTestGroup } from "../src/Domain/CppUTestGroup"; 4 | 5 | describe("CppUTestGroup should", () => { 6 | let testGroup: CppUTestGroup; 7 | beforeEach(() => { 8 | testGroup = new CppUTestGroup("TestGroup", "TestGroupId"); 9 | }) 10 | 11 | it("be creatable with all information", () => { 12 | expect(testGroup.label).to.be.equal("TestGroup"); 13 | expect(testGroup.id).to.be.equal("TestGroupId"); 14 | }) 15 | it("be able to add tests", () => { 16 | expect(testGroup.children.length).to.be.eq(0); 17 | testGroup.AddTest("myTest"); 18 | expect(testGroup.children.length).to.be.eq(1); 19 | }) 20 | 21 | it("find test by id", () => { 22 | const test = new CppUTest("myTest", "myGroup", "myId"); 23 | const id = test.id; 24 | testGroup.children.push(test); 25 | testGroup.AddTest("randomTest1"); 26 | testGroup.AddTest("randomTest2"); 27 | testGroup.AddTest("randomTest3"); 28 | testGroup.AddTest("randomTest4"); 29 | 30 | const foundTest = testGroup.FindTest(id); 31 | expect(foundTest).to.have.lengthOf(1); 32 | expect(foundTest[0].id).to.be.eq(id); 33 | }) 34 | 35 | it("find all tests that belong to a group", () => { 36 | testGroup.AddTest("randomTest1"); 37 | testGroup.AddTest("randomTest2"); 38 | testGroup.AddTest("randomTest3"); 39 | testGroup.AddTest("randomTest4"); 40 | 41 | const tests = testGroup.FindTest(testGroup.id); 42 | 43 | expect(tests).to.have.lengthOf(4); 44 | expect(tests).to.be.deep.eq(testGroup.children); 45 | }) 46 | 47 | it("find all tests in nested groups", () => { 48 | const subTestGroup1 = new CppUTestGroup("subTestGroup1", "subTestGroupId1"); 49 | const subTestGroup2 = new CppUTestGroup("subTestGroup2", "subTestGroupId2"); 50 | testGroup.children.push(subTestGroup1); 51 | testGroup.children.push(subTestGroup2); 52 | subTestGroup1.AddTest("test1"); 53 | subTestGroup1.AddTest("test2"); 54 | subTestGroup2.AddTest("test1"); 55 | subTestGroup2.AddTest("test2"); 56 | 57 | const tests = testGroup.FindTest(testGroup.id); 58 | 59 | expect(tests).to.have.lengthOf(4); 60 | expect(tests).to.be.deep.eq(subTestGroup1.children.concat(subTestGroup2.children)); 61 | }) 62 | }); -------------------------------------------------------------------------------- /src/Domain/CppUTestSuite.ts: -------------------------------------------------------------------------------- 1 | import { CppUTestGroup } from './CppUTestGroup'; 2 | 3 | export default class CppUTestSuite extends CppUTestGroup { 4 | 5 | constructor(label: string) { 6 | super(label, label); 7 | } 8 | 9 | public UpdateFromTestListString(testListString: string, hasLocation: boolean): void { 10 | if (hasLocation) { 11 | this.UpdateFromTestListStringWithLocation(testListString); 12 | } else { 13 | this.UpdateFromTestListStringWithoutLocation(testListString); 14 | } 15 | } 16 | 17 | private UpdateFromTestListStringWithoutLocation(testListString: string): void { 18 | const groupAndGroupStrings: string[] = testListString.split(" "); 19 | this.children.splice(0, this.children.length); 20 | groupAndGroupStrings 21 | .map(gs => gs.split(".")) 22 | .map(split => this.UpdateGroupAndTest(split[0], split[1], undefined, undefined)); 23 | } 24 | 25 | private UpdateFromTestListStringWithLocation(testListString: string): void { 26 | const groupAndGroupStrings: string[] = testListString.split("\n").filter(gs => (gs.length > 0)); 27 | this.children.splice(0, this.children.length); 28 | groupAndGroupStrings 29 | .map(gs => this.ParseTestInfo(gs)) 30 | .map(split => this.UpdateGroupAndTest(split[0], split[1], split[2], split[3])); 31 | } 32 | 33 | private UpdateGroupAndTest(groupName: string, testName: string, file: string | undefined, line: string | undefined): void { 34 | let testGroup = this.children.find(c => c.label === groupName) as CppUTestGroup; 35 | if (!testGroup) { 36 | testGroup = this.AddTestGroup(groupName); 37 | } 38 | testGroup.AddTest(testName, file, line ? parseInt(line) : undefined); 39 | } 40 | 41 | private ParseTestInfo(testInfo: string): string[] { 42 | const firstSeparatorIndex = testInfo.indexOf("."); 43 | const secondSeparatorIndex = testInfo.indexOf(".", firstSeparatorIndex + 1); 44 | const lastSeparatorIndex = testInfo.lastIndexOf("."); 45 | const groupName = testInfo.substring(0, firstSeparatorIndex); 46 | const testName = testInfo.substring(firstSeparatorIndex + 1, secondSeparatorIndex); 47 | const file = testInfo.substring(secondSeparatorIndex + 1, lastSeparatorIndex); 48 | const line = testInfo.substring(lastSeparatorIndex + 1); 49 | return [groupName, testName, file, line]; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests/RegexResultParser.spec.ts: -------------------------------------------------------------------------------- 1 | // import { instance, mock } from "ts-mockito"; 2 | 3 | import { expect } from "chai"; 4 | import { readFileSync } from "fs"; 5 | import { RegexResultParser } from "../src/Domain/RegexResultParser"; 6 | import { TestResult } from "../src/Domain/TestResult"; 7 | import { TestState } from "../src/Domain/TestState"; 8 | import { RunResultStatus, RunResult } from "../src/Infrastructure/ExecutableRunner"; 9 | 10 | const allTests: { name: string, status: string, value: string[], expected: string[] }[] = 11 | JSON.parse(readFileSync("tests/testResults.json").toString()); 12 | 13 | describe("RegexResultParser should", () => { 14 | allTests.filter(t => t.name.startsWith("failing")).forEach(test => { 15 | it(`return failed result for '${test.name}''`, () => { 16 | const resultParser = new RegexResultParser(); 17 | 18 | const expectedTestResult = new TestResult(TestState.Failed, test.expected.join("\n")); 19 | const statusFromRunner : RunResultStatus = (RunResultStatus as any)[test.status]; 20 | const stringFromRunner = test.value.join("\n"); 21 | expect(resultParser.GetResult(new RunResult(statusFromRunner, stringFromRunner))).to.be.deep.eq(expectedTestResult); 22 | }) 23 | }) 24 | 25 | allTests.filter(t => t.name.startsWith("passing")).forEach(test => { 26 | it(`return passing result for '${test.name}''`, () => { 27 | const resultParser = new RegexResultParser(); 28 | 29 | const expectedTestResult = new TestResult(TestState.Passed, test.expected.join("\n")); 30 | const statusFromRunner : RunResultStatus = (RunResultStatus as any)[test.status]; 31 | const stringFromRunner = test.value.join("\n"); 32 | expect(resultParser.GetResult(new RunResult(statusFromRunner, stringFromRunner))).to.be.deep.eq(expectedTestResult); 33 | }) 34 | }) 35 | 36 | allTests.filter(t => t.name.startsWith("ignored")).forEach(test => { 37 | it(`return skipped result for '${test.name}''`, () => { 38 | const resultParser = new RegexResultParser(); 39 | 40 | const expectedTestResult = new TestResult(TestState.Skipped, test.expected.join("\n")); 41 | const statusFromRunner : RunResultStatus = (RunResultStatus as any)[test.status]; 42 | const stringFromRunner = test.value.join("\n"); 43 | expect(resultParser.GetResult(new RunResult(statusFromRunner, stringFromRunner))).to.be.deep.eq(expectedTestResult); 44 | }) 45 | }) 46 | }); -------------------------------------------------------------------------------- /src/Domain/RegexResultParser.ts: -------------------------------------------------------------------------------- 1 | import { TestResult } from "./TestResult"; 2 | import { TestState } from "./TestState"; 3 | import { ResultParser } from "./ResultParser"; 4 | import { RunResult, RunResultStatus } from "../Infrastructure/ExecutableRunner"; 5 | 6 | export class RegexResultParser implements ResultParser { 7 | 8 | public GetResult(resultOutput: RunResult): TestResult { 9 | switch (resultOutput.Status) { 10 | case RunResultStatus.Success: 11 | return this.ParseResultSuccess(resultOutput.Text); 12 | case RunResultStatus.Failure: 13 | return this.ParseResultFailure(resultOutput.Text); 14 | case RunResultStatus.Error: 15 | return new TestResult(TestState.Failed, resultOutput.Text.trimRight()); 16 | } 17 | } 18 | 19 | private ParseResultSuccess(resultText: string): TestResult { 20 | const lines = resultText.split("\n"); 21 | const regexPattern: RegExp = /(\w*)_*TEST\((\w*), (\w*)\)/; 22 | const match = regexPattern.exec(lines[0]); 23 | if (match !== null) { 24 | if (match[1] == "IGNORE_") { 25 | return new TestResult(TestState.Skipped, ""); 26 | } else { 27 | return new TestResult(TestState.Passed, ""); 28 | } 29 | } else { 30 | const message = lines.slice(1).join("\n"); 31 | return new TestResult(TestState.Unknown, message.trimRight()); 32 | } 33 | } 34 | 35 | private ParseResultFailure(resultText: string): TestResult { 36 | const lines = resultText.split("\n"); 37 | let state: TestState = TestState.Unknown; 38 | let message = ""; 39 | const regexPatternHeading: RegExp = /(\w*)_*TEST\((\w*), (\w*)\)/; 40 | const match = regexPatternHeading.exec(lines[0]); 41 | if (match === null) { 42 | const message = lines.slice(1).join("\n"); 43 | return new TestResult(TestState.Unknown, message.trimRight()); 44 | } 45 | const regexPatternTail: RegExp = /^\s*-\s\d+\sms\s*$/; 46 | for (const line of lines.slice(1)) { 47 | if (line.includes("Failure in")) { 48 | state = TestState.Failed; 49 | message = message.concat(line, "\n"); 50 | continue; 51 | } 52 | if (state === TestState.Failed) { 53 | const match = regexPatternTail.exec(line); 54 | if (match === null) { 55 | message = message.concat(line, "\n"); 56 | } else { 57 | return new TestResult(state, message.trimRight()); 58 | } 59 | } 60 | } 61 | return new TestResult(state, message.trimRight()); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CppUTest Test Adapter for Visual Studio Code 2 | 3 | [![Tests](https://github.com/bneumann/CppUTest-Test-Adapter/actions/workflows/unit_tests.yml/badge.svg?branch=master)](https://github.com/bneumann/CppUTest-Test-Adapter/actions/workflows/unit_tests.yml) 4 | 5 | 6 | 7 | A Visual Studio Code extension that integrates the [CppUTest](https://cpputest.github.io/) C/C++ unit testing framework into VS Code's Test Explorer. Run, debug, and manage your CppUTest tests seamlessly within your development environment. 8 | 9 | --- 10 | 11 | ## Features 12 | - **Run and debug** CppUTest tests directly from VS Code. 13 | - **Supports multiple test executables** and wildcards for flexible test discovery. 14 | - **Pre-launch tasks** to build tests before execution. 15 | - **Logging support** for debugging and troubleshooting. 16 | 17 | --- 18 | 19 | ## Setup 20 | 21 | ### 1. Configure Test Executables 22 | Add the following to your VS Code `settings.json` to specify your test executables: 23 | 24 | ```json 25 | { 26 | "cpputestTestAdapter.testExecutable": "${workspaceFolder}/test/testrunner;${workspaceFolder}/test/subFolder/ut_*", 27 | "cpputestTestAdapter.testExecutablePath": "\${workspaceFolder}/test" 28 | } 29 | ``` 30 | 31 | - ```testExecutable```: Path(s) to your test executables. Supports wildcards (*) and semicolon-separated lists. 32 | - ```testExecutablePath```: Working directory for running tests. If not set, each executable runs from its own directory. 33 | 34 | Both settings support the ```${workspaceFolder}``` variable. 35 | 36 | ## Pre-Launch Task (Optional) 37 | 38 | To rebuild your tests before running, define a task in tasks.json and reference it: 39 | ```json 40 | { 41 | "cpputestTestAdapter.preLaunchTask": "build-tests" 42 | } 43 | ``` 44 | ## Logging 45 | Enable logging for debugging: 46 | 47 | ```json 48 | { 49 | "cpputestTestAdapter.logpanel": true, 50 | "cpputestTestAdapter.logfile": "C:/temp/cpputest-adapter.log" 51 | } 52 | ``` 53 | - logpanel: Shows logs in VS Code's Output panel ("CppUTest Test Adapter Log"). 54 | - logfile: Saves logs to a file. Use absolute paths and ensure the directory exists. 55 | 56 | ## Debugging 57 | To debug your tests, configure your launch.json like you would with any debugger (in this case gdb): 58 | 59 | ```json 60 | { 61 | "version": "0.2.0", 62 | "configurations": [ 63 | { 64 | "name": "Debug CppUTest", 65 | "type": "cppdbg", 66 | "request": "launch", 67 | "program": "${workspaceFolder}/test/testrunner", 68 | "args": [], 69 | "stopAtEntry": false, 70 | "cwd": "${workspaceFolder}", 71 | "environment": [], 72 | "externalConsole": false, 73 | "MIMode": "gdb", 74 | "miDebuggerPath": "/path/to/gdb" 75 | } 76 | ] 77 | } 78 | ``` 79 | - The adapter automatically attaches to the test process. 80 | 81 | ## Contributing 82 | Contributions are welcome! Feel free to open an issue or submit a pull request. 83 | -------------------------------------------------------------------------------- /.github/workflows/do_release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | on: 3 | push: 4 | branches: 5 | - 'feat/**' 6 | - 'fix/**' 7 | - master 8 | paths-ignore: 9 | - '.vscode/**' 10 | 11 | 12 | jobs: 13 | cicd: 14 | name: Release package to Github page 15 | runs-on: ubuntu-latest 16 | steps: 17 | - name: Checkout repo 18 | uses: actions/checkout@v4 19 | with: 20 | fetch-depth: 0 21 | 22 | - name: Install gitversion tool 23 | uses: gittools/actions/gitversion/setup@v2.0.1 24 | with: 25 | versionSpec: '5.x' 26 | 27 | - name: Execute gitversion 28 | id: gitversion # step id used as reference for output values 29 | uses: gittools/actions/gitversion/execute@v2.0.1 30 | with: 31 | useConfigFile: true 32 | 33 | - name: Setup node 34 | uses: actions/setup-node@v4 35 | with: 36 | node-version: '22.x' 37 | 38 | - name: Clean install dependencies 39 | run: npm ci 40 | 41 | - name: Update metadata in package.json 42 | uses: onlyutkarsh/patch-files-action@v1.0.5 43 | with: 44 | files: '${{github.workspace}}/package.json' 45 | patch-syntax: | 46 | = /version => "${{ steps.gitversion.outputs.semVer }}" 47 | = /displayName => "CppUTest Test Adapter" 48 | = /description => "Run your CppUTest tests in the Sidebar of Visual Studio Code" 49 | 50 | - name: Add version in CHANGELOG.md 51 | uses: cschleiden/replace-tokens@v1.3 52 | with: 53 | files: '${{github.workspace}}/CHANGELOG.md' 54 | env: 55 | VERSION: "${{ steps.gitversion.outputs.semVer }}" 56 | 57 | - name: Compile and create vsix 58 | run: npm run package 59 | 60 | - name: upload vsix as artifact 61 | uses: actions/upload-artifact@v4 62 | with: 63 | name: cpputest-test-adapter-${{steps.gitversion.outputs.semVer}}.vsix 64 | if-no-files-found: error 65 | path: ${{github.workspace}}/cpputest-test-adapter-${{steps.gitversion.outputs.semVer}}.vsix 66 | 67 | - name: Publish to Open VSX Registry 68 | if: github.ref == 'refs/heads/master' 69 | uses: HaaLeo/publish-vscode-extension@v2 70 | id: publishToOpenVSX 71 | with: 72 | pat: ${{ secrets.OPEN_VSX_TOKEN }} 73 | 74 | - name: Publish to Visual Studio Marketplace 75 | if: github.ref == 'refs/heads/master' 76 | uses: HaaLeo/publish-vscode-extension@v2 77 | with: 78 | pat: ${{ secrets.VS_MARKETPLACE_TOKEN }} 79 | registryUrl: https://marketplace.visualstudio.com 80 | extensionFile: ${{ steps.publishToOpenVSX.outputs.vsixPath }} 81 | 82 | - name: create a release 83 | if: github.ref == 'refs/heads/master' 84 | uses: actions/create-release@v1 85 | env: 86 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 87 | with: 88 | tag_name: v${{ steps.gitversion.outputs.semVer }} 89 | release_name: v${{ steps.gitversion.outputs.semVer }} 90 | 91 | -------------------------------------------------------------------------------- /tests/CppUTestSuite.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import { CppUTestGroup } from '../src/Domain/CppUTestGroup'; 3 | import CppUTestSuite from '../src/Domain/CppUTestSuite'; 4 | 5 | describe('CppUTestSuite should', () => { 6 | const suite = new CppUTestSuite("Label"); 7 | 8 | it('create a TestSuite from an test list string without location', () => { 9 | const testListString = "Group1.Test1 Group1.Test2 Group2.Test1"; 10 | suite.UpdateFromTestListString(testListString, false); 11 | expect(suite.label).to.be.equal("Label"); 12 | expect(suite.children.length).to.be.eq(2); 13 | expect(suite.children[0].label).to.be.equal("Group1"); 14 | expect(suite.children[1].label).to.be.equal("Group2"); 15 | expect((suite.children[0] as CppUTestGroup).children.length).to.be.equal(2); 16 | expect((suite.children[0] as CppUTestGroup).children[0].label).to.be.equal("Test2"); 17 | expect((suite.children[0] as CppUTestGroup).children[0].file).to.be.equal(undefined); 18 | expect((suite.children[0] as CppUTestGroup).children[0].line).to.be.equal(undefined); 19 | expect((suite.children[0] as CppUTestGroup).children[1].label).to.be.equal("Test1"); 20 | expect((suite.children[0] as CppUTestGroup).children[1].file).to.be.equal(undefined); 21 | expect((suite.children[0] as CppUTestGroup).children[1].line).to.be.equal(undefined); 22 | expect((suite.children[1] as CppUTestGroup).children.length).to.be.equal(1); 23 | expect((suite.children[1] as CppUTestGroup).children[0].label).to.be.equal("Test1"); 24 | expect((suite.children[1] as CppUTestGroup).children[0].file).to.be.equal(undefined); 25 | expect((suite.children[1] as CppUTestGroup).children[0].line).to.be.equal(undefined); 26 | }); 27 | 28 | it('create a TestSuite from an test list string with location', () => { 29 | const testListString = "Group1.Test1.C:\\File\Name.cpp.345\nGroup1.Test2./path/to.test.file.9873\nGroup2.Test1.Unknown.9889877\n"; 30 | suite.UpdateFromTestListString(testListString, true); 31 | expect(suite.label).to.be.equal("Label"); 32 | expect(suite.children.length).to.be.eq(2); 33 | expect(suite.children[0].label).to.be.equal("Group1"); 34 | expect(suite.children[1].label).to.be.equal("Group2"); 35 | expect((suite.children[0] as CppUTestGroup).children.length).to.be.equal(2); 36 | expect((suite.children[0] as CppUTestGroup).children[0].label).to.be.equal("Test2"); 37 | expect((suite.children[0] as CppUTestGroup).children[0].file).to.be.equal("/path/to.test.file"); 38 | expect((suite.children[0] as CppUTestGroup).children[0].line).to.be.equal(9873); 39 | expect((suite.children[0] as CppUTestGroup).children[1].label).to.be.equal("Test1"); 40 | expect((suite.children[0] as CppUTestGroup).children[1].file).to.be.equal("C:\\File\Name.cpp"); 41 | expect((suite.children[0] as CppUTestGroup).children[1].line).to.be.equal(345); 42 | expect((suite.children[1] as CppUTestGroup).children.length).to.be.equal(1); 43 | expect((suite.children[1] as CppUTestGroup).children[0].label).to.be.equal("Test1"); 44 | expect((suite.children[1] as CppUTestGroup).children[0].file).to.be.equal("Unknown"); 45 | expect((suite.children[1] as CppUTestGroup).children[0].line).to.be.equal(9889877); 46 | }); 47 | }); 48 | -------------------------------------------------------------------------------- /src/Infrastructure/SettingsProvider.ts: -------------------------------------------------------------------------------- 1 | import { Log } from 'vscode-test-adapter-util'; 2 | import { IWorkspaceFolder } from './IWorkspaceFolder'; 3 | import { IDebugConfiguration } from './IDebugConfiguration'; 4 | import { IWorkspaceConfiguration } from './IWorkspaceConfiguration'; 5 | import { TestLocationFetchMode } from './TestLocationFetchMode'; 6 | 7 | export abstract class SettingsProvider { 8 | protected log: Log; 9 | protected config: IWorkspaceConfiguration; 10 | 11 | constructor(log: Log) { 12 | const configSection = "cpputestTestAdapter"; 13 | 14 | this.log = log; 15 | this.config = this.GetConfig(configSection); 16 | } 17 | 18 | protected abstract GetConfig(section: string): IWorkspaceConfiguration; 19 | public abstract GetObjDumpPath(): string; 20 | public abstract GetTestRunners(): string[]; 21 | public abstract GetTestPath(): string; 22 | public abstract GetPreLaunchTask(): string; 23 | public abstract GetDebugConfiguration(): (IDebugConfiguration | undefined); 24 | public abstract GetWorkspaceFolders(): readonly IWorkspaceFolder[] | undefined 25 | protected abstract GetCurrentFilename(): string 26 | protected abstract GetCurrentWorkspaceFolder(): string 27 | protected abstract GlobFiles(wildcardString: string): string[] 28 | 29 | public get TestLocationFetchMode(): TestLocationFetchMode { 30 | switch (this.config.testLocationFetchMode) { 31 | case 'test query': 32 | return TestLocationFetchMode.TestQuery; 33 | case 'debug dump': 34 | return TestLocationFetchMode.DebugDump; 35 | case 'auto': 36 | return TestLocationFetchMode.Auto; 37 | case 'disabled': 38 | default: 39 | return TestLocationFetchMode.Disabled; 40 | } 41 | } 42 | 43 | protected IsCCppDebugger(config: any) { 44 | const isWin = process.platform === "win32"; 45 | // This is my way of saying: If we are using windows check for a config that has an .exe program. 46 | const executionExtension: boolean = isWin ? config.program.endsWith(".exe") : true; 47 | return config.request == 'launch' && 48 | typeof config.type == 'string' && 49 | executionExtension && 50 | (config.type.startsWith('cpp') || 51 | config.type.startsWith('lldb') || 52 | config.type.startsWith('gdb')); 53 | } 54 | 55 | protected SplitRunners(executablesString: string | undefined): string[] { 56 | if (executablesString) { 57 | if (executablesString.indexOf(";") === -1) { 58 | return this.GlobFiles(this.ResolveSettingsVariable(executablesString)); 59 | } 60 | return executablesString 61 | .split(";") 62 | .map(r => this.ResolveSettingsVariable(r)) 63 | .map(r => this.GlobFiles(r)) 64 | .reduce((flatten, arr) => [...flatten, ...arr]); 65 | } else { 66 | return []; 67 | } 68 | } 69 | 70 | /** 71 | * This function converts some of the VSCode variables like workspaceFolder 72 | * into their correspoing values. This is a workaround for https://github.com/microsoft/vscode/issues/46471 73 | * @param input Input string from settings.json 74 | */ 75 | protected ResolveSettingsVariable(input: string | undefined): string { 76 | if (input) { 77 | const result: string[] | null = input.match(/\$\{(.*)\}/gmi); 78 | if (result && result.length > 0) { 79 | this.log.info(`replacing config variabe "${input}"`); 80 | input = input.replace(/(\$\{file\})/gmi, this.GetCurrentFilename()); 81 | input = input.replace(/(\$\{workspaceFolder\})/gmi, this.GetCurrentWorkspaceFolder()); 82 | this.log.info(`replaced variable is now "${input}"`); 83 | } 84 | return input; 85 | } 86 | else { 87 | return ""; 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/Infrastructure/VscodeSettingsProvider.ts: -------------------------------------------------------------------------------- 1 | import * as vscode from 'vscode'; 2 | import glob = require('glob'); 3 | import { SettingsProvider } from './SettingsProvider'; 4 | import { IWorkspaceConfiguration } from './IWorkspaceConfiguration'; 5 | import { IDebugConfiguration } from './IDebugConfiguration'; 6 | import { Log } from 'vscode-test-adapter-util'; 7 | import { IWorkspaceFolder } from './IWorkspaceFolder'; 8 | 9 | 10 | export default class VscodeSettingsProvider extends SettingsProvider { 11 | private configSection: string = "cpputestTestAdapter"; 12 | 13 | constructor(log: Log) { 14 | super(log) 15 | 16 | vscode.workspace.onDidChangeConfiguration(event => { 17 | if (event.affectsConfiguration(this.configSection)) { 18 | const wsConfig = vscode.workspace.getConfiguration(this.configSection); 19 | this.config = { 20 | debugLaunchConfigurationName: wsConfig["debugLaunchConfigurationName"], 21 | logfile: wsConfig["logfile"], 22 | logpanel: wsConfig["logpanel"], 23 | objDumpExecutable: wsConfig["objDumpExecutable"], 24 | testExecutable: wsConfig["testExecutable"], 25 | testExecutablePath: wsConfig["testExecutablePath"], 26 | testLocationFetchMode: wsConfig["testLocationFetchMode"], 27 | preLaunchTask: wsConfig["preLaunchTask"] 28 | } 29 | } 30 | }) 31 | } 32 | 33 | protected override GetConfig(configSection: string): IWorkspaceConfiguration { 34 | return (vscode.workspace.getConfiguration(configSection) as any); 35 | } 36 | 37 | public override GetPreLaunchTask(): string { 38 | return this.config.preLaunchTask; 39 | } 40 | 41 | GetObjDumpPath(): string { 42 | return this.ResolveSettingsVariable(this.config.objDumpExecutable); 43 | } 44 | 45 | GetWorkspaceFolders(): readonly IWorkspaceFolder[] | undefined { 46 | return vscode.workspace.workspaceFolders; 47 | } 48 | 49 | public GetTestRunners(): string[] { 50 | return this.SplitRunners(this.config.testExecutable); 51 | } 52 | 53 | public GetTestPath(): string { 54 | return this.ResolveSettingsVariable(this.config.testExecutablePath); 55 | } 56 | 57 | public GetDebugConfiguration(): (IDebugConfiguration | undefined) { 58 | // Thanks to: https://github.com/matepek/vscode-catch2-test-adapter/blob/9a2e9f5880ef3907d80ff99f3d6d028270923c95/src/Configurations.ts#L125 59 | if (vscode.workspace.workspaceFolders === undefined) { 60 | return undefined; 61 | } 62 | const wpLaunchConfigs: string | undefined = vscode.workspace 63 | .getConfiguration('launch', vscode.workspace.workspaceFolders[0].uri) 64 | .get('configurations'); 65 | 66 | const hasConfiguredLaunchProfiles: boolean = this.config.debugLaunchConfigurationName !== undefined; 67 | 68 | if (wpLaunchConfigs && Array.isArray(wpLaunchConfigs) && wpLaunchConfigs.length > 0) { 69 | if (hasConfiguredLaunchProfiles) { 70 | // try and match the config by name 71 | for (let i = 0; i < wpLaunchConfigs.length; ++i) { 72 | if (wpLaunchConfigs[i].name == this.config.debugLaunchConfigurationName) { 73 | const debugConfig: vscode.DebugConfiguration = Object.assign({}, wpLaunchConfigs[i], { 74 | program: "", 75 | target: "" 76 | }); 77 | return debugConfig; 78 | } 79 | } 80 | } 81 | 82 | //fallback to the first debug configuration that is a c++ debugger 83 | for (let i = 0; i < wpLaunchConfigs.length; ++i) { 84 | if (this.IsCCppDebugger(wpLaunchConfigs[i])) { 85 | const debugConfig: vscode.DebugConfiguration = Object.assign({}, wpLaunchConfigs[i], { 86 | program: "", 87 | target: "" 88 | }); 89 | return debugConfig; 90 | } 91 | } 92 | 93 | } 94 | 95 | return undefined; 96 | } 97 | 98 | protected GlobFiles(wildcardString: string): string[] { 99 | return glob.sync(wildcardString) 100 | } 101 | 102 | protected override GetCurrentFilename(): string { 103 | return vscode.window.activeTextEditor ? vscode.window.activeTextEditor.document.uri.fsPath : ""; 104 | } 105 | 106 | protected override GetCurrentWorkspaceFolder(): string { 107 | return vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders[0].uri.fsPath : ""; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cpputest-test-adapter", 3 | "displayName": "CppUTest Test Adapter", 4 | "description": "Run your CppUTest tests in the Sidebar of Visual Studio Code", 5 | "icon": "img/icon.png", 6 | "author": "Benjamin Giesinger ", 7 | "publisher": "bneumann", 8 | "version": "1.3.4", 9 | "license": "MIT", 10 | "homepage": "https://github.com/bneumann/CppUTest-Test-Adapter", 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/bneumann/CppUTest-Test-Adapter.git" 14 | }, 15 | "bugs": { 16 | "url": "https://github.com/bneumann/CppUTest-Test-Adapter/issues" 17 | }, 18 | "categories": [ 19 | "Other" 20 | ], 21 | "keywords": [ 22 | "test", 23 | "testing" 24 | ], 25 | "main": "out/main.js", 26 | "scripts": { 27 | "clean": "rimraf out *.vsix", 28 | "build": "node esbuild.js", 29 | "watch": "tsc -w", 30 | "dev": "node esbuild.js --watch", 31 | "test": "mocha --require ts-node/register **/*.spec.ts", 32 | "rebuild": "npm run clean && npm run build", 33 | "package": "npm run build && vsce package", 34 | "publish": "npm run rebuild && vsce publish", 35 | "publish-buildserver": "vsce publish", 36 | "publish:patch": "npm run rebuild && vsce publish patch", 37 | "publish:pre": "npm run rebuild && vsce publish --pre-release" 38 | }, 39 | "dependencies": { 40 | "glob": "^11.1.0", 41 | "tslib": "^2.3.1", 42 | "vscode-test-adapter-api": "^1.9.0", 43 | "vscode-test-adapter-util": "^0.7.1", 44 | "xml2js": "^0.5.0" 45 | }, 46 | "devDependencies": { 47 | "@types/chai": "^4.2.22", 48 | "@types/chai-as-promised": "^7.1.4", 49 | "@types/glob": "^7.1.4", 50 | "@types/mocha": "^9.1.1", 51 | "@types/node": "^22.7.4", 52 | "@types/vscode": "~1.61.0", 53 | "@types/xml2js": "^0.4.9", 54 | "@vscode/vsce": "^3.1.1", 55 | "chai": "^4.3.4", 56 | "chai-as-promised": "^7.1.1", 57 | "esbuild": "^0.25.0", 58 | "mocha": "^11.7.5", 59 | "rimraf": "^3.0.2", 60 | "ts-mockito": "^2.6.1", 61 | "ts-node": "^10.9.2", 62 | "typescript": "^5.4.5" 63 | }, 64 | "engines": { 65 | "vscode": "^1.63.0" 66 | }, 67 | "extensionDependencies": [ 68 | "hbenl.vscode-test-explorer" 69 | ], 70 | "activationEvents": [ 71 | "onStartupFinished" 72 | ], 73 | "contributes": { 74 | "configuration": { 75 | "type": "object", 76 | "title": "CppUTest Test Adapter", 77 | "properties": { 78 | "cpputestTestAdapter.testExecutable": { 79 | "description": "Executable that contains all tests. Can be multiple executables separated by semicolon. Wildcard is supported (e.g. myExecutable;bin/test/ut_*)", 80 | "default": "", 81 | "type": "string", 82 | "scope": "resource" 83 | }, 84 | "cpputestTestAdapter.testExecutablePath": { 85 | "description": "Path where the executable should be run in", 86 | "default": "", 87 | "type": "string", 88 | "scope": "resource" 89 | }, 90 | "cpputestTestAdapter.preLaunchTask": { 91 | "description": "Task to run before running the test executable", 92 | "default": "", 93 | "type": "string", 94 | "scope": "resource" 95 | }, 96 | "cpputestTestAdapter.testLocationFetchMode": { 97 | "description": "", 98 | "default": "auto", 99 | "type": "string", 100 | "enum": [ 101 | "auto", 102 | "test query", 103 | "debug dump", 104 | "disabled" 105 | ], 106 | "enumDescriptions": [ 107 | "Selects the best fetch mode for the current operating system and CppUTest capabilities", 108 | "Gets test location information by querying the test executables", 109 | "Gets test location information from debug symbols in test executables", 110 | "Test location fetch disabled" 111 | ], 112 | "scope": "resource" 113 | }, 114 | "cpputestTestAdapter.logpanel": { 115 | "description": "Write diagnostic logs to an output panel", 116 | "type": "boolean", 117 | "scope": "resource" 118 | }, 119 | "cpputestTestAdapter.logfile": { 120 | "description": "Write diagnostic logs to the given file", 121 | "type": "string", 122 | "scope": "resource" 123 | }, 124 | "cpputestTestAdapter.debugLaunchConfigurationName": { 125 | "description": "The name of the configuration object from the launch.json file to use for debugging tests. Defaults to the first profile", 126 | "default": "", 127 | "type": "string", 128 | "scope": "resource" 129 | }, 130 | "cpputestTestAdapter.objDumpExecutable": { 131 | "description": "The path to the objdump executable", 132 | "default": "objdump", 133 | "type": "string", 134 | "scope": "resource" 135 | } 136 | } 137 | } 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /tests/testResults.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "failingTest1", 4 | "status": "Failure", 5 | "value": [ 6 | "TEST(ClassName, Create)", 7 | "C:\\Users\\tests\\basicTests.cpp(58): error: Failure in TEST(ClassName, Create)", 8 | " Message: This is failing", 9 | " CHECK(false) failed", 10 | " ", 11 | " - 0 ms" 12 | ], 13 | "expected": [ 14 | "C:\\Users\\tests\\basicTests.cpp(58): error: Failure in TEST(ClassName, Create)", 15 | " Message: This is failing", 16 | " CHECK(false) failed" 17 | ] 18 | }, 19 | { 20 | "name": "failingTest2", 21 | "status": "Failure", 22 | "value": [ 23 | "TEST(ClassName, Create)", 24 | "/tests/basicTests.cpp(43): error: Failure in TEST(SecondClass, ShouldFail)", 25 | "Message: This is also failing", 26 | "", 27 | "CHECK(false) failed", 28 | "", 29 | "- 0 ms" 30 | ], 31 | "expected": [ 32 | "/tests/basicTests.cpp(43): error: Failure in TEST(SecondClass, ShouldFail)", 33 | "Message: This is also failing", 34 | "", 35 | "CHECK(false) failed" 36 | ] 37 | }, 38 | { 39 | "name": "failingTest3", 40 | "status": "Failure", 41 | "value": [ 42 | "TEST(TEST_GROUP1, TEST1)Memutil uses static memory", 43 | "Memutil heap size = 0x4000000 (67108864 bytes)", 44 | "memutil_heap_ptr: addr=0x22a40c0", 45 | "memutil_heap_initialized ? true", 46 | "src/tests/omss_unit_tests/src/ut/ut_real/inventory_manager/TEST_GROUP1.cpp:51: error: Failure in TEST(TEST_GROUP1, TEST1)", 47 | "expected <0>", 48 | "but was <-2>", 49 | "difference starts at position 0 at: < -2 >", 50 | " ^", 51 | "", 52 | "- 25 ms", 53 | "Errors (1 failures, 1283 tests, 1 ran, 79 checks, 0 ignored, 1282 filtered out, 25 ms)" 54 | ], 55 | "expected":[ 56 | "src/tests/omss_unit_tests/src/ut/ut_real/inventory_manager/TEST_GROUP1.cpp:51: error: Failure in TEST(TEST_GROUP1, TEST1)", 57 | "expected <0>", 58 | "but was <-2>", 59 | "difference starts at position 0 at: < -2 >", 60 | " ^" 61 | ] 62 | }, 63 | { 64 | "name": "failingTest4", 65 | "status": "Error", 66 | "value": [ 67 | "terminate called after throwing an instance of 'std::runtime_error'", 68 | " what(): Invalid parameter value." 69 | ], 70 | "expected": [ 71 | "terminate called after throwing an instance of 'std::runtime_error'", 72 | " what(): Invalid parameter value." 73 | ] 74 | }, 75 | { 76 | "name": "passingTest1", 77 | "status": "Success", 78 | "value": [ 79 | "TEST(ClassName, ShouldPass) - 0 ms" 80 | ], 81 | "expected": [""] 82 | }, 83 | { 84 | "name": "passingTest2", 85 | "status": "Success", 86 | "value":[ 87 | "TEST(TEST_GROUP1, ouptpuTEST1)Memutil uses static memory", 88 | "Memutil heap size = 0x4000000 (67108864 bytes)", 89 | "memutil_heap_ptr: addr=0x22a40c0\nmemutil_heap_initialized ? true", 90 | "- 26 ms", 91 | "", 92 | "OK (1283 tests, 1 ran, 79 checks, 0 ignored, 1282 filtered out, 26 ms)" 93 | ], 94 | "expected": [""] 95 | }, 96 | { 97 | "name": "ignoredTest1", 98 | "status": "Success", 99 | "value": [ 100 | "IGNORE_TEST(SecondClass, ShouldBeIgnored) - 0 ms" 101 | ], 102 | "expected": [""] 103 | }, 104 | { 105 | "name":"ignoredTest2", 106 | "status": "Success", 107 | "value":[ 108 | "IGNORE_TEST(TEST_GROUP1, TEST1) - 0 ms", 109 | "", 110 | "OK (1283 tests, 0 ran, 0 checks, 1 ignored, 1282 filtered out, 0 ms)" 111 | ], 112 | "expected": [""] 113 | } 114 | ] -------------------------------------------------------------------------------- /src/Infrastructure/ExecutableRunner.ts: -------------------------------------------------------------------------------- 1 | import { ExecException } from "child_process"; 2 | import { basename, dirname } from "path"; 3 | import { ProcessExecuter } from "../Application/ProcessExecuter"; 4 | import { TestLocationFetchMode } from './TestLocationFetchMode'; 5 | import { Log } from "vscode-test-adapter-util"; 6 | 7 | export enum RunResultStatus { 8 | Success, 9 | Failure, 10 | Error, 11 | } 12 | 13 | export class RunResult { 14 | readonly Status: RunResultStatus; 15 | readonly Text: string; 16 | constructor(status: RunResultStatus, text: string) { 17 | this.Status = status; 18 | this.Text = text; 19 | } 20 | } 21 | 22 | export interface ExecutableRunnerOptions { 23 | workingDirectory?: string; 24 | objDumpExecutable?: string; 25 | } 26 | 27 | export default class ExecutableRunner { 28 | private readonly exec: Function; 29 | private readonly execFile: Function; 30 | private readonly kill: Function; 31 | private readonly command: string; 32 | private readonly workingDirectory: string; 33 | private readonly objDumpExecutable: string; 34 | private readonly tempFile: string; 35 | public readonly Name: string; 36 | private dumpCached: boolean; 37 | private tryGetLocation: boolean; 38 | private log: Log; 39 | 40 | constructor(processExecuter: ProcessExecuter, command: string, log: Log, options?: ExecutableRunnerOptions) { 41 | this.exec = processExecuter.Exec; 42 | this.execFile = processExecuter.ExecFile; 43 | this.kill = processExecuter.KillProcess; 44 | this.command = command; 45 | 46 | 47 | 48 | const wd = options?.workingDirectory; 49 | this.workingDirectory = this.isEmptyString(wd) ? dirname(command) : wd!.trim(); 50 | 51 | this.objDumpExecutable = options?.objDumpExecutable ?? "objdump"; 52 | this.Name = basename(command); 53 | this.tempFile = `${this.Name}.dump` 54 | this.dumpCached = false; 55 | this.log = log; 56 | this.tryGetLocation = true; 57 | 58 | } 59 | 60 | public get Command(): string { return this.command; } 61 | 62 | public get WorkingDirectory(): string { return this.workingDirectory; } 63 | 64 | public GetTestList(testLocationFetchMode: TestLocationFetchMode): Promise<[string, boolean]> { 65 | 66 | switch (testLocationFetchMode) { 67 | case TestLocationFetchMode.TestQuery: 68 | return this.GetTestListWithLocation(true); 69 | case TestLocationFetchMode.Auto: 70 | if (this.tryGetLocation) { 71 | return this.GetTestListWithLocation(false).catch(() => { 72 | this.tryGetLocation = false; 73 | return this.GetTestListGroupAndNames(); 74 | }); 75 | } else { 76 | return this.GetTestListGroupAndNames(); 77 | } 78 | default: 79 | return this.GetTestListGroupAndNames(); 80 | } 81 | } 82 | 83 | private GetTestListWithLocation(printError: boolean): Promise<[string, boolean]> { 84 | return new Promise<[string, boolean]>((resolve, reject) => this.execFile(this.command, ["-ll"], { cwd: this.workingDirectory }, (error: any, stdout: any, stderr: any) => { 85 | if (error) { 86 | if (printError) { 87 | this.log.error(error); 88 | } 89 | reject(error); 90 | } 91 | this.log.info(stdout); 92 | resolve([stdout, true]); 93 | })); 94 | } 95 | 96 | private GetTestListGroupAndNames(): Promise<[string, boolean]> { 97 | return new Promise<[string, boolean]>((resolve, reject) => this.execFile(this.command, ["-ln"], { cwd: this.workingDirectory }, (error: any, stdout: any, stderr: any) => { 98 | if (error) { 99 | console.error('stderr', error); 100 | reject(error); 101 | } 102 | resolve([stdout, false]); 103 | })); 104 | } 105 | 106 | // TODO: Make this abstract in case windows will be required 107 | public GetDebugSymbols(group: string, test: string): Promise { 108 | const sourceGrep = `grep -m 2 -A 2 TEST_${group}_${test} ${this.tempFile}`; 109 | // Windows: 110 | // const sourceGrep = `findstr TEST_${group}_${test}` 111 | return this.DumpSymbols().then(() => { 112 | return new Promise((resolve, reject) => { 113 | this.exec(sourceGrep, { cwd: this.workingDirectory }, (error: ExecException | null, stdout: string, stderr: string) => { 114 | if (error) { 115 | console.error('stderr', error); 116 | reject(stderr); 117 | } else { 118 | resolve(stdout); 119 | } 120 | }); 121 | }); 122 | }) 123 | } 124 | 125 | private DumpSymbols(): Promise { 126 | if (this.dumpCached) { return Promise.resolve(); } 127 | // Linux: 128 | const sourceGrep = `${this.objDumpExecutable} -lSd ${this.command} > ${this.tempFile}`; 129 | // Windows: 130 | // const sourceGrep = `windObjDumpOrWhatEver ${command} | findstr TEST_${group}_${test}` 131 | return new Promise((resolve, reject) => { 132 | this.exec(sourceGrep, { cwd: this.workingDirectory }, (error: ExecException | null, stdout: string, stderr: string) => { 133 | if (error) { 134 | console.error('stderr', error); 135 | reject(stderr); 136 | } else { 137 | this.dumpCached = true; 138 | resolve(); 139 | } 140 | }); 141 | }); 142 | } 143 | 144 | public RunTest(group: string, test: string): Promise { 145 | return new Promise((resolve, reject) => { 146 | this.execFile(this.command, ["-sg", group, "-sn", test, "-v"], { cwd: this.workingDirectory }, (error: ExecException | null, stdout: string, stderr: string) => { 147 | if (error && error.code === null) { 148 | console.error('stderr', error); 149 | reject(stderr); 150 | } 151 | if (stderr.trim() != "") { 152 | resolve(new RunResult(RunResultStatus.Error, stderr)); 153 | } else if (error && error.code !== 0) { 154 | resolve(new RunResult(RunResultStatus.Failure, stdout)); 155 | } else { 156 | resolve(new RunResult(RunResultStatus.Success, stdout)); 157 | } 158 | }) 159 | }); 160 | } 161 | 162 | public KillProcess() { 163 | this.kill(); 164 | } 165 | 166 | private isEmptyString(value: string | undefined | null): boolean { 167 | return typeof value !== 'string' || value.trim() === ''; 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /src/adapter.ts: -------------------------------------------------------------------------------- 1 | import * as vscode from 'vscode'; 2 | import { TestAdapter, TestLoadStartedEvent, TestLoadFinishedEvent, TestRunStartedEvent, TestRunFinishedEvent, TestSuiteEvent, TestEvent, RetireEvent } from 'vscode-test-adapter-api'; 3 | import { Log } from 'vscode-test-adapter-util'; 4 | 5 | import CppUTestContainer from "./Domain/CppUTestContainer"; 6 | import { CppUTestGroup } from './Domain/CppUTestGroup'; 7 | import { TestState } from './Domain/TestState'; 8 | import { TestResult } from './Domain/TestResult'; 9 | import { CppUTest } from './Domain/CppUTest'; 10 | import { RegexResultParser } from "./Domain/RegexResultParser"; 11 | import VscodeSettingsProvider from "./Infrastructure/VscodeSettingsProvider"; 12 | import ExecutableRunner, { ExecutableRunnerOptions } from "./Infrastructure/ExecutableRunner"; 13 | import { NodeProcessExecuter } from './Application/NodeProcessExecuter'; 14 | import { VscodeAdapterImplementation } from "./Application/VscodeAdapterImplementation"; 15 | import { SettingsProvider } from "./Infrastructure/SettingsProvider"; 16 | import { ProcessExecuter } from "./Application/ProcessExecuter"; 17 | 18 | export class CppUTestAdapter implements TestAdapter { 19 | 20 | private disposables: { dispose(): void }[] = []; 21 | 22 | private readonly testsEmitter = new vscode.EventEmitter(); 23 | private readonly testStatesEmitter = new vscode.EventEmitter(); 24 | private readonly autorunEmitter = new vscode.EventEmitter(); 25 | private readonly mainSuite: CppUTestGroup; 26 | private readonly root: CppUTestContainer; 27 | private readonly settingsProvider: SettingsProvider; 28 | private readonly processExecuter: ProcessExecuter; 29 | 30 | get tests(): vscode.Event { return this.testsEmitter.event; } 31 | get testStates(): vscode.Event { return this.testStatesEmitter.event; } 32 | get retire(): vscode.Event | undefined { return this.autorunEmitter.event; } 33 | 34 | constructor( 35 | public readonly workspace: vscode.WorkspaceFolder, 36 | private readonly log: Log 37 | ) { 38 | 39 | this.log.info('Initializing adapter'); 40 | 41 | this.disposables.push(this.testsEmitter, this.testStatesEmitter, this.autorunEmitter); 42 | 43 | this.settingsProvider = new VscodeSettingsProvider(this.log); 44 | this.processExecuter = new NodeProcessExecuter(); 45 | const vscodeAdapter = new VscodeAdapterImplementation(); 46 | const resultParser = new RegexResultParser(); 47 | 48 | this.root = new CppUTestContainer(this.settingsProvider, vscodeAdapter, resultParser); 49 | this.root.OnTestFinish = this.handleTestFinished.bind(this); 50 | this.root.OnTestStart = this.handleTestStarted.bind(this); 51 | 52 | this.mainSuite = new CppUTestGroup("Main Suite", ""); 53 | // runners.forEach(runner => fs.watchFile(runner, (cur: fs.Stats, prev: fs.Stats) => { 54 | // if (cur.mtimeMs !== prev.mtimeMs) { 55 | // this.log.info("Executable changed, updating test cases"); 56 | // this.load(); 57 | // // this.autorunEmitter.fire(); 58 | // } 59 | // })); 60 | } 61 | 62 | public async load(): Promise { 63 | this.testsEmitter.fire({ type: 'started' }); 64 | this.log.info('Loading tests'); 65 | 66 | await this.updateTests(); 67 | 68 | this.log.info('Tests loaded'); 69 | } 70 | 71 | public async run(tests: string[]): Promise { 72 | this.testStatesEmitter.fire({ type: 'started', tests }); 73 | this.log.info('Running tests'); 74 | if (await this.updateTests()) 75 | { 76 | if (tests.length == 1 && tests[0] == this.mainSuite.id || tests[0] == 'error') { 77 | await this.root.RunAllTests(); 78 | } else { 79 | await this.root.RunTest(...tests); 80 | } 81 | } 82 | this.log.info('Done'); 83 | this.testStatesEmitter.fire({ type: 'finished' }); 84 | } 85 | 86 | public async debug(tests: string[]): Promise { 87 | if (await this.updateTests()) 88 | return this.root.DebugTest(...tests); 89 | } 90 | 91 | public cancel(): void { 92 | this.root.KillRunningProcesses(); 93 | this.testStatesEmitter.fire({ type: 'finished' }); 94 | } 95 | 96 | public dispose(): void { 97 | this.cancel(); 98 | for (const disposable of this.disposables) { 99 | disposable.dispose(); 100 | } 101 | this.disposables = []; 102 | } 103 | 104 | private async updateTests(): Promise { 105 | const preLaunchTask = await this.getPreLaunchTask(); 106 | if (preLaunchTask) { 107 | const errorCode = await this.runTask(preLaunchTask); 108 | if (errorCode) { 109 | this.mainSuite.children = []; 110 | this.testsEmitter.fire({ 111 | type: 'finished', 112 | errorMessage: `preLaunchTask "${preLaunchTask.name}" Failed [exit code: ${errorCode}]`, 113 | }); 114 | return false; 115 | } 116 | } 117 | this.root.ClearTests(); 118 | const runners = this.settingsProvider.GetTestRunners().map(runner => new ExecutableRunner(this.processExecuter, runner, this.log, this.GetExecutionOptions())); 119 | const loadedTests = await this.root.LoadTests(runners); 120 | this.mainSuite.children = loadedTests; 121 | this.testsEmitter.fire({ type: 'finished', suite: this.mainSuite }); 122 | return true; 123 | } 124 | 125 | private async getPreLaunchTask(): Promise { 126 | const preLaunchTaskName = this.settingsProvider.GetPreLaunchTask(); 127 | if (!preLaunchTaskName) 128 | return undefined; 129 | const tasks = await vscode.tasks.fetchTasks(); 130 | return tasks.find((value, ..._) => value.name == preLaunchTaskName); 131 | } 132 | 133 | private async runTask(task: vscode.Task): Promise { 134 | const taskProcessEnded: Promise = new Promise((resolve, _) => { 135 | const hook_disposable = vscode.tasks.onDidEndTaskProcess((e) => { 136 | if (e.execution.task !== task) 137 | return; 138 | hook_disposable.dispose(); 139 | resolve(e.exitCode ?? 0); 140 | }, this, this.disposables) 141 | }); 142 | 143 | await vscode.tasks.executeTask(task); 144 | return await taskProcessEnded; 145 | } 146 | 147 | private GetExecutionOptions(): ExecutableRunnerOptions | undefined { 148 | return { 149 | objDumpExecutable: this.settingsProvider.GetObjDumpPath(), 150 | workingDirectory: this.settingsProvider.GetTestPath() 151 | } 152 | } 153 | 154 | private handleTestStarted(test: CppUTest): void { 155 | const event = this.mapTestResultToTestEvent(test); 156 | this.testStatesEmitter.fire(event) 157 | } 158 | 159 | private handleTestFinished(test: CppUTest, testResult: TestResult): void { 160 | const event = this.mapTestResultToTestEvent(test, testResult); 161 | this.testStatesEmitter.fire(event) 162 | } 163 | 164 | private mapTestResultToTestEvent(test: CppUTest, testResult?: TestResult): TestEvent { 165 | let state: "running" | "passed" | "failed" | "skipped" | "errored" = "running"; 166 | if (!testResult) { 167 | return { 168 | type: "test", 169 | state, 170 | test 171 | }; 172 | } 173 | switch (testResult.state) { 174 | case TestState.Errored: 175 | case TestState.Unknown: 176 | state = "errored"; 177 | break; 178 | case TestState.Failed: 179 | state = "failed"; 180 | break; 181 | case TestState.Skipped: 182 | state = "skipped"; 183 | break; 184 | default: 185 | state = "passed"; 186 | break; 187 | } 188 | return { 189 | type: 'test', 190 | test, 191 | state, 192 | message: testResult.message 193 | }; 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /src/Domain/CppUTestContainer.ts: -------------------------------------------------------------------------------- 1 | import { DebugConfiguration, WorkspaceFolder } from "vscode"; 2 | import { CppUTestGroup } from "./CppUTestGroup"; 3 | import CppUTestSuite from "./CppUTestSuite"; 4 | import { TestResult } from "./TestResult"; 5 | import { CppUTest } from "./CppUTest"; 6 | import { TestState } from "./TestState"; 7 | import { ResultParser } from "./ResultParser"; 8 | import ExecutableRunner from "../Infrastructure/ExecutableRunner"; 9 | import { SettingsProvider } from "../Infrastructure/SettingsProvider"; 10 | import { TestLocationFetchMode } from '../Infrastructure/TestLocationFetchMode'; 11 | import { VscodeAdapter } from "../Infrastructure/VscodeAdapter"; 12 | 13 | export default class CppUTestContainer { 14 | private runners: ExecutableRunner[]; 15 | private suites: Map; 16 | private settingsProvider: SettingsProvider; 17 | private vscodeAdapter: VscodeAdapter; 18 | private resultParser: ResultParser; 19 | private onTestFinishHandler: (test: CppUTest, result: TestResult) => void = () => { }; 20 | private onTestStartHandler: (test: CppUTest) => void = () => { }; 21 | // private dirty: boolean; 22 | public set OnTestFinish(handler: (test: CppUTest, result: TestResult) => void) { 23 | this.onTestFinishHandler = handler; 24 | } 25 | public set OnTestStart(handler: (test: CppUTest) => void) { 26 | this.onTestStartHandler = handler; 27 | } 28 | 29 | constructor(settingsProvider: SettingsProvider, vscodeAdapter: VscodeAdapter, resultParser: ResultParser) { 30 | this.settingsProvider = settingsProvider; 31 | this.runners = []; 32 | this.vscodeAdapter = vscodeAdapter; 33 | this.resultParser = resultParser; 34 | this.suites = new Map(); 35 | } 36 | 37 | public LoadTests(runners: ExecutableRunner[]): Promise { 38 | this.runners = runners; 39 | return Promise.all(this.runners 40 | .map(runner => runner.GetTestList(this.settingsProvider.TestLocationFetchMode) 41 | .then(testList => this.UpdateTestSuite(runner, testList[0], testList[1])) 42 | .catch(error => this.CreateTestSuiteError(runner.Name)) 43 | )); 44 | } 45 | 46 | public ClearTests() { 47 | this.suites = new Map(); 48 | } 49 | 50 | public async RunAllTests(): Promise { 51 | const testResults: TestResult[] = new Array(); 52 | for (const executableGroup of this.suites.values()) { 53 | for (const testGroup of executableGroup.children) { 54 | for (const test of (testGroup as CppUTestGroup).children) { 55 | const runner = this.runners.filter(r => r.Name === executableGroup.label)[0]; 56 | this.onTestStartHandler((test as CppUTest)); 57 | const runResult = await runner.RunTest(testGroup.label, test.label); 58 | const testResult = this.resultParser.GetResult(runResult); 59 | this.onTestFinishHandler((test as CppUTest), testResult); 60 | testResults.push(testResult); 61 | } 62 | } 63 | } 64 | return testResults; 65 | } 66 | 67 | public async RunTest(...testId: string[]): Promise { 68 | const testResults: TestResult[] = new Array(); 69 | const testsToRun: CppUTest[] = new Array(); 70 | for (const executableGroup of this.suites.values()) { 71 | testsToRun.splice(0, testsToRun.length); 72 | if (testId.includes(executableGroup.id)) { 73 | testsToRun.push(...executableGroup.Tests); 74 | } else { 75 | for (const testGroup of executableGroup.children) { 76 | testsToRun.push(...this.GetAllChildTestsById(testId, (testGroup as CppUTestGroup))); 77 | } 78 | } 79 | const runner = this.runners.filter(r => r.Name === executableGroup.label)[0]; 80 | for (const test of testsToRun) { 81 | if (test && runner) { 82 | this.onTestStartHandler(test); 83 | try { 84 | const runResult = await runner.RunTest(test.group, test.label); 85 | const testResult = this.resultParser.GetResult(runResult); 86 | this.onTestFinishHandler(test, testResult); 87 | testResults.push(testResult); 88 | } catch (error) { 89 | console.error(error); 90 | } 91 | } 92 | } 93 | } 94 | if (testResults.length > 0) { 95 | return testResults; 96 | } 97 | return [new TestResult(TestState.Failed, "Unable to run or find test")]; 98 | } 99 | 100 | public async DebugTest(...testId: string[]): Promise { 101 | const config = this.settingsProvider.GetDebugConfiguration(); 102 | const workspaceFolders = this.settingsProvider.GetWorkspaceFolders(); 103 | if (config === undefined) { 104 | throw new Error("No debug configuration found. Not able to debug!"); 105 | } 106 | if (!workspaceFolders) { 107 | throw new Error("No workspaceFolders found. Not able to debug!"); 108 | } 109 | for (const executableGroup of this.suites.values()) { 110 | const testOrGroup = this.GetGroupOrTest(testId, executableGroup); 111 | const runner = this.runners.filter(r => r.Name === executableGroup.label)[0]; 112 | if (testOrGroup && runner) { 113 | const debugConfig = config as unknown as DebugConfiguration; 114 | const isSuite = testOrGroup instanceof CppUTestSuite; 115 | 116 | // If debugging entire suite, run without test filters 117 | if (isSuite) { 118 | debugConfig.name = testOrGroup.label; 119 | debugConfig.args = []; 120 | } else { 121 | const isTest = testOrGroup instanceof CppUTest; 122 | const testRunName = isTest ? `${testOrGroup.group}.${testOrGroup.label}` : `${testOrGroup.label}`; 123 | const testRunArg = isTest ? "-t" : "-sg"; 124 | debugConfig.name = testRunName; 125 | debugConfig.args = [testRunArg, testRunName]; 126 | } 127 | 128 | debugConfig.program = runner.Command; 129 | debugConfig.target = runner.Command; 130 | await this.vscodeAdapter.StartDebugger((workspaceFolders as WorkspaceFolder[]), debugConfig); 131 | } 132 | } 133 | } 134 | 135 | private GetGroupOrTest(testIds: string[], testEntity: CppUTestGroup | CppUTest): CppUTestGroup | CppUTest | undefined { 136 | if (testEntity instanceof CppUTestGroup) { 137 | for (const child of testEntity.children) { 138 | const entity = this.GetGroupOrTest(testIds, (child as CppUTest | CppUTestGroup)); 139 | if (entity) return entity; 140 | } 141 | } 142 | for (const testId of testIds) { 143 | if (testEntity.id === testId) { 144 | return testEntity; 145 | } 146 | } 147 | return undefined; 148 | } 149 | 150 | public KillRunningProcesses() { 151 | this.runners.forEach(r => r.KillProcess()); 152 | } 153 | 154 | private GetAllChildTestsById(testId: string[], testGroup: CppUTestGroup): CppUTest[] { 155 | const tests: CppUTest[][] = testId.map(id => (testGroup as CppUTestGroup).FindTest(id)); 156 | return Array().concat(...tests); 157 | } 158 | 159 | private async UpdateTestSuite(runner: ExecutableRunner, testString: string, hasLocation: boolean): Promise { 160 | const testSuite = this.GetTestSuite(runner.Name); 161 | testSuite.UpdateFromTestListString(testString, hasLocation); 162 | if((this.settingsProvider.TestLocationFetchMode == TestLocationFetchMode.DebugDump) || 163 | ((this.settingsProvider.TestLocationFetchMode == TestLocationFetchMode.Auto) && !hasLocation && (process.platform != "win32"))) { 164 | for(const test of testSuite.Tests) { 165 | try { 166 | const debugString = await runner.GetDebugSymbols(test.group, test.label); 167 | test.AddDebugInformation(debugString); 168 | } catch (error) { 169 | console.error(error); 170 | } 171 | } 172 | } 173 | return testSuite; 174 | } 175 | 176 | private async CreateTestSuiteError(runnerName: string): Promise { 177 | const testSuite = this.GetTestSuite(runnerName); 178 | testSuite.AddTestGroup("ERROR LOADING TESTS"); 179 | return testSuite; 180 | } 181 | 182 | private GetTestSuite(runnerName: string): CppUTestSuite { 183 | if (this.suites.has(runnerName)) { 184 | return (this.suites.get(runnerName) as CppUTestSuite); 185 | } else { 186 | const testSuite = new CppUTestSuite(runnerName); 187 | this.suites.set(runnerName, testSuite); 188 | return testSuite; 189 | } 190 | } 191 | } 192 | -------------------------------------------------------------------------------- /img/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 20 | 22 | 25 | 29 | 33 | 34 | 37 | 41 | 45 | 46 | 56 | 66 | 75 | 84 | 93 | 102 | 103 | 126 | 128 | 129 | 131 | image/svg+xml 132 | 134 | 135 | 136 | 137 | 138 | 142 | 150 | 155 | 160 | 163 | 170 | 176 | 177 | 178 | 179 | -------------------------------------------------------------------------------- /tests/ExecutableRunner.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect, use as chaiUse } from 'chai'; 2 | import * as chaiAsPromised from "chai-as-promised"; 3 | import ExecutableRunner, { RunResultStatus, RunResult } from '../src/Infrastructure/ExecutableRunner'; 4 | import { ProcessExecuter } from '../src/Application/ProcessExecuter'; 5 | import { anything, instance, mock, verify, when } from "ts-mockito"; 6 | import { ExecException } from "child_process"; 7 | import { Log } from 'vscode-test-adapter-util'; 8 | import { TestLocationFetchMode } from '../src/Infrastructure/Infrastructure'; 9 | 10 | chaiUse(chaiAsPromised); 11 | 12 | const outputStrings = [ 13 | { 14 | name: "short", 15 | value: "Group1.Test1.File.Name1.cpp.789\nGroup2.File_name_2.874", 16 | hasLocation: true 17 | }, 18 | { 19 | name: "long", 20 | value: "Group1.Test1 Group2.Test2 Group2.Test3 Group2.Test4", 21 | hasLocation: false 22 | } 23 | ] 24 | 25 | const debugStrings = [ 26 | { 27 | name: "short", 28 | value: "test\nline\nout" 29 | }, 30 | { 31 | name: "long", 32 | value: "test\nline\nout\nthat\nis\nlonger" 33 | } 34 | ] 35 | 36 | class ExecError implements ExecException { 37 | name: string = ""; 38 | message: string = ""; 39 | cmd?: string; 40 | killed?: boolean; 41 | code?: number; 42 | signal?: NodeJS.Signals; 43 | constructor(code?: number) { 44 | this.code = code; 45 | } 46 | } 47 | 48 | function createFailingTestString(group: string, test: string): string { 49 | return `TEST(${group}, ${test})\n` + 50 | `/home/user/test/myTests/.cpp(58): error: Failure in TEST(${group}, ${test})\n` + 51 | "Message: This is failing\n" + 52 | "CHECK(false) failed\n\n" + 53 | "- 4 ms\n\n" + 54 | "Errors (1 failures, 9 tests, 1 ran, 1 checks, 0 ignored, 8 filtered out, 6 ms)"; 55 | } 56 | 57 | describe("ExecutableRunner should", () => { 58 | outputStrings.forEach(testOutput => { 59 | it(`get the test list string for ${testOutput.name}`, async () => { 60 | const log = mock(); 61 | const processExecuter = setupMockCalls(undefined, testOutput.value, ""); 62 | const command = "runnable"; 63 | 64 | let runner = new ExecutableRunner(processExecuter, command, log, undefined); 65 | 66 | let testList = await runner.GetTestList(testOutput.hasLocation ? TestLocationFetchMode.TestQuery : TestLocationFetchMode.Disabled); 67 | 68 | expect(testList[0]).to.be.eq(testOutput.value); 69 | expect(testList[1]).to.be.eq(testOutput.hasLocation); 70 | }) 71 | }) 72 | 73 | it("throw an exception if an error occured", () => { 74 | const log = mock(); 75 | const processExecuter = setupMockCalls(new Error("whoops"), "", "Something happened"); 76 | const command = "runnable"; 77 | 78 | let runner = new ExecutableRunner(processExecuter, command, log); 79 | return expect(runner.GetTestList(TestLocationFetchMode.Auto)).to.be.eventually. 80 | be.rejectedWith("whoops") 81 | .and.be.an.instanceOf(Error) 82 | }) 83 | 84 | debugStrings.forEach(testOutput => { 85 | const log = mock(); 86 | it(`get the debug string for ${testOutput.name}`, async () => { 87 | const processExecuter = setupMockCalls(undefined, testOutput.value, ""); 88 | const command = "runnable"; 89 | 90 | let runner = new ExecutableRunner(processExecuter, command, log); 91 | 92 | let testListString = await runner.GetDebugSymbols("someGroup", "someTest"); 93 | 94 | expect(testListString).to.be.eq(testOutput.value); 95 | }) 96 | }) 97 | 98 | it("execute the command in the correct path", async () => { 99 | const log = mock(); 100 | const command = "runnable"; 101 | let calledOptions: any = {}; 102 | const processExecuter = mock(); 103 | 104 | when(processExecuter.Exec(anything(), anything(), anything())).thenCall((cmd: string, options: any, callback: Function) => { 105 | calledOptions = options; 106 | callback(undefined, "Group1.Test1", undefined); 107 | }); 108 | 109 | when(processExecuter.ExecFile(anything(), anything(), anything(), anything())).thenCall((cmd: string, args: any, options: any, callback: Function) => { 110 | calledOptions = options; 111 | callback(undefined, "Group1.Test1", undefined); 112 | }); 113 | 114 | let runner = new ExecutableRunner(instance(processExecuter), command, log, { "workingDirectory": "/tmp/myPath" }); 115 | await runner.GetTestList(TestLocationFetchMode.Auto); 116 | expect(calledOptions.cwd).to.be.eq("/tmp/myPath"); 117 | }) 118 | 119 | it("return the correct string on successful run", async () => { 120 | const log = mock(); 121 | const resultString = createFailingTestString("myGroup", "myTest"); 122 | const processExecuter = setupMockCalls(undefined, resultString, ""); 123 | const command = "runnable"; 124 | 125 | let runner = new ExecutableRunner(processExecuter, command, log); 126 | 127 | let actualTestOutput = await runner.RunTest("myGroup", "myTest"); 128 | 129 | expect(actualTestOutput).to.be.deep.equal(new RunResult(RunResultStatus.Success, resultString)); 130 | }) 131 | 132 | it("return the correct string on run with failure", async () => { 133 | const log = mock(); 134 | const errorString = "unexpected failure string"; 135 | const processExecuter = setupMockCalls(new ExecError(1), errorString, ""); 136 | const command = "runnable"; 137 | 138 | let runner = new ExecutableRunner(processExecuter, command, log); 139 | 140 | let actualTestOutput = await runner.RunTest("myGroup", "myTest"); 141 | 142 | expect(actualTestOutput).to.be.deep.equal(new RunResult(RunResultStatus.Failure, errorString)); 143 | }) 144 | 145 | it("return the correct string on run with error", async () => { 146 | const log = mock(); 147 | const errorString = "unexpected error string"; 148 | const processExecuter = setupMockCalls(new ExecError(1), "", errorString); 149 | const command = "runnable"; 150 | 151 | let runner = new ExecutableRunner(processExecuter, command, log); 152 | 153 | let actualTestOutput = await runner.RunTest("myGroup", "myTest"); 154 | 155 | expect(actualTestOutput).to.be.deep.equal(new RunResult(RunResultStatus.Error, errorString)); 156 | }) 157 | 158 | it("kill the process currently running", () => { 159 | const log = mock(); 160 | const mockedExecuter = mock(); 161 | when(mockedExecuter.KillProcess()).thenCall(() => { }); 162 | const executer = instance(mockedExecuter); 163 | let runner = new ExecutableRunner(executer, "exec", log); 164 | runner.KillProcess(); 165 | verify(mockedExecuter.KillProcess()).called(); 166 | }) 167 | 168 | describe("workingDirectory option", () => { 169 | it("should use provided workingDirectory option", async () => { 170 | const log = mock(); 171 | const mockedExecuter = mock(); 172 | const customWorkingDir = "/custom/working/directory"; 173 | let capturedOptions: any; 174 | 175 | when(mockedExecuter.ExecFile(anything(), anything(), anything(), anything())) 176 | .thenCall((cmd: string, args: any, options: any, callback: Function) => { 177 | capturedOptions = options; 178 | callback(undefined, "output", ""); 179 | }); 180 | 181 | const executer = instance(mockedExecuter); 182 | const runner = new ExecutableRunner(executer, "/path/to/test.exe", log, 183 | { workingDirectory: customWorkingDir }); 184 | 185 | await runner.GetTestList(TestLocationFetchMode.Disabled); 186 | 187 | expect(capturedOptions.cwd).to.equal(customWorkingDir); 188 | }); 189 | 190 | it("should use executable directory when workingDirectory not provided", async () => { 191 | const log = mock(); 192 | const mockedExecuter = mock(); 193 | const executablePath = "/path/to/test.exe"; 194 | let capturedOptions: any; 195 | 196 | when(mockedExecuter.ExecFile(anything(), anything(), anything(), anything())) 197 | .thenCall((cmd: string, args: any, options: any, callback: Function) => { 198 | capturedOptions = options; 199 | callback(undefined, "output", ""); 200 | }); 201 | 202 | const executer = instance(mockedExecuter); 203 | const runner = new ExecutableRunner(executer, executablePath, log); 204 | 205 | await runner.GetTestList(TestLocationFetchMode.Disabled); 206 | 207 | expect(capturedOptions.cwd).to.equal("/path/to"); 208 | }); 209 | 210 | it("should use workingDirectory for test execution", async () => { 211 | const log = mock(); 212 | const mockedExecuter = mock(); 213 | const customWorkingDir = "/custom/test/directory"; 214 | let capturedOptions: any; 215 | 216 | when(mockedExecuter.ExecFile(anything(), anything(), anything(), anything())) 217 | .thenCall((cmd: string, args: any, options: any, callback: Function) => { 218 | capturedOptions = options; 219 | callback(undefined, ".", ""); 220 | }); 221 | 222 | const executer = instance(mockedExecuter); 223 | const runner = new ExecutableRunner(executer, "/path/to/test.exe", log, 224 | { workingDirectory: customWorkingDir }); 225 | 226 | await runner.RunTest("TestGroup", "TestName"); 227 | 228 | expect(capturedOptions.cwd).to.equal(customWorkingDir); 229 | }); 230 | }); 231 | }) 232 | 233 | function setupMockCalls(error: ExecException | undefined, returnValue: string, errorValue: string) { 234 | const mockedExecuter = mock(); 235 | when(mockedExecuter.Exec(anything(), anything(), anything())).thenCall((cmd: string, options: any, callback: Function) => callback(error, returnValue, errorValue)); 236 | when(mockedExecuter.ExecFile(anything(), anything(), anything(), anything())).thenCall((cmd: string, args: any, options: any, callback: Function) => callback(error, returnValue, errorValue)); 237 | when(mockedExecuter.KillProcess()).thenCall(() => { }); 238 | return instance(mockedExecuter); 239 | } 240 | -------------------------------------------------------------------------------- /tests/SettingsProvider.spec.ts: -------------------------------------------------------------------------------- 1 | import { mock } from "ts-mockito"; 2 | import { IDebugConfiguration, IWorkspaceConfiguration, IWorkspaceFolder, SettingsProvider } from '../src/Infrastructure/Infrastructure'; 3 | import { Log } from 'vscode-test-adapter-util'; 4 | import { expect } from "chai"; 5 | import ExecutableRunner from '../src/Infrastructure/ExecutableRunner'; 6 | 7 | 8 | class TestSettingsProvider extends SettingsProvider { 9 | protected config: IWorkspaceConfiguration; 10 | private filesToFind: string[]; 11 | 12 | constructor(logger: Log, vscodeSettings: IWorkspaceConfiguration, filesToFind: string[]) { 13 | super(logger); 14 | 15 | this.config = vscodeSettings; 16 | this.filesToFind = filesToFind; 17 | } 18 | protected GetConfig(section: string): IWorkspaceConfiguration { 19 | return this.config; 20 | } 21 | 22 | public GetPreLaunchTask(): string { 23 | return this.config.preLaunchTask; 24 | } 25 | 26 | GetObjDumpPath(): string { 27 | return this.config.objDumpExecutable ?? ""; 28 | } 29 | GetTestRunners(): string[] { 30 | return this.SplitRunners(this.config.testExecutable); 31 | } 32 | GetTestPath(): string { 33 | return this.ResolveSettingsVariable(this.config.testExecutablePath); 34 | } 35 | GetDebugConfiguration(): (IDebugConfiguration | undefined) { 36 | return { 37 | name: "", 38 | args: undefined, 39 | program: undefined, 40 | target: undefined 41 | } 42 | } 43 | GetWorkspaceFolders(): readonly IWorkspaceFolder[] | undefined { 44 | return [{ 45 | name: "myWorkspace", 46 | index: 1, 47 | uri: "." 48 | }]; 49 | } 50 | protected GetCurrentFilename(): string { 51 | return "myFile.c"; 52 | } 53 | protected GetCurrentWorkspaceFolder(): string { 54 | return "myFolder"; 55 | } 56 | protected GlobFiles(wildcardString: string): string[] { 57 | if(wildcardString.indexOf("*") === -1) 58 | return [wildcardString]; 59 | const splitPath = wildcardString.split("/"); 60 | const testPrefix = splitPath.slice(-1)[0].slice(0, -1); 61 | const binFolder = splitPath.slice(0, -1).join("/") 62 | return this.filesToFind 63 | .filter(f => f.indexOf(testPrefix) != -1) 64 | .map(file => `${binFolder}/${file}`); 65 | } 66 | } 67 | 68 | describe("SettingsProvider should", () => { 69 | const config: IWorkspaceConfiguration = { 70 | debugLaunchConfigurationName: "", 71 | objDumpExecutable: "", 72 | logfile: "", 73 | logpanel: false, 74 | testExecutable: "", 75 | testExecutablePath: "", 76 | testLocationFetchMode: "", 77 | preLaunchTask: "" 78 | }; 79 | let filesToFind = [""]; 80 | 81 | it("return all executables", () => { 82 | const logger = mock(); 83 | const expectedPath = "/mnt/myPath/myExecutable"; 84 | 85 | config.testExecutable = "/mnt/myPath/myExecutable"; 86 | 87 | const settingsProvider = new TestSettingsProvider(logger, config, filesToFind); 88 | 89 | const actualPath = settingsProvider.GetTestRunners() 90 | expect(actualPath).to.have.members([expectedPath]); 91 | }) 92 | 93 | it("return all executables with wildcard", () => { 94 | const logger = mock(); 95 | const expectedPath = [ 96 | "myFolder/unittest/build/HAL_UnitTests1", 97 | "myFolder/unittest/build/HAL_UnitTests2", 98 | "myFolder/unittest/build/HAL_UnitTests3"]; 99 | 100 | config.testExecutable = "${workspaceFolder}/unittest/build/HAL_UnitTests*"; 101 | filesToFind = [ 102 | "HAL_UnitTests1", 103 | "HAL_UnitTests2", 104 | "HAL_UnitTests3", 105 | ] 106 | const settingsProvider = new TestSettingsProvider(logger, config, filesToFind); 107 | 108 | const actualPath = settingsProvider.GetTestRunners() 109 | expect(actualPath).to.be.deep.eq(expectedPath); 110 | }) 111 | 112 | it("return all executables in different paths with wildcard", () => { 113 | const logger = mock(); 114 | const expectedPath = [ 115 | "myFolder/app/build/App_UnitTests1", 116 | "myFolder/app/build/App_UnitTests2", 117 | "myFolder/hal/build/HAL_UnitTests1", 118 | "myFolder/hal/build/HAL_UnitTests2", 119 | "myFolder/hal/build/HAL_UnitTests3", 120 | "myFolder/pal/build/PAL_UnitTests1", 121 | ]; 122 | 123 | config.testExecutable = "${workspaceFolder}/app/build/App_UnitTests*;" + 124 | "${workspaceFolder}/pal/build/PAL_UnitTests*;" + 125 | "${workspaceFolder}/hal/build/HAL_UnitTests*"; 126 | filesToFind = [ 127 | "App_UnitTests1", 128 | "App_UnitTests2", 129 | "HAL_UnitTests1", 130 | "HAL_UnitTests2", 131 | "HAL_UnitTests3", 132 | "PAL_UnitTests1" 133 | ] 134 | const settingsProvider = new TestSettingsProvider(logger, config, filesToFind); 135 | 136 | const actualPath = settingsProvider.GetTestRunners() 137 | expect(actualPath).to.have.members(expectedPath); 138 | }) 139 | 140 | describe("testExecutablePath variable resolution", () => { 141 | it("should resolve ${workspaceFolder} in testExecutablePath", () => { 142 | const logger = mock(); 143 | config.testExecutablePath = "${workspaceFolder}/test"; 144 | const expectedPath = "myFolder/test"; 145 | 146 | const settingsProvider = new TestSettingsProvider(logger, config, filesToFind); 147 | 148 | const actualPath = settingsProvider.GetTestPath(); 149 | expect(actualPath).to.equal(expectedPath); 150 | }); 151 | 152 | it("should return empty string when testExecutablePath is empty", () => { 153 | const logger = mock(); 154 | config.testExecutablePath = ""; 155 | 156 | const settingsProvider = new TestSettingsProvider(logger, config, filesToFind); 157 | 158 | const actualPath = settingsProvider.GetTestPath(); 159 | expect(actualPath).to.equal(""); 160 | }); 161 | 162 | it("should return string as-is when no variables present", () => { 163 | const logger = mock(); 164 | config.testExecutablePath = "/absolute/path/test"; 165 | 166 | const settingsProvider = new TestSettingsProvider(logger, config, filesToFind); 167 | 168 | const actualPath = settingsProvider.GetTestPath(); 169 | expect(actualPath).to.equal("/absolute/path/test"); 170 | }); 171 | }); 172 | 173 | describe("Enhanced workingDirectory logic with whitespace handling", () => { 174 | const logger = mock(); 175 | const testCommand = "/path/to/test/executable"; 176 | const expectedDirname = "/path/to/test"; 177 | const mockProcessExecuter = { 178 | Exec: () => {}, 179 | ExecFile: () => {}, 180 | KillProcess: () => {} 181 | }; 182 | 183 | it("should use dirname(command) when workingDirectory is empty string", () => { 184 | const options = { workingDirectory: "" }; 185 | const runner = new ExecutableRunner( 186 | mockProcessExecuter, testCommand, logger, options 187 | ); 188 | 189 | const workingDir = runner.WorkingDirectory; 190 | expect(workingDir).to.equal(expectedDirname); 191 | }); 192 | 193 | it("should use dirname(command) when workingDirectory is whitespace only", () => { 194 | const whitespaceOnlyCases = [" ", "\t", "\n", " \t \n ", "\t\t"]; 195 | 196 | whitespaceOnlyCases.forEach(whitespace => { 197 | const options = { workingDirectory: whitespace }; 198 | const runner = new ExecutableRunner( 199 | mockProcessExecuter, testCommand, logger, options 200 | ); 201 | 202 | const workingDir = runner.WorkingDirectory; 203 | expect(workingDir).to.equal(expectedDirname, 204 | `Failed for whitespace: "${whitespace}"`); 205 | }); 206 | }); 207 | 208 | it("should use dirname(command) when workingDirectory is undefined", () => { 209 | const options = { workingDirectory: undefined }; 210 | const runner = new ExecutableRunner( 211 | mockProcessExecuter, testCommand, logger, options 212 | ); 213 | 214 | const workingDir = runner.WorkingDirectory; 215 | expect(workingDir).to.equal(expectedDirname); 216 | }); 217 | 218 | it("should use dirname(command) when workingDirectory is null", () => { 219 | const options = { workingDirectory: null as any }; 220 | const runner = new ExecutableRunner( 221 | mockProcessExecuter, testCommand, logger, options 222 | ); 223 | 224 | const workingDir = runner.WorkingDirectory; 225 | expect(workingDir).to.equal(expectedDirname); 226 | }); 227 | 228 | it("should use dirname(command) when options is undefined", () => { 229 | const runner = new ExecutableRunner( 230 | mockProcessExecuter, testCommand, logger, undefined 231 | ); 232 | 233 | const workingDir = runner.WorkingDirectory; 234 | expect(workingDir).to.equal(expectedDirname); 235 | }); 236 | 237 | it("should preserve valid workingDirectory and trim whitespace", () => { 238 | const validPath = "/custom/working/directory"; 239 | const pathWithWhitespace = ` ${validPath} `; 240 | 241 | const options = { workingDirectory: pathWithWhitespace }; 242 | const runner = new ExecutableRunner( 243 | mockProcessExecuter, testCommand, logger, options 244 | ); 245 | 246 | const workingDir = runner.WorkingDirectory; 247 | expect(workingDir).to.equal(validPath); // Should be trimmed 248 | }); 249 | 250 | it("should preserve valid workingDirectory without whitespace", () => { 251 | const validPath = "/custom/working/directory"; 252 | 253 | const options = { workingDirectory: validPath }; 254 | const runner = new ExecutableRunner( 255 | mockProcessExecuter, testCommand, logger, options 256 | ); 257 | 258 | const workingDir = runner.WorkingDirectory; 259 | expect(workingDir).to.equal(validPath); 260 | }); 261 | 262 | it("should handle relative paths correctly", () => { 263 | const relativePath = "./relative/path"; 264 | 265 | const options = { workingDirectory: relativePath }; 266 | const runner = new ExecutableRunner( 267 | mockProcessExecuter, testCommand, logger, options 268 | ); 269 | 270 | const workingDir = runner.WorkingDirectory; 271 | expect(workingDir).to.equal(relativePath); 272 | }); 273 | }); 274 | }); -------------------------------------------------------------------------------- /tests/CppUTestContainer.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect } from "chai"; 2 | import { mock, instance, when, verify, anything, reset, anyString } from "ts-mockito"; 3 | import ExecutableRunner, { RunResult, RunResultStatus } from "../src/Infrastructure/ExecutableRunner"; 4 | import CppUTestContainer from "../src/Domain/CppUTestContainer"; 5 | import { CppUTestGroup } from "../src/Domain/CppUTestGroup"; 6 | import { TestSuiteInfo } from "vscode-test-adapter-api"; 7 | import { TestResult } from "../src/Domain/TestResult"; 8 | import { DebugConfiguration, WorkspaceFolder } from "vscode"; 9 | import { TestState } from "../src/Domain/TestState"; 10 | import { ResultParser } from "../src/Domain/ResultParser"; 11 | import { IDebugConfiguration, SettingsProvider, TestLocationFetchMode, VscodeAdapter } from "../src/Infrastructure/Infrastructure"; 12 | import { CppUTest } from "../src/Domain/CppUTest"; 13 | 14 | describe("CppUTestContainer should", () => { 15 | let mockRunner: ExecutableRunner; 16 | let mockSetting: SettingsProvider; 17 | let mockAdapter: VscodeAdapter; 18 | let mockResultParser: ResultParser; 19 | 20 | beforeEach(() => { 21 | mockRunner = createMockRunner("Exec1", TestLocationFetchMode.Disabled, "Group1.Test1 Group2.Test2", false); 22 | mockSetting = mock(); 23 | mockAdapter = mock(); 24 | mockResultParser = mock(); 25 | when(mockSetting.GetTestPath()).thenReturn("/test/myPath"); 26 | when(mockSetting.GetTestRunners()).thenReturn(["/test/myPath/Exec1"]); 27 | when(mockSetting.TestLocationFetchMode).thenReturn(TestLocationFetchMode.Disabled); 28 | when(mockResultParser.GetResult(anything())).thenReturn(new TestResult(TestState.Passed, "")); 29 | }) 30 | 31 | it("load all tests from all testrunners without location info", async () => { 32 | const mockRunner1 = createMockRunner("Exec1", TestLocationFetchMode.Disabled, "Group1.Test1 Group2.Test2", false); 33 | const mockRunner2 = createMockRunner("Exec2", TestLocationFetchMode.Disabled, "Group4.Test1 Group5.Test2 Group5.Test42", false); 34 | const mockSetting = mock(); 35 | when(mockSetting.TestLocationFetchMode).thenReturn(TestLocationFetchMode.Disabled); 36 | 37 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 38 | 39 | const allTests = await container.LoadTests([instance(mockRunner1), instance(mockRunner2)]); 40 | expect(allTests).to.be.lengthOf(2); 41 | 42 | expect(allTests[0].label).to.be.eq("Exec1"); 43 | expect(allTests[0].children).to.be.lengthOf(2); 44 | expect(allTests[0].children[0].label).to.be.eq("Group1"); 45 | expect(allTests[0].children[0].type).to.be.eq("suite"); 46 | expect((allTests[0].children[0] as CppUTestGroup).children).to.be.lengthOf(1); 47 | expect((allTests[0].children[0] as CppUTestGroup).children[0].label).to.be.eq("Test1"); 48 | expect((allTests[0].children[0] as CppUTestGroup).children[0].type).to.be.eq("test"); 49 | expect((allTests[0].children[0] as CppUTestGroup).children[0].file).to.be.eq(undefined); 50 | expect((allTests[0].children[0] as CppUTestGroup).children[0].line).to.be.eq(undefined); 51 | expect(allTests[0].children[1].label).to.be.eq("Group2"); 52 | expect(allTests[0].children[1].type).to.be.eq("suite"); 53 | expect((allTests[0].children[1] as CppUTestGroup).children).to.be.lengthOf(1); 54 | expect((allTests[0].children[1] as CppUTestGroup).children[0].label).to.be.eq("Test2"); 55 | expect((allTests[0].children[1] as CppUTestGroup).children[0].type).to.be.eq("test"); 56 | expect((allTests[0].children[1] as CppUTestGroup).children[0].file).to.be.eq(undefined); 57 | expect((allTests[0].children[1] as CppUTestGroup).children[0].line).to.be.eq(undefined); 58 | 59 | expect(allTests[1].label).to.be.eq("Exec2"); 60 | expect(allTests[1].children).to.be.lengthOf(2); 61 | expect(allTests[1].children[0].label).to.be.eq("Group4"); 62 | expect(allTests[1].children[0].type).to.be.eq("suite"); 63 | expect((allTests[1].children[0] as CppUTestGroup).children).to.be.lengthOf(1); 64 | expect((allTests[1].children[0] as CppUTestGroup).children[0].label).to.be.eq("Test1"); 65 | expect((allTests[1].children[0] as CppUTestGroup).children[0].type).to.be.eq("test"); 66 | expect((allTests[1].children[0] as CppUTestGroup).children[0].file).to.be.eq(undefined); 67 | expect((allTests[1].children[0] as CppUTestGroup).children[0].line).to.be.eq(undefined); 68 | expect(allTests[1].children[1].label).to.be.eq("Group5"); 69 | expect(allTests[1].children[1].type).to.be.eq("suite"); 70 | expect((allTests[1].children[1] as CppUTestGroup).children).to.be.lengthOf(2); 71 | expect((allTests[1].children[1] as CppUTestGroup).children[0].label).to.be.eq("Test42"); 72 | expect((allTests[1].children[1] as CppUTestGroup).children[0].type).to.be.eq("test"); 73 | expect((allTests[1].children[1] as CppUTestGroup).children[0].file).to.be.eq(undefined); 74 | expect((allTests[1].children[1] as CppUTestGroup).children[0].line).to.be.eq(undefined); 75 | expect((allTests[1].children[1] as CppUTestGroup).children[1].label).to.be.eq("Test2"); 76 | expect((allTests[1].children[1] as CppUTestGroup).children[1].type).to.be.eq("test"); 77 | expect((allTests[1].children[1] as CppUTestGroup).children[1].file).to.be.eq(undefined); 78 | expect((allTests[1].children[1] as CppUTestGroup).children[1].line).to.be.eq(undefined); 79 | }) 80 | 81 | it("load all tests from all testrunners with location info", async () => { 82 | const mockRunner1 = createMockRunner("Exec1", TestLocationFetchMode.TestQuery, "Group1.Test1.Test.file.name.cpp.12\nGroup2.Test2.Test2.cpp.4356\n", true); 83 | const mockRunner2 = createMockRunner("Exec2", TestLocationFetchMode.TestQuery, "Group4.Test1.File4.75\nGroup5.Test2.File5.342\nGroup5.Test42.File5.4", true); 84 | const mockSetting = mock(); 85 | when(mockSetting.TestLocationFetchMode).thenReturn(TestLocationFetchMode.TestQuery); 86 | 87 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 88 | 89 | const allTests = await container.LoadTests([instance(mockRunner1), instance(mockRunner2)]); 90 | expect(allTests).to.be.lengthOf(2); 91 | 92 | expect(allTests[0].label).to.be.eq("Exec1"); 93 | expect(allTests[0].children).to.be.lengthOf(2); 94 | expect(allTests[0].children[0].label).to.be.eq("Group1"); 95 | expect(allTests[0].children[0].type).to.be.eq("suite"); 96 | expect((allTests[0].children[0] as CppUTestGroup).children).to.be.lengthOf(1); 97 | expect((allTests[0].children[0] as CppUTestGroup).children[0].label).to.be.eq("Test1"); 98 | expect((allTests[0].children[0] as CppUTestGroup).children[0].type).to.be.eq("test"); 99 | expect((allTests[0].children[0] as CppUTestGroup).children[0].file).to.be.eq("Test.file.name.cpp"); 100 | expect((allTests[0].children[0] as CppUTestGroup).children[0].line).to.be.eq(12); 101 | expect(allTests[0].children[1].label).to.be.eq("Group2"); 102 | expect(allTests[0].children[1].type).to.be.eq("suite"); 103 | expect((allTests[0].children[1] as CppUTestGroup).children).to.be.lengthOf(1); 104 | expect((allTests[0].children[1] as CppUTestGroup).children[0].label).to.be.eq("Test2"); 105 | expect((allTests[0].children[1] as CppUTestGroup).children[0].type).to.be.eq("test"); 106 | expect((allTests[0].children[1] as CppUTestGroup).children[0].file).to.be.eq("Test2.cpp"); 107 | expect((allTests[0].children[1] as CppUTestGroup).children[0].line).to.be.eq(4356); 108 | 109 | expect(allTests[1].label).to.be.eq("Exec2"); 110 | expect(allTests[1].children).to.be.lengthOf(2); 111 | expect(allTests[1].children[0].label).to.be.eq("Group4"); 112 | expect(allTests[1].children[0].type).to.be.eq("suite"); 113 | expect((allTests[1].children[0] as CppUTestGroup).children).to.be.lengthOf(1); 114 | expect((allTests[1].children[0] as CppUTestGroup).children[0].label).to.be.eq("Test1"); 115 | expect((allTests[1].children[0] as CppUTestGroup).children[0].type).to.be.eq("test"); 116 | expect((allTests[1].children[0] as CppUTestGroup).children[0].file).to.be.eq("File4"); 117 | expect((allTests[1].children[0] as CppUTestGroup).children[0].line).to.be.eq(75); 118 | expect(allTests[1].children[1].label).to.be.eq("Group5"); 119 | expect(allTests[1].children[1].type).to.be.eq("suite"); 120 | expect((allTests[1].children[1] as CppUTestGroup).children).to.be.lengthOf(2); 121 | expect((allTests[1].children[1] as CppUTestGroup).children[0].label).to.be.eq("Test42"); 122 | expect((allTests[1].children[1] as CppUTestGroup).children[0].type).to.be.eq("test"); 123 | expect((allTests[1].children[1] as CppUTestGroup).children[0].file).to.be.eq("File5"); 124 | expect((allTests[1].children[1] as CppUTestGroup).children[0].line).to.be.eq(4); 125 | expect((allTests[1].children[1] as CppUTestGroup).children[1].label).to.be.eq("Test2"); 126 | expect((allTests[1].children[1] as CppUTestGroup).children[1].type).to.be.eq("test"); 127 | expect((allTests[1].children[1] as CppUTestGroup).children[1].file).to.be.eq("File5"); 128 | expect((allTests[1].children[1] as CppUTestGroup).children[1].line).to.be.eq(342); 129 | }) 130 | 131 | it("reload all tests after clear", async () => { 132 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 133 | const testList1 = await container.LoadTests([instance(mockRunner)]); 134 | container.ClearTests() 135 | const testList2 = await container.LoadTests([instance(mockRunner)]); 136 | expect(JSON.stringify(testList1)).to.be.eq(JSON.stringify(testList2)); 137 | }) 138 | 139 | it("get the same id on consecutive loads", async () => { 140 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 141 | const testList1 = await container.LoadTests([instance(mockRunner)]); 142 | const testList2 = await container.LoadTests([instance(mockRunner)]); 143 | expect(JSON.stringify(testList1)).to.be.eq(JSON.stringify(testList2)); 144 | }) 145 | 146 | it("run all tests", async () => { 147 | const mockRunner = createMockRunner("Exec1", TestLocationFetchMode.Disabled, "Group1.Test1 Group2.Test2", false); 148 | when(mockSetting.TestLocationFetchMode).thenReturn(TestLocationFetchMode.Disabled); 149 | 150 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 151 | 152 | await container.LoadTests([instance(mockRunner)]); 153 | await container.RunAllTests(); 154 | verify(mockRunner.RunTest("Group1", "Test1")).called(); 155 | verify(mockRunner.RunTest("Group2", "Test2")).called(); 156 | }) 157 | 158 | it("run test by id", async () => { 159 | const mockRunner = createMockRunner("Exec1", TestLocationFetchMode.Disabled, "Group1.Test1 Group2.Test2", false); 160 | 161 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 162 | 163 | const allTests = await container.LoadTests([instance(mockRunner)]); 164 | const testToRun = (allTests[0].children[0] as TestSuiteInfo).children[0]; 165 | await container.RunTest(testToRun.id); 166 | verify(mockRunner.RunTest("Group1", "Test1")).called(); 167 | verify(mockRunner.RunTest("Group2", "Test2")).never(); 168 | }) 169 | 170 | it("run tests by ids", async () => { 171 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 172 | 173 | const allTests = await container.LoadTests([instance(mockRunner)]); 174 | const testsToRun = [ 175 | (allTests[0].children[0] as TestSuiteInfo).children[0], 176 | (allTests[0].children[1] as TestSuiteInfo).children[0] 177 | ]; 178 | await container.RunTest(...testsToRun.map(t => t.id)); 179 | verify(mockRunner.RunTest("Group1", "Test1")).called(); 180 | verify(mockRunner.RunTest("Group2", "Test2")).called(); 181 | }) 182 | 183 | it("run tests by executable group id", async () => { 184 | const mockRunner1 = createMockRunner("Exec1", TestLocationFetchMode.Disabled, "Group1.Test1 Group2.Test2", false); 185 | const mockRunner2 = createMockRunner("Exec2", TestLocationFetchMode.Disabled, "Group3.Test1 Group4.Test2", false); 186 | 187 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 188 | 189 | const allTests = await container.LoadTests([instance(mockRunner1), instance(mockRunner2)]); 190 | expect(allTests).to.have.lengthOf(2); 191 | const testsToRun = allTests[0]; 192 | await container.RunTest(testsToRun.id); 193 | verify(mockRunner1.RunTest("Group1", "Test1")).once(); 194 | verify(mockRunner1.RunTest("Group2", "Test2")).once(); 195 | verify(mockRunner2.RunTest(anyString(),anyString())).never(); 196 | }) 197 | 198 | it("run tests by group ids", async () => { 199 | const mockRunner = createMockRunner("Exec1", TestLocationFetchMode.Disabled, "Group1.Test1 Group1.Test2 Group2.Test2 Group2.Test5", false); 200 | 201 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 202 | 203 | const allTests = await container.LoadTests([instance(mockRunner)]); 204 | expect(allTests).to.have.lengthOf(1); 205 | expect(allTests[0].children).to.have.lengthOf(2); 206 | const testsToRunId = allTests[0].children[0].id; 207 | 208 | await container.RunTest(testsToRunId); 209 | verify(mockRunner.RunTest("Group1", "Test1")).once(); 210 | verify(mockRunner.RunTest("Group1", "Test2")).once(); 211 | verify(mockRunner.RunTest("Group2", "Test2")).never(); 212 | verify(mockRunner.RunTest("Group2", "Test5")).never(); 213 | }) 214 | 215 | it("return a TestResult after sucessful run", async () => { 216 | const mockRunner = createMockRunner("Exec1", TestLocationFetchMode.Disabled, "Group1.Test1 Group1.Test2", false); 217 | when(mockRunner.RunTest(anyString(), anyString())).thenResolve(new RunResult(RunResultStatus.Success, "Success")); 218 | 219 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 220 | 221 | const allTests = await container.LoadTests([instance(mockRunner)]); 222 | const testToRun = (allTests[0].children[0] as TestSuiteInfo).children[0]; 223 | return expect(container.RunTest(testToRun.id)).to.be 224 | .eventually.fulfilled 225 | .and.to.have.deep.members([ 226 | { message: "", state: TestState.Passed } 227 | ]) 228 | }) 229 | 230 | it("return a TestResult after failed run", async () => { 231 | const mockRunner = createMockRunner("Exec1", TestLocationFetchMode.Disabled, "Group1.Test1 Group1.Test2 Group1.Test3", false); 232 | const runResult1 = new RunResult(RunResultStatus.Success, "Success"); 233 | const runResult2 = new RunResult(RunResultStatus.Failure, "Failed"); 234 | const runResult3 = new RunResult(RunResultStatus.Error, "Error") 235 | when(mockRunner.RunTest("Group1", "Test1")).thenResolve(runResult1); 236 | when(mockRunner.RunTest("Group1", "Test2")).thenResolve(runResult2); 237 | when(mockRunner.RunTest("Group1", "Test3")).thenResolve(runResult3); 238 | reset(mockResultParser); 239 | when(mockResultParser.GetResult(runResult1)).thenReturn(new TestResult(TestState.Passed, "")); 240 | when(mockResultParser.GetResult(runResult2)).thenReturn(new TestResult(TestState.Failed, "Failed")); 241 | when(mockResultParser.GetResult(runResult3)).thenReturn(new TestResult(TestState.Failed, "Error")); 242 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 243 | 244 | const allTests = await container.LoadTests([instance(mockRunner)]); 245 | const testToRun = (allTests[0].children[0] as TestSuiteInfo); 246 | return expect(container.RunTest(testToRun.id)).to.be 247 | .eventually.fulfilled 248 | .and.to.have.deep.members([ 249 | { message: "", state: TestState.Passed }, 250 | { message: "Failed", state: TestState.Failed }, 251 | { message: "Error", state: TestState.Failed } 252 | ]) 253 | }) 254 | 255 | it("start the debugger for the given test", async () => { 256 | const mockFolder = mock(); 257 | const debugConfigSpy = { 258 | name: "", 259 | request: "", 260 | type: "" 261 | } as IDebugConfiguration; 262 | when(mockSetting.GetWorkspaceFolders()).thenReturn([instance(mockFolder)]); 263 | when(mockSetting.GetDebugConfiguration()).thenReturn(debugConfigSpy); 264 | when(mockRunner.Command).thenReturn("myProgram"); 265 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 266 | 267 | const allTests = await container.LoadTests([instance(mockRunner)]); 268 | expect(allTests).to.have.lengthOf(1); 269 | const testsToRunId = (allTests[0].children[0] as CppUTestGroup).children[0].id; 270 | 271 | await container.DebugTest(testsToRunId); 272 | verify(mockAdapter.StartDebugger(anything(), anything())).called(); 273 | expect((debugConfigSpy as DebugConfiguration).name).to.be.eq("Group1.Test1"); 274 | expect((debugConfigSpy as DebugConfiguration).args).to.be.deep.eq(["-t", "Group1.Test1"]); 275 | expect((debugConfigSpy as DebugConfiguration).program).to.be.deep.eq("myProgram"); 276 | }) 277 | 278 | it("start the debugger for a given test group", async () => { 279 | const mockFolder = mock(); 280 | const debugConfigSpy = { 281 | name: "", 282 | request: "", 283 | type: "" 284 | } as DebugConfiguration; 285 | when(mockSetting.GetWorkspaceFolders()).thenReturn([instance(mockFolder)]); 286 | when(mockSetting.GetDebugConfiguration()).thenReturn(debugConfigSpy); 287 | when(mockRunner.Command).thenReturn("myProgram"); 288 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 289 | 290 | const allTests = await container.LoadTests([instance(mockRunner)]); 291 | expect(allTests).to.have.lengthOf(1); 292 | const testsToRunId = allTests[0].children[0].id; 293 | 294 | await container.DebugTest(testsToRunId); 295 | verify(mockAdapter.StartDebugger(anything(), anything())).called(); 296 | expect((debugConfigSpy as DebugConfiguration).name).to.be.eq("Group1"); 297 | expect((debugConfigSpy as DebugConfiguration).args).to.be.deep.eq(["-sg", "Group1"]); 298 | expect((debugConfigSpy as DebugConfiguration).program).to.be.deep.eq("myProgram"); 299 | }) 300 | 301 | it("debug the entire test suite when main suite id is provided", async () => { 302 | const mockFolder = mock(); 303 | const debugConfigSpy = { 304 | name: "", 305 | request: "", 306 | type: "" 307 | } as DebugConfiguration; 308 | when(mockSetting.GetWorkspaceFolders()).thenReturn([instance(mockFolder)]); 309 | when(mockSetting.GetDebugConfiguration()).thenReturn(debugConfigSpy); 310 | when(mockRunner.Command).thenReturn("myProgram"); 311 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 312 | 313 | const allTests = await container.LoadTests([instance(mockRunner)]); 314 | expect(allTests).to.have.lengthOf(1); 315 | 316 | // When the main suite (executable) id is provided, it should be able to debug 317 | const mainSuiteId = allTests[0].id; 318 | 319 | await container.DebugTest(mainSuiteId); 320 | 321 | // The debugger should be started for the entire suite 322 | verify(mockAdapter.StartDebugger(anything(), anything())).called(); 323 | // When debugging the entire suite, args should be empty or contain no test filters 324 | expect((debugConfigSpy as DebugConfiguration).name).to.be.eq("Exec1"); 325 | expect((debugConfigSpy as DebugConfiguration).args).to.be.deep.eq([]); 326 | }) 327 | 328 | it("thrown an error if the debugger is started without config", async () => { 329 | mockSetting = mock(); 330 | when(mockSetting.GetDebugConfiguration()).thenReturn(undefined); 331 | when(mockSetting.TestLocationFetchMode).thenReturn(TestLocationFetchMode.Disabled); 332 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 333 | 334 | const allTests = await container.LoadTests([instance(mockRunner)]); 335 | const testsToRunId = allTests[0].id; 336 | 337 | return expect(container.DebugTest(testsToRunId)).to.be. 338 | eventually.rejectedWith("No debug configuration found. Not able to debug"); 339 | }) 340 | 341 | it("thrown an error if the debugger is started without workspaceFolders", async () => { 342 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 343 | 344 | const allTests = await container.LoadTests([instance(mockRunner)]); 345 | const testsToRunId = allTests[0].id; 346 | 347 | return expect(container.DebugTest(testsToRunId)).to.be. 348 | eventually.rejectedWith("No workspaceFolders found. Not able to debug!"); 349 | }) 350 | 351 | it("notify the caller on test start and finish", async () => { 352 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 353 | const allTests = await container.LoadTests([instance(mockRunner)]); 354 | 355 | const testToRun = (allTests[0].children[0] as TestSuiteInfo).children[0]; 356 | let testOnStart: CppUTest = new CppUTest("", "", "", undefined, undefined); 357 | let testOnFinish: CppUTest = new CppUTest("", "", "", undefined, undefined); 358 | let testResultOnFinish: TestResult | undefined = undefined; 359 | container.OnTestStart = test => testOnStart = test; 360 | container.OnTestFinish = (test, result) => { testOnFinish = test; testResultOnFinish = result }; 361 | await container.RunTest(testToRun.id); 362 | expect(testOnStart).to.be.deep.eq(testToRun); 363 | expect(testOnFinish).to.be.deep.eq(testToRun); 364 | expect(testResultOnFinish).to.be.not.undefined; 365 | }) 366 | 367 | it("kill the process currently running", async () => { 368 | const mockRunner = createMockRunner("Exec1", TestLocationFetchMode.Disabled, "Group1.Test1 Group2.Test2", false); 369 | const container = new CppUTestContainer(instance(mockSetting), instance(mockAdapter), instance(mockResultParser)); 370 | 371 | await container.LoadTests([instance(mockRunner)]); 372 | 373 | container.KillRunningProcesses(); 374 | verify(mockRunner.KillProcess()).called(); 375 | }) 376 | }); 377 | 378 | function createMockRunner(runnerName: string, fetchMode: TestLocationFetchMode, testListString: string, hasLocation: boolean) { 379 | const mockRunner = mock(); 380 | when(mockRunner.Name).thenReturn(runnerName); 381 | when(mockRunner.GetTestList(fetchMode)).thenResolve([testListString, hasLocation]); 382 | return mockRunner; 383 | } 384 | --------------------------------------------------------------------------------