2024-09-11 17:28:29 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
|
*
|
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
*
|
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import type {
|
|
|
|
|
|
FullConfig, FullResult, Reporter, Suite, TestCase
|
|
|
|
|
|
} from '@playwright/test/reporter';
|
|
|
|
|
|
import fs from 'fs';
|
2024-09-11 22:33:25 +02:00
|
|
|
|
import { parseBidiExpectations as parseExpectations, projectExpectationPath } from './expectationUtil';
|
|
|
|
|
|
import type { TestExpectation } from './expectationUtil';
|
2024-09-11 17:28:29 +02:00
|
|
|
|
|
|
|
|
|
|
type ReporterOptions = {
|
|
|
|
|
|
rebase?: boolean;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ExpectationReporter implements Reporter {
|
|
|
|
|
|
private _suite: Suite;
|
|
|
|
|
|
private _options: ReporterOptions;
|
2024-09-11 22:33:25 +02:00
|
|
|
|
private _pendingUpdates: Promise<void>[] = [];
|
2024-09-11 17:28:29 +02:00
|
|
|
|
|
|
|
|
|
|
constructor(options: ReporterOptions) {
|
|
|
|
|
|
this._options = options;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onBegin(config: FullConfig, suite: Suite) {
|
|
|
|
|
|
this._suite = suite;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onEnd(result: FullResult) {
|
|
|
|
|
|
if (!this._options.rebase)
|
|
|
|
|
|
return;
|
|
|
|
|
|
for (const project of this._suite.suites)
|
2024-09-11 22:33:25 +02:00
|
|
|
|
this._pendingUpdates.push(this._updateProjectExpectations(project));
|
2024-09-11 17:28:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-11 22:33:25 +02:00
|
|
|
|
async onExit() {
|
|
|
|
|
|
await Promise.all(this._pendingUpdates);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async _updateProjectExpectations(project: Suite) {
|
2024-09-11 17:28:29 +02:00
|
|
|
|
const outputFile = projectExpectationPath(project.title);
|
2024-09-11 22:33:25 +02:00
|
|
|
|
const expectations = await parseExpectations(project.title);
|
|
|
|
|
|
for (const test of project.allTests()) {
|
|
|
|
|
|
const outcome = getOutcome(test);
|
2024-09-12 00:15:10 +02:00
|
|
|
|
// Strip root and project names.
|
|
|
|
|
|
const key = test.titlePath().slice(2).join(' › ');
|
2025-01-09 20:10:14 +01:00
|
|
|
|
if (outcome === 'timeout')
|
2024-09-11 22:33:25 +02:00
|
|
|
|
expectations.set(key, outcome);
|
2025-01-24 18:09:57 +01:00
|
|
|
|
else if (expectations.has(key) && test.outcome() !== 'skipped')
|
|
|
|
|
|
expectations.delete(key); // Remove tests that no longer timeout.
|
2024-09-11 22:33:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
const keys = Array.from(expectations.keys());
|
|
|
|
|
|
keys.sort();
|
|
|
|
|
|
const results = keys.map(key => `${key} [${expectations.get(key)}]`);
|
2024-09-11 17:28:29 +02:00
|
|
|
|
console.log('Writing new expectations to', outputFile);
|
2024-09-11 22:33:25 +02:00
|
|
|
|
await fs.promises.writeFile(outputFile, results.join('\n'));
|
2024-09-11 17:28:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
printsToStdio(): boolean {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-11 22:33:25 +02:00
|
|
|
|
function getOutcome(test: TestCase): TestExpectation {
|
2024-09-11 17:28:29 +02:00
|
|
|
|
if (test.results.length === 0)
|
|
|
|
|
|
return 'unknown';
|
|
|
|
|
|
if (test.results.every(r => r.status === 'timedOut'))
|
|
|
|
|
|
return 'timeout';
|
|
|
|
|
|
if (test.outcome() === 'expected')
|
|
|
|
|
|
return 'pass';
|
|
|
|
|
|
if (test.outcome() === 'unexpected')
|
|
|
|
|
|
return 'fail';
|
|
|
|
|
|
if (test.outcome() === 'flaky')
|
|
|
|
|
|
return 'flaky';
|
|
|
|
|
|
return 'unknown';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-20 18:17:09 +01:00
|
|
|
|
export default ExpectationReporter;
|