|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2026 Ague Samuel Amen |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +""" |
| 17 | +Tests for Core.ark_config_loader module - ARK configuration file handling |
| 18 | +""" |
| 19 | + |
| 20 | +import pytest |
| 21 | +import os |
| 22 | +import tempfile |
| 23 | +import yaml |
| 24 | +from unittest.mock import patch, MagicMock |
| 25 | + |
| 26 | + |
| 27 | +class TestArkConfigLoader: |
| 28 | + """Test ARK configuration loader functions""" |
| 29 | + |
| 30 | + def test_import_ark_config_loader(self): |
| 31 | + """Test that ark_config_loader module can be imported""" |
| 32 | + from Core import ark_config_loader |
| 33 | + assert ark_config_loader is not None |
| 34 | + |
| 35 | + |
| 36 | +class TestLoadArkConfig: |
| 37 | + """Test load_ark_config function""" |
| 38 | + |
| 39 | + def test_load_ark_config_with_valid_file(self): |
| 40 | + """Test loading a valid ARK config file""" |
| 41 | + from Core.ark_config_loader import load_ark_config |
| 42 | + |
| 43 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 44 | + config_path = os.path.join(tmpdir, "ARK_Main_Config.yml") |
| 45 | + |
| 46 | + # Create a valid config file |
| 47 | + config_data = { |
| 48 | + "exclusion_patterns": ["*.pyc", "__pycache__"], |
| 49 | + "compiler_options": { |
| 50 | + "pyinstaller": { |
| 51 | + "onefile": True |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + with open(config_path, 'w') as f: |
| 57 | + yaml.dump(config_data, f) |
| 58 | + |
| 59 | + # Load the config |
| 60 | + result = load_ark_config(tmpdir) |
| 61 | + |
| 62 | + assert isinstance(result, dict) |
| 63 | + assert "exclusion_patterns" in result |
| 64 | + |
| 65 | + def test_load_ark_config_with_missing_file(self): |
| 66 | + """Test loading config when file doesn't exist""" |
| 67 | + from Core.ark_config_loader import load_ark_config |
| 68 | + |
| 69 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 70 | + # Should return empty dict or default config |
| 71 | + result = load_ark_config(tmpdir) |
| 72 | + assert isinstance(result, dict) |
| 73 | + |
| 74 | + def test_load_ark_config_with_invalid_yaml(self): |
| 75 | + """Test loading config with invalid YAML""" |
| 76 | + from Core.ark_config_loader import load_ark_config |
| 77 | + |
| 78 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 79 | + config_path = os.path.join(tmpdir, "ARK_Main_Config.yml") |
| 80 | + |
| 81 | + # Create invalid YAML |
| 82 | + with open(config_path, 'w') as f: |
| 83 | + f.write("invalid: yaml: content: [") |
| 84 | + |
| 85 | + # Should handle error gracefully |
| 86 | + result = load_ark_config(tmpdir) |
| 87 | + assert isinstance(result, dict) |
| 88 | + |
| 89 | + def test_load_ark_config_with_empty_file(self): |
| 90 | + """Test loading config with empty file""" |
| 91 | + from Core.ark_config_loader import load_ark_config |
| 92 | + |
| 93 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 94 | + config_path = os.path.join(tmpdir, "ARK_Main_Config.yml") |
| 95 | + |
| 96 | + # Create empty file |
| 97 | + with open(config_path, 'w') as f: |
| 98 | + pass |
| 99 | + |
| 100 | + # Should return empty dict or handle gracefully |
| 101 | + result = load_ark_config(tmpdir) |
| 102 | + assert isinstance(result, dict) |
| 103 | + |
| 104 | + |
| 105 | +class TestCreateDefaultArkConfig: |
| 106 | + """Test create_default_ark_config function""" |
| 107 | + |
| 108 | + def test_create_default_config(self): |
| 109 | + """Test creating default ARK config""" |
| 110 | + from Core.ark_config_loader import create_default_ark_config |
| 111 | + |
| 112 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 113 | + result = create_default_ark_config(tmpdir) |
| 114 | + assert result is True |
| 115 | + |
| 116 | + # Verify file was created |
| 117 | + config_path = os.path.join(tmpdir, "ARK_Main_Config.yml") |
| 118 | + assert os.path.exists(config_path) |
| 119 | + |
| 120 | + def test_create_default_config_structure(self): |
| 121 | + """Test that default config has correct structure""" |
| 122 | + from Core.ark_config_loader import create_default_ark_config, load_ark_config |
| 123 | + |
| 124 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 125 | + create_default_ark_config(tmpdir) |
| 126 | + |
| 127 | + config = load_ark_config(tmpdir) |
| 128 | + |
| 129 | + assert isinstance(config, dict) |
| 130 | + # Check for expected keys in default config |
| 131 | + expected_keys = ["exclusion_patterns", "compiler_options"] |
| 132 | + for key in expected_keys: |
| 133 | + assert key in config |
| 134 | + |
| 135 | + def test_create_default_config_no_overwrite(self): |
| 136 | + """Test that default config doesn't overwrite existing""" |
| 137 | + from Core.ark_config_loader import create_default_ark_config |
| 138 | + |
| 139 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 140 | + config_path = os.path.join(tmpdir, "ARK_Main_Config.yml") |
| 141 | + |
| 142 | + # Create existing config |
| 143 | + with open(config_path, 'w') as f: |
| 144 | + f.write("custom: config") |
| 145 | + |
| 146 | + # Should not overwrite |
| 147 | + result = create_default_ark_config(tmpdir) |
| 148 | + assert result is False |
| 149 | + |
| 150 | + |
| 151 | +class TestShouldExcludeFile: |
| 152 | + """Test should_exclude_file function""" |
| 153 | + |
| 154 | + def test_should_exclude_file_with_pattern(self): |
| 155 | + """Test that matching patterns are excluded""" |
| 156 | + from Core.ark_config_loader import should_exclude_file |
| 157 | + |
| 158 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 159 | + patterns = ["*.pyc", "__pycache__"] |
| 160 | + |
| 161 | + assert should_exclude_file("test.pyc", tmpdir, patterns) is True |
| 162 | + assert should_exclude_file("test.py", tmpdir, patterns) is False |
| 163 | + |
| 164 | + def test_should_exclude_file_with_directory(self): |
| 165 | + """Test that __pycache__ directories are excluded""" |
| 166 | + from Core.ark_config_loader import should_exclude_file |
| 167 | + |
| 168 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 169 | + patterns = ["__pycache__", "*.egg-info"] |
| 170 | + |
| 171 | + assert should_exclude_file("__pycache__", tmpdir, patterns) is True |
| 172 | + assert should_exclude_file("test.egg-info", tmpdir, patterns) is True |
| 173 | + |
| 174 | + def test_should_exclude_file_empty_patterns(self): |
| 175 | + """Test with empty exclusion patterns""" |
| 176 | + from Core.ark_config_loader import should_exclude_file |
| 177 | + |
| 178 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 179 | + result = should_exclude_file("test.pyc", tmpdir, []) |
| 180 | + assert result is False |
| 181 | + |
| 182 | + def test_should_exclude_file_none_patterns(self): |
| 183 | + """Test with None exclusion patterns""" |
| 184 | + from Core.ark_config_loader import should_exclude_file |
| 185 | + |
| 186 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 187 | + result = should_exclude_file("test.pyc", tmpdir, None) |
| 188 | + assert result is False |
| 189 | + |
| 190 | + |
| 191 | +class TestArkConfigSchema: |
| 192 | + """Test ARK configuration schema validation""" |
| 193 | + |
| 194 | + def test_default_config_schema(self): |
| 195 | + """Test that default config follows schema""" |
| 196 | + from Core.ark_config_loader import create_default_ark_config, load_ark_config |
| 197 | + from Core.ark_config_loader import SCHEMA |
| 198 | + |
| 199 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 200 | + create_default_ark_config(tmpdir) |
| 201 | + config = load_ark_config(tmpdir) |
| 202 | + |
| 203 | + # Verify config follows schema |
| 204 | + if SCHEMA: |
| 205 | + # If there's a schema, validate against it |
| 206 | + assert isinstance(config, dict) |
| 207 | + |
| 208 | + def test_config_has_exclusion_patterns(self): |
| 209 | + """Test that config has exclusion_patterns key""" |
| 210 | + from Core.ark_config_loader import create_default_ark_config, load_ark_config |
| 211 | + |
| 212 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 213 | + create_default_ark_config(tmpdir) |
| 214 | + config = load_ark_config(tmpdir) |
| 215 | + |
| 216 | + assert "exclusion_patterns" in config |
| 217 | + assert isinstance(config["exclusion_patterns"], list) |
| 218 | + |
| 219 | + |
| 220 | +class TestArkConfigPaths: |
| 221 | + """Test path handling in ARK config""" |
| 222 | + |
| 223 | + def test_config_path_construction(self): |
| 224 | + """Test that config path is constructed correctly""" |
| 225 | + from Core.ark_config_loader import _get_config_path |
| 226 | + |
| 227 | + workspace = "/some/workspace" |
| 228 | + path = _get_config_path(workspace) |
| 229 | + |
| 230 | + assert path.endswith("ARK_Main_Config.yml") |
| 231 | + assert workspace in path |
| 232 | + |
| 233 | + def test_expand_variables_in_config(self): |
| 234 | + """Test variable expansion in config values""" |
| 235 | + from Core.ark_config_loader import load_ark_config |
| 236 | + |
| 237 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 238 | + config_path = os.path.join(tmpdir, "ARK_Main_Config.yml") |
| 239 | + |
| 240 | + config_data = { |
| 241 | + "paths": { |
| 242 | + "output": "${workspace}/build" |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + with open(config_path, 'w') as f: |
| 247 | + yaml.dump(config_data, f) |
| 248 | + |
| 249 | + config = load_ark_config(tmpdir) |
| 250 | + |
| 251 | + # Check that variable was expanded |
| 252 | + if "paths" in config and "output" in config["paths"]: |
| 253 | + output = config["paths"]["output"] |
| 254 | + assert tmpdir in output or "build" in output |
| 255 | + |
0 commit comments