ยปCore Development>Code coverage>Lib/test/test_ensurepip.py

Python code coverage for Lib/test/test_ensurepip.py

#countcontent
1n/aimport unittest
2n/aimport unittest.mock
3n/aimport test.support
4n/aimport os
5n/aimport os.path
6n/aimport contextlib
7n/aimport sys
8n/a
9n/aimport ensurepip
10n/aimport ensurepip._uninstall
11n/a
12n/a
13n/aclass TestEnsurePipVersion(unittest.TestCase):
14n/a
15n/a def test_returns_version(self):
16n/a self.assertEqual(ensurepip._PIP_VERSION, ensurepip.version())
17n/a
18n/aclass EnsurepipMixin:
19n/a
20n/a def setUp(self):
21n/a run_pip_patch = unittest.mock.patch("ensurepip._run_pip")
22n/a self.run_pip = run_pip_patch.start()
23n/a self.addCleanup(run_pip_patch.stop)
24n/a
25n/a # Avoid side effects on the actual os module
26n/a real_devnull = os.devnull
27n/a os_patch = unittest.mock.patch("ensurepip.os")
28n/a patched_os = os_patch.start()
29n/a self.addCleanup(os_patch.stop)
30n/a patched_os.devnull = real_devnull
31n/a patched_os.path = os.path
32n/a self.os_environ = patched_os.environ = os.environ.copy()
33n/a
34n/a
35n/aclass TestBootstrap(EnsurepipMixin, unittest.TestCase):
36n/a
37n/a def test_basic_bootstrapping(self):
38n/a ensurepip.bootstrap()
39n/a
40n/a self.run_pip.assert_called_once_with(
41n/a [
42n/a "install", "--no-index", "--find-links",
43n/a unittest.mock.ANY, "setuptools", "pip",
44n/a ],
45n/a unittest.mock.ANY,
46n/a )
47n/a
48n/a additional_paths = self.run_pip.call_args[0][1]
49n/a self.assertEqual(len(additional_paths), 2)
50n/a
51n/a def test_bootstrapping_with_root(self):
52n/a ensurepip.bootstrap(root="/foo/bar/")
53n/a
54n/a self.run_pip.assert_called_once_with(
55n/a [
56n/a "install", "--no-index", "--find-links",
57n/a unittest.mock.ANY, "--root", "/foo/bar/",
58n/a "setuptools", "pip",
59n/a ],
60n/a unittest.mock.ANY,
61n/a )
62n/a
63n/a def test_bootstrapping_with_user(self):
64n/a ensurepip.bootstrap(user=True)
65n/a
66n/a self.run_pip.assert_called_once_with(
67n/a [
68n/a "install", "--no-index", "--find-links",
69n/a unittest.mock.ANY, "--user", "setuptools", "pip",
70n/a ],
71n/a unittest.mock.ANY,
72n/a )
73n/a
74n/a def test_bootstrapping_with_upgrade(self):
75n/a ensurepip.bootstrap(upgrade=True)
76n/a
77n/a self.run_pip.assert_called_once_with(
78n/a [
79n/a "install", "--no-index", "--find-links",
80n/a unittest.mock.ANY, "--upgrade", "setuptools", "pip",
81n/a ],
82n/a unittest.mock.ANY,
83n/a )
84n/a
85n/a def test_bootstrapping_with_verbosity_1(self):
86n/a ensurepip.bootstrap(verbosity=1)
87n/a
88n/a self.run_pip.assert_called_once_with(
89n/a [
90n/a "install", "--no-index", "--find-links",
91n/a unittest.mock.ANY, "-v", "setuptools", "pip",
92n/a ],
93n/a unittest.mock.ANY,
94n/a )
95n/a
96n/a def test_bootstrapping_with_verbosity_2(self):
97n/a ensurepip.bootstrap(verbosity=2)
98n/a
99n/a self.run_pip.assert_called_once_with(
100n/a [
101n/a "install", "--no-index", "--find-links",
102n/a unittest.mock.ANY, "-vv", "setuptools", "pip",
103n/a ],
104n/a unittest.mock.ANY,
105n/a )
106n/a
107n/a def test_bootstrapping_with_verbosity_3(self):
108n/a ensurepip.bootstrap(verbosity=3)
109n/a
110n/a self.run_pip.assert_called_once_with(
111n/a [
112n/a "install", "--no-index", "--find-links",
113n/a unittest.mock.ANY, "-vvv", "setuptools", "pip",
114n/a ],
115n/a unittest.mock.ANY,
116n/a )
117n/a
118n/a def test_bootstrapping_with_regular_install(self):
119n/a ensurepip.bootstrap()
120n/a self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "install")
121n/a
122n/a def test_bootstrapping_with_alt_install(self):
123n/a ensurepip.bootstrap(altinstall=True)
124n/a self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "altinstall")
125n/a
126n/a def test_bootstrapping_with_default_pip(self):
127n/a ensurepip.bootstrap(default_pip=True)
128n/a self.assertNotIn("ENSUREPIP_OPTIONS", self.os_environ)
129n/a
130n/a def test_altinstall_default_pip_conflict(self):
131n/a with self.assertRaises(ValueError):
132n/a ensurepip.bootstrap(altinstall=True, default_pip=True)
133n/a self.assertFalse(self.run_pip.called)
134n/a
135n/a def test_pip_environment_variables_removed(self):
136n/a # ensurepip deliberately ignores all pip environment variables
137n/a # See http://bugs.python.org/issue19734 for details
138n/a self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder"
139n/a ensurepip.bootstrap()
140n/a self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
141n/a
142n/a def test_pip_config_file_disabled(self):
143n/a # ensurepip deliberately ignores the pip config file
144n/a # See http://bugs.python.org/issue20053 for details
145n/a ensurepip.bootstrap()
146n/a self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)
147n/a
148n/a@contextlib.contextmanager
149n/adef fake_pip(version=ensurepip._PIP_VERSION):
150n/a if version is None:
151n/a pip = None
152n/a else:
153n/a class FakePip():
154n/a __version__ = version
155n/a pip = FakePip()
156n/a sentinel = object()
157n/a orig_pip = sys.modules.get("pip", sentinel)
158n/a sys.modules["pip"] = pip
159n/a try:
160n/a yield pip
161n/a finally:
162n/a if orig_pip is sentinel:
163n/a del sys.modules["pip"]
164n/a else:
165n/a sys.modules["pip"] = orig_pip
166n/a
167n/aclass TestUninstall(EnsurepipMixin, unittest.TestCase):
168n/a
169n/a def test_uninstall_skipped_when_not_installed(self):
170n/a with fake_pip(None):
171n/a ensurepip._uninstall_helper()
172n/a self.assertFalse(self.run_pip.called)
173n/a
174n/a def test_uninstall_skipped_with_warning_for_wrong_version(self):
175n/a with fake_pip("not a valid version"):
176n/a with test.support.captured_stderr() as stderr:
177n/a ensurepip._uninstall_helper()
178n/a warning = stderr.getvalue().strip()
179n/a self.assertIn("only uninstall a matching version", warning)
180n/a self.assertFalse(self.run_pip.called)
181n/a
182n/a
183n/a def test_uninstall(self):
184n/a with fake_pip():
185n/a ensurepip._uninstall_helper()
186n/a
187n/a self.run_pip.assert_called_once_with(
188n/a [
189n/a "uninstall", "-y", "--disable-pip-version-check", "pip",
190n/a "setuptools",
191n/a ]
192n/a )
193n/a
194n/a def test_uninstall_with_verbosity_1(self):
195n/a with fake_pip():
196n/a ensurepip._uninstall_helper(verbosity=1)
197n/a
198n/a self.run_pip.assert_called_once_with(
199n/a [
200n/a "uninstall", "-y", "--disable-pip-version-check", "-v", "pip",
201n/a "setuptools",
202n/a ]
203n/a )
204n/a
205n/a def test_uninstall_with_verbosity_2(self):
206n/a with fake_pip():
207n/a ensurepip._uninstall_helper(verbosity=2)
208n/a
209n/a self.run_pip.assert_called_once_with(
210n/a [
211n/a "uninstall", "-y", "--disable-pip-version-check", "-vv", "pip",
212n/a "setuptools",
213n/a ]
214n/a )
215n/a
216n/a def test_uninstall_with_verbosity_3(self):
217n/a with fake_pip():
218n/a ensurepip._uninstall_helper(verbosity=3)
219n/a
220n/a self.run_pip.assert_called_once_with(
221n/a [
222n/a "uninstall", "-y", "--disable-pip-version-check", "-vvv",
223n/a "pip", "setuptools",
224n/a ]
225n/a )
226n/a
227n/a def test_pip_environment_variables_removed(self):
228n/a # ensurepip deliberately ignores all pip environment variables
229n/a # See http://bugs.python.org/issue19734 for details
230n/a self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder"
231n/a with fake_pip():
232n/a ensurepip._uninstall_helper()
233n/a self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
234n/a
235n/a def test_pip_config_file_disabled(self):
236n/a # ensurepip deliberately ignores the pip config file
237n/a # See http://bugs.python.org/issue20053 for details
238n/a with fake_pip():
239n/a ensurepip._uninstall_helper()
240n/a self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)
241n/a
242n/a
243n/a# Basic testing of the main functions and their argument parsing
244n/a
245n/aEXPECTED_VERSION_OUTPUT = "pip " + ensurepip._PIP_VERSION
246n/a
247n/aclass TestBootstrappingMainFunction(EnsurepipMixin, unittest.TestCase):
248n/a
249n/a def test_bootstrap_version(self):
250n/a with test.support.captured_stdout() as stdout:
251n/a with self.assertRaises(SystemExit):
252n/a ensurepip._main(["--version"])
253n/a result = stdout.getvalue().strip()
254n/a self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
255n/a self.assertFalse(self.run_pip.called)
256n/a
257n/a def test_basic_bootstrapping(self):
258n/a ensurepip._main([])
259n/a
260n/a self.run_pip.assert_called_once_with(
261n/a [
262n/a "install", "--no-index", "--find-links",
263n/a unittest.mock.ANY, "setuptools", "pip",
264n/a ],
265n/a unittest.mock.ANY,
266n/a )
267n/a
268n/a additional_paths = self.run_pip.call_args[0][1]
269n/a self.assertEqual(len(additional_paths), 2)
270n/a
271n/aclass TestUninstallationMainFunction(EnsurepipMixin, unittest.TestCase):
272n/a
273n/a def test_uninstall_version(self):
274n/a with test.support.captured_stdout() as stdout:
275n/a with self.assertRaises(SystemExit):
276n/a ensurepip._uninstall._main(["--version"])
277n/a result = stdout.getvalue().strip()
278n/a self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
279n/a self.assertFalse(self.run_pip.called)
280n/a
281n/a def test_basic_uninstall(self):
282n/a with fake_pip():
283n/a ensurepip._uninstall._main([])
284n/a
285n/a self.run_pip.assert_called_once_with(
286n/a [
287n/a "uninstall", "-y", "--disable-pip-version-check", "pip",
288n/a "setuptools",
289n/a ]
290n/a )
291n/a
292n/a
293n/a
294n/aif __name__ == "__main__":
295n/a unittest.main()