Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
Binary file added .coverage
Binary file not shown.
37 changes: 37 additions & 0 deletions tests/test_build_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_codestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
17 changes: 17 additions & 0 deletions tests/test_dunder_main.py
Original file line number Diff line number Diff line change
@@ -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]
37 changes: 37 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -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!"
Expand Down Expand Up @@ -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