Repository: pyscript/pyscript
File: pyscriptjs/tests/integration/test_01_basic.py (Python)

Code before:
    def test_py_script_src_attribute(self):
        self.writefile("foo.py", "print('hello from foo')")
        self.pyscript_run(
            """
            <py-script src="foo.py"></py-script>
            """
        )
        assert self.console.log.lines[0] == self.PY_COMPLETE
        assert self.console.log.lines[-1] == "hello from foo"

    def test_py_script_src_not_found(self):
        self.pyscript_run(
            """
            <py-script src="foo.py"></py-script>
            """
        )
        assert self.PY_COMPLETE in self.console.log.lines

        assert "Failed to load resource" in self.console.error.lines[0]
        with pytest.raises(JsErrors) as exc:
            self.check_js_errors()

        error_msg = str(exc.value)
        if self.is_fake_server:
            assert "Failed to fetch" in error_msg
        else:
            assert (
                "Fetching from URL foo.py failed with error 404 (File not found)"
                in error_msg
            )

        pyscript_tag = self.page.locator("py-script")
        assert pyscript_tag.inner_html() == ""

    def test_js_version(self):
        self.pyscript_run(
            """
            <py-script>
            </py-script>
            """
        )
        self.page.add_script_tag(content="console.log(pyscript.version)")

        assert (
            re.match(r"\d{4}\.\d{2}\.\d+(\.[a-zA-Z0-9]+)?", self.console.log.lines[-1])
            is not None
        )

    def test_python_version(self):
        self.pyscript_run(
            """

Review comment (suggestion):
Probably late but this error message and the one below it are extremely similar.
I guess we can make the error message common and then remove the `if/else` check here.

Code after:
    def test_py_script_src_attribute(self):
        self.writefile("foo.py", "print('hello from foo')")
        self.pyscript_run(
            """
            <py-script src="foo.py"></py-script>
            """
        )
        assert self.console.log.lines[0] == self.PY_COMPLETE
        assert self.console.log.lines[-1] == "hello from foo"

    def test_py_script_src_not_found(self):
        self.pyscript_run(
            """
            <py-script src="foo.py"></py-script>
            """
        )
        assert self.PY_COMPLETE in self.console.log.lines

        assert "Failed to load resource" in self.console.error.lines[0]
        with pytest.raises(JsErrors) as exc:
            self.check_js_errors()

        error_msg = str(exc.value)
        print(error_msg)
        if self.is_fake_server:
            assert (
                "Fetching from URL foo.py failed with error 404 (Not Found)."
                in error_msg
            )
        else:
            assert (
                "Fetching from URL foo.py failed with error 404 (File not found)"
                in error_msg
            )

        pyscript_tag = self.page.locator("py-script")
        assert pyscript_tag.inner_html() == ""

    def test_js_version(self):
        self.pyscript_run(
            """
            <py-script>
            </py-script>
            """
        )
        self.page.add_script_tag(content="console.log(pyscript.version)")

        assert (
            re.match(r"\d{4}\.\d{2}\.\d+(\.[a-zA-Z0-9]+)?", self.console.log.lines[-1])
            is not None
        )