|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Tests for Core/Compiler/ark_config_loader.py behaviors |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import os |
| 7 | +from pathlib import Path |
| 8 | +import importlib |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def mod(): |
| 15 | + return importlib.import_module("Core.Compiler.ark_config_loader") |
| 16 | + |
| 17 | + |
| 18 | +def test_load_defaults_when_no_file(tmp_path: Path, mod): |
| 19 | + cfg = mod.load_ark_config(str(tmp_path)) |
| 20 | + # Basic structure keys |
| 21 | + for key in ("exclusion_patterns", "inclusion_patterns", "pyinstaller", "nuitka", "output", "dependencies", "plugins", "advanced"): |
| 22 | + assert key in cfg |
| 23 | + # Inclusion defaults |
| 24 | + assert isinstance(cfg["inclusion_patterns"], list) |
| 25 | + assert any(p.endswith("*.py") for p in cfg["inclusion_patterns"]) |
| 26 | + |
| 27 | + |
| 28 | +def test_create_default_config_file_idempotent(tmp_path: Path, mod): |
| 29 | + # First call should create file |
| 30 | + created_first = mod.create_default_ark_config(str(tmp_path)) |
| 31 | + assert created_first is True |
| 32 | + conf_path = tmp_path / "ARK_Main_Config.yml" |
| 33 | + assert conf_path.is_file() |
| 34 | + # Second call should not overwrite and return False |
| 35 | + created_second = mod.create_default_ark_config(str(tmp_path)) |
| 36 | + assert created_second is False |
| 37 | + |
| 38 | + |
| 39 | +def test_merge_user_config_and_normalization(tmp_path: Path, mod): |
| 40 | + # Write a minimal user config overriding nested values and adding custom patterns |
| 41 | + user_cfg = """ |
| 42 | +pyinstaller: |
| 43 | + onefile: false |
| 44 | + windowed: true |
| 45 | +exclusion_patterns: |
| 46 | + - "custom_ignore/**" |
| 47 | + - "**/*.pyo" |
| 48 | +main_file_names: |
| 49 | + - main.py |
| 50 | + - start.py |
| 51 | + """ |
| 52 | + (tmp_path / "ARK_Main_Config.yml").write_text(user_cfg, encoding="utf-8") |
| 53 | + |
| 54 | + cfg = mod.load_ark_config(str(tmp_path)) |
| 55 | + |
| 56 | + # Nested overrides applied |
| 57 | + assert cfg["pyinstaller"]["onefile"] is False |
| 58 | + assert cfg["pyinstaller"]["windowed"] is True |
| 59 | + |
| 60 | + # Exclusion patterns contain defaults plus user entries (deduped) |
| 61 | + patterns = set(cfg["exclusion_patterns"]) |
| 62 | + assert "custom_ignore/**" in patterns |
| 63 | + # Default like "**/*.pyc" should still be present |
| 64 | + assert any(p.endswith("*.pyc") for p in patterns) |
| 65 | + |
| 66 | + # Normalized main_file_names list |
| 67 | + assert set(cfg["main_file_names"]) >= {"main.py", "start.py"} |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.parametrize( |
| 71 | + "compiler_key", |
| 72 | + ["pyinstaller", "nuitka", "cx_freeze"], |
| 73 | +) |
| 74 | +def test_get_compiler_options_present(tmp_path: Path, mod, compiler_key: str): |
| 75 | + cfg = mod.load_ark_config(str(tmp_path)) |
| 76 | + opts = mod.get_compiler_options(cfg, compiler_key) |
| 77 | + assert isinstance(opts, dict) |
| 78 | + |
| 79 | + |
| 80 | +def test_option_accessors(tmp_path: Path, mod): |
| 81 | + cfg = mod.load_ark_config(str(tmp_path)) |
| 82 | + assert isinstance(mod.get_output_options(cfg), dict) |
| 83 | + assert isinstance(mod.get_dependency_options(cfg), dict) |
| 84 | + assert isinstance(mod.get_plugin_options(cfg), dict) |
| 85 | + assert isinstance(mod.get_advanced_options(cfg), dict) |
| 86 | + |
| 87 | + |
| 88 | +def test_should_exclude_file_logic(tmp_path: Path, mod): |
| 89 | + # Create workspace and files |
| 90 | + ws = tmp_path |
| 91 | + (ws / "venv" / "lib").mkdir(parents=True) |
| 92 | + inside_ok = ws / "src" / "app.py" |
| 93 | + inside_ok.parent.mkdir(parents=True) |
| 94 | + inside_ok.write_text("print('x')", encoding="utf-8") |
| 95 | + excluded_by_pattern = ws / "venv" / "lib" / "x.py" |
| 96 | + excluded_by_pattern.write_text("print('y')", encoding="utf-8") |
| 97 | + |
| 98 | + patterns = mod.DEFAULT_EXCLUSION_PATTERNS |
| 99 | + |
| 100 | + # File outside workspace must be excluded |
| 101 | + assert mod.should_exclude_file("/tmp/somewhere_else.py", str(ws), patterns) is True |
| 102 | + |
| 103 | + # File matching exclusion pattern is excluded |
| 104 | + assert mod.should_exclude_file(str(excluded_by_pattern), str(ws), patterns) is True |
| 105 | + |
| 106 | + # Regular file inside workspace not matching patterns is not excluded |
| 107 | + assert mod.should_exclude_file(str(inside_ok), str(ws), patterns) is False |
0 commit comments