|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Basic pytest suite covering core components without launching the GUI loop |
| 3 | + |
| 4 | +import os |
| 5 | +import types |
| 6 | +import importlib |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(autouse=True) |
| 12 | +def _ensure_offscreen_qt(monkeypatch): |
| 13 | + # Make Qt use an offscreen backend to avoid display/server requirements during import |
| 14 | + monkeypatch.setenv("QT_QPA_PLATFORM", os.environ.get("QT_QPA_PLATFORM", "offscreen")) |
| 15 | + monkeypatch.setenv("PYTHONUTF8", "1") |
| 16 | + monkeypatch.setenv("PYTHONIOENCODING", "utf-8") |
| 17 | + |
| 18 | + |
| 19 | +def test_core_version_and_api_import(): |
| 20 | + # Import Core public package and ensure version exists and API snapshot can be materialized |
| 21 | + Core = importlib.import_module("Core") |
| 22 | + assert hasattr(Core, "__version__") |
| 23 | + assert isinstance(Core.__version__, str) and len(Core.__version__) > 0 |
| 24 | + |
| 25 | + # Access lazy API snapshot (triggers internal lazy loads where possible) |
| 26 | + api = getattr(Core, "api") |
| 27 | + assert isinstance(api, dict) |
| 28 | + assert "ui" in api |
| 29 | + |
| 30 | + |
| 31 | +def test_core_clear_lazy_caches_idempotent(): |
| 32 | + Core = importlib.import_module("Core") |
| 33 | + # Should not raise and be idempotent |
| 34 | + assert hasattr(Core, "_clear_lazy_caches") |
| 35 | + Core._clear_lazy_caches() |
| 36 | + Core._clear_lazy_caches() |
| 37 | + |
| 38 | + |
| 39 | +def test_engines_registry_register_and_create(): |
| 40 | + registry = importlib.import_module("Core.engines_loader.registry") |
| 41 | + base = importlib.import_module("Core.engines_loader.base") |
| 42 | + |
| 43 | + class DummyEngine(base.CompilerEngine): |
| 44 | + id = "dummy-test-engine" |
| 45 | + name = "DummyTestEngine" |
| 46 | + |
| 47 | + def build_command(self, gui, file: str): |
| 48 | + return ["python", "-c", "print('ok')"] |
| 49 | + |
| 50 | + # Ensure clean slate for this id |
| 51 | + registry.unregister(DummyEngine.id) |
| 52 | + |
| 53 | + # Register |
| 54 | + engine_cls = registry.register(DummyEngine) |
| 55 | + assert engine_cls is DummyEngine |
| 56 | + assert DummyEngine.id in registry.available_engines() |
| 57 | + |
| 58 | + # get_engine |
| 59 | + looked_up = registry.get_engine(DummyEngine.id) |
| 60 | + assert looked_up is DummyEngine |
| 61 | + |
| 62 | + # create returns instance |
| 63 | + inst = registry.create(DummyEngine.id) |
| 64 | + assert isinstance(inst, DummyEngine) |
| 65 | + |
| 66 | + # Unregister should remove it from listing and lookup |
| 67 | + registry.unregister(DummyEngine.id) |
| 68 | + assert DummyEngine.id not in registry.available_engines() |
| 69 | + assert registry.get_engine(DummyEngine.id) is None |
| 70 | + |
| 71 | + |
| 72 | +def test_base_engine_program_and_args_split(): |
| 73 | + base = importlib.import_module("Core.engines_loader.base") |
| 74 | + |
| 75 | + class E(base.CompilerEngine): |
| 76 | + id = "splitter" |
| 77 | + |
| 78 | + def build_command(self, gui, file: str): |
| 79 | + return ["python", "-m", "pip", "--version"] |
| 80 | + |
| 81 | + e = E() |
| 82 | + prog, args = e.program_and_args(gui=None, file="x.py") |
| 83 | + assert prog == "python" |
| 84 | + assert args[:2] == ["-m", "pip"] |
| 85 | + |
| 86 | + |
| 87 | +def test_deps_analyser_stdlib_detection(): |
| 88 | + analyser = importlib.import_module("Core.deps_analyser.analyser") |
| 89 | + is_std = getattr(analyser, "_is_stdlib_module") |
| 90 | + |
| 91 | + assert is_std("sys") is True |
| 92 | + assert is_std("json") is True |
| 93 | + # A very unlikely third-party module name should not be detected as stdlib |
| 94 | + assert is_std("this_module_name_should_not_exist_12345") is False |
| 95 | + |
| 96 | + |
| 97 | +def test_main_module_imports_and_has_main(): |
| 98 | + # Importing main should not execute the app because __main__ guard prevents that |
| 99 | + m = importlib.import_module("main") |
| 100 | + assert hasattr(m, "main") and isinstance(m.main, types.FunctionType) |
| 101 | + # Do not call main() here to avoid starting a Qt event loop in tests |
0 commit comments