|
14 | 14 | # limitations under the License. |
15 | 15 | from __future__ import annotations |
16 | 16 |
|
| 17 | +import os |
17 | 18 | from pathlib import Path |
18 | 19 | import tempfile |
19 | 20 | from unittest.mock import MagicMock, patch |
@@ -89,23 +90,96 @@ def test_find_file_path_success(self): |
89 | 90 | assert result == mock_path |
90 | 91 | mock_resolve.assert_called_once_with("test.txt") |
91 | 92 |
|
92 | | - def test_resolve_file_path_code_package_exists(self): |
93 | | - """Test _resolve_file_path when code package exists and file is found.""" |
| 93 | + def test_resolve_file_path_env_var_set_file_exists(self): |
| 94 | + """Test _resolve_file_path when environment variable is set and file exists.""" |
94 | 95 | finder = DefaultFindFilePath() |
95 | 96 |
|
96 | | - with patch.object( |
97 | | - finder, "_code_package_exists", return_value=True |
98 | | - ) as mock_exists: |
99 | | - with patch.object(finder, "_get_code_package_file_path") as mock_get_path: |
100 | | - mock_path = MagicMock() |
101 | | - mock_path.exists.return_value = True |
102 | | - mock_get_path.return_value = mock_path |
| 97 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 98 | + test_file = Path(temp_dir) / "test.txt" |
| 99 | + test_file.write_text("test content") |
103 | 100 |
|
| 101 | + with patch.dict(os.environ, {finder.DEFAULT_ENV_VAR: str(temp_dir)}): |
104 | 102 | result = finder._resolve_file_path("test.txt") |
105 | 103 |
|
106 | | - assert result == mock_path |
107 | | - mock_exists.assert_called_once() |
108 | | - mock_get_path.assert_called_once_with("test.txt") |
| 104 | + assert result == test_file |
| 105 | + assert result.exists() |
| 106 | + |
| 107 | + def test_resolve_file_path_env_var_set_file_not_found(self): |
| 108 | + """Test _resolve_file_path when environment variable is set but file not found, |
| 109 | + falls back to code package.""" |
| 110 | + finder = DefaultFindFilePath() |
| 111 | + |
| 112 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 113 | + # Set env var to a directory that doesn't contain the file |
| 114 | + with patch.dict(os.environ, {finder.DEFAULT_ENV_VAR: str(temp_dir)}): |
| 115 | + with patch.object( |
| 116 | + finder, "_code_package_exists", return_value=True |
| 117 | + ) as mock_exists: |
| 118 | + with patch.object( |
| 119 | + finder, "_get_code_package_file_path" |
| 120 | + ) as mock_get_path: |
| 121 | + mock_path = MagicMock() |
| 122 | + mock_path.exists.return_value = True |
| 123 | + mock_get_path.return_value = mock_path |
| 124 | + |
| 125 | + result = finder._resolve_file_path("test.txt") |
| 126 | + |
| 127 | + assert result == mock_path |
| 128 | + mock_exists.assert_called_once() |
| 129 | + mock_get_path.assert_called_once_with("test.txt") |
| 130 | + |
| 131 | + def test_resolve_file_path_env_var_not_set(self): |
| 132 | + """Test _resolve_file_path when environment variable is not set, |
| 133 | + uses normal flow.""" |
| 134 | + finder = DefaultFindFilePath() |
| 135 | + |
| 136 | + # Ensure env var is not set |
| 137 | + env_backup = os.environ.pop(finder.DEFAULT_ENV_VAR, None) |
| 138 | + try: |
| 139 | + with patch.object( |
| 140 | + finder, "_code_package_exists", return_value=True |
| 141 | + ) as mock_exists: |
| 142 | + with patch.object( |
| 143 | + finder, "_get_code_package_file_path" |
| 144 | + ) as mock_get_path: |
| 145 | + mock_path = MagicMock() |
| 146 | + mock_path.exists.return_value = True |
| 147 | + mock_get_path.return_value = mock_path |
| 148 | + |
| 149 | + result = finder._resolve_file_path("test.txt") |
| 150 | + |
| 151 | + assert result == mock_path |
| 152 | + mock_exists.assert_called_once() |
| 153 | + mock_get_path.assert_called_once_with("test.txt") |
| 154 | + finally: |
| 155 | + if env_backup is not None: |
| 156 | + os.environ[finder.DEFAULT_ENV_VAR] = env_backup |
| 157 | + |
| 158 | + def test_resolve_file_path_code_package_exists(self): |
| 159 | + """Test _resolve_file_path when code package exists and file is found.""" |
| 160 | + finder = DefaultFindFilePath() |
| 161 | + |
| 162 | + # Ensure env var is not set to test normal flow |
| 163 | + env_backup = os.environ.pop(finder.DEFAULT_ENV_VAR, None) |
| 164 | + try: |
| 165 | + with patch.object( |
| 166 | + finder, "_code_package_exists", return_value=True |
| 167 | + ) as mock_exists: |
| 168 | + with patch.object( |
| 169 | + finder, "_get_code_package_file_path" |
| 170 | + ) as mock_get_path: |
| 171 | + mock_path = MagicMock() |
| 172 | + mock_path.exists.return_value = True |
| 173 | + mock_get_path.return_value = mock_path |
| 174 | + |
| 175 | + result = finder._resolve_file_path("test.txt") |
| 176 | + |
| 177 | + assert result == mock_path |
| 178 | + mock_exists.assert_called_once() |
| 179 | + mock_get_path.assert_called_once_with("test.txt") |
| 180 | + finally: |
| 181 | + if env_backup is not None: |
| 182 | + os.environ[finder.DEFAULT_ENV_VAR] = env_backup |
109 | 183 |
|
110 | 184 | def test_resolve_file_path_code_package_exists_file_not_found(self): |
111 | 185 | """Test _resolve_file_path when code package exists but file not found, |
|
0 commit comments