Skip to content

Commit 39e5133

Browse files
bmeurerDevtools-frontend LUCI CQ
authored andcommitted
[test] Introduce Platform.DevToolsPath.urlString helper.
This adds a helper function to avoid sprinkling casts to `Platform.DevToolsPath.UrlString` all over our unit test suite, combined with an ESLint rule to enforce that we use this helper consistently. This leads to much more readable test code for unit tests that are concerned with APIs that take `UrlString`s, and the code is more consistent. Bug: 323797639 Change-Id: I6d797c0db9e5bd2fd019b83b9a7a269c7437ebdf Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6148578 Auto-Submit: Benedikt Meurer <[email protected]> Commit-Queue: Benedikt Meurer <[email protected]> Reviewed-by: Danil Somsikov <[email protected]>
1 parent b4e8dc7 commit 39e5133

File tree

118 files changed

+1513
-1209
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+1513
-1209
lines changed

eslint.config.mjs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,11 @@ export default [
268268
parserOptions: {
269269
allowAutomaticSingleRunInference: true,
270270
project: join(
271-
import.meta.dirname,
272-
'config',
273-
'typescript',
274-
'tsconfig.eslint.json',
275-
),
271+
import.meta.dirname,
272+
'config',
273+
'typescript',
274+
'tsconfig.eslint.json',
275+
),
276276
},
277277
},
278278

@@ -462,12 +462,12 @@ export default [
462462
{
463463
// Enforce that any import of models/trace/trace.js names the import Trace.
464464
modulePath: join(
465-
import.meta.dirname,
466-
'front_end',
467-
'models',
468-
'trace',
469-
'trace.js',
470-
),
465+
import.meta.dirname,
466+
'front_end',
467+
'models',
468+
'trace',
469+
'trace.js',
470+
),
471471
importName: 'Trace',
472472
},
473473
],
@@ -579,6 +579,7 @@ export default [
579579
'rulesdir/prefer-assert-instance-of': 'error',
580580
'rulesdir/prefer-assert-is-ok': 'error',
581581
'rulesdir/prefer-assert-length-of': 'error',
582+
'rulesdir/prefer-url-string': 'error',
582583
'rulesdir/trace-engine-test-timeouts': 'error',
583584
},
584585

front_end/core/common/ParsedURL.test.ts

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import type * as Platform from '../platform/platform.js';
5+
import * as Platform from '../platform/platform.js';
66

77
import * as Common from './common.js';
88

9+
const {urlString} = Platform.DevToolsPath;
910
const ParsedURL = Common.ParsedURL.ParsedURL;
1011

1112
function assertEqualUrlStringString(actual: Platform.DevToolsPath.UrlString|null, expected: string, message?: string) {
12-
assert.strictEqual(actual, expected as Platform.DevToolsPath.UrlString, message);
13+
assert.strictEqual(actual, urlString`${expected}`, message);
1314
}
1415

1516
describe('Parsed URL', () => {
@@ -146,13 +147,13 @@ describe('Parsed URL', () => {
146147
});
147148

148149
it('converts path that starts with "file://" to a platform path', () => {
149-
const pathTest = 'file://usr/lib' as Platform.DevToolsPath.UrlString;
150+
const pathTest = urlString`file://usr/lib`;
150151
const convertedPath = ParsedURL.urlToRawPathString(pathTest);
151152
assert.strictEqual(convertedPath, 'usr/lib', 'URL was not converted successfully');
152153
});
153154

154155
it('converts path that starts with "file:///" to a platform path on Windows', () => {
155-
const pathTest = 'file:///usr/lib' as Platform.DevToolsPath.UrlString;
156+
const pathTest = urlString`file:///usr/lib`;
156157
const convertedPath = ParsedURL.urlToRawPathString(pathTest, true);
157158
assert.strictEqual(convertedPath, 'usr\\lib', 'URL was not converted successfully');
158159
});
@@ -191,26 +192,26 @@ describe('Parsed URL', () => {
191192
});
192193

193194
it('extracts the path from a valid URL', () => {
194-
const urlTest = 'http://www.example.com/test/path' as Platform.DevToolsPath.UrlString;
195+
const urlTest = urlString`http://www.example.com/test/path`;
195196
const extractedPath = ParsedURL.extractPath(urlTest);
196197
assert.strictEqual(extractedPath, '/test/path', 'path extracted incorrectly');
197198
});
198199

199200
it('returns an empty string as a path if the URL is not valid', () => {
200-
const urlTest = 'www.example.com/test/path' as Platform.DevToolsPath.UrlString;
201+
const urlTest = urlString`www.example.com/test/path`;
201202
const extractedPath = ParsedURL.extractPath(urlTest);
202203
assert.strictEqual(extractedPath, '', 'did not return an empty path');
203204
});
204205

205206
it('extracts the origin from a valid URL', () => {
206-
const urlTest = 'http://www.example.com/test/path' as Platform.DevToolsPath.UrlString;
207+
const urlTest = urlString`http://www.example.com/test/path`;
207208
const extractedOrigin = ParsedURL.extractOrigin(urlTest);
208209
assert.strictEqual(extractedOrigin, 'http://www.example.com', 'origin extracted incorrectly');
209210
});
210211

211212
it('returns an empty string as a origin if the URL is not valid', () => {
212213
const urlTest = 'www.example.com/test/path';
213-
const extractedOrigin = ParsedURL.extractOrigin(urlTest as Platform.DevToolsPath.UrlString);
214+
const extractedOrigin = ParsedURL.extractOrigin(urlString`${urlTest}`);
214215
assert.strictEqual(extractedOrigin, '', 'did not return an empty path');
215216
});
216217

@@ -270,28 +271,28 @@ describe('Parsed URL', () => {
270271

271272
it('uses the completeURL function to return a data URL as it is', () => {
272273
const hrefTest = 'data:http://www.example.com';
273-
const baseUrlTest = 'www.example.com' as Platform.DevToolsPath.UrlString;
274+
const baseUrlTest = urlString`www.example.com`;
274275
const completeUrl = ParsedURL.completeURL(baseUrlTest, hrefTest);
275276
assertEqualUrlStringString(completeUrl, hrefTest, 'complete URL is not returned correctly');
276277
});
277278

278279
it('uses the completeURL function to return a blob URL as it is', () => {
279280
const hrefTest = 'blob:http://www.example.com';
280-
const baseUrlTest = 'www.example.com' as Platform.DevToolsPath.UrlString;
281+
const baseUrlTest = urlString`www.example.com`;
281282
const completeUrl = ParsedURL.completeURL(baseUrlTest, hrefTest);
282283
assertEqualUrlStringString(completeUrl, hrefTest, 'complete URL is not returned correctly');
283284
});
284285

285286
it('uses the completeURL function to return a javascript URL as it is', () => {
286287
const hrefTest = 'javascript:http://www.example.com';
287-
const baseUrlTest = 'www.example.com' as Platform.DevToolsPath.UrlString;
288+
const baseUrlTest = urlString`www.example.com`;
288289
const completeUrl = ParsedURL.completeURL(baseUrlTest, hrefTest);
289290
assertEqualUrlStringString(completeUrl, hrefTest, 'complete URL is not returned correctly');
290291
});
291292

292293
it('uses the completeURL function to return a mailto URL as it is', () => {
293294
const hrefTest = 'mailto:http://www.example.com';
294-
const baseUrlTest = 'www.example.com' as Platform.DevToolsPath.UrlString;
295+
const baseUrlTest = urlString`www.example.com`;
295296
const completeUrl = ParsedURL.completeURL(baseUrlTest, hrefTest);
296297
assertEqualUrlStringString(completeUrl, hrefTest, 'complete URL is not returned correctly');
297298
});
@@ -347,7 +348,7 @@ describe('Parsed URL', () => {
347348

348349
for (const {href, expected} of cases) {
349350
it(`can use completeURL to normalize "${href}"`, () => {
350-
const baseUrlTest = 'www.example.com' as Platform.DevToolsPath.UrlString;
351+
const baseUrlTest = urlString`www.example.com`;
351352
const completeUrl = ParsedURL.completeURL(baseUrlTest, href);
352353
assertEqualUrlStringString(completeUrl, expected, 'complete URL is not returned correctly');
353354
});
@@ -356,44 +357,44 @@ describe('Parsed URL', () => {
356357

357358
it('uses the completeURL function to return null for invalid href and invalid base URL', () => {
358359
const hrefTest = 'www.example.com';
359-
const baseUrlTest = 'www.example.com' as Platform.DevToolsPath.UrlString;
360+
const baseUrlTest = urlString`www.example.com`;
360361
const completeUrl = ParsedURL.completeURL(baseUrlTest, hrefTest);
361362
assert.isNull(completeUrl, 'complete URL is not returned correctly');
362363
});
363364

364365
it('uses the completeURL function to return the href if the base URL is a data URL', () => {
365366
const hrefTest = 'www.example.com';
366-
const baseUrlTest = 'data://www.example.com' as Platform.DevToolsPath.UrlString;
367+
const baseUrlTest = urlString`data://www.example.com`;
367368
const completeUrl = ParsedURL.completeURL(baseUrlTest, hrefTest);
368369
assertEqualUrlStringString(completeUrl, hrefTest, 'complete URL is not returned correctly');
369370
});
370371

371372
it('uses the completeURL function to return the href with scheme if the base URL was valid and the href scheme was dropped',
372373
() => {
373374
const hrefTest = '//www.example.com';
374-
const baseUrlTest = 'http://www.example.com/' as Platform.DevToolsPath.UrlString;
375+
const baseUrlTest = urlString`http://www.example.com/`;
375376
const completeUrl = ParsedURL.completeURL(baseUrlTest, hrefTest);
376377
assertEqualUrlStringString(completeUrl, 'http:' + hrefTest, 'complete URL is not returned correctly');
377378
});
378379

379380
it('uses the completeURL function to resolve an empty href to a base URL without fragment', () => {
380381
const hrefTest = '';
381-
const baseUrlTest = 'http://www.example.com/?testParam=t' as Platform.DevToolsPath.UrlString;
382+
const baseUrlTest = urlString`http://www.example.com/?testParam=t`;
382383
const completeUrl = ParsedURL.completeURL(baseUrlTest, hrefTest);
383384
assertEqualUrlStringString(completeUrl, baseUrlTest, 'complete URL is not returned correctly');
384385
});
385386

386387
it('uses the completeURL function to resolve a fragment href to a base URL with fragment', () => {
387388
const hrefTest = '#testFragment';
388-
const baseUrlTest = 'http://www.example.com/?testParam=t' as Platform.DevToolsPath.UrlString;
389+
const baseUrlTest = urlString`http://www.example.com/?testParam=t`;
389390
const completeUrl = ParsedURL.completeURL(baseUrlTest, hrefTest);
390391
assertEqualUrlStringString(completeUrl, baseUrlTest + hrefTest, 'complete URL is not returned correctly');
391392
});
392393

393394
it('uses the completeURL function to resolve a parameters href to a base URL with the parameters from the href while the base URL has parameters',
394395
() => {
395396
const hrefTest = '?hrefParams=t';
396-
const baseUrlTest = 'http://www.example.com/?testParam=t' as Platform.DevToolsPath.UrlString;
397+
const baseUrlTest = urlString`http://www.example.com/?testParam=t`;
397398
const completeUrl = ParsedURL.completeURL(baseUrlTest, hrefTest);
398399
assertEqualUrlStringString(
399400
completeUrl, 'http://www.example.com/' + hrefTest, 'complete URL is not returned correctly');
@@ -402,7 +403,7 @@ describe('Parsed URL', () => {
402403
it('uses the completeURL function to resolve a parameters href to a base URL with the parameters from the href while the base URL does not have parameters',
403404
() => {
404405
const hrefTest = '?hrefParams=t';
405-
const baseUrlTest = 'http://www.example.com/' as Platform.DevToolsPath.UrlString;
406+
const baseUrlTest = urlString`http://www.example.com/`;
406407
const completeUrl = ParsedURL.completeURL(baseUrlTest, hrefTest);
407408
assertEqualUrlStringString(completeUrl, baseUrlTest + hrefTest, 'complete URL is not returned correctly');
408409
});
@@ -590,18 +591,15 @@ describe('Parsed URL', () => {
590591

591592
it('returns the correct results for all ported web_tests unit tests', () => {
592593
assertEqualUrlStringString(
593-
ParsedURL.completeURL(
594-
'http://example.com/script.js' as Platform.DevToolsPath.UrlString, 'http://example.com/map.json'),
594+
ParsedURL.completeURL(urlString`http://example.com/script.js`, 'http://example.com/map.json'),
595595
'http://example.com/map.json');
596596
assertEqualUrlStringString(
597-
ParsedURL.completeURL('http://example.com/script.js' as Platform.DevToolsPath.UrlString, '/map.json'),
598-
'http://example.com/map.json');
597+
ParsedURL.completeURL(urlString`http://example.com/script.js`, '/map.json'), 'http://example.com/map.json');
599598
assertEqualUrlStringString(
600-
ParsedURL.completeURL(
601-
'http://example.com/scripts/script.js' as Platform.DevToolsPath.UrlString, '../maps/map.json'),
599+
ParsedURL.completeURL(urlString`http://example.com/scripts/script.js`, '../maps/map.json'),
602600
'http://example.com/maps/map.json');
603601

604-
const baseURL = 'http://a/b/c/d;p?q' as Platform.DevToolsPath.UrlString;
602+
const baseURL = urlString`http://a/b/c/d;p?q`;
605603

606604
assertEqualUrlStringString(ParsedURL.completeURL(baseURL, 'http://h'),
607605
'http://h/'); // modified from RFC3986
@@ -626,13 +624,11 @@ describe('Parsed URL', () => {
626624
assertEqualUrlStringString(ParsedURL.completeURL(baseURL, 'g#s/../x'), 'http://a/b/c/g#s/../x');
627625

628626
assertEqualUrlStringString(
629-
ParsedURL.completeURL('http://a/b/c/d;p?q' as Platform.DevToolsPath.UrlString, '//secure.com/moo'),
630-
'http://secure.com/moo');
627+
ParsedURL.completeURL(urlString`http://a/b/c/d;p?q`, '//secure.com/moo'), 'http://secure.com/moo');
631628
assertEqualUrlStringString(
632-
ParsedURL.completeURL('http://a/b/c/d;p?q' as Platform.DevToolsPath.UrlString, 'cat.jpeg'),
633-
'http://a/b/c/cat.jpeg');
629+
ParsedURL.completeURL(urlString`http://a/b/c/d;p?q`, 'cat.jpeg'), 'http://a/b/c/cat.jpeg');
634630
assertEqualUrlStringString(
635-
ParsedURL.completeURL('http://example.com/path.css?query#fragment' as Platform.DevToolsPath.UrlString, ''),
631+
ParsedURL.completeURL(urlString`http://example.com/path.css?query#fragment`, ''),
636632
'http://example.com/path.css?query');
637633
});
638634

@@ -687,20 +683,20 @@ describe('Parsed URL', () => {
687683
});
688684

689685
it('converts relative platform path and base URL to URL', () => {
690-
const baseUrl = 'http://localhost:8080/my%20folder/old%20path' as Platform.DevToolsPath.UrlString;
686+
const baseUrl = urlString`http://localhost:8080/my%20folder/old%20path`;
691687
const relativePath = 'new spaced%20name' as Platform.DevToolsPath.RawPathString;
692688
const convertedUrl = ParsedURL.relativePathToUrlString(relativePath, baseUrl);
693689
assert.strictEqual(convertedUrl, 'http://localhost:8080/my%20folder/new%20spaced%2520name');
694690
});
695691

696692
it('converts URL to a platform path that includes drive letter and spaces on Windows', () => {
697-
const urlTest = 'file:///C:/Program%20Files/Google' as Platform.DevToolsPath.UrlString;
693+
const urlTest = urlString`file:///C:/Program%20Files/Google`;
698694
const convertedUrl = ParsedURL.urlToRawPathString(urlTest, true);
699695
assert.strictEqual(convertedUrl, 'C:\\Program Files\\Google', 'URL was not converted successfully');
700696
});
701697

702698
it('converts URL to a platform path that includes spaces and percents', () => {
703-
const urlTest = 'file:///home/user/with%20space/with%2520escape' as Platform.DevToolsPath.UrlString;
699+
const urlTest = urlString`file:///home/user/with%20space/with%2520escape`;
704700
const convertedUrl = ParsedURL.urlToRawPathString(urlTest, false);
705701
assert.strictEqual(convertedUrl, '/home/user/with space/with%20escape', 'URL was not converted successfully');
706702
});
@@ -715,8 +711,8 @@ describe('Parsed URL', () => {
715711
it('converts platform path with variety of special characters to URL and back consistently with Chrome', () => {
716712
const platformPathTest =
717713
'/home/a:b@c(d, e+f)=&g;#h$' as Platform.DevToolsPath.RawPathString; // Valid filename on unix
718-
const urlTest = 'file:///home/a:b@c(d,%20e+f)=&g%3B%23h$' as
719-
Platform.DevToolsPath.UrlString; // URL in Chrome address bar if you open that file
714+
const urlTest =
715+
urlString`file:///home/a:b@c(d,%20e+f)=&g%3B%23h\$`; // URL in Chrome address bar if you open that file
720716
assert.strictEqual(ParsedURL.rawPathToUrlString(platformPathTest), urlTest);
721717
assert.strictEqual(ParsedURL.urlToRawPathString(urlTest), platformPathTest);
722718
});

0 commit comments

Comments
 (0)