From c178ae35d00dfff81c0fe08e0d63f4280b67e3e4 Mon Sep 17 00:00:00 2001 From: Hiroshige Hayashizaki Date: Wed, 16 Oct 2019 22:36:30 -0700 Subject: [PATCH] [Import Maps] WPT: Check parsed import maps' scopes Before this CL, only `imports` parts of import maps were tested. Chromium side: - This CL makes ImportMap::ToString() to output the whole import map, rather than only the `imports` value. - This CL removes white spaces for pretty printing, to simplify the logic. WPT side: - This CL checks the whole import maps in `jest-test-helper.js`. - Reflect the upstream changes in https://github.com/WICG/import-maps/pull/190 to `builtin-support.tentative/imported/resources/parsing-scope-keys.js`. Test expectation changes: - This CL exposes new test failures in `parsing-scope-keys.tentative.html` on Chromium due to the spec-inconformant behavior of Chromium's URL parser. - Other test expectation changes are just converting `x` to `{imports: x, scopes: {}}`. Bug: 990561, 927181 Change-Id: Ic6a0171298636170d2fd96db2ac1a8d60e3ff345 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1863854 Reviewed-by: Kouhei Ueno Commit-Queue: Hiroshige Hayashizaki Cr-Commit-Position: refs/heads/master@{#706790} --- .../imported/resources/parsing-scope-keys.js | 19 ++++++++-------- import-maps/resources/jest-test-helper.js | 22 +++++++++++++++---- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/import-maps/builtin-support.tentative/imported/resources/parsing-scope-keys.js b/import-maps/builtin-support.tentative/imported/resources/parsing-scope-keys.js index cd1d9b34890971..4993f3a9a8bac4 100644 --- a/import-maps/builtin-support.tentative/imported/resources/parsing-scope-keys.js +++ b/import-maps/builtin-support.tentative/imported/resources/parsing-scope-keys.js @@ -62,14 +62,14 @@ describe('Relative URL scope keys', () => { }); describe('Absolute URL scope keys', () => { - it('should only accept absolute URL scope keys with fetch schemes', () => { + it('should accept all absolute URL scope keys, with or without fetch schemes', () => { expectScopes( [ 'about:good', 'blob:good', 'data:good', 'file:///good', - 'filesystem:good', + 'filesystem:http://example.com/good/', 'http://good/', 'https://good/', 'ftp://good/', @@ -84,17 +84,16 @@ describe('Absolute URL scope keys', () => { 'blob:good', 'data:good', 'file:///good', - 'filesystem:good', + 'filesystem:http://example.com/good/', 'http://good/', 'https://good/', - 'ftp://good/' + 'ftp://good/', + 'import:bad', + 'mailto:bad', + 'javascript:bad', + 'wss://ba/' ], - [ - 'Invalid scope "import:bad". Scope URLs must have a fetch scheme.', - 'Invalid scope "mailto:bad". Scope URLs must have a fetch scheme.', - 'Invalid scope "javascript:bad". Scope URLs must have a fetch scheme.', - 'Invalid scope "wss://ba/". Scope URLs must have a fetch scheme.' - ] + [] ); }); diff --git a/import-maps/resources/jest-test-helper.js b/import-maps/resources/jest-test-helper.js index 8d9236a84d81f8..e6f12cd9e55ce5 100644 --- a/import-maps/resources/jest-test-helper.js +++ b/import-maps/resources/jest-test-helper.js @@ -15,6 +15,22 @@ function require(name) { }, exports); } +// Sort keys and then stringify for comparison. +function stringifyImportMap(importMap) { + function getKeys(m) { + if (typeof m !== 'object') + return []; + + let keys = []; + for (const key in m) { + keys.push(key); + keys = keys.concat(getKeys(m[key])); + } + return keys; + } + return JSON.stringify(importMap, getKeys(importMap).sort()); +} + function expect(v) { return { toMatchURL: expected => assert_equals(v, expected), @@ -34,10 +50,8 @@ function expect(v) { const actualParsedImportMap = JSON.parse( internals.getParsedImportMap(v.contentDocument)); assert_equals( - JSON.stringify(actualParsedImportMap, - Object.keys(actualParsedImportMap).sort()), - JSON.stringify(expected.imports, - Object.keys(expected.imports).sort()) + stringifyImportMap(actualParsedImportMap), + stringifyImportMap(expected) ); } else { assert_object_equals(v, expected);