diff --git a/tests/test_scripts.py b/tests/test_scripts.py index 00da886780..6018254dc1 100644 --- a/tests/test_scripts.py +++ b/tests/test_scripts.py @@ -2,6 +2,7 @@ import shlex import pyhf import time +import sys import pytest @@ -520,6 +521,35 @@ def test_patchset_download(datadir, script_runner, archive): ) +def test_missing_contrib_extra(datadir, script_runner): + module_name = "requests" + + # hide + CACHE_MODULE, sys.modules[module_name] = sys.modules[module_name], None + assert sys.modules[module_name] is None + + archive_url = "https://www.hepdata.net/record/resource/1408476?view=true" + command = ( + f'pyhf contrib download {archive_url} {datadir.join("likelihoods").strpath}' + ) + ret = script_runner.run(*shlex.split(command)) + assert ( + "import of requests halted; None in sys.modules" + + "\nInstallation of the contrib extra is required for: pyhf contrib download" + + "\nPlease install with: python -m pip install pyhf[contrib]" + in ret.stdout + ) + assert ( + "ModuleNotFoundError: import of requests halted; None in sys.modules" + in ret.stderr + ) + assert not ret.success + + # put back + CACHE_MODULE, sys.modules[module_name] = None, CACHE_MODULE + assert sys.modules[module_name] is not None + + @pytest.mark.parametrize('output_file', [False, True]) @pytest.mark.parametrize('with_metadata', [False, True]) def test_patchset_extract(datadir, tmpdir, script_runner, output_file, with_metadata):