8000 Enable worker tests by antocuni · Pull Request #1757 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

Enable worker tests #1757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
run black
  • Loading branch information
antocuni committed Sep 26, 2023
commit f554fdb3b79ec5e39d34e399339d66dae5a93d58
24 changes: 17 additions & 7 deletions pyscript.core/tests/integration/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,18 @@ def decorated(self, *args):

return decorator


def only_main(fn):
"""
Decorator to mark a test which make sense only in the main thread
"""

@functools.wraps(fn)
def decorated(self, *args):
if self.execution_thread == "worker":
return
return fn(self, *args)

return decorated


Expand Down Expand Up @@ -190,7 +193,7 @@ def init(self, request, tmpdir, logger, page, execution_thread):
# create a symlink to BUILD inside tmpdir
tmpdir.join("build").mksymlinkto(BUILD)
self.tmpdir.chdir()
self.tmpdir.join('favicon.ico').write("")
self.tmpdir.join("favicon.ico").write("")
self.logger = logger
self.execution_thread = execution_thread
self.dev_server = None
Expand Down Expand Up @@ -387,7 +390,13 @@ def goto(self, path):
self.page.goto(url, timeout=0)

def wait_for_console(
self, text, *, match_substring=False, repeat=None, timeout=None, check_js_errors=True
self,
text,
*,
match_substring=False,
repeat=None,
timeout=None,
check_js_errors=True,
):
"""
Wait until the given message appear in the console. If the message was
Expand Down Expand Up @@ -461,8 +470,10 @@ def wait_for_pyscript(self, *, timeout=None, check_js_errors=True):
If check_js_errors is True (the default), it also checks that no JS
errors were raised during the waiting.
"""
scripts = (self.page.locator('script[type=py]').all() +
self.page.locator('py-script').all())
scripts = (
self.page.locator("script[type=py]").all()
+ self.page.locator("py-script").all()
)
n_scripts = len(scripts)

# this is printed by core.js:onAfterRun
Expand All @@ -479,14 +490,13 @@ def wait_for_pyscript(self, *, timeout=None, check_js_errors=True):
# events aren't being triggered in the tests.
self.page.wait_for_timeout(100)


SCRIPT_TAG_REGEX = re.compile('(<script type="py"|<py-script)')

def _pyscript_format(self, snippet, *, execution_thread, extra_head=""):
if execution_thread == 'worker':
if execution_thread == "worker":
# turn <script type="py"> into <script type="py" worker>, and
# similarly for <py-script>
snippet = self.SCRIPT_TAG_REGEX.sub(r'\1 worker', snippet)
snippet = self.SCRIPT_TAG_REGEX.sub(r"\1 worker", snippet)

doc = f"""
<html>
Expand Down
34 changes: 20 additions & 14 deletions pyscript.core/tests/integration/test_01_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ def test_script_py_hello(self):
)
if self.execution_thread == "main":
# in main, the order of execution is guaranteed
assert self.console.log.lines == ["1. hello from script py",
"2. hello from py-script"]
assert self.console.log.lines == [
"1. hello from script py",
"2. hello from py-script",
]
else:
# in workers, each tag is executed by its own worker, so they can
# come out of order
lines = sorted(self.console.log.lines)
assert lines == ["1. hello from script py",
"2. hello from py-script"]
assert lines == ["1. hello from script py", "2. hello from py-script"]

def test_execution_thread(self):
self.pyscript_run(
Expand All @@ -48,9 +49,7 @@ def test_execution_thread(self):

# TODO: if there's no py-script there are surely no plugins neither
# this test must be discussed or rewritten to make sense now
@pytest.mark.skip(
reason="NEXT: No banner and should also add a WARNING about CORS"
)
@pytest.mark.skip(reason="NEXT: No banner and should also add a WARNING about CORS")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it just CORS or all the worker related headers?

def test_no_cors_headers(self):
self.disable_cors_headers()
self.pyscript_run(
Expand Down Expand Up @@ -175,10 +174,12 @@ def test_escaping_of_angle_brackets(self):
)
# in workers the order of execution is not guaranteed, better to play safe
lines = sorted(self.console.log.lines[-4:])
assert lines == ["A true false",
"B <div></div>",
"C true false",
"D <div></div>"]
assert lines == [
"A true false",
"B <div></div>",
"C true false",
"D <div></div>",
]

def test_packages(self):
self.pyscript_run(
Expand Down Expand Up @@ -341,10 +342,15 @@ def test_getPySrc_returns_source_code(self):
)
pyscript_tag = self.page.locator("py-script")
assert pyscript_tag.inner_html() == ""
assert pyscript_tag.evaluate("node => node.srcCode") == 'print("hello from py-script")'
assert (
pyscript_tag.evaluate("node => node.srcCode")
== 'print("hello from py-script")'
)
script_py_tag = self.page.locator('script[type="py"]')
assert script_py_tag.evaluate("node => node.srcCode") == 'print("hello from script py")'

assert (
script_py_tag.evaluate("node => node.srcCode")
== 'print("hello from script py")'
)

@skip_worker("NEXT: py-click doesn't work inside workers")
def test_py_attribute_without_id(self):
Expand Down
8 changes: 4 additions & 4 deletions pyscript.core/tests/integration/test_02_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
filter_page_content,
wait_for_render,
skip_worker,
only_main
only_main,
)

DISPLAY_OUTPUT_ID_PATTERN = r'script-py[id^="py-"]'
Expand Down Expand Up @@ -149,7 +149,7 @@ def test_multiple_display_calls_same_tag(self):
lines = tag.inner_text().splitlines()
assert lines == ["hello", "world"]

@only_main # with workers, two tags are two separate interpreters
@only_main # with workers, two tags are two separate interpreters
def test_implicit_target_from_a_different_tag(self):
self.pyscript_run(
"""
Expand Down Expand Up @@ -338,7 +338,7 @@ def test_display_should_escape(self):
)
out = self.page.locator("script-py > div")
assert out.inner_html() == html.escape("<p>hello world</p>")
assert out.inner_text() == '<p>hello world</p>'
assert out.inner_text() == "<p>hello world</p>"

def test_display_HTML(self):
self.pyscript_run(
Expand Down Expand Up @@ -367,7 +367,7 @@ def test_image_display(self):
display(plt)
</script>
""",
timeout=30*1000,
timeout=30 * 1000,
)
wait_for_render(self.page, "*", "<img src=['\"]data:image")
test = self.page.wait_for_selector("img")
Expand Down
12 changes: 4 additions & 8 deletions pyscript.core/tests/integration/test_py_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .support import PyScriptTest, with_execution_thread


# Disable the main/worker dual testing, for two reasons:
#
# 1. the <py-config> logic happens before we start the worker, so there is
Expand Down Expand Up @@ -47,7 +48,6 @@ def test_py_config_inline_scriptpy(self):
)
assert self.console.log.lines[-1] == "config name: foobar"


@pytest.mark.skip("NEXT: works with <py-script> not with <script>")
def test_py_config_external(self):
pyconfig_toml = """
Expand All @@ -67,7 +67,6 @@ def test_py_config_external(self):
)
assert self.console.log.lines[-1] == "config name: app with external config"


@pytest.mark.skip("NEXT: We need to restore the banner.")
def test_invalid_json_config(self):
# we need wait_for_pyscript=False because we bail out very soon,
Expand All @@ -81,11 +80,8 @@ def test_invalid_json_config(self):
wait_for_pyscript=False,
)
banner = self.page.wait_for_selector(".py-error")
#assert "Unexpected end of JSON input" in self.console.error.text
expected = (
"(PY1000): Invalid JSON\n"
"Unexpected end of JSON input"
)
# assert "Unexpected end of JSON input" in self.console.error.text
expected = "(PY1000): Invalid JSON\n" "Unexpected end of JSON input"
assert banner.inner_text() == expected

def test_invalid_toml_config(self):
Expand All @@ -100,7 +96,7 @@ def test_invalid_toml_config(self):
wait_for_pyscript=False,
)
banner = self.page.wait_for_selector(".py-error")
#assert "Expected DoubleQuote" in self.console.error.text
# assert "Expected DoubleQuote" in self.console.error.text
expected = (
"(PY1000): Invalid TOML\n"
"Expected DoubleQuote, Whitespace, or [a-z], [A-Z], "
Expand Down
1 change: 1 addition & 0 deletions pyscript.core/tests/integration/test_script_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .support import PyScriptTest, with_execution_thread


# these tests don't need to run in 'main' and 'worker' modes: the workers are
# already tested explicitly by some of them (see e.g.
# test_script_type_py_worker_attribute)
Expand Down
4 changes: 1 addition & 3 deletions pyscript.core/tests/integration/test_splashscreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

from .support import PyScriptTest, skip_worker

pytest.skip(
reason="NEXT: Should we remove the splashscreen?", allow_module_level=True
)
pytest.skip(reason="NEXT: Should we remove the splashscreen?", allow_module_level=True)


class TestSplashscreen(PyScriptTest):
Expand Down
1 change: 0 additions & 1 deletion pyscript.core/tests/integration/test_when.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class TestEventHandler(PyScriptTest):

def test_when_decorator_with_event(self):
"""When the decorated function takes a single parameter,
it should be passed the event object
Expand Down
0