From 1d59b19594e1acca3df07887642f42fedf8e160a Mon Sep 17 00:00:00 2001 From: Siza Date: Thu, 26 Aug 2021 22:07:48 +1200 Subject: [PATCH] Fix and refactor xml file parsing with test --- CHANGELOG.md | 2 +- package.json | 2 +- src/framework/CppUTestRunnable.ts | 35 ++++++++++++------ test/framework/CppUTestRunnable.test.ts | 48 +++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b7280ff..4dca6263 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [Unreleased] -## [3.7.0] - 2021-08-21 +## [3.7.0] Feature Added: CppUTest Framework Support with tests diff --git a/package.json b/package.json index bacdb30b..b26c899d 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "icon": "resources/icon.png", "author": "Mate Pek", "publisher": "matepek", - "version": "3.6.26", + "version": "3.7.0", "license": "MIT", "homepage": "https://github.com/matepek/vscode-catch2-test-adapter", "repository": { diff --git a/src/framework/CppUTestRunnable.ts b/src/framework/CppUTestRunnable.ts index d2626fa0..d5a54a83 100644 --- a/src/framework/CppUTestRunnable.ts +++ b/src/framework/CppUTestRunnable.ts @@ -51,13 +51,12 @@ export class CppUTestRunnable extends AbstractRunnable { const reloadResult = new RunnableReloadResult(); - for (let i = 0; i < xml.testsuites.testsuite.length; ++i) { - const suiteName = xml.testsuites.testsuite[i].$.name; + const processTestcases = async (testsuite: any, reloadResult: RunnableReloadResult) => { + const suiteName = testsuite.$.name; + for (let i = 0; i < testsuite.testcase.length; i++) { + if (cancellationFlag.isCancellationRequested) return; - for (let j = 0; j < xml.testsuites.testsuite[i].testcase.length; j++) { - if (cancellationFlag.isCancellationRequested) return reloadResult; - - const testCase = xml.testsuites.testsuite[i].testcase[j]; + const testCase = testsuite.testcase[i]; const testName = testCase.$.name.startsWith('DISABLED_') ? testCase.$.name.substr(9) : testCase.$.name; const testNameAsId = suiteName + '.' + testCase.$.name; @@ -76,6 +75,15 @@ export class CppUTestRunnable extends AbstractRunnable { )), ); } + }; + + if (xml.testsuites !== undefined) { + for (let i = 0; i < xml.testsuites.testsuite.length; ++i) { + await processTestcases(xml.testsuites.testsuite[i], reloadResult) + .catch((err) => this._shared.log.info('Error', err)); + } + } else { + await processTestcases(xml.testsuite, reloadResult); } return reloadResult; @@ -160,15 +168,20 @@ export class CppUTestRunnable extends AbstractRunnable { .catch(err => this._shared.log.error('error creating xmls folder: ', junitXmlsFolderPath, err)); //Generate xml files const args = this.properties.prependTestListingArgs.concat(['-ojunit']); - const options = this.properties.options; - options.cwd = junitXmlsFolderPath; + const options = { cwd: junitXmlsFolderPath }; await this.properties.spawner .spawnAsync(this.properties.path, args, options, 30000) .then(() => this._shared.log.info('create cpputest xmls', this.properties.path, args, options.cwd)); //Merge xmls into single xml - await mergeFiles(cacheFile, [junitXmlsFolderPath + '/*.xml']) - .then(() => this._shared.log.info('cache xml written', cacheFile)) - .catch(err => this._shared.log.warn('combine xml cache file could not create: ', cacheFile, err)); + fs.readdir(junitXmlsFolderPath, (err, files) => { + if (files.length > 1) { + mergeFiles(cacheFile, [junitXmlsFolderPath + '/*.xml']) + .then(() => this._shared.log.info('cache xml written', cacheFile)) + .catch(err => this._shared.log.warn('combine xml cache file could not create: ', cacheFile, err)); + } else { + fs.copyFile(junitXmlsFolderPath + '/' + files[0], cacheFile); + } + }); //Delete xmls folder fs.remove(junitXmlsFolderPath) .then(() => this._shared.log.info('junit-xmls folder deleted', junitXmlsFolderPath)) diff --git a/test/framework/CppUTestRunnable.test.ts b/test/framework/CppUTestRunnable.test.ts index 46077093..c9cc9c16 100644 --- a/test/framework/CppUTestRunnable.test.ts +++ b/test/framework/CppUTestRunnable.test.ts @@ -5,6 +5,7 @@ import { CppUTestRunnable } from '../../src/framework/CppUTestRunnable'; import { RootSuite } from '../../src/RootSuite'; import { RunnableProperties } from '../../src/RunnableProperties'; import { DefaultSpawner } from '../../src/Spawner'; +import { EOL } from 'os'; /// @@ -165,4 +166,51 @@ describe(pathlib.basename(__filename), function () { } }); }); + + context('Testing _reloadFromXml', function () { + it('should reload ex.1', async function () { + const { root, runnable } = createCppUTestRunnable(); + + const testOutput: string[] = [ + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + ',', + ]; + const res = await runnable['_reloadFromXml'](testOutput.join(EOL), { isCancellationRequested: false }); + + const tests = [...res.tests].sort((a, b) => a.testNameAsId.localeCompare(b.testNameAsId)); + + assert.strictEqual(tests.length, 2); + + assert.strictEqual(tests[0].testNameAsId, 'FirstTestGroup.FirstTest'); + assert.strictEqual(tests[0].label, 'FirstTest'); + assert.strictEqual(tests[0].file, pathlib.normalize('/mnt/c/Users/testcppcpputestcpputest1.cpp')); + assert.strictEqual(tests[0].line, 14); + assert.strictEqual(tests[0].skipped, false); + assert.strictEqual(tests[0].getStaticEvent('1'), undefined); + assert.strictEqual(tests[1].testNameAsId, 'FirstTestGroup.SecondTest'); + assert.strictEqual(tests[1].label, 'SecondTest'); + assert.strictEqual(tests[1].file, pathlib.normalize('/mnt/c/Users/testcppcpputestcpputest1.cpp')); + assert.strictEqual(tests[1].line, 19); + assert.strictEqual(tests[1].skipped, false); + assert.strictEqual(tests[1].getStaticEvent('1'), undefined); + + assert.strictEqual(root.children.length, 1); + const suite1 = root.children[0]; + assert.strictEqual(suite1.label, 'name'); + if (suite1.type === 'suite') { + assert.strictEqual(suite1.children.length, 2); + } else { + assert.strictEqual(suite1.type, 'suite'); + } + }); + }); });