@@ -698,6 +698,18 @@ def test_parse_args_prefer_system():
698698 assert nodeenv .parse_args ().prefer_system is False
699699
700700
701+ def test_parse_args_python_virtualenv ():
702+ with mock .patch .object (sys , 'argv' , ['nodeenv' , '-p' ]):
703+ assert nodeenv .parse_args ().python_virtualenv is True
704+ with mock .patch .object (sys , 'argv' , ['nodeenv' , '-p' , 'venv' ]):
705+ assert nodeenv .parse_args ().python_virtualenv == 'venv'
706+ with mock .patch .object (
707+ sys , 'argv' , ['nodeenv' , '--python-virtualenv=venv' ]):
708+ assert nodeenv .parse_args ().python_virtualenv == 'venv'
709+ with mock .patch .object (sys , 'argv' , ['nodeenv' , 'env' ]):
710+ assert nodeenv .parse_args ().python_virtualenv is False
711+
712+
701713def test_isolate_npm_default ():
702714 assert nodeenv .Config ._default ['isolate_npm' ] is False
703715
@@ -1622,7 +1634,8 @@ def test_with_python_virtualenv_real_prefix(self):
16221634 test_prefix = '/path/to/virtualenv'
16231635
16241636 with mock .patch .object (sys , 'real_prefix' , test_prefix , create = True ), \
1625- mock .patch .object (sys , 'prefix' , test_prefix ):
1637+ mock .patch .object (sys , 'prefix' , test_prefix ), \
1638+ mock .patch .dict (os .environ , {}, clear = True ):
16261639 result = nodeenv .get_env_dir (args )
16271640 assert result == test_prefix
16281641
@@ -1637,12 +1650,14 @@ def test_with_python_virtualenv_base_prefix(self):
16371650 if hasattr (sys , 'real_prefix' ):
16381651 with mock .patch .object (sys , 'real_prefix' , create = False ):
16391652 with mock .patch .object (sys , 'prefix' , test_prefix ), \
1640- mock .patch .object (sys , 'base_prefix' , test_base_prefix ):
1653+ mock .patch .object (sys , 'base_prefix' , test_base_prefix ), \
1654+ mock .patch .dict (os .environ , {}, clear = True ):
16411655 result = nodeenv .get_env_dir (args )
16421656 assert result == test_prefix
16431657 else :
16441658 with mock .patch .object (sys , 'prefix' , test_prefix ), \
1645- mock .patch .object (sys , 'base_prefix' , test_base_prefix ):
1659+ mock .patch .object (sys , 'base_prefix' , test_base_prefix ), \
1660+ mock .patch .dict (os .environ , {}, clear = True ):
16461661 result = nodeenv .get_env_dir (args )
16471662 assert result == test_prefix
16481663
@@ -1658,14 +1673,14 @@ def test_with_python_virtualenv_conda_prefix(self):
16581673 env_dict = {'CONDA_PREFIX' : test_prefix }
16591674 with mock .patch .object (sys , 'prefix' , test_prefix ), \
16601675 mock .patch .object (sys , 'base_prefix' , test_prefix ), \
1661- mock .patch .dict (os .environ , env_dict ):
1676+ mock .patch .dict (os .environ , env_dict , clear = True ):
16621677 result = nodeenv .get_env_dir (args )
16631678 assert result == test_prefix
16641679 else :
16651680 env_dict = {'CONDA_PREFIX' : test_prefix }
16661681 with mock .patch .object (sys , 'prefix' , test_prefix ), \
16671682 mock .patch .object (sys , 'base_prefix' , test_prefix ), \
1668- mock .patch .dict (os .environ , env_dict ):
1683+ mock .patch .dict (os .environ , env_dict , clear = True ):
16691684 result = nodeenv .get_env_dir (args )
16701685 assert result == test_prefix
16711686
@@ -1716,6 +1731,74 @@ def test_with_python_virtualenv_no_virtualenv_exits(self):
17161731 nodeenv .get_env_dir (args )
17171732 assert exc_info .value .code == 2
17181733
1734+ def test_with_python_virtualenv_dir (self , tmpdir ):
1735+ """Test get_env_dir when a virtualenv directory is given"""
1736+ args = mock .Mock ()
1737+ args .python_virtualenv = str (tmpdir )
1738+
1739+ env_dict = {'VIRTUAL_ENV' : '/path/to/other/venv' }
1740+ with mock .patch .dict (os .environ , env_dict , clear = True ):
1741+ result = nodeenv .get_env_dir (args )
1742+ assert result == str (tmpdir )
1743+
1744+ def test_with_python_virtualenv_missing_dir_exits (self , tmpdir ):
1745+ """Test get_env_dir exits when the given virtualenv doesn't exist"""
1746+ args = mock .Mock ()
1747+ args .python_virtualenv = str (tmpdir .join ('missing' ))
1748+
1749+ with pytest .raises (SystemExit ) as exc_info :
1750+ nodeenv .get_env_dir (args )
1751+ assert exc_info .value .code == 2
1752+
1753+ def test_with_python_virtualenv_prefers_virtual_env (self ):
1754+ """Test get_env_dir prefers VIRTUAL_ENV over nodeenv's own venv"""
1755+ args = mock .Mock ()
1756+ args .python_virtualenv = True
1757+ # nodeenv itself is installed into its own virtualenv
1758+ test_prefix = '/path/to/nodeenv/venv'
1759+ virtual_env = '/path/to/activated/venv'
1760+
1761+ env_dict = {'VIRTUAL_ENV' : virtual_env }
1762+ with mock .patch .object (sys , 'real_prefix' , test_prefix , create = True ), \
1763+ mock .patch .object (sys , 'prefix' , test_prefix ), \
1764+ mock .patch .dict (os .environ , env_dict , clear = True ), \
1765+ mock .patch .object (nodeenv .logger , 'warning' ) as mck :
1766+ result = nodeenv .get_env_dir (args )
1767+ assert result == virtual_env
1768+ # the ignored virtualenv is not silently dropped
1769+ assert mck .call_count == 1
1770+ assert mck .call_args [0 ][1 :] == (virtual_env , test_prefix )
1771+
1772+ def test_with_python_virtualenv_same_venv_is_quiet (self ):
1773+ """Test get_env_dir doesn't warn when both point to the same venv"""
1774+ args = mock .Mock ()
1775+ args .python_virtualenv = True
1776+ test_prefix = '/path/to/venv'
1777+
1778+ env_dict = {'VIRTUAL_ENV' : test_prefix }
1779+ with mock .patch .object (sys , 'real_prefix' , test_prefix , create = True ), \
1780+ mock .patch .object (sys , 'prefix' , test_prefix ), \
1781+ mock .patch .dict (os .environ , env_dict , clear = True ), \
1782+ mock .patch .object (nodeenv .logger , 'warning' ) as mck :
1783+ result = nodeenv .get_env_dir (args )
1784+ assert result == test_prefix
1785+ mck .assert_not_called ()
1786+
1787+ def test_with_python_virtualenv_system_python_is_quiet (self ):
1788+ """Test get_env_dir doesn't warn when nodeenv runs system-wide"""
1789+ args = mock .Mock ()
1790+ args .python_virtualenv = True
1791+ virtual_env = '/path/to/activated/venv'
1792+
1793+ env_dict = {'VIRTUAL_ENV' : virtual_env }
1794+ with mock .patch .object (sys , 'prefix' , '/usr' ), \
1795+ mock .patch .object (sys , 'base_prefix' , '/usr' ), \
1796+ mock .patch .dict (os .environ , env_dict , clear = True ), \
1797+ mock .patch .object (nodeenv .logger , 'warning' ) as mck :
1798+ result = nodeenv .get_env_dir (args )
1799+ assert result == virtual_env
1800+ mck .assert_not_called ()
1801+
17191802 def test_without_python_virtualenv (self ):
17201803 """Test get_env_dir when not using python virtualenv"""
17211804 args = mock .Mock ()
0 commit comments