diff --git a/.coverage b/.coverage new file mode 100644 index 0000000..08b2420 Binary files /dev/null and b/.coverage differ diff --git a/tests/test_build_class.py b/tests/test_build_class.py index 2408147..7d7d9bb 100644 --- a/tests/test_build_class.py +++ b/tests/test_build_class.py @@ -3,6 +3,8 @@ from __future__ import annotations import platform +import runpy +import shutil as shutil_module import subprocess from pathlib import Path from unittest.mock import patch @@ -147,6 +149,41 @@ def test_build_failure_raises( with pytest.raises(RuntimeError, match="Build failed with exit code 1"): builder.build() + @patch("hello_python.build.subprocess.run") + @patch("hello_python.build.shutil.which", return_value="/usr/bin/poetry") + @patch("hello_python.build.platform.system", return_value="Windows") + def test_build_windows_exe_path( + self, + _mock_sys: object, + _mock_which: object, + mock_run: object, + tmp_path: Path, + ) -> None: + mock_run.return_value = subprocess.CompletedProcess(args=[], returncode=0) + + builder = BuildExecutable(root_dir=tmp_path) + result = builder.build() + + assert result.name == "hello-python-windows.exe" + assert result == builder.dist_path / "hello-python-windows.exe" + + +class TestBuildExecutableDunderMain: + """Test the ``if __name__ == '__main__'`` guard in build.py.""" + + def test_build_dunder_main(self, monkeypatch: pytest.MonkeyPatch) -> None: + """Running build.py as __main__ creates a BuildExecutable and calls build.""" + monkeypatch.setattr(shutil_module, "which", lambda x: "/usr/bin/poetry") + monkeypatch.setattr( + subprocess, + "run", + lambda *a, **kw: subprocess.CompletedProcess(args=[], returncode=0), + ) + + runpy.run_path( + str(ROOT_DIR / "src" / "hello_python" / "build.py"), run_name="__main__" + ) + @pytest.mark.slow class TestBuildExecutableIntegration: diff --git a/tests/test_codestyle.py b/tests/test_codestyle.py index b854406..566160b 100644 --- a/tests/test_codestyle.py +++ b/tests/test_codestyle.py @@ -2,7 +2,10 @@ from __future__ import annotations +import runpy +import shutil as shutil_module import subprocess +import sys from pathlib import Path from unittest.mock import call, patch @@ -138,3 +141,29 @@ def test_check_raises_on_failure( style = CodeStyle(root_dir=tmp_path) with pytest.raises(subprocess.CalledProcessError): style.check() + + +class TestCodeStyleDunderMain: + """Test the ``if __name__ == '__main__'`` guard in codestyle.py.""" + + def _run_script(self, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(shutil_module, "which", lambda x: "/usr/bin/poetry") + monkeypatch.setattr( + subprocess, + "run", + lambda *a, **kw: subprocess.CompletedProcess(args=[], returncode=0), + ) + runpy.run_path( + str(ROOT_DIR / "src" / "hello_python" / "codestyle.py"), + run_name="__main__", + ) + + def test_dunder_main_format_mode(self, monkeypatch: pytest.MonkeyPatch) -> None: + """No 'check' argument → format() branch is executed.""" + monkeypatch.setattr(sys, "argv", ["codestyle.py"]) + self._run_script(monkeypatch) # Should complete without error + + def test_dunder_main_check_mode(self, monkeypatch: pytest.MonkeyPatch) -> None: + """'check' argument → check() branch is executed.""" + monkeypatch.setattr(sys, "argv", ["codestyle.py", "check"]) + self._run_script(monkeypatch) # Should complete without error diff --git a/tests/test_dunder_main.py b/tests/test_dunder_main.py new file mode 100644 index 0000000..cfa72b4 --- /dev/null +++ b/tests/test_dunder_main.py @@ -0,0 +1,17 @@ +"""Tests for hello_python/__main__.py entry point.""" + +from __future__ import annotations + +import runpy + +import pytest + + +def test_dunder_main_calls_main(monkeypatch: pytest.MonkeyPatch) -> None: + """Cover ``__main__.py``: importing main and calling it when run as __main__.""" + called: list[bool] = [] + monkeypatch.setattr("hello_python.main.main", lambda: called.append(True)) + + runpy.run_module("hello_python", run_name="__main__", alter_sys=False) + + assert called == [True] diff --git a/tests/test_main.py b/tests/test_main.py index 784c6ee..62fd3b8 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,10 +1,16 @@ +import runpy +from pathlib import Path + import pytest +from PyQt5 import QtWidgets from PyQt5.QtWidgets import QApplication import hello_python.main as main_module from hello_python.main import get_hello_message from hello_python.window import HelloWindow +ROOT_DIR = Path(__file__).resolve().parent.parent + def test_get_hello_message() -> None: assert get_hello_message() == "Hello, world!" @@ -72,3 +78,34 @@ def show(self) -> None: assert captured["resize"] == (320, 120) assert captured["show_called"] is True assert captured["exec_called"] is True + + +def test_main_py_name_guard(monkeypatch: pytest.MonkeyPatch) -> None: + """Cover the ``if __name__ == '__main__': main()`` guard in main.py.""" + executed: list[str] = [] + + class FakeApp: + def __init__(self, args: list[str]) -> None: + pass + + def exec(self) -> None: + executed.append("exec") + + class FakeWindow: + def __init__(self) -> None: + pass + + def resize(self, width: int, height: int) -> None: + pass + + def show(self) -> None: + pass + + monkeypatch.setattr(QtWidgets, "QApplication", FakeApp) + monkeypatch.setattr("hello_python.window.HelloWindow", FakeWindow) + + runpy.run_path( + str(ROOT_DIR / "src" / "hello_python" / "main.py"), run_name="__main__" + ) + + assert "exec" in executed