mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-20 02:37:41 -05:00
* kill the PyScript class and the weird pyscript instance; from the user point of view its functionalities are still available as pyscript.*, but pyscript is not the module, not the instance of PyScript * simplify the code in _set_version_info, while I'm at it * start to implement DeprecatedGlobal * DeprecatedGlobal.__getattr__ * don't show the same warning twice * DeprecatedGlobal.__call__ * make it possible to specify a different warning message for every global * WIP: carefully use DeprecatedGlobal to show reasonable warning messages depending on which name you are accessing to. More names to follow * deprecate more names * deprecate private names * depreacte direct usage of console and document * deprecate the PyScript class * use a better error message * fix test_pyscript.py * introduce a __repr__ for DeprecatedGlobal * add an helper to ensure that we don't show any error or warning on the page * WIP: ensure that examples don't use depreacted features. Many tests are failing * don't deprecate Element * don't use the global micropip to install packages, else we trigger a warning * use a better error message for micropip * fix test_todo_pylist to avoid using deprecated globals * fix test_webgl_raycaster * fix tests * make HTML globally available * add MIME_RENDERERS and MIME_METHODS * fix the typing of Micropip, thanks to @FabioRosado
189 lines
5.5 KiB
Python
189 lines
5.5 KiB
Python
from .support import PyScriptTest
|
|
|
|
# Source code of a simple plugin that creates a Custom Element for testing purposes
|
|
CE_PLUGIN_CODE = """
|
|
from pyscript import Plugin
|
|
from js import console
|
|
|
|
plugin = Plugin('py-upper')
|
|
|
|
console.log("py_upper Plugin loaded")
|
|
|
|
@plugin.register_custom_element('py-up')
|
|
class Upper:
|
|
def __init__(self, element):
|
|
self.element = element
|
|
|
|
def connect(self):
|
|
console.log("Upper plugin connected")
|
|
return self.element.originalInnerHTML.upper()
|
|
"""
|
|
|
|
# Source of a plugin hooks into the PyScript App lifecycle events
|
|
HOOKS_PLUGIN_CODE = """
|
|
from pyscript import Plugin
|
|
from js import console
|
|
|
|
class TestLogger(Plugin):
|
|
def configure(self, config):
|
|
console.log('configure called')
|
|
|
|
def beforeLaunch(self, config):
|
|
console.log('beforeLaunch called')
|
|
|
|
def afterSetup(self, config):
|
|
console.log('afterSetup called')
|
|
|
|
def afterStartup(self, config):
|
|
console.log('afterStartup called')
|
|
|
|
def onUserError(self, config):
|
|
console.log('onUserError called')
|
|
|
|
|
|
plugin = TestLogger()
|
|
"""
|
|
|
|
# Source of a script that doesn't call define a `plugin` attribute
|
|
NO_PLUGIN_CODE = """
|
|
from pyscript import Plugin
|
|
from js import console
|
|
|
|
class TestLogger(Plugin):
|
|
pass
|
|
"""
|
|
|
|
# Source code of a simple plugin that creates a Custom Element for testing purposes
|
|
CODE_CE_PLUGIN_BAD_RETURNS = """
|
|
from pyscript import Plugin
|
|
from js import console
|
|
|
|
plugin = Plugin('py-broken')
|
|
|
|
@plugin.register_custom_element('py-up')
|
|
class Upper:
|
|
def __init__(self, element):
|
|
self.element = element
|
|
|
|
def connect(self):
|
|
# Just returning something... anything other than a string should be ignore
|
|
return Plugin
|
|
"""
|
|
HTML_TEMPLATE_WITH_TAG = """
|
|
<py-config>
|
|
plugins = [
|
|
"./{plugin_name}.py"
|
|
]
|
|
</py-config>
|
|
|
|
<{tagname}>
|
|
{html}
|
|
</{tagname}>
|
|
"""
|
|
HTML_TEMPLATE_NO_TAG = """
|
|
<py-config>
|
|
plugins = [
|
|
"./{plugin_name}.py"
|
|
]
|
|
</py-config>
|
|
"""
|
|
|
|
|
|
def prepare_test(
|
|
plugin_name, code, tagname="", html="", template=HTML_TEMPLATE_WITH_TAG
|
|
):
|
|
"""
|
|
Prepares the test by writing a new plugin file named `plugin_name`.py, with `code` as its
|
|
content and run `pyscript_run` on `template` formatted with the above inputs to create the
|
|
page HTML code.
|
|
|
|
For example:
|
|
|
|
>> @prepare_test('py-upper', CE_PLUGIN_CODE, tagname='py-up', html="Hello World")
|
|
>> def my_foo(...):
|
|
>> ...
|
|
|
|
will:
|
|
|
|
* write a new `py-upper.py` file to the FS
|
|
* the contents of `py-upper.py` is equal to CE_PLUGIN_CODE
|
|
* call self.pyscript_run with the following string:
|
|
'''
|
|
<py-config>
|
|
plugins = [
|
|
"./py-upper.py"
|
|
]
|
|
</py-config>
|
|
|
|
<py-up>
|
|
{html}
|
|
</py-up>
|
|
'''
|
|
* call `my_foo` just like a normal decorator would
|
|
|
|
"""
|
|
|
|
def dec(f):
|
|
def _inner(self, *args, **kws):
|
|
self.writefile(f"{plugin_name}.py", code)
|
|
page_html = template.format(
|
|
plugin_name=plugin_name, tagname=tagname, html=html
|
|
)
|
|
self.pyscript_run(page_html)
|
|
return f(self, *args, **kws)
|
|
|
|
return _inner
|
|
|
|
return dec
|
|
|
|
|
|
class TestPlugin(PyScriptTest):
|
|
@prepare_test("py-upper", CE_PLUGIN_CODE, tagname="py-up", html="Hello World")
|
|
def test_py_plugin_inline(self):
|
|
"""Test that a regular plugin that returns new HTML content from connected works"""
|
|
# GIVEN a plugin that returns the all caps version of the tag innerHTML and logs text
|
|
# during it's execution/hooks
|
|
|
|
# EXPECT the plugin logs to be present in the console logs
|
|
log_lines = self.console.log.lines
|
|
for log_line in ["py_upper Plugin loaded", "Upper plugin connected"]:
|
|
assert log_line in log_lines
|
|
|
|
# EXPECT the inner text of the Plugin CustomElement to be all caps
|
|
rendered_text = self.page.locator("py-up").inner_text()
|
|
assert rendered_text == "HELLO WORLD"
|
|
|
|
@prepare_test("hooks_logger", HOOKS_PLUGIN_CODE, template=HTML_TEMPLATE_NO_TAG)
|
|
def test_execution_hooks(self):
|
|
"""Test that a Plugin that hooks into the PyScript App events, gets called
|
|
for each one of them"""
|
|
# GIVEN a plugin that logs specific strings for each app execution event
|
|
hooks_available = ["afterSetup", "afterStartup"]
|
|
hooks_unavailable = ["configure", "beforeLaunch"]
|
|
|
|
# EXPECT it to log the correct logs for the events it intercepts
|
|
log_lines = self.console.log.lines
|
|
for method in hooks_available:
|
|
assert f"{method} called" in log_lines
|
|
|
|
# EXPECT it to NOT be called (hence not log anything) the events that happen
|
|
# before it's ready, hence is not called
|
|
for method in hooks_unavailable:
|
|
assert f"{method} called" not in log_lines
|
|
|
|
# TODO: It'd be actually better to check that the events get called in order
|
|
|
|
@prepare_test("no_plugin", NO_PLUGIN_CODE)
|
|
def test_no_plugin_attribute_error(self):
|
|
"""
|
|
Test a plugin that do not add the `plugin` attribute to its module
|
|
"""
|
|
# GIVEN a Plugin NO `plugin` attribute in it's module
|
|
error_msg = (
|
|
"[pyscript/main] Cannot find plugin on Python module no_plugin! Python plugins "
|
|
'modules must contain a "plugin" attribute. For more information check the '
|
|
"plugins documentation."
|
|
)
|
|
# EXPECT an error for the missing attribute
|
|
assert error_msg in self.console.error.lines
|