diff --git a/import_tracker/import_tracker.py b/import_tracker/import_tracker.py index b5d0f5c..3ce9e86 100644 --- a/import_tracker/import_tracker.py +++ b/import_tracker/import_tracker.py @@ -7,6 +7,7 @@ from contextlib import contextmanager from types import ModuleType from typing import Dict, List, Optional +import copy import importlib import inspect import json @@ -210,14 +211,10 @@ def _track_deps(name: str, package: Optional[str] = None): ) if package is not None: cmd += f" --package {package}" - res = subprocess.run( - shlex.split(cmd), - stdout=subprocess.PIPE, - env={ - MODE_ENV_VAR: LAZY, - "PYTHONPATH": ":".join(sys.path), - }, - ) + env = dict(copy.deepcopy(os.environ)) + env[MODE_ENV_VAR] = LAZY + env["PYTHONPATH"] = ":".join(sys.path) + res = subprocess.run(shlex.split(cmd), stdout=subprocess.PIPE, env=env) assert res.returncode == 0, f"Failed to track {name}" deps = json.loads(res.stdout) diff --git a/test/helpers.py b/test/helpers.py index 1019c12..b92044e 100644 --- a/test/helpers.py +++ b/test/helpers.py @@ -77,6 +77,18 @@ def TRACKING_MODE(): yield +def remove_test_deps(deps): + """If running with pytest coverage enabled, these deps will show up. We + don't want run-env-dependent tests, so we just pop them out. + """ + for test_dep in ["pytest_cov", "coverage"]: + try: + deps.remove(test_dep) + except ValueError: + continue + return deps + + ## Implementations ############################################################# diff --git a/test/sample_libs/env_import/__init__.py b/test/sample_libs/env_import/__init__.py new file mode 100644 index 0000000..e04e4ca --- /dev/null +++ b/test/sample_libs/env_import/__init__.py @@ -0,0 +1,8 @@ +""" +This is a sample library that requires an env var at import time +""" + +# Standard +import os + +assert "SAMPLE_ENV_VAR" in os.environ diff --git a/test/test_import_tracker.py b/test/test_import_tracker.py index 2cbc63e..bf3c4f9 100644 --- a/test/test_import_tracker.py +++ b/test/test_import_tracker.py @@ -19,6 +19,7 @@ LAZY_MODE, PROACTIVE_MODE, TRACKING_MODE, + remove_test_deps, reset_static_trackers, reset_sys_modules, ) @@ -300,10 +301,12 @@ def test_import_module_tracking_direct(TRACKING_MODE): """ submod1 = import_tracker.import_module("sample_lib.submod1") submod2 = import_tracker.import_module("sample_lib.submod2") - assert import_tracker.get_required_imports("sample_lib.submod1") == [ - "conditional_deps" - ] - assert import_tracker.get_required_imports("sample_lib.submod2") == ["alog"] + assert remove_test_deps( + import_tracker.get_required_imports("sample_lib.submod1") + ) == ["conditional_deps"] + assert remove_test_deps( + import_tracker.get_required_imports("sample_lib.submod2") + ) == ["alog"] def test_import_module_tracking_update_static(TRACKING_MODE): @@ -328,3 +331,11 @@ def test_import_module_tracking_with_package(TRACKING_MODE): expected (this is mostly for coverage) """ import_tracker.import_module(".submod1", "sample_lib") + + +def test_import_module_tracking_env_passthrough(TRACKING_MODE): + """Test that performing tracking when the submodule has a package works as + expected (this is mostly for coverage) + """ + os.environ["SAMPLE_ENV_VAR"] = "something" + import_tracker.import_module("env_import")