Skip to content

Commit 2ea4368

Browse files
Connor ClarkDevtools-frontend LUCI CQ
authored andcommitted
Resolve snapshot file path in karma server
This CL fixes snapshot testings when run from a Chromium checkout. Instead of resolving the URL to the snapshot file from the unit test, use a new endpoint on the karma server to get the snapshot from path relative to the source root. The previous way (using import.meta.url) did not work in Chromium checkouts because for some reason the snapshot files were not being copied to the gen folder. Also removed the "copy to gen folder" BUILD step, as it is no longer needed. Fixed: 444157747 Change-Id: I1aad455dc54c239f2a81616f84768cf00ee36b0c Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6936941 Reviewed-by: Paul Irish <[email protected]> Commit-Queue: Connor Clark <[email protected]>
1 parent a8acb4a commit 2ea4368

File tree

6 files changed

+36
-35
lines changed

6 files changed

+36
-35
lines changed

front_end/models/ai_assistance/BUILD.gn

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,3 @@ ts_library("unittests") {
105105
"../trace:bundle",
106106
]
107107
}
108-
109-
copy_to_gen("snapshots") {
110-
sources = [
111-
"AiHistoryStorage.snapshot.txt",
112-
"data_formatters/PerformanceInsightFormatter.snapshot.txt",
113-
"data_formatters/PerformanceTraceFormatter.snapshot.txt",
114-
]
115-
}

front_end/panels/ai_assistance/BUILD.gn

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,3 @@ ts_library("unittests") {
104104
"../../ui/legacy:bundle",
105105
]
106106
}
107-
108-
copy_to_gen("snapshots") {
109-
sources = [ "AiAssistancePanel.snapshot.txt" ]
110-
}

front_end/testing/BUILD.gn

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,3 @@ ts_library("testing") {
8989
"../ui/visual_logging:testing",
9090
]
9191
}
92-
93-
copy_to_gen("snapshots") {
94-
sources = [ "SnapshotTester.snapshot.txt" ]
95-
}

front_end/testing/README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,6 @@ after(async () => {
110110
});
111111
```
112112

113-
Then add the `.snapshot.txt` file to the appropriate BUILD.gn:
114-
115-
```
116-
copy_to_gen("snapshots") {
117-
sources = [ "myfile.snapshot.txt" ]
118-
}
119-
```
120-
121113
Then inside a test, call `snapshotTester.assert(this, output)`. For an example, see SnapshotTester.test.ts.
122114

123115
To update snapshots, run `npm run test -- --on-diff=update ...`

front_end/testing/SnapshotTester.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,30 @@ function assertSnapshotContent(actual: string, expected: string): void {
3131
export class SnapshotTester {
3232
static #updateMode: boolean|null = null;
3333

34-
#snapshotUrl: string;
34+
#snapshotPath: string;
3535
#expected = new Map<string, string>();
3636
#actual = new Map<string, string>();
3737
#anyFailures = false;
3838
#newTests = false;
3939

4040
constructor(meta: ImportMeta) {
41-
this.#snapshotUrl = meta.url.replace('.test.js', '.snapshot.txt').split('?')[0];
41+
// out/Default/gen/third_party/devtools-frontend/src/front_end/testing/SnapshotTester.test.js?8ee4f2b123e221040a4aa075a28d0e5b41d3d3ed
42+
// ->
43+
// front_end/testing/SnapshotTester.snapshot.txt
44+
this.#snapshotPath =
45+
meta.url.substring(meta.url.lastIndexOf('front_end')).replace('.test.js', '.snapshot.txt').split('?')[0];
4246
}
4347

4448
async load() {
4549
if (SnapshotTester.#updateMode === null) {
4650
SnapshotTester.#updateMode = await this.#checkIfUpdateMode();
4751
}
4852

49-
const url = new URL(this.#snapshotUrl, import.meta.url);
53+
const url = new URL('/snapshot', import.meta.url);
54+
url.searchParams.set('snapshotPath', this.#snapshotPath);
5055
const response = await fetch(url);
5156
if (response.status === 404) {
52-
console.warn(`Snapshot file not found: ${url.href}. Will create it for you.`);
57+
console.warn(`Snapshot file not found: ${this.#snapshotPath}. Will create it for you.`);
5358
return;
5459
}
5560
if (response.status !== 200) {
@@ -85,15 +90,18 @@ export class SnapshotTester {
8590
title}), must run \`npm run test -- --on-diff=update ...\` to accept it.`);
8691
}
8792

88-
if (!SnapshotTester.#updateMode && actual !== expected) {
93+
const isDifferent = actual !== expected;
94+
if (isDifferent) {
8995
this.#anyFailures = true;
90-
assertSnapshotContent(actual, expected);
96+
if (!SnapshotTester.#updateMode) {
97+
assertSnapshotContent(actual, expected);
98+
}
9199
}
92100
}
93101

94102
async #postUpdate(): Promise<void> {
95103
const url = new URL('/update-snapshot', import.meta.url);
96-
url.searchParams.set('snapshotUrl', this.#snapshotUrl);
104+
url.searchParams.set('snapshotPath', this.#snapshotPath);
97105
const content = this.#serializeSnapshotFileContent();
98106
const response = await fetch(url, {method: 'POST', body: content});
99107
if (response.status !== 200) {

test/unit/karma.conf.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,31 @@ function snapshotTesterFactory() {
282282
return;
283283
}
284284

285+
if (req.url.startsWith('/snapshot')) {
286+
const parsedUrl = url.parse(req.url, true);
287+
if (typeof parsedUrl.query.snapshotPath !== 'string') {
288+
throw new Error('invalid snapshotPath');
289+
}
290+
291+
const snapshotPath = path.join(SOURCE_ROOT, parsedUrl.query.snapshotPath);
292+
if (!fs.existsSync(snapshotPath)) {
293+
res.writeHead(404);
294+
return;
295+
}
296+
297+
const snapshot = fs.readFileSync(snapshotPath, 'utf-8');
298+
res.writeHead(200);
299+
res.end(snapshot);
300+
return;
301+
}
302+
285303
if (req.url.startsWith('/update-snapshot')) {
286304
const parsedUrl = url.parse(req.url, true);
287-
if (typeof parsedUrl.query.snapshotUrl !== 'string') {
288-
throw new Error('invalid snapshotUrl');
305+
if (typeof parsedUrl.query.snapshotPath !== 'string') {
306+
throw new Error('invalid snapshotPath');
289307
}
290308

291-
const snapshotUrl = parsedUrl.query.snapshotUrl;
292-
const snapshotPath = path.join(SOURCE_ROOT, url.parse(snapshotUrl, false).pathname?.split('gen')[1] ?? '');
309+
const snapshotPath = path.join(SOURCE_ROOT, parsedUrl.query.snapshotPath);
293310

294311
let body = '';
295312
req.on('data', (chunk: any) => {

0 commit comments

Comments
 (0)