Skip to content

Commit fbce37d

Browse files
committed
Add test to mock lack of any local environment
1 parent 646f62f commit fbce37d

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

tests/tests/test_general.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def test_mock_pip_pkgs(tmp_path):
297297
and test that packages are logged correctly.
298298
"""
299299

300-
# Simulated `conda list --json` output
300+
# Simulated `pip list --json` output
301301
fake_pip_output = json.dumps(
302302
[
303303
{"name": "fancylog", "version": "1.1.1", "location": "fake_env"},
@@ -312,7 +312,7 @@ def test_mock_pip_pkgs(tmp_path):
312312
patch("subprocess.run") as mock_run,
313313
):
314314
# Eliminate conda environment packages triggers logging pip list
315-
(os.environ.pop("CONDA_PREFIX", None),)
315+
os.environ.pop("CONDA_PREFIX", None)
316316
os.environ.pop("CONDA_EXE", None)
317317

318318
mock_getenv.return_value = "fake_env"
@@ -380,3 +380,48 @@ def test_mock_conda_pkgs(tmp_path):
380380
assert "Environment packages (conda):" in file_content
381381
assert f"{'fancylog':20} {'1.1.1'}"
382382
assert f"{'pytest':20} {'1.1.1'}"
383+
384+
385+
def test_mock_no_environment(tmp_path):
386+
"""Mock lack of any environment,
387+
and test that packages are logged correctly.
388+
"""
389+
390+
# Simulated `pip list --json` output
391+
fake_pip_output = json.dumps(
392+
[
393+
{"name": "fancylog", "version": "1.1.1", "location": "fake_env"},
394+
{"name": "pytest", "version": "1.1.1", "location": "global_env"},
395+
]
396+
)
397+
398+
# Patch the environment and subprocess
399+
with (
400+
patch.dict(os.environ, {}, clear=False),
401+
patch("os.getenv") as mock_getenv,
402+
patch("subprocess.run") as mock_run,
403+
):
404+
# Eliminate conda environment packages triggers logging pip list
405+
os.environ.pop("CONDA_PREFIX", None)
406+
os.environ.pop("CONDA_EXE", None)
407+
408+
# Mock lack of any local environment
409+
mock_getenv.return_value = None
410+
411+
# Mocked subprocess result
412+
mock_run.return_value = MagicMock(stdout=fake_pip_output, returncode=0)
413+
414+
fancylog.start_logging(tmp_path, fancylog, write_env_packages=True)
415+
416+
log_file = next(tmp_path.glob("*.log"))
417+
418+
# Log contains conda subheaders and mocked pkgs versions
419+
with open(log_file) as file:
420+
file_content = file.read()
421+
422+
assert (
423+
"No environment found, reporting global pip packages"
424+
) in file_content
425+
426+
assert f"{'fancylog':20} {'1.1.1'}"
427+
assert f"{'pytest':20} {'1.1.1'}"

0 commit comments

Comments
 (0)