diff options
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_ensurepip.py | 75 | ||||
-rw-r--r-- | Lib/test/test_venv.py | 66 |
2 files changed, 125 insertions, 16 deletions
diff --git a/Lib/test/test_ensurepip.py b/Lib/test/test_ensurepip.py index abf00fd..c119327 100644 --- a/Lib/test/test_ensurepip.py +++ b/Lib/test/test_ensurepip.py @@ -4,6 +4,8 @@ import ensurepip import test.support import os import os.path +import contextlib +import sys class TestEnsurePipVersion(unittest.TestCase): @@ -122,6 +124,79 @@ class TestBootstrap(unittest.TestCase): def test_altinstall_default_pip_conflict(self): with self.assertRaises(ValueError): ensurepip.bootstrap(altinstall=True, default_pip=True) + self.run_pip.assert_not_called() + +@contextlib.contextmanager +def fake_pip(version=ensurepip._PIP_VERSION): + if version is None: + pip = None + else: + class FakePip(): + __version__ = version + pip = FakePip() + sentinel = object() + orig_pip = sys.modules.get("pip", sentinel) + sys.modules["pip"] = pip + try: + yield pip + finally: + if orig_pip is sentinel: + del sys.modules["pip"] + else: + sys.modules["pip"] = orig_pip + +class TestUninstall(unittest.TestCase): + + def setUp(self): + run_pip_patch = unittest.mock.patch("ensurepip._run_pip") + self.run_pip = run_pip_patch.start() + self.addCleanup(run_pip_patch.stop) + + def test_uninstall_skipped_when_not_installed(self): + with fake_pip(None): + ensurepip._uninstall() + self.run_pip.assert_not_called() + + def test_uninstall_fails_with_wrong_version(self): + with fake_pip("not a valid version"): + with self.assertRaises(RuntimeError): + ensurepip._uninstall() + self.run_pip.assert_not_called() + + + def test_uninstall(self): + with fake_pip(): + ensurepip._uninstall() + + self.run_pip.assert_called_once_with( + ["uninstall", "-y", "pip", "setuptools"] + ) + + def test_uninstall_with_verbosity_1(self): + with fake_pip(): + ensurepip._uninstall(verbosity=1) + + self.run_pip.assert_called_once_with( + ["uninstall", "-y", "-v", "pip", "setuptools"] + ) + + def test_uninstall_with_verbosity_2(self): + with fake_pip(): + ensurepip._uninstall(verbosity=2) + + self.run_pip.assert_called_once_with( + ["uninstall", "-y", "-vv", "pip", "setuptools"] + ) + + def test_uninstall_with_verbosity_3(self): + with fake_pip(): + ensurepip._uninstall(verbosity=3) + + self.run_pip.assert_called_once_with( + ["uninstall", "-y", "-vvv", "pip", "setuptools"] + ) + + if __name__ == "__main__": diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py index a92f492..c15610b 100644 --- a/Lib/test/test_venv.py +++ b/Lib/test/test_venv.py @@ -14,6 +14,7 @@ import sys import tempfile from test.support import (captured_stdout, captured_stderr, run_unittest, can_symlink, EnvironmentVarGuard) +import textwrap import unittest import venv try: @@ -258,30 +259,31 @@ class BasicTest(BaseTest): @skipInVenv class EnsurePipTest(BaseTest): """Test venv module installation of pip.""" - - def test_no_pip_by_default(self): - shutil.rmtree(self.env_dir) - self.run_with_capture(venv.create, self.env_dir) - envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) + def assert_pip_not_installed(self): + envpy = os.path.join(os.path.realpath(self.env_dir), + self.bindir, self.exe) try_import = 'try:\n import pip\nexcept ImportError:\n print("OK")' cmd = [envpy, '-c', try_import] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() - self.assertEqual(err, b"") - self.assertEqual(out.strip(), b"OK") + # We force everything to text, so unittest gives the detailed diff + # if we get unexpected results + err = err.decode("latin-1") # Force to text, prevent decoding errors + self.assertEqual(err, "") + out = out.decode("latin-1") # Force to text, prevent decoding errors + self.assertEqual(out.strip(), "OK") + + + def test_no_pip_by_default(self): + shutil.rmtree(self.env_dir) + self.run_with_capture(venv.create, self.env_dir) + self.assert_pip_not_installed() def test_explicit_no_pip(self): shutil.rmtree(self.env_dir) self.run_with_capture(venv.create, self.env_dir, with_pip=False) - envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) - try_import = 'try:\n import pip\nexcept ImportError:\n print("OK")' - cmd = [envpy, '-c', try_import] - p = subprocess.Popen(cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - out, err = p.communicate() - self.assertEqual(err, b"") - self.assertEqual(out.strip(), b"OK") + self.assert_pip_not_installed() # Temporary skip for http://bugs.python.org/issue19744 @unittest.skipIf(ssl is None, 'pip needs SSL support') @@ -293,7 +295,8 @@ class EnsurePipTest(BaseTest): # environment settings don't cause venv to fail. envvars["PYTHONWARNINGS"] = "e" # pip doesn't ignore environment variables when running in - # isolated mode, and we don't have an active virtualenv here + # isolated mode, and we don't have an active virtualenv here, + # we're relying on the native venv support in 3.3+ # See http://bugs.python.org/issue19734 for details del envvars["PIP_REQUIRE_VIRTUALENV"] try: @@ -304,6 +307,7 @@ class EnsurePipTest(BaseTest): details = exc.output.decode(errors="replace") msg = "{}\n\n**Subprocess Output**\n{}".format(exc, details) self.fail(msg) + # Ensure pip is available in the virtual environment envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) cmd = [envpy, '-Im', 'pip', '--version'] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, @@ -319,6 +323,36 @@ class EnsurePipTest(BaseTest): env_dir = os.fsencode(self.env_dir).decode("latin-1") self.assertIn(env_dir, out) + # http://bugs.python.org/issue19728 + # Check the private uninstall command provided for the Windows + # installers works (at least in a virtual environment) + cmd = [envpy, '-Im', 'ensurepip._uninstall'] + with EnvironmentVarGuard() as envvars: + # pip doesn't ignore environment variables when running in + # isolated mode, and we don't have an active virtualenv here, + # we're relying on the native venv support in 3.3+ + # See http://bugs.python.org/issue19734 for details + del envvars["PIP_REQUIRE_VIRTUALENV"] + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out, err = p.communicate() + # We force everything to text, so unittest gives the detailed diff + # if we get unexpected results + err = err.decode("latin-1") # Force to text, prevent decoding errors + self.assertEqual(err, "") + # Being really specific regarding the expected behaviour for the + # initial bundling phase in Python 3.4. If the output changes in + # future pip versions, this test can be relaxed a bit. + out = out.decode("latin-1") # Force to text, prevent decoding errors + expected_output = textwrap.dedent("""\ + Uninstalling pip: + Successfully uninstalled pip + Uninstalling setuptools: + Successfully uninstalled setuptools + """) + self.assertEqual(out, expected_output) + self.assert_pip_not_installed() + def test_main(): run_unittest(BasicTest, EnsurePipTest) |