Skip to content

Commit

Permalink
Bug 1693766 [wpt PR 27699] - No dynamic import in service worker, a=t…
Browse files Browse the repository at this point in the history
…estonly

Automatic update from web-platform-tests
No dynamic import in service worker (#27699)

Part of whatwg/html#6395 and w3c/ServiceWorker#1356
--

wpt-commits: a02460e35145989c6fe2365f03d1b0006f381d9d
wpt-pr: 27699

UltraBlame original commit: 4f89a146b69c4b14730d2418cbac4447c6ac461a
  • Loading branch information
marco-c committed Mar 15, 2021
1 parent 9506c08 commit 052d387
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 54 deletions.
13 changes: 12 additions & 1 deletion testing/web-platform/tests/docs/writing-tests/testharness.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ It is possible to customize the set of scopes with a metadata comment, such as
// ==> would only run in the window and service worker scope
// META: global=dedicatedworker
// ==> would run in the default dedicated worker scope
// META: global=dedicatedworker-module
// ==> would run in the dedicated worker scope as a module
// META: global=worker
// ==> would run in the dedicated, shared, and service worker scopes
```
Expand All @@ -149,8 +151,13 @@ are:

* `window` (default): to be run at <code><var>x</var>.any.html</code>
* `dedicatedworker` (default): to be run at <code><var>x</var>.any.worker.html</code>
* `serviceworker`: to be run at <code><var>x</var>.any.serviceworker.html</code> (`.https` is implied)
* `dedicatedworker-module` to be run at <code><var>x</var>.any.worker-module.html</code>
* `serviceworker`: to be run at <code><var>x</var>.any.serviceworker.html</code> (`.https` is
implied)
* `serviceworker-module`: to be run at <code><var>x</var>.any.serviceworker-module.html</code>
(`.https` is implied)
* `sharedworker`: to be run at <code><var>x</var>.any.sharedworker.html</code>
* `sharedworker-module`: to be run at <code><var>x</var>.any.sharedworker-module.html</code>
* `jsshell`: to be run in a JavaScript shell, without access to the DOM
(currently only supported in SpiderMonkey, and skipped in wptrunner)
* `worker`: shorthand for the dedicated, shared, and service worker scopes
Expand Down Expand Up @@ -183,6 +190,10 @@ Use `// META: script=link/to/resource.js` at the beginning of the resource. For

can be used to include both the global and a local `utils.js` in a test.

In window environments, the script will be included using a classic `<script>` tag. In classic
worker environments, the script will be imported using `importScripts()`. In module worker
environments, the script will be imported using a static `import`.

### Specifying a timeout of long

Use `// META: timeout=long` at the beginning of the resource.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@




import './basic-module-2.js';

import './resources/no-dynamic-import.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


importScripts('resources/no-dynamic-import.js');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'hello again!';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'hello!';
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

const importUrlTests = [
["Module URL", "./basic-module.js"],

["Another module URL", "./basic-module-2.js"],
[
"Module data: URL",
"data:text/javascript;charset=utf-8," +
encodeURIComponent(`export default 'hello!';`),
],
];

for (const [name, url] of importUrlTests) {
promise_test(
(t) => promise_rejects_js(t, TypeError, import(url), "Import must reject"),
name
);
}
4 changes: 4 additions & 0 deletions testing/web-platform/tests/tools/manifest/sourcefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ def read_script_metadata(f, regexp):
_any_variants = {
"window": {"suffix": ".any.html"},
"serviceworker": {"force_https": True},
"serviceworker-module": {"force_https": True},
"sharedworker": {},
"sharedworker-module": {},
"dedicatedworker": {"suffix": ".any.worker.html"},
"dedicatedworker-module": {"suffix": ".any.worker-module.html"},
"worker": {"longhand": {"dedicatedworker", "sharedworker", "serviceworker"}},
"worker-module": {},
"jsshell": {"suffix": ".any.js"},
}

Expand Down
113 changes: 101 additions & 12 deletions testing/web-platform/tests/tools/serve/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,22 @@ class WorkersHandler(HtmlWrapperHandler):
"""


class WorkerModulesHandler(HtmlWrapperHandler):
global_type = "dedicatedworker-module"
path_replace = [(".any.worker-module.html", ".any.js", ".any.worker-module.js"),
(".worker.html", ".worker.js")]
wrapper = """<!doctype html>
<meta charset=utf-8>
%(meta)s
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script>
fetch_tests_from_worker(new Worker("%(path)s%(query)s", { type: "module" }));
</script>
"""


class WindowHandler(HtmlWrapperHandler):
path_replace = [(".window.html", ".window.js")]
wrapper = """<!doctype html>
Expand Down Expand Up @@ -275,6 +291,21 @@ class SharedWorkersHandler(HtmlWrapperHandler):
"""


class SharedWorkerModulesHandler(HtmlWrapperHandler):
global_type = "sharedworker-module"
path_replace = [(".any.sharedworker-module.html", ".any.js", ".any.worker-module.js")]
wrapper = """<!doctype html>
<meta charset=utf-8>
%(meta)s
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script>
fetch_tests_from_worker(new SharedWorker("%(path)s%(query)s", { type: "module" }));
</script>
"""


class ServiceWorkersHandler(HtmlWrapperHandler):
global_type = "serviceworker"
path_replace = [(".any.serviceworker.html", ".any.js", ".any.worker.js")]
Expand All @@ -296,8 +327,54 @@ class ServiceWorkersHandler(HtmlWrapperHandler):
"""


class AnyWorkerHandler(WrapperHandler):
class ServiceWorkerModulesHandler(HtmlWrapperHandler):
global_type = "serviceworker-module"
path_replace = [(".any.serviceworker-module.html",
".any.js", ".any.worker-module.js")]
wrapper = """<!doctype html>
<meta charset=utf-8>
%(meta)s
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script>
(async function() {
const scope = 'does/not/exist';
let reg = await navigator.serviceWorker.getRegistration(scope);
if (reg) await reg.unregister();
reg = await navigator.serviceWorker.register(
"%(path)s%(query)s",
{ scope, type: 'module' },
);
fetch_tests_from_worker(reg.installing);
})();
</script>
"""


class BaseWorkerHandler(WrapperHandler):
headers = [('Content-Type', 'text/javascript')]

def _meta_replacement(self, key, value):
return None

@abc.abstractmethod
def _create_script_import(self, attribute):


pass

def _script_replacement(self, key, value):
if key == "script":
attribute = value.replace("\\", "\\\\").replace('"', '\\"')
return self._create_script_import(attribute)
if key == "title":
value = value.replace("\\", "\\\\").replace('"', '\\"')
return 'self.META_TITLE = "%s";' % value
return None


class ClassicWorkerHandler(BaseWorkerHandler):
path_replace = [(".any.worker.js", ".any.js")]
wrapper = """%(meta)s
self.GLOBAL = {
Expand All @@ -310,17 +387,25 @@ class AnyWorkerHandler(WrapperHandler):
done();
"""

def _meta_replacement(self, key, value):
return None
def _create_script_import(self, attribute):
return 'importScripts("%s")' % attribute

def _script_replacement(self, key, value):
if key == "script":
attribute = value.replace("\\", "\\\\").replace('"', '\\"')
return 'importScripts("%s")' % attribute
if key == "title":
value = value.replace("\\", "\\\\").replace('"', '\\"')
return 'self.META_TITLE = "%s";' % value
return None

class ModuleWorkerHandler(BaseWorkerHandler):
path_replace = [(".any.worker-module.js", ".any.js")]
wrapper = """%(meta)s
self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
};
import "/resources/testharness.js";
%(script)s
import "%(path)s";
done();
"""

def _create_script_import(self, attribute):
return 'import "%s";' % attribute


rewrites = [("GET", "/resources/WebIDLParser.js", "/resources/webidl2/lib/webidl2.js")]
Expand Down Expand Up @@ -368,11 +453,15 @@ def add_mount_point(self, url_base, path):

routes = [
("GET", "*.worker.html", WorkersHandler),
("GET", "*.worker-module.html", WorkerModulesHandler),
("GET", "*.window.html", WindowHandler),
("GET", "*.any.html", AnyHtmlHandler),
("GET", "*.any.sharedworker.html", SharedWorkersHandler),
("GET", "*.any.sharedworker-module.html", SharedWorkerModulesHandler),
("GET", "*.any.serviceworker.html", ServiceWorkersHandler),
("GET", "*.any.worker.js", AnyWorkerHandler),
("GET", "*.any.serviceworker-module.html", ServiceWorkerModulesHandler),
("GET", "*.any.worker.js", ClassicWorkerHandler),
("GET", "*.any.worker-module.js", ModuleWorkerHandler),
("GET", "*.asis", handlers.AsIsHandler),
("GET", "/.well-known/origin-policy", handlers.PythonScriptHandler),
("*", "*.py", handlers.PythonScriptHandler),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,12 @@ def test_serviceworker_html(self):
'text/html', serve.ServiceWorkersHandler)


class TestAnyWorkerHandler(TestWrapperHandlerUsingServer):
class TestClassicWorkerHandler(TestWrapperHandlerUsingServer):
dummy_files = {'bar.any.js': b''}

def test_any_work_js(self):
self.run_wrapper_test('bar.any.worker.js', 'text/javascript',
serve.AnyWorkerHandler)
serve.ClassicWorkerHandler)


if __name__ == '__main__':
Expand Down

0 comments on commit 052d387

Please sign in to comment.