blob: abf00fd1884712bc746a4e56d88e48e128614ab9 [file] [log] [blame]
Nick Coghland0cf0632013-11-11 22:11:55 +10001import unittest
2import unittest.mock
3import ensurepip
4import test.support
Nick Coghland3e83e22013-11-13 22:24:58 +10005import os
6import os.path
Nick Coghland0cf0632013-11-11 22:11:55 +10007
8
9class TestEnsurePipVersion(unittest.TestCase):
10
11 def test_returns_version(self):
12 self.assertEqual(ensurepip._PIP_VERSION, ensurepip.version())
13
14
15class TestBootstrap(unittest.TestCase):
16
17 def setUp(self):
18 run_pip_patch = unittest.mock.patch("ensurepip._run_pip")
19 self.run_pip = run_pip_patch.start()
20 self.addCleanup(run_pip_patch.stop)
21
Nick Coghland3e83e22013-11-13 22:24:58 +100022 # Avoid side effects on the actual os module
23 os_patch = unittest.mock.patch("ensurepip.os")
24 patched_os = os_patch.start()
25 self.addCleanup(os_patch.stop)
26 patched_os.path = os.path
27 self.os_environ = patched_os.environ = os.environ.copy()
Nick Coghland0cf0632013-11-11 22:11:55 +100028
29 def test_basic_bootstrapping(self):
30 ensurepip.bootstrap()
31
32 self.run_pip.assert_called_once_with(
33 [
34 "install", "--no-index", "--find-links",
35 unittest.mock.ANY, "--pre", "setuptools", "pip",
36 ],
37 unittest.mock.ANY,
38 )
39
40 additional_paths = self.run_pip.call_args[0][1]
41 self.assertEqual(len(additional_paths), 2)
42
43 def test_bootstrapping_with_root(self):
44 ensurepip.bootstrap(root="/foo/bar/")
45
46 self.run_pip.assert_called_once_with(
47 [
48 "install", "--no-index", "--find-links",
49 unittest.mock.ANY, "--pre", "--root", "/foo/bar/",
50 "setuptools", "pip",
51 ],
52 unittest.mock.ANY,
53 )
54
55 def test_bootstrapping_with_user(self):
56 ensurepip.bootstrap(user=True)
57
58 self.run_pip.assert_called_once_with(
59 [
60 "install", "--no-index", "--find-links",
61 unittest.mock.ANY, "--pre", "--user", "setuptools", "pip",
62 ],
63 unittest.mock.ANY,
64 )
65
66 def test_bootstrapping_with_upgrade(self):
67 ensurepip.bootstrap(upgrade=True)
68
69 self.run_pip.assert_called_once_with(
70 [
71 "install", "--no-index", "--find-links",
72 unittest.mock.ANY, "--pre", "--upgrade", "setuptools", "pip",
73 ],
74 unittest.mock.ANY,
75 )
76
77 def test_bootstrapping_with_verbosity_1(self):
78 ensurepip.bootstrap(verbosity=1)
79
80 self.run_pip.assert_called_once_with(
81 [
82 "install", "--no-index", "--find-links",
83 unittest.mock.ANY, "--pre", "-v", "setuptools", "pip",
84 ],
85 unittest.mock.ANY,
86 )
87
88 def test_bootstrapping_with_verbosity_2(self):
89 ensurepip.bootstrap(verbosity=2)
90
91 self.run_pip.assert_called_once_with(
92 [
93 "install", "--no-index", "--find-links",
94 unittest.mock.ANY, "--pre", "-vv", "setuptools", "pip",
95 ],
96 unittest.mock.ANY,
97 )
98
99 def test_bootstrapping_with_verbosity_3(self):
100 ensurepip.bootstrap(verbosity=3)
101
102 self.run_pip.assert_called_once_with(
103 [
104 "install", "--no-index", "--find-links",
105 unittest.mock.ANY, "--pre", "-vvv", "setuptools", "pip",
106 ],
107 unittest.mock.ANY,
108 )
109
110 def test_bootstrapping_with_regular_install(self):
111 ensurepip.bootstrap()
112 self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "install")
113
114 def test_bootstrapping_with_alt_install(self):
115 ensurepip.bootstrap(altinstall=True)
116 self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "altinstall")
117
118 def test_bootstrapping_with_default_pip(self):
119 ensurepip.bootstrap(default_pip=True)
120 self.assertNotIn("ENSUREPIP_OPTIONS", self.os_environ)
121
122 def test_altinstall_default_pip_conflict(self):
123 with self.assertRaises(ValueError):
124 ensurepip.bootstrap(altinstall=True, default_pip=True)
125
126
127if __name__ == "__main__":
128 test.support.run_unittest(__name__)