From a453a89fee45e2b22315555dd982675faf3c612f Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Wed, 19 Jun 2019 20:36:54 -0500 Subject: [PATCH 1/2] TST: Fix flaky import test I'm not sure what, but the missing depedency test is causing issues. Now we check that things work by running it in a subprocess with site-packages disabled. Closes https://github.com/pandas-dev/pandas/issues/26952 --- pandas/tests/test_downstream.py | 36 +++++++++------------------------ 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/pandas/tests/test_downstream.py b/pandas/tests/test_downstream.py index 14d3ee5ac4fe2..80d78cee1e143 100644 --- a/pandas/tests/test_downstream.py +++ b/pandas/tests/test_downstream.py @@ -1,7 +1,6 @@ """ Testing that we work in the downstream packages """ -import builtins import importlib import subprocess import sys @@ -134,30 +133,13 @@ def test_pyarrow(df): tm.assert_frame_equal(result, df) -def test_missing_required_dependency(monkeypatch): +def test_missing_required_dependency(capsys): # GH 23868 - original_import = __import__ - - def mock_import_fail(name, *args, **kwargs): - if name == "numpy": - raise ImportError("cannot import name numpy") - elif name == "pytz": - raise ImportError("cannot import name some_dependency") - elif name == "dateutil": - raise ImportError("cannot import name some_other_dependency") - else: - return original_import(name, *args, **kwargs) - - expected_msg = ( - "Unable to import required dependencies:" - "\nnumpy: cannot import name numpy" - "\npytz: cannot import name some_dependency" - "\ndateutil: cannot import name some_other_dependency" - ) - - import pandas as pd - - with monkeypatch.context() as m: - m.setattr(builtins, "__import__", mock_import_fail) - with pytest.raises(ImportError, match=expected_msg): - importlib.reload(pd) + # use the -S flag to disable site-packages + call = ['python', '-S', '-c', 'import pandas'] + + with pytest.raises(subprocess.CalledProcessError) as exc: + subprocess.check_output(call, stderr=subprocess.STDOUT) + + output = exc.value.stdout.decode() + assert all(x in output for x in ['numpy', 'pytz', 'dateutil']) From df1030b22ad6574e9bfa57f4f8623af9f41da24e Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Wed, 19 Jun 2019 20:38:27 -0500 Subject: [PATCH 2/2] remove capsys --- pandas/tests/test_downstream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/test_downstream.py b/pandas/tests/test_downstream.py index 80d78cee1e143..9fe8b0f9563ef 100644 --- a/pandas/tests/test_downstream.py +++ b/pandas/tests/test_downstream.py @@ -133,7 +133,7 @@ def test_pyarrow(df): tm.assert_frame_equal(result, df) -def test_missing_required_dependency(capsys): +def test_missing_required_dependency(): # GH 23868 # use the -S flag to disable site-packages call = ['python', '-S', '-c', 'import pandas']