mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-19 18:27:29 -05:00
Add Deprecation message when loading from latest (#1848)
* add tests to verify if we show an error banner when users load from latest * add deprecation manager to take care of showing a notification in case script src is being loaded from latest * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * make sure deprecation warning also register onWorker * restore tests for banner in worker * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add a wait selector when testing banner since worker seems to take too long to render in CI * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix test_deprecate_loading_scripts_from_latest: I think that the previous failure was because we actually TRIED to execute the js from latest/core.js and it conflicted with our local copy. But to trigger the warning is enough to have a script pointing to pyscript.net/latest, there is no need to execute it: modify it with type="ignore-me" and an URL which doesn't exist. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Antonio Cuni <anto.cuni@gmail.com>
This commit is contained in:
27
pyscript.core/src/plugins/deprecations-manager.js
Normal file
27
pyscript.core/src/plugins/deprecations-manager.js
Normal file
@@ -0,0 +1,27 @@
|
||||
// PyScript Derepcations Plugin
|
||||
import { hooks } from "../core.js";
|
||||
import { notify } from "./error.js";
|
||||
|
||||
// react lazily on PyScript bootstrap
|
||||
hooks.main.onReady.add(checkDeprecations);
|
||||
hooks.main.onWorker.add(checkDeprecations);
|
||||
|
||||
/**
|
||||
* Check that there are no scripts loading from pyscript.net/latest
|
||||
*/
|
||||
function checkDeprecations() {
|
||||
const scripts = document.querySelectorAll("script");
|
||||
for (const script of scripts) checkLoadingScriptsFromLatest(script.src);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if src being loaded from pyscript.net/latest and display a notification if true
|
||||
* * @param {string} src
|
||||
*/
|
||||
function checkLoadingScriptsFromLatest(src) {
|
||||
if (/\/pyscript\.net\/latest/.test(src)) {
|
||||
notify(
|
||||
"Loading scripts from latest is deprecated and will be removed soon. Please use a specific version instead.",
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -545,7 +545,9 @@ class PyScriptTest:
|
||||
- wait until pyscript has been fully loaded
|
||||
"""
|
||||
doc = self._pyscript_format(
|
||||
snippet, execution_thread=self.execution_thread, extra_head=extra_head
|
||||
snippet,
|
||||
execution_thread=self.execution_thread,
|
||||
extra_head=extra_head,
|
||||
)
|
||||
if not wait_for_pyscript and timeout is not None:
|
||||
raise ValueError("Cannot set a timeout if wait_for_pyscript=False")
|
||||
|
||||
@@ -1,13 +1,35 @@
|
||||
import pytest
|
||||
|
||||
from .support import PyScriptTest
|
||||
|
||||
pytest.skip(reason="NEXT: Restore the banner", allow_module_level=True)
|
||||
from .support import PyScriptTest, skip_worker
|
||||
|
||||
|
||||
class TestWarningsAndBanners(PyScriptTest):
|
||||
# Test the behavior of generated warning banners
|
||||
|
||||
def test_deprecate_loading_scripts_from_latest(self):
|
||||
# Use a script tag with an invalid output attribute to generate a warning, but only one
|
||||
self.pyscript_run(
|
||||
"""
|
||||
<script type="py">
|
||||
print("whatever..")
|
||||
</script>
|
||||
""",
|
||||
extra_head='<script type="ignore-me" src="https://pyscript.net/latest/any-path-triggers-the-warning-anyway.js"></script>',
|
||||
)
|
||||
|
||||
# wait for the banner to appear (we could have a page.locater call but for some reason
|
||||
# the worker takes to long to render on CI, since it's a test we can afford 2 calls)
|
||||
loc = self.page.wait_for_selector(".py-error")
|
||||
assert (
|
||||
loc.inner_text()
|
||||
== "Loading scripts from latest is deprecated and will be removed soon. Please use a specific version instead."
|
||||
)
|
||||
|
||||
# Only one banner should appear
|
||||
loc = self.page.locator(".py-error")
|
||||
assert loc.count() == 1
|
||||
|
||||
@pytest.mark.skip("NEXT: To check if behaviour is consistent with classic")
|
||||
def test_create_singular_warning(self):
|
||||
# Use a script tag with an invalid output attribute to generate a warning, but only one
|
||||
self.pyscript_run(
|
||||
|
||||
1
pyscript.core/types/plugins.d.ts
vendored
1
pyscript.core/types/plugins.d.ts
vendored
@@ -1,4 +1,5 @@
|
||||
declare const _default: {
|
||||
"deprecations-manager": () => Promise<typeof import("./plugins/deprecations-manager.js")>;
|
||||
error: () => Promise<typeof import("./plugins/error.js")>;
|
||||
"py-terminal": () => Promise<typeof import("./plugins/py-terminal.js")>;
|
||||
};
|
||||
|
||||
1
pyscript.core/types/plugins/deprecations-manager.d.ts
vendored
Normal file
1
pyscript.core/types/plugins/deprecations-manager.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export {};
|
||||
9
pyscript.core/types/stdlib/pyscript.d.ts
vendored
9
pyscript.core/types/stdlib/pyscript.d.ts
vendored
@@ -1,14 +1,13 @@
|
||||
declare const _default: {
|
||||
pyscript: {
|
||||
declare namespace _default {
|
||||
let pyscript: {
|
||||
"__init__.py": string;
|
||||
"display.py": string;
|
||||
"event_handling.py": string;
|
||||
"magic_js.py": string;
|
||||
"util.py": string;
|
||||
};
|
||||
"pyscript.py": string;
|
||||
pyweb: {
|
||||
let pyweb: {
|
||||
"pydom.py": string;
|
||||
};
|
||||
};
|
||||
}
|
||||
export default _default;
|
||||
|
||||
Reference in New Issue
Block a user