diff --git a/src/crystal/url_input.py b/src/crystal/url_input.py index eaf9f15b..f9b46907 100644 --- a/src/crystal/url_input.py +++ b/src/crystal/url_input.py @@ -83,17 +83,6 @@ def _candidate_urls_from_user_input(url_input: str) -> List[str]: * If additionally no www. domain prefix is given, try first without the prefix then with the prefix. """ - # While running tests, unless CRYSTAL_URLOPEN_MOCKED=True, - # assume any URL input is already a valid URL without - # performing any (possibly real) network requests - if os.environ.get('CRYSTAL_RUNNING_TESTS', 'False') == 'True': - if os.environ.get('CRYSTAL_URLOPEN_MOCKED', 'False') == 'True': - # OK - pass - else: - # Assume valid, without performing any network requests - return [url_input] - if url_input.strip() == '': return [url_input] @@ -172,6 +161,23 @@ def _resolve_url_from_candidates( if len(url_candidates) == 1: return url_candidates[0] + # Disallow network requests while running tests, + # unless CRYSTAL_URLOPEN_MOCKED=True + if os.environ.get('CRYSTAL_RUNNING_TESTS', 'False') == 'True': + if os.environ.get('CRYSTAL_URLOPEN_MOCKED', 'False') == 'True': + # OK + pass + else: + raise AssertionError( + 'Attempting to resolve URL candidates with real ' + 'network requests while automated tests are running. ' + + 'Please either (1) enter URLs with http:// or https:// ' + 'schema that does not need to be resolved, or (2) ' + 'mock the urlopen() function with something like ' + '_urlopen_responding_with().' + ) + # Look for URL candidate which can be fetched successfully for url_candidate in url_candidates: if did_cancel_func():