Skip to content

Commit

Permalink
Merge pull request #10 from IBM/PassThroughEnv
Browse files Browse the repository at this point in the history
PassThroughEnv: Pass the full env through in TRACKING mode
  • Loading branch information
gabe-l-hart authored Dec 23, 2021
2 parents 995235d + 5e245ba commit fa5ac5a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
13 changes: 5 additions & 8 deletions import_tracker/import_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
12 changes: 12 additions & 0 deletions test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 #############################################################


Expand Down
8 changes: 8 additions & 0 deletions test/sample_libs/env_import/__init__.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 15 additions & 4 deletions test/test_import_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
LAZY_MODE,
PROACTIVE_MODE,
TRACKING_MODE,
remove_test_deps,
reset_static_trackers,
reset_sys_modules,
)
Expand Down Expand Up @@ -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):
Expand All @@ -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")

0 comments on commit fa5ac5a

Please sign in to comment.