@@ -297,7 +297,7 @@ def test_mock_pip_pkgs(tmp_path):
297
297
and test that packages are logged correctly.
298
298
"""
299
299
300
- # Simulated `conda list --json` output
300
+ # Simulated `pip list --json` output
301
301
fake_pip_output = json .dumps (
302
302
[
303
303
{"name" : "fancylog" , "version" : "1.1.1" , "location" : "fake_env" },
@@ -312,7 +312,7 @@ def test_mock_pip_pkgs(tmp_path):
312
312
patch ("subprocess.run" ) as mock_run ,
313
313
):
314
314
# Eliminate conda environment packages triggers logging pip list
315
- ( os .environ .pop ("CONDA_PREFIX" , None ), )
315
+ os .environ .pop ("CONDA_PREFIX" , None )
316
316
os .environ .pop ("CONDA_EXE" , None )
317
317
318
318
mock_getenv .return_value = "fake_env"
@@ -380,3 +380,48 @@ def test_mock_conda_pkgs(tmp_path):
380
380
assert "Environment packages (conda):" in file_content
381
381
assert f"{ 'fancylog' :20} { '1.1.1' } "
382
382
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