|
4 | 4 |
|
5 | 5 | import array
|
6 | 6 | from functools import partial
|
| 7 | +import importlib |
7 | 8 | import subprocess
|
8 | 9 | import sys
|
9 | 10 |
|
@@ -186,41 +187,21 @@ def test_yaml_dump(df):
|
186 | 187 | tm.assert_frame_equal(df, loaded2)
|
187 | 188 |
|
188 | 189 |
|
189 |
| -@pytest.mark.single_cpu |
190 |
| -def test_missing_required_dependency(): |
191 |
| - # GH 23868 |
192 |
| - # To ensure proper isolation, we pass these flags |
193 |
| - # -S : disable site-packages |
194 |
| - # -s : disable user site-packages |
195 |
| - # -E : disable PYTHON* env vars, especially PYTHONPATH |
196 |
| - # https://github.com/MacPython/pandas-wheels/pull/50 |
197 |
| - |
198 |
| - pyexe = sys.executable.replace("\\", "/") |
199 |
| - |
200 |
| - # We skip this test if pandas is installed as a site package. We first |
201 |
| - # import the package normally and check the path to the module before |
202 |
| - # executing the test which imports pandas with site packages disabled. |
203 |
| - call = [pyexe, "-c", "import pandas;print(pandas.__file__)"] |
204 |
| - output = subprocess.check_output(call).decode() |
205 |
| - if "site-packages" in output: |
206 |
| - pytest.skip("pandas installed as site package") |
207 |
| - |
208 |
| - # This test will fail if pandas is installed as a site package. The flags |
209 |
| - # prevent pandas being imported and the test will report Failed: DID NOT |
210 |
| - # RAISE <class 'subprocess.CalledProcessError'> |
211 |
| - call = [pyexe, "-sSE", "-c", "import pandas"] |
212 |
| - |
213 |
| - msg = ( |
214 |
| - rf"Command '\['{pyexe}', '-sSE', '-c', 'import pandas'\]' " |
215 |
| - "returned non-zero exit status 1." |
216 |
| - ) |
| 190 | +@pytest.mark.parametrize("dependency", ["numpy", "dateutil"]) |
| 191 | +def test_missing_required_dependency(monkeypatch, dependency): |
| 192 | + # GH#61030 |
| 193 | + original_import = __import__ |
| 194 | + mock_error = ImportError(f"Mock error for {dependency}") |
| 195 | + |
| 196 | + def mock_import(name, *args, **kwargs): |
| 197 | + if name == dependency: |
| 198 | + raise mock_error |
| 199 | + return original_import(name, *args, **kwargs) |
217 | 200 |
|
218 |
| - with pytest.raises(subprocess.CalledProcessError, match=msg) as exc: |
219 |
| - subprocess.check_output(call, stderr=subprocess.STDOUT) |
| 201 | + monkeypatch.setattr("builtins.__import__", mock_import) |
220 | 202 |
|
221 |
| - output = exc.value.stdout.decode() |
222 |
| - for name in ["numpy", "dateutil"]: |
223 |
| - assert name in output |
| 203 | + with pytest.raises(ImportError, match=dependency): |
| 204 | + importlib.reload(importlib.import_module("pandas")) |
224 | 205 |
|
225 | 206 |
|
226 | 207 | def test_frame_setitem_dask_array_into_new_col(request):
|
|
0 commit comments