Skip to content

Commit

Permalink
Normalize file paths during XML generation (#23)
Browse files Browse the repository at this point in the history
* Normalize file paths during XML generation
  • Loading branch information
munderseth authored Jul 15, 2024
1 parent 010b961 commit d735239
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cypress-xml-reporter",
"version": "1.0.1",
"version": "1.0.2",
"description": "A JUnit XML reporter for Cypress that includes screenshots, videos, and logs.",
"main": "src/reporter.js",
"scripts": {
Expand Down
15 changes: 11 additions & 4 deletions src/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function createTestRecord(test, specRelativePath) {
let imageBasename = testFullName.replaceAll(UNSAFE_REGEX, '').substring(0, 242)+' (failed).png';
let imageFile = path.join(config.screenshotsFolder, specRelativePath, imageBasename);
if (fs.existsSync(imageFile)) {
record['system-out'] = '[[ATTACHMENT|'+imageFile+']]';
record['system-out'] = '[[ATTACHMENT|'+normalizePath(imageFile)+']]';
}
}
break;
Expand All @@ -139,6 +139,13 @@ function createTestRecord(test, specRelativePath) {
return record;
}

function normalizePath(pathString) {
if (path.sep !== '/') {
return pathString.replaceAll(path.sep, '/');
}
return pathString;
}

function CypressXML(runner, options) {
Mocha.reporters.Base.call(this, runner, options);

Expand Down Expand Up @@ -227,7 +234,7 @@ function CypressXML(runner, options) {
errors: 0,
time: (Date.now() - s.suite.timestamp) / 1000,
timestamp: new Date(s.suite.timestamp).toUTCString(),
file: s.suite.file
file: normalizePath(s.suite.file)
}
var testCases = [];
s.tests.forEach( function(t){
Expand All @@ -243,7 +250,7 @@ function CypressXML(runner, options) {
})
var logContent = '';
if (config.logsFolder) {
let relative = suiteStats.file.replace(config.logSpecRoot,'');
let relative = s.suite.file.replace(config.logSpecRoot,'');
relative = relative.substring(0, relative.lastIndexOf('.')) + config.logFileExt;
let logFile = path.join(config.logsFolder, relative);
if (fs.existsSync(logFile)) {
Expand All @@ -253,7 +260,7 @@ function CypressXML(runner, options) {
}
if (suiteStats.failures > 0) {
let videoFile = path.join(config.videosFolder, specRelativePath)+'.mp4';
logContent += '[[ATTACHMENT|' + videoFile +']]';
logContent += '[[ATTACHMENT|' + normalizePath(videoFile) +']]';
}
var suiteRecord = { $: suiteStats, testcase: testCases, 'system-out': logContent };
testSuites.push(suiteRecord);
Expand Down
14 changes: 7 additions & 7 deletions tests/test.desc-desc.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe(testName, () => {
expect(suites[0].$.tests).to.equal('0');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
});
describe('TEST1', () => {
Expand All @@ -51,11 +51,11 @@ describe(testName, () => {
expect(suites[1].$.tests).to.equal('3');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
it('System-out', () => {
var systemout = suites[1]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile.replaceAll(path.sep, '/')+']]');
});
describe('Testcases', ()=> {
var testcases;
Expand All @@ -73,7 +73,7 @@ describe(testName, () => {
});
it('"case3" Failure', () => {
var systemout = testcases[2]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile1+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile1.replaceAll(path.sep, '/')+']]');
})
});
});
Expand All @@ -85,11 +85,11 @@ describe(testName, () => {
expect(suites[2].$.tests).to.equal('3');
});
it('File Name', () => {
expect(suites[2].$.file).to.equal(testFile);
expect(suites[2].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
it('System-out', () => {
var systemout = suites[2]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile.replaceAll(path.sep, '/')+']]');
});
describe('Testcases', ()=> {
var testcases;
Expand All @@ -107,7 +107,7 @@ describe(testName, () => {
});
it('"case3" Failure', () => {
var systemout = testcases[2]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile2+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile2.replaceAll(path.sep, '/')+']]');
})
});
});
Expand Down
2 changes: 1 addition & 1 deletion tests/test.desc-fixture-hook-failure.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe(testName, () => {
expect(suites[0].$.tests).to.equal('0');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
});
});
8 changes: 4 additions & 4 deletions tests/test.desc-nest-desc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe(testName, () => {
expect(suites[0].$.tests).to.equal('0');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
});
describe('TEST1', () => {
Expand All @@ -66,11 +66,11 @@ describe(testName, () => {
expect(suites[1].$.tests).to.equal('6');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
it('System-out', () => {
var systemout = suites[1]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile.replaceAll(path.sep, '/')+']]');
});
describe('Testcases', ()=> {
var testcases;
Expand All @@ -97,7 +97,7 @@ describe(testName, () => {
});
it('"NEST -- case3" Failure', () => {
var systemout = testcases[5]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile.replaceAll(path.sep, '/')+']]');
})
});
});
Expand Down
12 changes: 6 additions & 6 deletions tests/test.desc-nest-nest-desc.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe(testName, () => {
expect(suites[0].$.tests).to.equal('0');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
});
describe('TEST1', () => {
Expand All @@ -68,11 +68,11 @@ describe(testName, () => {
expect(suites[1].$.tests).to.equal('9');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
it('System-out', () => {
var systemout = suites[1]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile.replaceAll(path.sep, '/')+']]');
});
describe('Testcases', ()=> {
var testcases;
Expand All @@ -90,7 +90,7 @@ describe(testName, () => {
});
it('"case3" Failure', () => {
var systemout = testcases[2]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile1+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile1.replaceAll(path.sep, '/')+']]');
});
it('"NEST1 -- case1" name', () => {
expect(testcases[3].$.name).to.equal('NEST1 -- case1');
Expand All @@ -103,7 +103,7 @@ describe(testName, () => {
});
it('"NEST1 -- case3" Failure', () => {
var systemout = testcases[5]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile2+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile2.replaceAll(path.sep, '/')+']]');
});
it('"NEST1 -- NEST2 -- case1" name', () => {
expect(testcases[6].$.name).to.equal('NEST1 -- NEST2 -- case1');
Expand All @@ -116,7 +116,7 @@ describe(testName, () => {
});
it('"NEST1 -- NEST2 -- case3" Failure', () => {
var systemout = testcases[8]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile3+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile3.replaceAll(path.sep, '/')+']]');
});

});
Expand Down
4 changes: 2 additions & 2 deletions tests/test.desc-nofailures.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe(testName, () => {
expect(suites[0].$.tests).to.equal('0');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
});
describe('TEST1', () => {
Expand All @@ -64,7 +64,7 @@ describe(testName, () => {
expect(suites[1].$.tests).to.equal('3');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
it('System-out', () => {
var systemout = suites[1]['system-out'][0];
Expand Down
4 changes: 2 additions & 2 deletions tests/test.desc-nofailures.log.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe(testName, () => {
expect(suites[0].$.tests).to.equal('0');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
});
describe('TEST1', () => {
Expand All @@ -66,7 +66,7 @@ describe(testName, () => {
expect(suites[1].$.tests).to.equal('3');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
it('System-out', () => {
var systemout = suites[1]['system-out'][0];
Expand Down
6 changes: 3 additions & 3 deletions tests/test.desc-pending.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe(testName, () => {
expect(suites[0].$.tests).to.equal('0');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
});
describe('TEST1', () => {
Expand All @@ -72,11 +72,11 @@ describe(testName, () => {
expect(suites[1].$.skipped).to.equal('3');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
it('System-out', () => {
var systemout = suites[1]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile.replaceAll(path.sep, '/')+']]');
});
describe('Testcases', ()=> {
var testcases;
Expand Down
8 changes: 4 additions & 4 deletions tests/test.desc-testname-chars.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe(testName, () => {
expect(suites[0].$.tests).to.equal('0');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
});
describe('"TEST SUITE" with Quotes and Slash', () => {
Expand All @@ -66,11 +66,11 @@ describe(testName, () => {
expect(suites[1].$.tests).to.equal('3');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
it('System-out', () => {
var systemout = suites[1]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile.replaceAll(path.sep, '/')+']]');
});
describe('Testcases', ()=> {
var testcases;
Expand All @@ -88,7 +88,7 @@ describe(testName, () => {
});
it('"case3" name with quotes and slash with failure', () => {
var systemout = testcases[2]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile.replaceAll(path.sep, '/')+']]');
})
});
});
Expand Down
8 changes: 4 additions & 4 deletions tests/test.desc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe(testName, () => {
expect(suites[0].$.tests).to.equal('0');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
});
describe('TEST1', () => {
Expand All @@ -66,11 +66,11 @@ describe(testName, () => {
expect(suites[1].$.tests).to.equal('3');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
it('System-out', () => {
var systemout = suites[1]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile.replaceAll(path.sep, '/')+']]');
});
describe('Testcases', ()=> {
var testcases;
Expand All @@ -88,7 +88,7 @@ describe(testName, () => {
});
it('"case3" Failure', () => {
var systemout = testcases[2]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile.replaceAll(path.sep, '/')+']]');
})
});
});
Expand Down
8 changes: 4 additions & 4 deletions tests/test.desc.log.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const screenshotFile = path.join('cypress', 'screenshots', testDataName, 'TEST1

// Remove "spaces" and "newlines" variants
const logContent = ":TEST1->case1TEST1->case2TEST1->case3cy:command(X):assertexpected**1**toequal**2**Actual:1Expected:2";
const systemOut = testFile+logContent+"[[ATTACHMENT|"+videoFile+"]]";
const systemOut = testFile+logContent+"[[ATTACHMENT|"+videoFile.replaceAll(path.sep, '/')+"]]";

/**
* Globals
Expand Down Expand Up @@ -59,7 +59,7 @@ describe(testName, () => {
expect(suites[0].$.tests).to.equal('0');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
});
describe('TEST1', () => {
Expand All @@ -70,7 +70,7 @@ describe(testName, () => {
expect(suites[1].$.tests).to.equal('3');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
it('System-out', () => {
var systemout = suites[1]['system-out'][0];
Expand Down Expand Up @@ -99,7 +99,7 @@ describe(testName, () => {
});
it('"case3" Failure System-out', () => {
var systemout = testcases[2]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile.replaceAll(path.sep, '/')+']]');
});
});
});
Expand Down
8 changes: 4 additions & 4 deletions tests/test.folder-desc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe(testName, () => {
expect(suites[0].$.tests).to.equal('0');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
});
describe('TEST1', () => {
Expand All @@ -66,11 +66,11 @@ describe(testName, () => {
expect(suites[1].$.tests).to.equal('3');
});
it('File Name', () => {
expect(suites[1].$.file).to.equal(testFile);
expect(suites[1].$.file).to.equal(testFile.replaceAll(path.sep, '/'));
});
it('System-out', () => {
var systemout = suites[1]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+videoFile.replaceAll(path.sep, '/')+']]');
});
describe('Testcases', ()=> {
var testcases;
Expand All @@ -88,7 +88,7 @@ describe(testName, () => {
});
it('"case3" Failure', () => {
var systemout = testcases[2]['system-out'][0];
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile+']]');
expect(systemout).to.equal('[[ATTACHMENT|'+screenshotFile.replaceAll(path.sep, '/')+']]');
})
});
});
Expand Down
Loading

0 comments on commit d735239

Please sign in to comment.