Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7751436

Browse files
committed
Extract venv management from test_installation
- Create and use a test.lib.helper.VirtualEnvironment class.- Import and use venv module instead of running "python -m venv".These changes make no significant difference in speed or clarityfor the existing test in test_installation. The reason for thischange is instead to support the use of new per-test virtualenvironments in at least one other test.
1 parent66ff4c1 commit7751436

File tree

2 files changed

+61
-22
lines changed

2 files changed

+61
-22
lines changed

‎test/lib/helper.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
importtextwrap
1515
importtime
1616
importunittest
17+
importvenv
1718

1819
importgitdb
1920

@@ -36,6 +37,7 @@
3637
"with_rw_repo",
3738
"with_rw_and_rw_remote_repo",
3839
"TestBase",
40+
"VirtualEnvironment",
3941
"TestCase",
4042
"SkipTest",
4143
"skipIf",
@@ -390,3 +392,42 @@ def _make_file(self, rela_path, data, repo=None):
390392
withopen(abs_path,"w")asfp:
391393
fp.write(data)
392394
returnabs_path
395+
396+
397+
classVirtualEnvironment:
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+
defenv_dir(self):
408+
"""The top-level directory of the environment."""
409+
returnself._env_dir
410+
411+
@property
412+
defpython(self):
413+
"""Path to the Python executable in the environment."""
414+
returnself._executable("python")
415+
416+
@property
417+
defpip(self):
418+
"""Path to the pip executable in the environment, or RuntimeError if absent."""
419+
returnself._executable("pip")
420+
421+
@property
422+
defsources(self):
423+
"""Path to a src directory in the environment, which may not exist yet."""
424+
returnos.path.join(self.env_dir,"src")
425+
426+
def_executable(self,basename):
427+
ifos.name=="nt":
428+
path=osp.join(self.env_dir,"Scripts",basename+".exe")
429+
else:
430+
path=osp.join(self.env_dir,"bin",basename)
431+
ifosp.isfile(path)orosp.islink(path):
432+
returnpath
433+
raiseRuntimeError(f"no regular file or symlink{path!r}")

‎test/test_installation.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,19 @@
44
importast
55
importos
66
importsubprocess
7-
importsys
87

9-
fromtest.libimportTestBase
10-
fromtest.lib.helperimportwith_rw_directory
8+
fromtest.libimportTestBase,VirtualEnvironment,with_rw_directory
119

1210

1311
classTestInstallation(TestBase):
14-
defsetUp_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"ifos.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
2513
deftest_installation(self,rw_dir):
26-
self.setUp_venv(rw_dir)
14+
venv=self._set_up_venv(rw_dir)
2715

2816
result=subprocess.run(
29-
[self.pip,"install","."],
17+
[venv.pip,"install","."],
3018
stdout=subprocess.PIPE,
31-
cwd=self.sources,
19+
cwd=venv.sources,
3220
)
3321
self.assertEqual(
3422
0,
@@ -37,9 +25,9 @@ def test_installation(self, rw_dir):
3725
)
3826

3927
result=subprocess.run(
40-
[self.python,"-c","import git"],
28+
[venv.python,"-c","import git"],
4129
stdout=subprocess.PIPE,
42-
cwd=self.sources,
30+
cwd=venv.sources,
4331
)
4432
self.assertEqual(
4533
0,
@@ -48,9 +36,9 @@ def test_installation(self, rw_dir):
4836
)
4937

5038
result=subprocess.run(
51-
[self.python,"-c","import gitdb; import smmap"],
39+
[venv.python,"-c","import gitdb; import smmap"],
5240
stdout=subprocess.PIPE,
53-
cwd=self.sources,
41+
cwd=venv.sources,
5442
)
5543
self.assertEqual(
5644
0,
@@ -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.
6452
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)"],
6654
stdout=subprocess.PIPE,
67-
cwd=self.sources,
55+
cwd=venv.sources,
6856
)
6957
syspath=result.stdout.decode("utf-8").splitlines()[0]
7058
syspath=ast.literal_eval(syspath)
@@ -73,3 +61,13 @@ def test_installation(self, rw_dir):
7361
syspath[0],
7462
msg="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+
returnvenv

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp