| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 66ff4c1 commit 7751436
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,7 @@ | |||
| 14 | 14 | import textwrap | |
| 15 | 15 | import time | |
| 16 | 16 | import unittest | |
| 17 | + import venv | ||
| 17 | 18 | ||
| 18 | 19 | import gitdb | |
| 19 | 20 | ||
@@ -36,6 +37,7 @@ | |||
| 36 | 37 | "with_rw_repo", | |
| 37 | 38 | "with_rw_and_rw_remote_repo", | |
| 38 | 39 | "TestBase", | |
| 40 | + "VirtualEnvironment", | ||
| 39 | 41 | "TestCase", | |
| 40 | 42 | "SkipTest", | |
| 41 | 43 | "skipIf", | |
@@ -390,3 +392,42 @@ def _make_file(self, rela_path, data, repo=None): | |||
| 390 | 392 | with open(abs_path, "w") as fp: | |
| 391 | 393 | fp.write(data) | |
| 392 | 394 | return abs_path | |
| 395 | + | ||
| 396 | + | ||
| 397 | + class VirtualEnvironment: | ||
| 398 | + """A newly created Python virtual environment for use in a test.""" | ||
| 399 | + | ||
| 400 | + __slots__ = ("_env_dir",) | ||
| 401 | + | ||
| 402 | + def __init__(self, env_dir, *, with_pip): | ||
| 403 | + self._env_dir = env_dir | ||
| 404 | + venv.create(env_dir, symlinks=(os.name != "nt"), with_pip=with_pip) | ||
| 405 | + | ||
| 406 | + @property | ||
| 407 | + def env_dir(self): | ||
| 408 | + """The top-level directory of the environment.""" | ||
| 409 | + return self._env_dir | ||
| 410 | + | ||
| 411 | + @property | ||
| 412 | + def python(self): | ||
| 413 | + """Path to the Python executable in the environment.""" | ||
| 414 | + return self._executable("python") | ||
| 415 | + | ||
| 416 | + @property | ||
| 417 | + def pip(self): | ||
| 418 | + """Path to the pip executable in the environment, or RuntimeError if absent.""" | ||
| 419 | + return self._executable("pip") | ||
| 420 | + | ||
| 421 | + @property | ||
| 422 | + def sources(self): | ||
| 423 | + """Path to a src directory in the environment, which may not exist yet.""" | ||
| 424 | + return os.path.join(self.env_dir, "src") | ||
| 425 | + | ||
| 426 | + def _executable(self, basename): | ||
| 427 | + if os.name == "nt": | ||
| 428 | + path = osp.join(self.env_dir, "Scripts", basename + ".exe") | ||
| 429 | + else: | ||
| 430 | + path = osp.join(self.env_dir, "bin", basename) | ||
| 431 | + if osp.isfile(path) or osp.islink(path): | ||
| 432 | + return path | ||
| 433 | + raise RuntimeError(f"no regular file or symlink {path!r}") | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,31 +4,19 @@ | |||
| 4 | 4 | import ast | |
| 5 | 5 | import os | |
| 6 | 6 | import subprocess | |
| 7 | - import sys | ||
| 8 | 7 | ||
| 9 | - from test.lib import TestBase | ||
| 10 | - from test.lib.helper import with_rw_directory | ||
| 8 | + from test.lib import TestBase, VirtualEnvironment, with_rw_directory | ||
| 11 | 9 | ||
| 12 | 10 | ||
| 13 | 11 | class TestInstallation(TestBase): | |
| 14 | - def setUp_venv(self, rw_dir): | ||
| 15 | - self.venv = rw_dir | ||
| 16 | - subprocess.run([sys.executable, "-m", "venv", self.venv], stdout=subprocess.PIPE) | ||
| 17 | - bin_name = "Scripts" if os.name == "nt" else "bin" | ||
| 18 | - self.python = os.path.join(self.venv, bin_name, "python") | ||
| 19 | - self.pip = os.path.join(self.venv, bin_name, "pip") | ||
| 20 | - self.sources = os.path.join(self.venv, "src") | ||
| 21 | - self.cwd = os.path.dirname(os.path.dirname(__file__)) | ||
| 22 | - os.symlink(self.cwd, self.sources, target_is_directory=True) | ||
| 23 | - | ||
| 24 | 12 | @with_rw_directory | |
| 25 | 13 | def test_installation(self, rw_dir): | |
| 26 | - self.setUp_venv(rw_dir) | ||
| 14 | + venv = self._set_up_venv(rw_dir) | ||
| 27 | 15 | ||
| 28 | 16 | result = subprocess.run( | |
| 29 | - [self.pip, "install", "."], | ||
| 17 | + [venv.pip, "install", "."], | ||
| 30 | 18 | stdout=subprocess.PIPE, | |
| 31 | - cwd=self.sources, | ||
| 19 | + cwd=venv.sources, | ||
| 32 | 20 | ) | |
| 33 | 21 | self.assertEqual( | |
| 34 | 22 | 0, | |
@@ -37,9 +25,9 @@ def test_installation(self, rw_dir): | |||
| 37 | 25 | ) | |
| 38 | 26 | ||
| 39 | 27 | result = subprocess.run( | |
| 40 | - [self.python, "-c", "import git"], | ||
| 28 | + [venv.python, "-c", "import git"], | ||
| 41 | 29 | stdout=subprocess.PIPE, | |
| 42 | - cwd=self.sources, | ||
| 30 | + cwd=venv.sources, | ||
| 43 | 31 | ) | |
| 44 | 32 | self.assertEqual( | |
| 45 | 33 | 0, | |
@@ -48,9 +36,9 @@ def test_installation(self, rw_dir): | |||
| 48 | 36 | ) | |
| 49 | 37 | ||
| 50 | 38 | result = subprocess.run( | |
| 51 | - [self.python, "-c", "import gitdb; import smmap"], | ||
| 39 | + [venv.python, "-c", "import gitdb; import smmap"], | ||
| 52 | 40 | stdout=subprocess.PIPE, | |
| 53 | - cwd=self.sources, | ||
| 41 | + cwd=venv.sources, | ||
| 54 | 42 | ) | |
| 55 | 43 | self.assertEqual( | |
| 56 | 44 | 0, | |
@@ -62,9 +50,9 @@ def test_installation(self, rw_dir): | |||
| 62 | 50 | # by inserting its location into PYTHONPATH or otherwise patched into | |
| 63 | 51 | # sys.path, make sure it is not wrongly inserted as the *first* entry. | |
| 64 | 52 | result = subprocess.run( | |
| 65 | - [self.python, "-c", "import sys; import git; print(sys.path)"], | ||
| 53 | + [venv.python, "-c", "import sys; import git; print(sys.path)"], | ||
| 66 | 54 | stdout=subprocess.PIPE, | |
| 67 | - cwd=self.sources, | ||
| 55 | + cwd=venv.sources, | ||
| 68 | 56 | ) | |
| 69 | 57 | syspath = result.stdout.decode("utf-8").splitlines()[0] | |
| 70 | 58 | syspath = ast.literal_eval(syspath) | |
@@ -73,3 +61,13 @@ def test_installation(self, rw_dir): | |||
| 73 | 61 | syspath[0], | |
| 74 | 62 | msg="Failed to follow the conventions for https://docs.python.org/3/library/sys.html#sys.path", | |
| 75 | 63 | ) | |
| 64 | + | ||
| 65 | + @staticmethod | ||
| 66 | + def _set_up_venv(rw_dir): | ||
| 67 | + venv = VirtualEnvironment(rw_dir, with_pip=True) | ||
| 68 | + os.symlink( | ||
| 69 | + os.path.dirname(os.path.dirname(__file__)), | ||
| 70 | + venv.sources, | ||
| 71 | + target_is_directory=True, | ||
| 72 | + ) | ||
| 73 | + return venv | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments