Skip to content

Commit

Permalink
fix: trim test path parts (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
devpow112 authored Nov 28, 2024
1 parent a27bf7e commit 8cd1acf
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 28 deletions.
3 changes: 2 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ const playwrightConfigs = addExtensions(
rules: {
'playwright/expect-expect': 'off',
'playwright/no-skipped-test': 'off',
'playwright/no-conditional-in-test': 'off'
'playwright/no-conditional-in-test': 'off',
'playwright/valid-title': 'off'
}
}
],
Expand Down
19 changes: 9 additions & 10 deletions src/helpers/report-builder.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { randomUUID } = require('node:crypto');
const { resolve } = require('node:path');
const { ReportConfiguration } = require('./report-configuration.cjs');
const { writeFileSync } = require('node:fs');
const { escapeSpecialCharacters } = require('./strings.cjs');

const defaultReportPath = './d2l-test-report.json';
const reportMemberPriority = [
Expand Down Expand Up @@ -58,7 +57,7 @@ class ReportBuilderBase {
this._data = {};
}

toJSON() {
get data() {
return this._data;
}

Expand Down Expand Up @@ -187,7 +186,7 @@ class ReportDetailBuilder extends ReportBuilderBase {
}

setName(name, options) {
this._setProperty('name', escapeSpecialCharacters(name), options);
this._setProperty('name', name, options);

return this;
}
Expand Down Expand Up @@ -350,8 +349,8 @@ class ReportBuilder extends ReportBuilderBase {
let countSkipped = 0;
let countFlaky = 0;

for (const [, detail] of this._data.details) {
const { status, retries } = detail.toJSON();
for (const [, { data: detail }] of this._data.details) {
const { status, retries } = detail;

if (status === 'passed') {
if (retries !== 0) {
Expand All @@ -366,7 +365,7 @@ class ReportBuilder extends ReportBuilderBase {
}

if (this._verbose) {
const { name, location, type, tool, experience } = detail.toJSON();
const { name, location, type, tool, experience } = detail;
const prefix = `Test '${name}' at '${location}' is missing`;

if (!type) {
Expand All @@ -393,12 +392,12 @@ class ReportBuilder extends ReportBuilderBase {
}

toJSON() {
const data = super.toJSON();
const { summary, details } = this.data;

return {
...data,
summary: data.summary.toJSON(),
details: [...data.details].map(([, detail]) => detail.toJSON())
...this.data,
summary: summary.data,
details: [...details.values()].map(({ data }) => data)
};
}

Expand Down
6 changes: 4 additions & 2 deletions src/helpers/strings.cjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const escapeSpecialCharacters = (str) => {
return str
.replace(/[\\]/g, '\\\\')
.replace(/[\0]/g, '\\0')
.replace(/[\b]/g, '\\b')
.replace(/[\f]/g, '\\f')
.replace(/[\n]/g, '\\n')
.replace(/[\r]/g, '\\r')
.replace(/[\t]/g, '\\t');
.replace(/[\t]/g, '\\t')
.replace(/[\v]/g, '\\v')
.replace(/[\f]/g, '\\f');
};

module.exports = { escapeSpecialCharacters };
6 changes: 5 additions & 1 deletion src/reporters/mocha.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { reporters: { Base, Spec }, Runner: { constants } } = require('mocha');
const { escapeSpecialCharacters } = require('../helpers/strings.cjs');
const { ReportBuilder } = require('../helpers/report-builder.cjs');

const { consoleLog, color } = Base;
Expand All @@ -19,7 +20,10 @@ class MochaLogger {
}

const makeDetailName = (test) => {
return test.titlePath().join(' > ');
return test
.titlePath()
.map(titlePart => escapeSpecialCharacters(titlePart).trim())
.join(' > ');
};

const makeDetailId = (file, name) => {
Expand Down
5 changes: 4 additions & 1 deletion src/reporters/playwright.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createRequire } from 'node:module';
import { colors } from 'playwright-core/lib/utilsBundle';
import { escapeSpecialCharacters } from '../helpers/strings.cjs';

const require = createRequire(import.meta.url);

Expand All @@ -18,7 +19,9 @@ const makeTestName = (test) => {
const [, projectName, , ...titles] = test.titlePath();
const titlePaths = projectName ? [`[${projectName}]`, ...titles] : titles;

return titlePaths.join(' > ');
return titlePaths
.map(titlePart => escapeSpecialCharacters(titlePart).trim())
.join(' > ');
};

const getBrowser = (project) => {
Expand Down
13 changes: 9 additions & 4 deletions src/reporters/web-test-runner.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chalk from 'chalk';
import { escapeSpecialCharacters } from '../helpers/strings.cjs';
import { ReportBuilder } from '../helpers/report-builder.cjs';
import { SESSION_STATUS } from '@web/test-runner-core';
import chalk from 'chalk';

const { yellow, red, cyan, bold } = chalk;

Expand All @@ -11,8 +12,12 @@ class WebTestRunnerLogger {
location(message, location) { this.info(`${message}: ${cyan(bold(location))}`); }
}

const sanitizeName = (name) => {
return escapeSpecialCharacters(name).trim();
};

const makeDetailName = (prefix, testName) => {
return `${prefix}${testName}`;
return `${prefix}${sanitizeName(testName)}`;
};

const makeDetailId = (sessionId, file, name) => {
Expand Down Expand Up @@ -93,7 +98,7 @@ export function reporter(options = {}) {
collectTests(session, prefix, suite.tests);

for (const childSuite of suite.suites) {
const newPrefix = `${prefix}${childSuite.name} > `;
const newPrefix = `${prefix}${sanitizeName(childSuite.name)} > `;

collectSuite(session, newPrefix, childSuite);
}
Expand All @@ -105,7 +110,7 @@ export function reporter(options = {}) {
for (const session of sessions) {
const { passed, group: { name: groupName }, testResults } = session;
const isGroupName = groupName && testConfig.groups?.some(({ name }) => groupName === name);
const prefix = isGroupName ? `[${groupName}] > ` : '';
const prefix = isGroupName ? `[${sanitizeName(groupName)}] > ` : '';

overallPassed &= passed;

Expand Down
2 changes: 1 addition & 1 deletion test/integration/data/tests/mocha/reporter-1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('reporter 1', () => {

it('failed', () => { throw new Error('fail'); });

it('special/characters "(\n\r\t\b\f)"', async() => { await delay(); });
it(' special/characters "(\n\r\t\b\f)" ', async() => { await delay(); });

afterEach(async() => { await delay(250); });

Expand Down
2 changes: 1 addition & 1 deletion test/integration/data/tests/mocha/reporter-2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('reporter 2', () => {

it('failed', () => { throw new Error('fail'); });

it('special/characters "(\n\r\t\b\f)"', async() => { await delay(); });
it(' special/characters "(\n\r\t\b\f)" ', async() => { await delay(); });

afterEach(async() => { await delay(250); });

Expand Down
2 changes: 1 addition & 1 deletion test/integration/data/tests/mocha/reporter-3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('reporter 3', () => {

it('failed', () => { throw new Error('fail'); });

it('special/characters "(\n\r\t\b\f)"', async() => { await delay(); });
it(' special/characters "(\n\r\t\b\f)" ', async() => { await delay(); });

afterEach(async() => { await delay(250); });

Expand Down
2 changes: 1 addition & 1 deletion test/integration/data/tests/playwright/reporter-1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test.describe('reporter 1', () => {
throw new Error('fail');
});

test('special/characters "(\n\r\t\b\f)"', async() => { await delay(); });
test(' special/characters "(\n\r\t\b\f)" ', async() => { await delay(); });

test.afterEach(async() => { await delay(250); });

Expand Down
2 changes: 1 addition & 1 deletion test/integration/data/tests/playwright/reporter-2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test.describe('reporter 2', () => {
throw new Error('fail');
});

test('special/characters "(\n\r\t\b\f)"', async() => { await delay(); });
test(' special/characters "(\n\r\t\b\f)" ', async() => { await delay(); });

test.afterEach(async() => { await delay(250); });

Expand Down
2 changes: 1 addition & 1 deletion test/integration/data/tests/playwright/reporter-3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test.describe('reporter 3', () => {
throw new Error('fail');
});

test('special/characters "(\n\r\t\b\f)"', async() => { await delay(); });
test(' special/characters "(\n\r\t\b\f)" ', async() => { await delay(); });

test.afterEach(async() => { await delay(250); });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('reporter 1', () => {

it('failed', () => { throw new Error('fail'); });

it('special/characters "(\n\r\t\b\f)"', async() => { await delay(); });
it(' special/characters "(\n\r\t\b\f)" ', async() => { await delay(); });

afterEach(async() => { await delay(250); });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('reporter 2', () => {

it('failed', () => { throw new Error('fail'); });

it('special/characters "(\n\r\t\b\f)"', async() => { await delay(); });
it(' special/characters "(\n\r\t\b\f)" ', async() => { await delay(); });

afterEach(async() => { await delay(250); });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('reporter 3', () => {

it('failed', () => { throw new Error('fail'); });

it('special/characters "(\n\r\t\b\f)"', async() => { await delay(); });
it(' special/characters "(\n\r\t\b\f)" ', async() => { await delay(); });

afterEach(async() => { await delay(250); });

Expand Down

0 comments on commit 8cd1acf

Please sign in to comment.