22
33import subprocess
44import sys
5+ import os
56from test .support import script_helper
67import unittest
78from unittest import mock
@@ -73,7 +74,7 @@ class TestScriptHelperEnvironment(unittest.TestCase):
7374
7475 def setUp (self ):
7576 self .assertTrue (
76- hasattr (script_helper , '__cached_interp_requires_environment' ))
77+ hasattr (script_helper , '__cached_interp_requires_environment' ))
7778 # Reset the private cached state.
7879 script_helper .__dict__ ['__cached_interp_requires_environment' ] = None
7980
@@ -83,27 +84,41 @@ def tearDown(self):
8384
8485 @mock .patch ('subprocess.check_call' )
8586 def test_interpreter_requires_environment_true (self , mock_check_call ):
86- mock_check_call .side_effect = subprocess .CalledProcessError ('' , '' )
87- self .assertTrue (script_helper .interpreter_requires_environment ())
88- self .assertTrue (script_helper .interpreter_requires_environment ())
89- self .assertEqual (1 , mock_check_call .call_count )
87+ with mock .patch .dict (os .environ ):
88+ os .environ .pop ('PYTHONHOME' , None )
89+ mock_check_call .side_effect = subprocess .CalledProcessError ('' , '' )
90+ self .assertTrue (script_helper .interpreter_requires_environment ())
91+ self .assertTrue (script_helper .interpreter_requires_environment ())
92+ self .assertEqual (1 , mock_check_call .call_count )
9093
9194 @mock .patch ('subprocess.check_call' )
9295 def test_interpreter_requires_environment_false (self , mock_check_call ):
93- # The mocked subprocess.check_call fakes a no-error process.
94- script_helper .interpreter_requires_environment ()
95- self .assertFalse (script_helper .interpreter_requires_environment ())
96- self .assertEqual (1 , mock_check_call .call_count )
96+ with mock .patch .dict (os .environ ):
97+ os .environ .pop ('PYTHONHOME' , None )
98+ # The mocked subprocess.check_call fakes a no-error process.
99+ script_helper .interpreter_requires_environment ()
100+ self .assertFalse (script_helper .interpreter_requires_environment ())
101+ self .assertEqual (1 , mock_check_call .call_count )
97102
98103 @mock .patch ('subprocess.check_call' )
99104 def test_interpreter_requires_environment_details (self , mock_check_call ):
100- script_helper .interpreter_requires_environment ()
101- self .assertFalse (script_helper .interpreter_requires_environment ())
102- self .assertFalse (script_helper .interpreter_requires_environment ())
103- self .assertEqual (1 , mock_check_call .call_count )
104- check_call_command = mock_check_call .call_args [0 ][0 ]
105- self .assertEqual (sys .executable , check_call_command [0 ])
106- self .assertIn ('-E' , check_call_command )
105+ with mock .patch .dict (os .environ ):
106+ os .environ .pop ('PYTHONHOME' , None )
107+ script_helper .interpreter_requires_environment ()
108+ self .assertFalse (script_helper .interpreter_requires_environment ())
109+ self .assertFalse (script_helper .interpreter_requires_environment ())
110+ self .assertEqual (1 , mock_check_call .call_count )
111+ check_call_command = mock_check_call .call_args [0 ][0 ]
112+ self .assertEqual (sys .executable , check_call_command [0 ])
113+ self .assertIn ('-E' , check_call_command )
114+
115+ @mock .patch ('subprocess.check_call' )
116+ def test_interpreter_requires_environment_with_pythonhome (self , mock_check_call ):
117+ with mock .patch .dict (os .environ ):
118+ os .environ ['PYTHONHOME' ] = 'MockedHome'
119+ self .assertTrue (script_helper .interpreter_requires_environment ())
120+ self .assertTrue (script_helper .interpreter_requires_environment ())
121+ self .assertEqual (0 , mock_check_call .call_count )
107122
108123
109124if __name__ == '__main__' :
0 commit comments