Skip to content

Commit

Permalink
Fix and refactor xml file parsing with test
Browse files Browse the repository at this point in the history
  • Loading branch information
SizaSL committed Aug 27, 2021
1 parent c9ca98e commit 1d59b19
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
35 changes: 24 additions & 11 deletions src/framework/CppUTestRunnable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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))
Expand Down
48 changes: 48 additions & 0 deletions test/framework/CppUTestRunnable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

///

Expand Down Expand Up @@ -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[] = [
'<?xml version="1.0" encoding="UTF-8" ?>',
'<testsuite errors="0" failures="0" hostname="localhost" name="FirstTestGroup" tests="2" time="4.002" timestamp="2021-08-26T16:19:26">',
'<properties>',
'</properties>',
'<testcase classname="FirstTestGroup" name="SecondTest" assertions="0" time="2.001" file="/mnt/c/Users/testcppcpputestcpputest1.cpp" line="20">',
'</testcase>',
'<testcase classname="FirstTestGroup" name="FirstTest" assertions="0" time="2.001" file="/mnt/c/Users/testcppcpputestcpputest1.cpp" line="15">',
'</testcase>',
'<system-out></system-out>',
'<system-err></system-err>',
'</testsuite>,',
];
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');
}
});
});
});

0 comments on commit 1d59b19

Please sign in to comment.