|
15 | 15 | # specific language governing permissions and limitations
|
16 | 16 | # under the License.
|
17 | 17 | import os
|
| 18 | +from typing import Any, Dict, Optional |
18 | 19 | from unittest import mock
|
19 | 20 |
|
20 | 21 | import pytest
|
@@ -93,3 +94,84 @@ def test_from_configuration_files_get_typed_value(tmp_path_factory: pytest.TempP
|
93 | 94 |
|
94 | 95 | assert Config().get_bool("legacy-current-snapshot-id")
|
95 | 96 | assert Config().get_int("max-workers") == 4
|
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.parametrize( |
| 100 | + "config_setup, expected_result", |
| 101 | + [ |
| 102 | + # PYICEBERG_HOME takes precedence |
| 103 | + ( |
| 104 | + { |
| 105 | + "pyiceberg_home_content": "https://service.io/pyiceberg_home", |
| 106 | + "home_content": "https://service.io/user-home", |
| 107 | + "cwd_content": "https://service.io/cwd", |
| 108 | + }, |
| 109 | + "https://service.io/pyiceberg_home", |
| 110 | + ), |
| 111 | + # Home directory (~) is checked after PYICEBERG_HOME |
| 112 | + ( |
| 113 | + { |
| 114 | + "pyiceberg_home_content": None, |
| 115 | + "home_content": "https://service.io/user-home", |
| 116 | + "cwd_content": "https://service.io/cwd", |
| 117 | + }, |
| 118 | + "https://service.io/user-home", |
| 119 | + ), |
| 120 | + # Current working directory (.) is the last fallback |
| 121 | + ( |
| 122 | + { |
| 123 | + "pyiceberg_home_content": None, |
| 124 | + "home_content": None, |
| 125 | + "cwd_content": "https://service.io/cwd", |
| 126 | + }, |
| 127 | + "https://service.io/cwd", |
| 128 | + ), |
| 129 | + # No configuration files found |
| 130 | + ( |
| 131 | + { |
| 132 | + "pyiceberg_home_content": None, |
| 133 | + "home_content": None, |
| 134 | + "cwd_content": None, |
| 135 | + }, |
| 136 | + None, |
| 137 | + ), |
| 138 | + ], |
| 139 | +) |
| 140 | +def test_config_lookup_order( |
| 141 | + monkeypatch: pytest.MonkeyPatch, |
| 142 | + tmp_path_factory: pytest.TempPathFactory, |
| 143 | + config_setup: Dict[str, Any], |
| 144 | + expected_result: Optional[str], |
| 145 | +) -> None: |
| 146 | + """ |
| 147 | + Test that the configuration lookup prioritizes PYICEBERG_HOME, then home (~), then cwd. |
| 148 | + """ |
| 149 | + |
| 150 | + def create_config_file(path: str, uri: Optional[str]) -> None: |
| 151 | + if uri: |
| 152 | + config_file_path = os.path.join(path, ".pyiceberg.yaml") |
| 153 | + content = {"catalog": {"default": {"uri": uri}}} |
| 154 | + with open(config_file_path, "w", encoding="utf-8") as file: |
| 155 | + yaml_str = as_document(content).as_yaml() |
| 156 | + file.write(yaml_str) |
| 157 | + |
| 158 | + # Create temporary directories for PYICEBERG_HOME, home (~), and cwd |
| 159 | + pyiceberg_home = str(tmp_path_factory.mktemp("pyiceberg_home")) |
| 160 | + home_dir = str(tmp_path_factory.mktemp("home")) |
| 161 | + cwd_dir = str(tmp_path_factory.mktemp("cwd")) |
| 162 | + |
| 163 | + # Create configuration files in the respective directories |
| 164 | + create_config_file(pyiceberg_home, config_setup.get("pyiceberg_home_content")) |
| 165 | + create_config_file(home_dir, config_setup.get("home_content")) |
| 166 | + create_config_file(cwd_dir, config_setup.get("cwd_content")) |
| 167 | + |
| 168 | + # Mock environment and paths |
| 169 | + monkeypatch.setenv("PYICEBERG_HOME", pyiceberg_home) |
| 170 | + monkeypatch.setattr(os.path, "expanduser", lambda _: home_dir) |
| 171 | + monkeypatch.chdir(cwd_dir) |
| 172 | + |
| 173 | + # Perform the lookup and validate the result |
| 174 | + result = Config()._from_configuration_files() |
| 175 | + assert ( |
| 176 | + result["catalog"]["default"]["uri"] if result else None # type: ignore |
| 177 | + ) == expected_result, f"Unexpected configuration result. Expected: {expected_result}, Actual: {result}" |
0 commit comments