chore: relative paths in telereporter (#21887)

This commit is contained in:
Yury Semikhatsky 2023-03-22 13:59:57 -07:00 committed by GitHub
parent 65da1d2835
commit a2a2b78cc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 17 deletions

View file

@ -104,11 +104,14 @@ export type JsonTestStepEnd = {
export class TeleReporterReceiver { export class TeleReporterReceiver {
private _rootSuite: TeleSuite; private _rootSuite: TeleSuite;
private _pathSeparator: string;
private _reporter: Reporter; private _reporter: Reporter;
private _tests = new Map<string, TeleTestCase>(); private _tests = new Map<string, TeleTestCase>();
private _rootDir!: string;
constructor(reporter: Reporter) { constructor(pathSeparator: string, reporter: Reporter) {
this._rootSuite = new TeleSuite('', 'root'); this._rootSuite = new TeleSuite('', 'root');
this._pathSeparator = pathSeparator;
this._reporter = reporter; this._reporter = reporter;
} }
@ -149,6 +152,7 @@ export class TeleReporterReceiver {
} }
private _onBegin(config: JsonConfig, projects: JsonProject[]) { private _onBegin(config: JsonConfig, projects: JsonProject[]) {
this._rootDir = config.rootDir;
const removeMissing = config.listOnly; const removeMissing = config.listOnly;
for (const project of projects) { for (const project of projects) {
let projectSuite = this._rootSuite.suites.find(suite => suite.project()!.id === project.id); let projectSuite = this._rootSuite.suites.find(suite => suite.project()!.id === project.id);
@ -191,7 +195,7 @@ export class TeleReporterReceiver {
titlePath: () => [], titlePath: () => [],
title: payload.title, title: payload.title,
category: payload.category, category: payload.category,
location: payload.location, location: this._absoluteLocation(payload.location),
startTime: new Date(payload.startTime), startTime: new Date(payload.startTime),
duration: 0, duration: 0,
steps: [], steps: [],
@ -245,17 +249,17 @@ export class TeleReporterReceiver {
id: project.id, id: project.id,
metadata: project.metadata, metadata: project.metadata,
name: project.name, name: project.name,
outputDir: project.outputDir, outputDir: this._absolutePath(project.outputDir),
repeatEach: project.repeatEach, repeatEach: project.repeatEach,
retries: project.retries, retries: project.retries,
testDir: project.testDir, testDir: this._absolutePath(project.testDir),
testIgnore: parseRegexPatterns(project.testIgnore), testIgnore: parseRegexPatterns(project.testIgnore),
testMatch: parseRegexPatterns(project.testMatch), testMatch: parseRegexPatterns(project.testMatch),
timeout: project.timeout, timeout: project.timeout,
grep: parseRegexPatterns(project.grep) as RegExp[], grep: parseRegexPatterns(project.grep) as RegExp[],
grepInvert: parseRegexPatterns(project.grepInvert) as RegExp[], grepInvert: parseRegexPatterns(project.grepInvert) as RegExp[],
dependencies: project.dependencies, dependencies: project.dependencies,
snapshotDir: project.snapshotDir, snapshotDir: this._absolutePath(project.snapshotDir),
use: {}, use: {},
}; };
} }
@ -268,7 +272,7 @@ export class TeleReporterReceiver {
targetSuite.parent = parent; targetSuite.parent = parent;
parent.suites.push(targetSuite); parent.suites.push(targetSuite);
} }
targetSuite.location = jsonSuite.location; targetSuite.location = this._absoluteLocation(jsonSuite.location);
targetSuite._fileId = jsonSuite.fileId; targetSuite._fileId = jsonSuite.fileId;
targetSuite._parallelMode = jsonSuite.parallelMode; targetSuite._parallelMode = jsonSuite.parallelMode;
this._mergeSuitesInto(jsonSuite.suites, targetSuite, removeMissing); this._mergeSuitesInto(jsonSuite.suites, targetSuite, removeMissing);
@ -284,7 +288,7 @@ export class TeleReporterReceiver {
for (const jsonTest of jsonTests) { for (const jsonTest of jsonTests) {
let targetTest = parent.tests.find(s => s.title === jsonTest.title); let targetTest = parent.tests.find(s => s.title === jsonTest.title);
if (!targetTest) { if (!targetTest) {
targetTest = new TeleTestCase(jsonTest.testId, jsonTest.title, jsonTest.location); targetTest = new TeleTestCase(jsonTest.testId, jsonTest.title, this._absoluteLocation(jsonTest.location));
targetTest.parent = parent; targetTest.parent = parent;
parent.tests.push(targetTest); parent.tests.push(targetTest);
this._tests.set(targetTest.id, targetTest); this._tests.set(targetTest.id, targetTest);
@ -301,11 +305,31 @@ export class TeleReporterReceiver {
test.id = payload.testId; test.id = payload.testId;
test.expectedStatus = payload.expectedStatus; test.expectedStatus = payload.expectedStatus;
test.timeout = payload.timeout; test.timeout = payload.timeout;
test.location = payload.location; test.location = this._absoluteLocation(payload.location);
test.annotations = payload.annotations; test.annotations = payload.annotations;
test.retries = payload.retries; test.retries = payload.retries;
return test; return test;
} }
private _absoluteLocation(location: Location): Location;
private _absoluteLocation(location?: Location): Location | undefined;
private _absoluteLocation(location: Location | undefined): Location | undefined {
if (!location)
return location;
return {
...location,
file: this._absolutePath(location.file),
};
}
private _absolutePath(relativePath: string): string;
private _absolutePath(relativePath?: string): string | undefined;
private _absolutePath(relativePath?: string): string | undefined {
if (!relativePath)
return relativePath;
return this._rootDir + this._pathSeparator + relativePath;
}
} }
export class TeleSuite implements SuitePrivate { export class TeleSuite implements SuitePrivate {

View file

@ -14,22 +14,25 @@
* limitations under the License. * limitations under the License.
*/ */
import type { FullConfig, FullResult, Reporter, TestError, TestResult, TestStep } from '../../types/testReporter'; import type { FullConfig, FullResult, Reporter, TestError, TestResult, TestStep, Location } from '../../types/testReporter';
import type { Suite, TestCase } from '../common/test'; import type { Suite, TestCase } from '../common/test';
import type { JsonConfig, JsonProject, JsonSuite, JsonTestCase, JsonTestResultEnd, JsonTestResultStart, JsonTestStepEnd, JsonTestStepStart } from '../isomorphic/teleReceiver'; import type { JsonConfig, JsonProject, JsonSuite, JsonTestCase, JsonTestResultEnd, JsonTestResultStart, JsonTestStepEnd, JsonTestStepStart } from '../isomorphic/teleReceiver';
import type { SuitePrivate } from '../../types/reporterPrivate'; import type { SuitePrivate } from '../../types/reporterPrivate';
import type { FullConfigInternal, FullProjectInternal } from '../common/types'; import type { FullConfigInternal, FullProjectInternal } from '../common/types';
import { createGuid } from 'playwright-core/lib/utils'; import { createGuid } from 'playwright-core/lib/utils';
import { serializeRegexPatterns } from '../isomorphic/teleReceiver'; import { serializeRegexPatterns } from '../isomorphic/teleReceiver';
import path from 'path';
export class TeleReporterEmitter implements Reporter { export class TeleReporterEmitter implements Reporter {
private _messageSink: (message: any) => void; private _messageSink: (message: any) => void;
private _rootDir!: string;
constructor(messageSink: (message: any) => void) { constructor(messageSink: (message: any) => void) {
this._messageSink = messageSink; this._messageSink = messageSink;
} }
onBegin(config: FullConfig, suite: Suite) { onBegin(config: FullConfig, suite: Suite) {
this._rootDir = config.rootDir;
const projects: any[] = []; const projects: any[] = [];
for (const projectSuite of suite.suites) { for (const projectSuite of suite.suites) {
const report = this._serializeProject(projectSuite); const report = this._serializeProject(projectSuite);
@ -113,7 +116,7 @@ export class TeleReporterEmitter implements Reporter {
private _serializeConfig(config: FullConfig): JsonConfig { private _serializeConfig(config: FullConfig): JsonConfig {
return { return {
rootDir: config.rootDir, rootDir: config.rootDir,
configFile: config.configFile, configFile: this._relativePath(config.configFile),
listOnly: (config as FullConfigInternal)._internal.listOnly, listOnly: (config as FullConfigInternal)._internal.listOnly,
}; };
} }
@ -124,10 +127,10 @@ export class TeleReporterEmitter implements Reporter {
id: (project as FullProjectInternal)._internal.id, id: (project as FullProjectInternal)._internal.id,
metadata: project.metadata, metadata: project.metadata,
name: project.name, name: project.name,
outputDir: project.outputDir, outputDir: this._relativePath(project.outputDir),
repeatEach: project.repeatEach, repeatEach: project.repeatEach,
retries: project.retries, retries: project.retries,
testDir: project.testDir, testDir: this._relativePath(project.testDir),
testIgnore: serializeRegexPatterns(project.testIgnore), testIgnore: serializeRegexPatterns(project.testIgnore),
testMatch: serializeRegexPatterns(project.testMatch), testMatch: serializeRegexPatterns(project.testMatch),
timeout: project.timeout, timeout: project.timeout,
@ -137,7 +140,7 @@ export class TeleReporterEmitter implements Reporter {
grep: serializeRegexPatterns(project.grep), grep: serializeRegexPatterns(project.grep),
grepInvert: serializeRegexPatterns(project.grepInvert || []), grepInvert: serializeRegexPatterns(project.grepInvert || []),
dependencies: project.dependencies, dependencies: project.dependencies,
snapshotDir: project.snapshotDir, snapshotDir: this._relativePath(project.snapshotDir),
}; };
return report; return report;
} }
@ -148,7 +151,7 @@ export class TeleReporterEmitter implements Reporter {
title: suite.title, title: suite.title,
fileId: (suite as SuitePrivate)._fileId, fileId: (suite as SuitePrivate)._fileId,
parallelMode: (suite as SuitePrivate)._parallelMode, parallelMode: (suite as SuitePrivate)._parallelMode,
location: suite.location, location: this._relativeLocation(suite.location),
suites: suite.suites.map(s => this._serializeSuite(s)), suites: suite.suites.map(s => this._serializeSuite(s)),
tests: suite.tests.map(t => this._serializeTest(t)), tests: suite.tests.map(t => this._serializeTest(t)),
}; };
@ -159,7 +162,7 @@ export class TeleReporterEmitter implements Reporter {
return { return {
testId: test.id, testId: test.id,
title: test.title, title: test.title,
location: test.location, location: this._relativeLocation(test.location),
expectedStatus: test.expectedStatus, expectedStatus: test.expectedStatus,
timeout: test.timeout, timeout: test.timeout,
annotations: test.annotations, annotations: test.annotations,
@ -193,7 +196,7 @@ export class TeleReporterEmitter implements Reporter {
title: step.title, title: step.title,
category: step.category, category: step.category,
startTime: step.startTime.toISOString(), startTime: step.startTime.toISOString(),
location: step.location, location: this._relativeLocation(step.location),
}; };
} }
@ -204,6 +207,25 @@ export class TeleReporterEmitter implements Reporter {
error: step.error, error: step.error,
}; };
} }
private _relativeLocation(location: Location): Location;
private _relativeLocation(location?: Location): Location | undefined;
private _relativeLocation(location: Location | undefined): Location | undefined {
if (!location)
return location;
return {
...location,
file: this._relativePath(location.file),
};
}
private _relativePath(absolutePath: string): string;
private _relativePath(absolutePath?: string): string | undefined;
private _relativePath(absolutePath?: string): string | undefined {
if (!absolutePath)
return absolutePath;
return path.relative(this._rootDir, absolutePath);
}
} }
const idSymbol = Symbol('id'); const idSymbol = Symbol('id');

View file

@ -549,7 +549,7 @@ const refreshRootSuite = (eraseResults: boolean): Promise<void> => {
skipped: 0, skipped: 0,
}; };
let config: FullConfig; let config: FullConfig;
receiver = new TeleReporterReceiver({ receiver = new TeleReporterReceiver(pathSeparator, {
onBegin: (c: FullConfig, suite: Suite) => { onBegin: (c: FullConfig, suite: Suite) => {
if (!rootSuite) if (!rootSuite)
rootSuite = suite; rootSuite = suite;