Extract venv management from test_installation · gitpython-developers/GitPython@7751436
@@ -4,31 +4,19 @@
44import ast
55import os
66import subprocess
7-import sys
879-from test.lib import TestBase
10-from test.lib.helper import with_rw_directory
8+from test.lib import TestBase, VirtualEnvironment, with_rw_directory
11912101311class 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-2412@with_rw_directory
2513def test_installation(self, rw_dir):
26-self.setUp_venv(rw_dir)
14+venv = self._set_up_venv(rw_dir)
27152816result = subprocess.run(
29- [self.pip, "install", "."],
17+ [venv.pip, "install", "."],
3018stdout=subprocess.PIPE,
31-cwd=self.sources,
19+cwd=venv.sources,
3220 )
3321self.assertEqual(
34220,
@@ -37,9 +25,9 @@ def test_installation(self, rw_dir):
3725 )
38263927result = subprocess.run(
40- [self.python, "-c", "import git"],
28+ [venv.python, "-c", "import git"],
4129stdout=subprocess.PIPE,
42-cwd=self.sources,
30+cwd=venv.sources,
4331 )
4432self.assertEqual(
45330,
@@ -48,9 +36,9 @@ def test_installation(self, rw_dir):
4836 )
49375038result = subprocess.run(
51- [self.python, "-c", "import gitdb; import smmap"],
39+ [venv.python, "-c", "import gitdb; import smmap"],
5240stdout=subprocess.PIPE,
53-cwd=self.sources,
41+cwd=venv.sources,
5442 )
5543self.assertEqual(
56440,
@@ -62,9 +50,9 @@ def test_installation(self, rw_dir):
6250# by inserting its location into PYTHONPATH or otherwise patched into
6351# sys.path, make sure it is not wrongly inserted as the *first* entry.
6452result = subprocess.run(
65- [self.python, "-c", "import sys; import git; print(sys.path)"],
53+ [venv.python, "-c", "import sys; import git; print(sys.path)"],
6654stdout=subprocess.PIPE,
67-cwd=self.sources,
55+cwd=venv.sources,
6856 )
6957syspath = result.stdout.decode("utf-8").splitlines()[0]
7058syspath = ast.literal_eval(syspath)
@@ -73,3 +61,13 @@ def test_installation(self, rw_dir):
7361syspath[0],
7462msg="Failed to follow the conventions for https://docs.python.org/3/library/sys.html#sys.path",
7563 )
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