Skip to content

Commit 0de3889

Browse files
committed
Compare against baseline
1 parent 1d7d474 commit 0de3889

2 files changed

Lines changed: 118 additions & 49 deletions

File tree

tests/diff.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* Copyright 2022 Google Inc. All Rights Reserved.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
import {readFile} from 'fs/promises';
15+
import {dirname, join} from 'path';
16+
import {fileURLToPath} from 'url';
17+
18+
interface TestMap<T> {
19+
[key: string]: SubtestMap<T> | undefined;
20+
}
21+
22+
interface SubtestMap<T> {
23+
[key: string]: T | undefined;
24+
}
25+
26+
interface BrowserResults {
27+
passed: string[];
28+
failed: string[];
29+
}
30+
31+
interface Diff<T> {
32+
before?: T;
33+
after?: T;
34+
}
35+
36+
function getTargetForDescriptor<T>(
37+
testMap: TestMap<T>,
38+
descriptor: TestDescriptor,
39+
defaultValue: () => T
40+
) {
41+
const testToSubtest = (testMap[descriptor.test] =
42+
testMap[descriptor.test] || {});
43+
const subtestToTarget = (testToSubtest[descriptor.subtest] =
44+
testToSubtest[descriptor.subtest] || defaultValue());
45+
46+
testMap[descriptor.test] = testToSubtest;
47+
testToSubtest[descriptor.subtest] = subtestToTarget;
48+
49+
return subtestToTarget;
50+
}
51+
52+
const newTestMap: TestMap<BrowserResults> = {};
53+
for (const browser of results) {
54+
for (const version of browser.versions) {
55+
if (version.data.type === DataType.Result) {
56+
const [passed, failed] = version.data.result;
57+
const name = `${browser.name} ${version.name}`;
58+
59+
for (const result of passed) {
60+
const target = getTargetForDescriptor(newTestMap, result, () => ({
61+
passed: [],
62+
failed: [],
63+
}));
64+
target.passed.push(name);
65+
}
66+
67+
for (const result of failed) {
68+
const target = getTargetForDescriptor(newTestMap, result, () => ({
69+
passed: [],
70+
failed: [],
71+
}));
72+
target.failed.push(name);
73+
}
74+
}
75+
}
76+
}
77+
78+
const baselineBuffer = await readFile(
79+
join(dirname(fileURLToPath(import.meta.url)), 'baseline.json')
80+
);
81+
const baselineTestMap: TestMap<BrowserResults> = JSON.parse(
82+
baselineBuffer.toString('utf-8')
83+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
84+
) as any;
85+
86+
const deltaTestMap: TestMap<Diff<BrowserResults>> = {};
87+
const allTestNames = new Set(
88+
[...Object.keys(baselineTestMap), ...Object.keys(newTestMap)].sort()
89+
);
90+
91+
for (const test in allTestNames) {
92+
const newSubtests = newTestMap[test] ?? {};
93+
const baselineSubtests = baselineTestMap[test] ?? {};
94+
const allSubtestNames = new Set(
95+
[...Object.keys(newSubtests), ...Object.keys(baselineSubtests)].sort()
96+
);
97+
98+
for (const subtest in allSubtestNames) {
99+
const target = getTargetForDescriptor<Diff<BrowserResults>>(
100+
deltaTestMap,
101+
{test, subtest},
102+
() => ({})
103+
);
104+
target.before = baselineSubtests[subtest];
105+
target.after = newSubtests[subtest];
106+
}
107+
}
108+
109+
console.info(JSON.stringify(deltaTestMap, null, 2));

tests/wpt.ts

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
*/
1313

1414
import {eachLimit, retry} from 'async';
15-
import {readFile} from 'fs/promises';
15+
import {readFile, writeFile} from 'fs/promises';
16+
import {dirname, join} from 'path';
1617
import {Agent} from 'http';
1718
import {Builder, By, until} from 'selenium-webdriver';
1819
import {Local} from 'browserstack-local';
20+
import {fileURLToPath} from 'url';
1921

2022
type Capabilities = Record<string, unknown>;
2123

@@ -441,54 +443,12 @@ async function main() {
441443
await stopLocalServer(server);
442444
}
443445

444-
interface TestToSubtestMap {
445-
[key: string]: SubtestMap;
446-
}
447-
448-
interface SubtestMap {
449-
[key: string]: {
450-
passed: string[];
451-
failed: string[];
452-
};
453-
}
454-
455-
function getTargetResultBucket(
456-
testToSubtestMap: TestToSubtestMap,
457-
descriptor: TestDescriptor
458-
) {
459-
const testToSubtest = (testToSubtestMap[descriptor.test] =
460-
testToSubtestMap[descriptor.test] || {});
461-
const subtestToBucket = (testToSubtest[descriptor.subtest] = testToSubtest[
462-
descriptor.subtest
463-
] || {passed: [], failed: []});
464-
465-
testToSubtestMap[descriptor.test] = testToSubtest;
466-
testToSubtest[descriptor.subtest] = subtestToBucket;
467-
468-
return subtestToBucket;
469-
}
470-
471-
const testToSubtests: TestToSubtestMap = {};
472-
for (const browser of results) {
473-
for (const version of browser.versions) {
474-
if (version.data.type === DataType.Result) {
475-
const [passed, failed] = version.data.result;
476-
const name = `${browser.name} ${version.name}`;
477-
478-
for (const result of passed) {
479-
const bucket = getTargetResultBucket(testToSubtests, result);
480-
bucket.passed.push(name);
481-
}
482-
483-
for (const result of failed) {
484-
const bucket = getTargetResultBucket(testToSubtests, result);
485-
bucket.failed.push(name);
486-
}
487-
}
488-
}
489-
}
490-
491-
console.info(JSON.stringify(testToSubtests), null, 2);
446+
const output = JSON.stringify(results, null, 2);
447+
await writeFile(
448+
join(dirname(fileURLToPath(import.meta.url)), 'results.json'),
449+
output
450+
);
451+
console.warn(output);
492452
}
493453

494454
try {

0 commit comments

Comments
 (0)