Skip to content

Commit 313b117

Browse files
committed
Fix and refactor xml file parsing with test
1 parent c9ca98e commit 313b117

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

src/framework/CppUTestRunnable.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@ export class CppUTestRunnable extends AbstractRunnable {
5151

5252
const reloadResult = new RunnableReloadResult();
5353

54-
for (let i = 0; i < xml.testsuites.testsuite.length; ++i) {
55-
const suiteName = xml.testsuites.testsuite[i].$.name;
54+
const processTestcases = async (testsuite: any, reloadResult: RunnableReloadResult) => {
55+
const suiteName = testsuite.$.name;
56+
for (let i = 0; i < testsuite.testcase.length; i++) {
57+
if (cancellationFlag.isCancellationRequested) return;
5658

57-
for (let j = 0; j < xml.testsuites.testsuite[i].testcase.length; j++) {
58-
if (cancellationFlag.isCancellationRequested) return reloadResult;
59-
60-
const testCase = xml.testsuites.testsuite[i].testcase[j];
59+
const testCase = testsuite.testcase[i];
6160
const testName = testCase.$.name.startsWith('DISABLED_') ? testCase.$.name.substr(9) : testCase.$.name;
6261
const testNameAsId = suiteName + '.' + testCase.$.name;
6362

@@ -76,6 +75,15 @@ export class CppUTestRunnable extends AbstractRunnable {
7675
)),
7776
);
7877
}
78+
};
79+
80+
if (xml.testsuites !== undefined) {
81+
for (let i = 0; i < xml.testsuites.testsuite.length; ++i) {
82+
await processTestcases(xml.testsuites.testsuite[i], reloadResult)
83+
.catch((err) => this._shared.log.info('Error', err));
84+
}
85+
} else {
86+
await processTestcases(xml.testsuite, reloadResult);
7987
}
8088

8189
return reloadResult;
@@ -160,15 +168,20 @@ export class CppUTestRunnable extends AbstractRunnable {
160168
.catch(err => this._shared.log.error('error creating xmls folder: ', junitXmlsFolderPath, err));
161169
//Generate xml files
162170
const args = this.properties.prependTestListingArgs.concat(['-ojunit']);
163-
const options = this.properties.options;
164-
options.cwd = junitXmlsFolderPath;
171+
const options = { cwd: junitXmlsFolderPath };
165172
await this.properties.spawner
166173
.spawnAsync(this.properties.path, args, options, 30000)
167174
.then(() => this._shared.log.info('create cpputest xmls', this.properties.path, args, options.cwd));
168175
//Merge xmls into single xml
169-
await mergeFiles(cacheFile, [junitXmlsFolderPath + '/*.xml'])
170-
.then(() => this._shared.log.info('cache xml written', cacheFile))
171-
.catch(err => this._shared.log.warn('combine xml cache file could not create: ', cacheFile, err));
176+
fs.readdir(junitXmlsFolderPath, (err, files) => {
177+
if (files.length > 1) {
178+
mergeFiles(cacheFile, [junitXmlsFolderPath + '/*.xml'])
179+
.then(() => this._shared.log.info('cache xml written', cacheFile))
180+
.catch(err => this._shared.log.warn('combine xml cache file could not create: ', cacheFile, err));
181+
} else {
182+
fs.copyFile(junitXmlsFolderPath + '/' + files[0], cacheFile);
183+
}
184+
});
172185
//Delete xmls folder
173186
fs.remove(junitXmlsFolderPath)
174187
.then(() => this._shared.log.info('junit-xmls folder deleted', junitXmlsFolderPath))

test/framework/CppUTestRunnable.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { CppUTestRunnable } from '../../src/framework/CppUTestRunnable';
55
import { RootSuite } from '../../src/RootSuite';
66
import { RunnableProperties } from '../../src/RunnableProperties';
77
import { DefaultSpawner } from '../../src/Spawner';
8+
import { EOL } from 'os';
89

910
///
1011

@@ -165,4 +166,51 @@ describe(pathlib.basename(__filename), function () {
165166
}
166167
});
167168
});
169+
170+
context('Testing _reloadFromXml', function () {
171+
it('should reload ex.1', async function () {
172+
const { root, runnable } = createCppUTestRunnable();
173+
174+
const testOutput: string[] = [
175+
'<?xml version="1.0" encoding="UTF-8" ?>',
176+
'<testsuite errors="0" failures="0" hostname="localhost" name="FirstTestGroup" tests="2" time="4.002" timestamp="2021-08-26T16:19:26">',
177+
'<properties>',
178+
'</properties>',
179+
'<testcase classname="FirstTestGroup" name="SecondTest" assertions="0" time="2.001" file="/mnt/c/Users/testcppcpputestcpputest1.cpp" line="20">',
180+
'</testcase>',
181+
'<testcase classname="FirstTestGroup" name="FirstTest" assertions="0" time="2.001" file="/mnt/c/Users/testcppcpputestcpputest1.cpp" line="15">',
182+
'</testcase>',
183+
'<system-out></system-out>',
184+
'<system-err></system-err>',
185+
'</testsuite>,',
186+
];
187+
const res = await runnable['_reloadFromXml'](testOutput.join(EOL), { isCancellationRequested: false });
188+
189+
const tests = [...res.tests].sort((a, b) => a.testNameAsId.localeCompare(b.testNameAsId));
190+
191+
assert.strictEqual(tests.length, 2);
192+
193+
assert.strictEqual(tests[0].testNameAsId, 'FirstTestGroup.FirstTest');
194+
assert.strictEqual(tests[0].label, 'FirstTest');
195+
assert.strictEqual(tests[0].file, pathlib.normalize('/mnt/c/Users/testcppcpputestcpputest1.cpp'));
196+
assert.strictEqual(tests[0].line, 14);
197+
assert.strictEqual(tests[0].skipped, false);
198+
assert.strictEqual(tests[0].getStaticEvent('1'), undefined);
199+
assert.strictEqual(tests[1].testNameAsId, 'FirstTestGroup.SecondTest');
200+
assert.strictEqual(tests[1].label, 'SecondTest');
201+
assert.strictEqual(tests[1].file, pathlib.normalize('/mnt/c/Users/testcppcpputestcpputest1.cpp'));
202+
assert.strictEqual(tests[1].line, 19);
203+
assert.strictEqual(tests[1].skipped, false);
204+
assert.strictEqual(tests[1].getStaticEvent('1'), undefined);
205+
206+
assert.strictEqual(root.children.length, 1);
207+
const suite1 = root.children[0];
208+
assert.strictEqual(suite1.label, 'name');
209+
if (suite1.type === 'suite') {
210+
assert.strictEqual(suite1.children.length, 2);
211+
} else {
212+
assert.strictEqual(suite1.type, 'suite');
213+
}
214+
});
215+
});
168216
});

0 commit comments

Comments
 (0)