22# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
33
44import ast
5+ import functools
56import os
67import subprocess
78
1112class TestInstallation (TestBase ):
1213 @with_rw_directory
1314 def test_installation (self , rw_dir ):
14- venv = self ._set_up_venv (rw_dir )
15+ venv , run = self ._set_up_venv (rw_dir )
1516
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" )
2619
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" )
3722
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" )
4825
4926 # Even IF gitdb or any other dependency is supplied during development by
5027 # inserting its location into PYTHONPATH or otherwise patched into sys.path,
5128 # 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 ]
5831 syspath = ast .literal_eval (syspath )
5932 self .assertEqual (
6033 "" ,
@@ -64,10 +37,37 @@ def test_installation(self, rw_dir):
6437
6538 @staticmethod
6639 def _set_up_venv (rw_dir ):
40+ # Initialize the virtual environment.
6741 venv = VirtualEnvironment (rw_dir , with_pip = True )
42+
43+ # Make its src directory a symlink to our own top-level source tree.
6844 os .symlink (
6945 os .path .dirname (os .path .dirname (__file__ )),
7046 venv .sources ,
7147 target_is_directory = True ,
7248 )
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