Skip to content

Commit a6199d6

Browse files
committed
test: add tests for get_env_dir
- Implemented multiple test cases for the get_env_dir function to handle various scenarios including Python virtualenv with real_prefix, base_prefix, conda prefix, and VIRTUAL_ENV variable. - Added tests to ensure proper exit behavior when no virtualenv is available and to verify UTF-8 string encoding.
1 parent 694509f commit a6199d6

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

tests/nodeenv_test.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,128 @@ def test_uppercase_machine_amd64(self):
493493
'node-v18.0.0-win-x64.zip'
494494
)
495495
assert url == expected
496+
497+
498+
class TestGetEnvDir:
499+
"""Tests for get_env_dir function"""
500+
501+
def test_with_python_virtualenv_real_prefix(self):
502+
"""Test get_env_dir when using python virtualenv with real_prefix"""
503+
args = mock.Mock()
504+
args.python_virtualenv = True
505+
test_prefix = '/path/to/virtualenv'
506+
507+
with mock.patch.object(sys, 'real_prefix', test_prefix, create=True), \
508+
mock.patch.object(sys, 'prefix', test_prefix):
509+
result = nodeenv.get_env_dir(args)
510+
assert result == test_prefix
511+
512+
def test_with_python_virtualenv_base_prefix(self):
513+
"""Test get_env_dir when using python virtualenv with base_prefix"""
514+
args = mock.Mock()
515+
args.python_virtualenv = True
516+
test_prefix = '/path/to/virtualenv'
517+
test_base_prefix = '/usr'
518+
519+
# Remove real_prefix if it exists
520+
if hasattr(sys, 'real_prefix'):
521+
with mock.patch.object(sys, 'real_prefix', create=False):
522+
with mock.patch.object(sys, 'prefix', test_prefix), \
523+
mock.patch.object(sys, 'base_prefix', test_base_prefix):
524+
result = nodeenv.get_env_dir(args)
525+
assert result == test_prefix
526+
else:
527+
with mock.patch.object(sys, 'prefix', test_prefix), \
528+
mock.patch.object(sys, 'base_prefix', test_base_prefix):
529+
result = nodeenv.get_env_dir(args)
530+
assert result == test_prefix
531+
532+
def test_with_python_virtualenv_conda_prefix(self):
533+
"""Test get_env_dir when using conda environment"""
534+
args = mock.Mock()
535+
args.python_virtualenv = True
536+
test_prefix = '/path/to/conda/env'
537+
538+
# Remove real_prefix if it exists
539+
if hasattr(sys, 'real_prefix'):
540+
with mock.patch.object(sys, 'real_prefix', create=False):
541+
env_dict = {'CONDA_PREFIX': test_prefix}
542+
with mock.patch.object(sys, 'prefix', test_prefix), \
543+
mock.patch.object(sys, 'base_prefix', test_prefix), \
544+
mock.patch.dict(os.environ, env_dict):
545+
result = nodeenv.get_env_dir(args)
546+
assert result == test_prefix
547+
else:
548+
env_dict = {'CONDA_PREFIX': test_prefix}
549+
with mock.patch.object(sys, 'prefix', test_prefix), \
550+
mock.patch.object(sys, 'base_prefix', test_prefix), \
551+
mock.patch.dict(os.environ, env_dict):
552+
result = nodeenv.get_env_dir(args)
553+
assert result == test_prefix
554+
555+
def test_with_python_virtualenv_virtual_env(self):
556+
"""Test get_env_dir when using VIRTUAL_ENV variable"""
557+
args = mock.Mock()
558+
args.python_virtualenv = True
559+
test_prefix = '/path/to/venv'
560+
virtual_env = '/path/to/virtual/env'
561+
562+
# Remove real_prefix if it exists
563+
if hasattr(sys, 'real_prefix'):
564+
with mock.patch.object(sys, 'real_prefix', create=False):
565+
env_dict = {'VIRTUAL_ENV': virtual_env}
566+
with mock.patch.object(sys, 'prefix', test_prefix), \
567+
mock.patch.object(sys, 'base_prefix', test_prefix), \
568+
mock.patch.dict(os.environ, env_dict, clear=True):
569+
result = nodeenv.get_env_dir(args)
570+
assert result == virtual_env
571+
else:
572+
env_dict = {'VIRTUAL_ENV': virtual_env}
573+
with mock.patch.object(sys, 'prefix', test_prefix), \
574+
mock.patch.object(sys, 'base_prefix', test_prefix), \
575+
mock.patch.dict(os.environ, env_dict, clear=True):
576+
result = nodeenv.get_env_dir(args)
577+
assert result == virtual_env
578+
579+
def test_with_python_virtualenv_no_virtualenv_exits(self):
580+
"""Test get_env_dir exits when no virtualenv is available"""
581+
args = mock.Mock()
582+
args.python_virtualenv = True
583+
test_prefix = '/usr'
584+
585+
# Remove real_prefix if it exists
586+
if hasattr(sys, 'real_prefix'):
587+
with mock.patch.object(sys, 'real_prefix', create=False):
588+
with mock.patch.object(sys, 'prefix', test_prefix), \
589+
mock.patch.object(sys, 'base_prefix', test_prefix), \
590+
mock.patch.dict(os.environ, {}, clear=True), \
591+
pytest.raises(SystemExit) as exc_info:
592+
nodeenv.get_env_dir(args)
593+
assert exc_info.value.code == 2
594+
else:
595+
with mock.patch.object(sys, 'prefix', test_prefix), \
596+
mock.patch.object(sys, 'base_prefix', test_prefix), \
597+
mock.patch.dict(os.environ, {}, clear=True), \
598+
pytest.raises(SystemExit) as exc_info:
599+
nodeenv.get_env_dir(args)
600+
assert exc_info.value.code == 2
601+
602+
def test_without_python_virtualenv(self):
603+
"""Test get_env_dir when not using python virtualenv"""
604+
args = mock.Mock()
605+
args.python_virtualenv = False
606+
args.env_dir = '/path/to/node/env'
607+
608+
result = nodeenv.get_env_dir(args)
609+
assert result == '/path/to/node/env'
610+
611+
def test_returns_utf8_encoded_string(self):
612+
"""Test that get_env_dir returns UTF-8 encoded string"""
613+
args = mock.Mock()
614+
args.python_virtualenv = False
615+
args.env_dir = '/path/to/env'
616+
617+
result = nodeenv.get_env_dir(args)
618+
# The to_utf8 function is applied,
619+
# but in Python 3 it returns the same string
620+
assert result == '/path/to/env'

0 commit comments

Comments
 (0)