2
2
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
3
3
4
4
import ast
5
+ import functools
5
6
import os
6
7
import subprocess
7
8
11
12
class TestInstallation (TestBase ):
12
13
@with_rw_directory
13
14
def test_installation (self , rw_dir ):
14
- venv = self ._set_up_venv (rw_dir )
15
+ venv , run = self ._set_up_venv (rw_dir )
15
16
16
- result = subprocess .run (
17
- [venv .pip , "install" , "." ],
18
- stdout = subprocess .PIPE ,
19
- cwd = venv .sources ,
20
- )
21
- self .assertEqual (
22
- 0 ,
23
- result .returncode ,
24
- msg = result .stderr or result .stdout or "Can't install project" ,
25
- )
17
+ result = run ([venv .pip , "install" , "." ])
18
+ self ._check_result (result , "Can't install project" )
26
19
27
- result = subprocess .run (
28
- [venv .python , "-c" , "import git" ],
29
- stdout = subprocess .PIPE ,
30
- cwd = venv .sources ,
31
- )
32
- self .assertEqual (
33
- 0 ,
34
- result .returncode ,
35
- msg = result .stderr or result .stdout or "Self-test failed" ,
36
- )
20
+ result = run ([venv .python , "-c" , "import git" ])
21
+ self ._check_result (result , "Self-test failed" )
37
22
38
- result = subprocess .run (
39
- [venv .python , "-c" , "import gitdb; import smmap" ],
40
- stdout = subprocess .PIPE ,
41
- cwd = venv .sources ,
42
- )
43
- self .assertEqual (
44
- 0 ,
45
- result .returncode ,
46
- msg = result .stderr or result .stdout or "Dependencies not installed" ,
47
- )
23
+ result = run ([venv .python , "-c" , "import gitdb; import smmap" ])
24
+ self ._check_result (result , "Dependencies not installed" )
48
25
49
26
# Even IF gitdb or any other dependency is supplied during development by
50
27
# inserting its location into PYTHONPATH or otherwise patched into sys.path,
51
28
# make sure it is not wrongly inserted as the *first* entry.
52
- result = subprocess .run (
53
- [venv .python , "-c" , "import sys; import git; print(sys.path)" ],
54
- stdout = subprocess .PIPE ,
55
- cwd = venv .sources ,
56
- )
57
- syspath = result .stdout .decode ("utf-8" ).splitlines ()[0 ]
29
+ result = run ([venv .python , "-c" , "import sys; import git; print(sys.path)" ])
30
+ syspath = result .stdout .splitlines ()[0 ]
58
31
syspath = ast .literal_eval (syspath )
59
32
self .assertEqual (
60
33
"" ,
@@ -64,10 +37,37 @@ def test_installation(self, rw_dir):
64
37
65
38
@staticmethod
66
39
def _set_up_venv (rw_dir ):
40
+ # Initialize the virtual environment.
67
41
venv = VirtualEnvironment (rw_dir , with_pip = True )
42
+
43
+ # Make its src directory a symlink to our own top-level source tree.
68
44
os .symlink (
69
45
os .path .dirname (os .path .dirname (__file__ )),
70
46
venv .sources ,
71
47
target_is_directory = True ,
72
48
)
73
- return venv
49
+
50
+ # Create a convenience function to run commands in it.
51
+ run = functools .partial (
52
+ subprocess .run ,
53
+ stdout = subprocess .PIPE ,
54
+ stderr = subprocess .PIPE ,
55
+ universal_newlines = True ,
56
+ cwd = venv .sources ,
57
+ env = {** os .environ , "PYTHONWARNINGS" : "error" },
58
+ )
59
+
60
+ return venv , run
61
+
62
+ def _check_result (self , result , failure_summary ):
63
+ self .assertEqual (
64
+ 0 ,
65
+ result .returncode ,
66
+ msg = self ._prepare_failure_message (result , failure_summary ),
67
+ )
68
+
69
+ @staticmethod
70
+ def _prepare_failure_message (result , failure_summary ):
71
+ stdout = result .stdout .rstrip ()
72
+ stderr = result .stderr .rstrip ()
73
+ return f"{ failure_summary } \n \n stdout:\n { stdout } \n \n stderr:\n { stderr } "
0 commit comments