Skip to content

Commit

Permalink
Merge pull request #36 from bryanbcook/bugfix/29-absolute-path-fix
Browse files Browse the repository at this point in the history
fix for absolute and relative paths
  • Loading branch information
ASaiAnudeep authored Nov 10, 2023
2 parents 45f2db4 + ec164d6 commit 8a8863e
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 30 deletions.
11 changes: 8 additions & 3 deletions src/helpers/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ const configured_parser = new XMLParser({
parseAttributeValue: true,
});

function getJsonFromXMLFile(filePath) {
function resolveFilePath(filePath) {
const cwd = process.cwd();
const xml = fs.readFileSync(path.join(cwd, filePath)).toString();
return path.isAbsolute(filePath) ? filePath : path.join(cwd, filePath);
}

function getJsonFromXMLFile(filePath) {
const xml = fs.readFileSync(resolveFilePath(filePath)).toString();
return configured_parser.parse(xml);
}

Expand All @@ -67,5 +71,6 @@ function getMatchingFilePaths(file_path) {

module.exports = {
getJsonFromXMLFile,
getMatchingFilePaths
getMatchingFilePaths,
resolveFilePath
}
5 changes: 2 additions & 3 deletions src/parsers/cucumber.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Parser for both Mocha Json report and Mochawesome json
*/
const path = require('path');
const { resolveFilePath } = require('../helpers/helper');

const TestResult = require('../models/TestResult');
const TestSuite = require('../models/TestSuite');
Expand Down Expand Up @@ -93,8 +93,7 @@ function preprocess(rawjson) {
}

function parse(file) {
const cwd = process.cwd();
const json = require(path.join(cwd, file));
const json = require(resolveFilePath(file));
return getTestResult(json);
}

Expand Down
5 changes: 2 additions & 3 deletions src/parsers/mocha.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Parser for both Mocha Json report and Mochawesome json
*/
const path = require('path');
const { resolveFilePath } = require('../helpers/helper');

const TestResult = require('../models/TestResult');
const TestSuite = require('../models/TestSuite');
Expand Down Expand Up @@ -136,8 +136,7 @@ function flattenTestSuite(suite) {


function parse(file) {
const cwd = process.cwd();
const json = require(path.join(cwd, file));
const json = require(resolveFilePath(file));
return getTestResult(json);
}

Expand Down
12 changes: 12 additions & 0 deletions tests/parser.cucumber.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { parse } = require('../src');
const assert = require('assert');
const path = require('path');

describe('Parser - Cucumber Json', () => {
const testDataPath = "tests/data/cucumber"
Expand Down Expand Up @@ -47,6 +48,7 @@ describe('Parser - Cucumber Json', () => {
]
});
});

it('empty suite report', () => {
const result = parse({ type: 'cucumber', files: [`${testDataPath}/empty-suite.json`] });
assert.deepEqual(result, {
Expand Down Expand Up @@ -149,4 +151,14 @@ describe('Parser - Cucumber Json', () => {
]
});
});

it('can support absolute and relative file paths', () => {
let relativePath = `${testDataPath}/multiple-suites-multiple-tests.json`;
let absolutePath = path.resolve(relativePath);
const result1 = parse({ type: 'cucumber', files: [absolutePath] });
assert.notEqual(null, result1);
const result2 = parse({ type: 'cucumber', files: [ relativePath]});
assert.notEqual(null, result2);
});
});

29 changes: 19 additions & 10 deletions tests/parser.junit.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { parse } = require('../src');
const assert = require('assert');
const path = require('path');

describe('Parser - JUnit', () => {

const testDataPath = "tests/data/junit"
it('single suite with single test', () => {
const result = parse({ type: 'junit', files: ['tests/data/junit/single-suite.xml'] });
const result = parse({ type: 'junit', files: [`${testDataPath}/single-suite.xml`] });
assert.deepEqual(result, {
id: '',
name: 'result name',
Expand Down Expand Up @@ -49,7 +50,7 @@ describe('Parser - JUnit', () => {
});

it('empty suite with single test', () => {
const result = parse({ type: 'junit', files: ['tests/data/junit/empty-suite.xml'] });
const result = parse({ type: 'junit', files: [`${testDataPath}/empty-suite.xml`] });
assert.deepEqual(result, {
id: '',
name: 'result name',
Expand Down Expand Up @@ -94,7 +95,7 @@ describe('Parser - JUnit', () => {
});

it('suite with skipped tests', () => {
const result = parse({ type: 'junit', files: ['tests/data/junit/skipped-tests.xml'] });
const result = parse({ type: 'junit', files: [`${testDataPath}/skipped-tests.xml`] });
assert.deepEqual(result, {
id: '',
name: 'result name',
Expand Down Expand Up @@ -139,7 +140,7 @@ describe('Parser - JUnit', () => {
});

it('multiple suites', () => {
const result = parse({ type: 'junit', files: ['tests/data/junit/multiple-suites.xml'] });
const result = parse({ type: 'junit', files: [`${testDataPath}/multiple-suites.xml`] });
assert.deepEqual(result, {
id: '',
name: 'result name',
Expand Down Expand Up @@ -211,7 +212,7 @@ 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'] });
const result = parse({ type: 'junit', files: [`${testDataPath}/single-suite.xml`, `${testDataPath}/single-suite.xml`] });
assert.deepEqual(result, {
id: '',
name: 'result name',
Expand Down Expand Up @@ -283,7 +284,7 @@ describe('Parser - JUnit', () => {
});

it('parse newman reporter', () => {
const result = parse({ type: 'junit', files: ['tests/data/junit/newman.xml'] });
const result = parse({ type: 'junit', files: [`${testDataPath}/newman.xml`] });
assert.deepEqual(result, {
id: '',
name: 'MyCollection',
Expand Down Expand Up @@ -328,7 +329,7 @@ describe('Parser - JUnit', () => {
});

it('parse newman with failures', () => {
const result = parse({ type: 'junit', files: ['tests/data/junit/newman-failures.xml'] });
const result = parse({ type: 'junit', files: [`${testDataPath}/newman-failures.xml`] });
assert.deepEqual(result, {
"id": "",
"name": "MainApi",
Expand Down Expand Up @@ -414,7 +415,7 @@ describe('Parser - JUnit', () => {
});

it('parse spekt/junit.testlogger', () => {
const result = parse({ type: 'junit', files: ['tests/data/junit/junit.testlogger.xml'] });
const result = parse({ type: 'junit', files: [`${testDataPath}/junit.testlogger.xml`] });
assert.deepEqual(result, {
"id": "",
"name": "",
Expand Down Expand Up @@ -501,7 +502,7 @@ describe('Parser - JUnit', () => {
});

it('empty suite with no tests', () => {
const result = parse({ type: 'junit', files: ['tests/data/junit/no-suites.xml'] });
const result = parse({ type: 'junit', files: [`${testDataPath}/no-suites.xml`] });
assert.deepEqual(result, {
id: '',
name: 'no suites',
Expand All @@ -517,4 +518,12 @@ describe('Parser - JUnit', () => {
});
});

it('can support absolute and relative file paths', () => {
let relativePath = `${testDataPath}/single-suite.xml`;
let absolutePath = path.resolve(relativePath);
const result1 = parse({ type: 'junit', files: [absolutePath] });
assert.notEqual(null, result1);
const result2 = parse({ type: 'junit', files: [relativePath]});
assert.notEqual(null, result2);
});
});
18 changes: 18 additions & 0 deletions tests/parser.mocha.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { parse } = require('../src');
const assert = require('assert');
const path = require('path');

describe('Parser - Mocha Json', () => {
const testDataPath = "tests/data/mocha/json"
Expand Down Expand Up @@ -206,6 +207,14 @@ describe('Parser - Mocha Json', () => {
]
});
});
it('can support absolute and relative file paths', () => {
let relativePath = `${testDataPath}/single-suite-single-test.json`;
let absolutePath = path.resolve(relativePath);
const result1 = parse({ type: 'mocha', files: [absolutePath] });
assert.notEqual(null, result1);
const result2 = parse({ type: 'mocha', files: [relativePath]});
assert.notEqual(null, result2);
});
});

describe('Parser - Mocha Awesmome Json', () => {
Expand Down Expand Up @@ -498,4 +507,13 @@ describe('Parser - Mocha Awesmome Json', () => {
]
});
});

it('can support absolute and relative file paths', () => {
let relativePath = `${testDataPath}/single-suite-single-test.json`;
let absolutePath = path.resolve(relativePath);
const result1 = parse({ type: 'mocha', files: [absolutePath] });
assert.notEqual(null, result1);
const result2 = parse({ type: 'mocha', files: [relativePath]});
assert.notEqual(null, result2);
});
});
26 changes: 18 additions & 8 deletions tests/parser.testng.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { parse } = require('../src');
const assert = require('assert');
const path = require('path');

describe('Parser - TestNG', () => {

const testDataPath = "tests/data/testng"
it('single suite with single test', () => {
const result = parse({ type: 'testng', files: ['tests/data/testng/single-suite.xml'] });
const result = parse({ type: 'testng', files: [`${testDataPath}/single-suite.xml`] });
assert.deepEqual(result, {
id: '',
name: 'Default suite',
Expand Down Expand Up @@ -91,7 +92,7 @@ describe('Parser - TestNG', () => {
});

it('single suite with multiple tests', () => {
const result = parse({ type: 'testng', files: ['tests/data/testng/single-suite-multiple-tests.xml'] });
const result = parse({ type: 'testng', files: [`${testDataPath}/single-suite-multiple-tests.xml`] });
assert.deepEqual(result, {
"id": "",
"name": "Regression Tests",
Expand Down Expand Up @@ -275,7 +276,7 @@ describe('Parser - TestNG', () => {
});

it('multiple suites with single test', () => {
const result = parse({ type: 'testng', files: ['tests/data/testng/multiple-suites-single-test.xml'] });
const result = parse({ type: 'testng', files: [`${testDataPath}/multiple-suites-single-test.xml`] });
assert.deepEqual(result, {
"id": "",
"name": "Default suite",
Expand Down Expand Up @@ -362,7 +363,7 @@ describe('Parser - TestNG', () => {
});

it('multiple suites with multiple tests', () => {
const result = parse({ type: 'testng', files: ['tests/data/testng/multiple-suites-multiple-tests.xml'] });
const result = parse({ type: 'testng', files: [`${testDataPath}/multiple-suites-multiple-tests.xml`] });
assert.deepEqual(result, {
"id": "",
"name": "Default suite 1",
Expand Down Expand Up @@ -518,7 +519,7 @@ describe('Parser - TestNG', () => {
});

it('multiple suites with retries', () => {
const result = parse({ type: 'testng', files: ['tests/data/testng/multiple-suites-retries.xml'] });
const result = parse({ type: 'testng', files: [`${testDataPath}/multiple-suites-retries.xml`] });
assert.deepEqual(result, {
"id": "",
"name": "Staging - UI Smoke Test Run",
Expand Down Expand Up @@ -674,7 +675,7 @@ describe('Parser - TestNG', () => {
});

it('results using glob', () => {
const result = parse({ type: 'testng', files: ['tests/data/testng/single-*.xml'] });
const result = parse({ type: 'testng', files: [`${testDataPath}/single-*.xml`] });
assert.deepEqual(result, {
"id": "",
"name": "Regression Tests",
Expand Down Expand Up @@ -927,7 +928,7 @@ describe('Parser - TestNG', () => {
});

it('single suite with no test', () => {
const result = parse({ type: 'testng', files: ['tests/data/testng/empty-suite.xml'] });
const result = parse({ type: 'testng', files: [`${testDataPath}/empty-suite.xml`] });
assert.deepEqual(result, {
id: '',
name: 'Empty Suite',
Expand All @@ -943,4 +944,13 @@ describe('Parser - TestNG', () => {
});
});

it('can support absolute and relative file paths', () => {
let relativePath = `${testDataPath}/single-suite.xml`;
let absolutePath = path.resolve(relativePath);
const result1 = parse({ type: 'testng', files: [absolutePath] });
assert.notEqual(null, result1);
const result2 = parse({ type: 'testng', files: [relativePath]});
assert.notEqual(null, result2);
});

});
17 changes: 14 additions & 3 deletions tests/parser.xunit.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const { parse } = require('../src');
const assert = require('assert');
const path = require('path');

describe('Parser - XUnit', () => {

const testDataPath = "tests/data/xunit";

it('single suite with single test', () => {
const result = parse({ type: 'xunit', files: ['tests/data/xunit/single-suite.xml'] });
const result = parse({ type: 'xunit', files: [`${testDataPath}/single-suite.xml`] });
assert.deepEqual(result, {
id: '',
name: 'single suite test',
Expand Down Expand Up @@ -49,7 +51,7 @@ describe('Parser - XUnit', () => {
});
});
it('suite with single skipped test', () => {
const result = parse({ type: 'xunit', files: ['tests/data/xunit/skipped-suite.xml'] });
const result = parse({ type: 'xunit', files: [`${testDataPath}/skipped-suite.xml`] });
assert.deepEqual(result, {
id: '',
name: 'Skipped test',
Expand Down Expand Up @@ -92,7 +94,7 @@ describe('Parser - XUnit', () => {
});
});
it('multiple suites', () => {
const result = parse({ type: 'xunit', files: ['tests/data/xunit/multiple-suites.xml'] });
const result = parse({ type: 'xunit', files: [`${testDataPath}/multiple-suites.xml`] });
const expectedObj = {
id: '',
name: 'Multiple suites',
Expand Down Expand Up @@ -245,4 +247,13 @@ describe('Parser - XUnit', () => {
}
assert.deepEqual(result, expectedObj );
});

it('can support absolute and relative file paths', () => {
let relativePath = `${testDataPath}/single-suite.xml`;
let absolutePath = path.resolve(relativePath);
const result1 = parse({ type: 'xunit', files: [absolutePath] });
assert.notEqual(null, result1);
const result2 = parse({ type: 'xunit', files: [relativePath]});
assert.notEqual(null, result2);
});
});

0 comments on commit 8a8863e

Please sign in to comment.