Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: read multiple files #15

Merged
merged 2 commits into from
Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "test-results-parser",
"version": "0.0.11",
"version": "0.1.0",
"description": "Parse test results from JUnit, TestNG, xUnit and many more",
"main": "src/index.js",
"types": "./src/index.d.ts",
Expand Down Expand Up @@ -29,7 +29,9 @@
},
"homepage": "https://github.com/test-results-reporter/parser#readme",
"dependencies": {
"fast-xml-parser": "^3.20.0"
"fast-xml-parser": "^3.20.0",
"globrex": "^0.1.2",
"totalist": "^3.0.0"
},
"devDependencies": {
"mocha": "^10.0.0"
Expand Down
24 changes: 23 additions & 1 deletion src/helpers/helper.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
const fs = require('fs');
const path = require('path');
const parser = require('fast-xml-parser');
const { totalist } = require('totalist/sync');
const globrex = require('globrex');

function getJsonFromXMLFile(filePath) {
const cwd = process.cwd();
const xml = fs.readFileSync(path.join(cwd, filePath)).toString();
return parser.parse(xml, { arrayMode: true, ignoreAttributes: false, parseAttributeValue: true });
}

/**
* @param {string} file_path
*/
function getMatchingFilePaths(file_path) {
if (file_path.includes('*')) {
const file_paths = [];
const result = globrex(file_path);
const dir_name = path.dirname(file_path.substring(0, file_path.indexOf('*') + 1));
totalist(dir_name, (name) => {
const current_file_path = `${dir_name}/${name}`;
if (result.regex.test(current_file_path)) {
file_paths.push(current_file_path);
}
});
return file_paths;
}
return [file_path];
}

module.exports = {
getJsonFromXMLFile
getJsonFromXMLFile,
getMatchingFilePaths
}
4 changes: 2 additions & 2 deletions src/parsers/cucumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ function preprocess(rawjson) {
return formattedResult;
}

function parse(options) {
function parse(file) {
const cwd = process.cwd();
const json = require(path.join(cwd, options.files[0]));
const json = require(path.join(cwd, file));
return getTestResult(json);
}

Expand Down
55 changes: 48 additions & 7 deletions src/parsers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,65 @@ const junit = require('./junit');
const xunit = require('./xunit');
const mocha = require('./mocha');
const cucumber = require('./cucumber');
const TestResult = require('../models/TestResult');
const { getMatchingFilePaths } = require('../helpers/helper');

function parse(options) {
switch (options.type) {
/**
* @param {import('../models/TestResult')[]} results
*/
function merge(results) {
const main_result = new TestResult();
for (let i = 0; i < results.length; i++) {
const current_result = results[i];
if (!main_result.name) {
main_result.name = current_result.name;
}
main_result.total = main_result.total + current_result.total;
main_result.passed = main_result.passed + current_result.passed;
main_result.failed = main_result.failed + current_result.failed;
main_result.errors = main_result.errors + current_result.errors;
main_result.skipped = main_result.skipped + current_result.skipped;
main_result.retried = main_result.retried + current_result.retried;
main_result.duration = main_result.duration + current_result.duration;
main_result.suites = main_result.suites.concat(...current_result.suites);
}
main_result.status = results.every(_result => _result.status === 'PASS') ? 'PASS' : 'FAIL';
return main_result;
}

function getParser(type) {
switch (type) {
case 'testng':
return testng.parse(options);
return testng;
case 'junit':
return junit.parse(options);
return junit;
case 'xunit':
return xunit.parse(options);
return xunit;
case 'mocha':
return mocha.parse(options);
return mocha;
case 'cucumber':
return cucumber.parse(options);
return cucumber;
default:
throw `UnSupported Result Type - ${options.type}`;
}
}

/**
* @param {import('../index').ParseOptions} options
*/
function parse(options) {
const parser = getParser(options.type);
const results = [];
ASaiAnudeep marked this conversation as resolved.
Show resolved Hide resolved
for (let i = 0; i < options.files.length; i++) {
const matched_files = getMatchingFilePaths(options.files[i]);
for (let j = 0; j < matched_files.length; j++) {
const file = matched_files[j];
results.push(parser.parse(file));
}
}
return merge(results);
}

module.exports = {
parse
}
4 changes: 2 additions & 2 deletions src/parsers/junit.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ function getTestResult(json) {
return result;
}

function parse(options) {
const json = getJsonFromXMLFile(options.files[0]);
function parse(file) {
const json = getJsonFromXMLFile(file);
return getTestResult(json);
}

Expand Down
4 changes: 2 additions & 2 deletions src/parsers/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ function formatMochaJsonReport(rawjson) {
return formattedJson;
}

function parse(options) {
function parse(file) {
const cwd = process.cwd();
const json = require(path.join(cwd, options.files[0]));
const json = require(path.join(cwd, file));
return getTestResult(json);
}

Expand Down
4 changes: 2 additions & 2 deletions src/parsers/testng.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ function getTestSuite(rawSuite) {
return suite;
}

function parse(options) {
function parse(file) {
// TODO - loop through files
const json = getJsonFromXMLFile(options.files[0]);
const json = getJsonFromXMLFile(file);
const result = new TestResult();
const results = json['testng-results'][0];
result.failed = results['@_failed'];
Expand Down
4 changes: 2 additions & 2 deletions src/parsers/xunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ function getTestResult(json) {
return result;
}

function parse(options) {
const json = getJsonFromXMLFile(options.files[0]);
function parse(file) {
const json = getJsonFromXMLFile(file);
return getTestResult(json);
}

Expand Down
72 changes: 72 additions & 0 deletions tests/parser.junit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,76 @@ describe('Parser - JUnit', () => {
});
});

it('multiple single suite files', () => {
const result = parse({ type: 'junit', files: ['tests/data/junit/single-suite.xml', 'tests/data/junit/single-suite.xml'] });
assert.deepEqual(result, {
id: '',
name: 'result name',
total: 2,
passed: 0,
failed: 2,
errors: 0,
skipped: 0,
retried: 0,
duration: 20000,
status: 'FAIL',
suites: [
{
id: '',
name: 'suite name',
total: 1,
passed: 0,
failed: 1,
errors: 0,
skipped: 0,
duration: 10000,
status: 'FAIL',
cases: [
{
duration: 10000,
errors: 0,
failed: 0,
failure: "PROGRAM.cbl:2 Use a program name that matches the source file name",
id: "",
name: "Use a program name that matches the source file name",
passed: 0,
skipped: 0,
stack_trace: "",
status: "FAIL",
steps: [],
total: 0
}
]
},
{
id: '',
name: 'suite name',
total: 1,
passed: 0,
failed: 1,
errors: 0,
skipped: 0,
duration: 10000,
status: 'FAIL',
cases: [
{
duration: 10000,
errors: 0,
failed: 0,
failure: "PROGRAM.cbl:2 Use a program name that matches the source file name",
id: "",
name: "Use a program name that matches the source file name",
passed: 0,
skipped: 0,
stack_trace: "",
status: "FAIL",
steps: [],
total: 0
}
]
}
]
});
});

});
Loading