11import unittest
22import unittest .mock
3- import ensurepip
43import test .support
54import os
65import os .path
76import contextlib
87import sys
98
9+ import ensurepip
10+ import ensurepip ._uninstall
1011
1112class TestEnsurePipVersion (unittest .TestCase ):
1213
1314 def test_returns_version (self ):
1415 self .assertEqual (ensurepip ._PIP_VERSION , ensurepip .version ())
1516
16-
17- class TestBootstrap (unittest .TestCase ):
17+ class EnsurepipMixin :
1818
1919 def setUp (self ):
2020 run_pip_patch = unittest .mock .patch ("ensurepip._run_pip" )
@@ -28,6 +28,8 @@ def setUp(self):
2828 patched_os .path = os .path
2929 self .os_environ = patched_os .environ = os .environ .copy ()
3030
31+ class TestBootstrap (EnsurepipMixin , unittest .TestCase ):
32+
3133 def test_basic_bootstrapping (self ):
3234 ensurepip .bootstrap ()
3335
@@ -153,59 +155,47 @@ class FakePip():
153155 else :
154156 sys .modules ["pip" ] = orig_pip
155157
156- class TestUninstall (unittest .TestCase ):
157-
158- def setUp (self ):
159- run_pip_patch = unittest .mock .patch ("ensurepip._run_pip" )
160- self .run_pip = run_pip_patch .start ()
161- self .addCleanup (run_pip_patch .stop )
162-
163- # Avoid side effects on the actual os module
164- os_patch = unittest .mock .patch ("ensurepip.os" )
165- patched_os = os_patch .start ()
166- self .addCleanup (os_patch .stop )
167- patched_os .path = os .path
168- self .os_environ = patched_os .environ = os .environ .copy ()
158+ class TestUninstall (EnsurepipMixin , unittest .TestCase ):
169159
170160 def test_uninstall_skipped_when_not_installed (self ):
171161 with fake_pip (None ):
172- ensurepip ._uninstall ()
162+ ensurepip ._uninstall_helper ()
173163 self .run_pip .assert_not_called ()
174164
175165 def test_uninstall_fails_with_wrong_version (self ):
176166 with fake_pip ("not a valid version" ):
177167 with self .assertRaises (RuntimeError ):
178- ensurepip ._uninstall ()
168+ ensurepip ._uninstall_helper ()
179169 self .run_pip .assert_not_called ()
180170
181171
182172 def test_uninstall (self ):
183173 with fake_pip ():
184- ensurepip ._uninstall ()
174+ ensurepip ._uninstall_helper ()
185175
186176 self .run_pip .assert_called_once_with (
187177 ["uninstall" , "-y" , "pip" , "setuptools" ]
188178 )
189179
190180 def test_uninstall_with_verbosity_1 (self ):
191181 with fake_pip ():
192- ensurepip ._uninstall (verbosity = 1 )
182+ ensurepip ._uninstall_helper (verbosity = 1 )
193183
194184 self .run_pip .assert_called_once_with (
195185 ["uninstall" , "-y" , "-v" , "pip" , "setuptools" ]
196186 )
197187
198188 def test_uninstall_with_verbosity_2 (self ):
199189 with fake_pip ():
200- ensurepip ._uninstall (verbosity = 2 )
190+ ensurepip ._uninstall_helper (verbosity = 2 )
201191
202192 self .run_pip .assert_called_once_with (
203193 ["uninstall" , "-y" , "-vv" , "pip" , "setuptools" ]
204194 )
205195
206196 def test_uninstall_with_verbosity_3 (self ):
207197 with fake_pip ():
208- ensurepip ._uninstall (verbosity = 3 )
198+ ensurepip ._uninstall_helper (verbosity = 3 )
209199
210200 self .run_pip .assert_called_once_with (
211201 ["uninstall" , "-y" , "-vvv" , "pip" , "setuptools" ]
@@ -216,10 +206,57 @@ def test_pip_environment_variables_removed(self):
216206 # See http://bugs.python.org/issue19734 for details
217207 self .os_environ ["PIP_THIS_SHOULD_GO_AWAY" ] = "test fodder"
218208 with fake_pip ():
219- ensurepip ._uninstall ()
209+ ensurepip ._uninstall_helper ()
220210 self .assertNotIn ("PIP_THIS_SHOULD_GO_AWAY" , self .os_environ )
221211
222212
213+ # Basic testing of the main functions and their argument parsing
214+
215+ EXPECTED_VERSION_OUTPUT = "pip " + ensurepip ._PIP_VERSION
216+
217+ class TestBootstrappingMainFunction (EnsurepipMixin , unittest .TestCase ):
218+
219+ def test_bootstrap_version (self ):
220+ with test .support .captured_stdout () as stdout :
221+ with self .assertRaises (SystemExit ):
222+ ensurepip ._main (["--version" ])
223+ result = stdout .getvalue ().strip ()
224+ self .assertEqual (result , EXPECTED_VERSION_OUTPUT )
225+ self .run_pip .assert_not_called ()
226+
227+ def test_basic_bootstrapping (self ):
228+ ensurepip ._main ([])
229+
230+ self .run_pip .assert_called_once_with (
231+ [
232+ "install" , "--no-index" , "--find-links" ,
233+ unittest .mock .ANY , "--pre" , "setuptools" , "pip" ,
234+ ],
235+ unittest .mock .ANY ,
236+ )
237+
238+ additional_paths = self .run_pip .call_args [0 ][1 ]
239+ self .assertEqual (len (additional_paths ), 2 )
240+
241+ class TestUninstallationMainFunction (EnsurepipMixin , unittest .TestCase ):
242+
243+ def test_uninstall_version (self ):
244+ with test .support .captured_stdout () as stdout :
245+ with self .assertRaises (SystemExit ):
246+ ensurepip ._uninstall ._main (["--version" ])
247+ result = stdout .getvalue ().strip ()
248+ self .assertEqual (result , EXPECTED_VERSION_OUTPUT )
249+ self .run_pip .assert_not_called ()
250+
251+ def test_basic_uninstall (self ):
252+ with fake_pip ():
253+ ensurepip ._uninstall ._main ([])
254+
255+ self .run_pip .assert_called_once_with (
256+ ["uninstall" , "-y" , "pip" , "setuptools" ]
257+ )
258+
259+
223260
224261if __name__ == "__main__" :
225262 test .support .run_unittest (__name__ )
0 commit comments