1
1
"""Utility functions for use in tests."""
2
2
3
- from __future__ import absolute_import
3
+ from __future__ import absolute_import , unicode_literals
4
4
5
5
import logging
6
6
import subprocess
16
16
log = logging .getLogger (__name__ )
17
17
18
18
19
- def check_output (l , env = () ):
20
- if env == ():
21
- output = subprocess .Popen ( l , stdout = subprocess .PIPE ). communicate ()[ 0 ]
22
- else :
23
- output = subprocess . Popen ( l , stdout = subprocess . PIPE ,
24
- env = env ). communicate ()[ 0 ]
19
+ def check_output (command , env = None ):
20
+ output = subprocess . Popen (
21
+ command , stdout = subprocess .PIPE , stderr = subprocess .PIPE ,
22
+ env = env
23
+ ). communicate ()[ 0 ]
24
+ log . info ( output )
25
25
return output
26
26
27
27
@@ -36,63 +36,76 @@ def make_test_git():
36
36
chdir (directory )
37
37
38
38
# Initialize and configure
39
- # TODO: move the ``log.info`` call inside the ``check_output```
40
- log .info (check_output (['git' , 'init' ] + [directory ], env = env ))
41
- log .info (check_output (
39
+ check_output (['git' , 'init' ] + [directory ], env = env )
40
+ check_output (
42
41
[
'git' ,
'config' ,
'user.email' ,
'[email protected] ' ],
43
42
env = env
44
- ))
45
- log . info ( check_output (
43
+ )
44
+ check_output (
46
45
['git' , 'config' , 'user.name' , 'Read the Docs' ],
47
46
env = env
48
- ))
47
+ )
49
48
50
49
# Set up the actual repository
51
- log . info ( check_output (['git' , 'add' , '.' ], env = env ) )
52
- log . info ( check_output (['git' , 'commit' , '-m"init"' ], env = env ) )
50
+ check_output (['git' , 'add' , '.' ], env = env )
51
+ check_output (['git' , 'commit' , '-m"init"' ], env = env )
53
52
54
53
# Add fake repo as submodule. We need to fake this here because local path
55
54
# URL are not allowed and using a real URL will require Internet to clone
56
55
# the repo
57
- log . info ( check_output (['git' , 'checkout' , '-b' , 'submodule' , 'master' ], env = env ) )
56
+ check_output (['git' , 'checkout' , '-b' , 'submodule' , 'master' ], env = env )
58
57
# https://stackoverflow.com/a/37378302/2187091
59
58
mkdir (pjoin (directory , 'foobar' ))
60
59
gitmodules_path = pjoin (directory , '.gitmodules' )
61
60
with open (gitmodules_path , 'w' ) as fh :
62
61
fh .write ('''[submodule "foobar"]\n \t path = foobar\n \t url = https://foobar.com/git\n ''' )
63
- log . info ( check_output (
62
+ check_output (
64
63
[
65
64
'git' , 'update-index' , '--add' , '--cacheinfo' , '160000' ,
66
65
'233febf4846d7a0aeb95b6c28962e06e21d13688' , 'foobar' ,
67
66
],
68
67
env = env ,
69
- ))
70
- log . info ( check_output (['git' , 'add' , '.' ], env = env ) )
71
- log . info ( check_output (['git' , 'commit' , '-m"Add submodule"' ], env = env ) )
68
+ )
69
+ check_output (['git' , 'add' , '.' ], env = env )
70
+ check_output (['git' , 'commit' , '-m"Add submodule"' ], env = env )
72
71
73
72
# Add a relative submodule URL in the relativesubmodule branch
74
- log . info ( check_output (['git' , 'checkout' , '-b' , 'relativesubmodule' , 'master' ], env = env ) )
75
- log . info ( check_output (
73
+ check_output (['git' , 'checkout' , '-b' , 'relativesubmodule' , 'master' ], env = env )
74
+ check_output (
76
75
['git' , 'submodule' , 'add' , '-b' , 'master' , './' , 'relativesubmodule' ],
77
76
env = env
78
- ))
79
- log . info ( check_output (['git' , 'add' , '.' ], env = env ) )
80
- log . info ( check_output (['git' , 'commit' , '-m"Add relative submodule"' ], env = env ) )
77
+ )
78
+ check_output (['git' , 'add' , '.' ], env = env )
79
+ check_output (['git' , 'commit' , '-m"Add relative submodule"' ], env = env )
81
80
# Add an invalid submodule URL in the invalidsubmodule branch
82
- log . info ( check_output (['git' , 'checkout' , '-b' , 'invalidsubmodule' , 'master' ], env = env ) )
83
- log . info ( check_output (
81
+ check_output (['git' , 'checkout' , '-b' , 'invalidsubmodule' , 'master' ], env = env )
82
+ check_output (
84
83
['git' , 'submodule' , 'add' , '-b' , 'master' , './' , 'invalidsubmodule' ],
85
84
env = env ,
86
- ))
87
- log . info ( check_output (['git' , 'add' , '.' ], env = env ) )
88
- log . info ( check_output (['git' , 'commit' , '-m"Add invalid submodule"' ], env = env ) )
85
+ )
86
+ check_output (['git' , 'add' , '.' ], env = env )
87
+ check_output (['git' , 'commit' , '-m"Add invalid submodule"' ], env = env )
89
88
90
89
# Checkout to master branch again
91
- log . info ( check_output (['git' , 'checkout' , 'master' ], env = env ) )
90
+ check_output (['git' , 'checkout' , 'master' ], env = env )
92
91
chdir (path )
93
92
return directory
94
93
95
94
95
+ def create_git_tag (directory , tag , annotated = False ):
96
+ env = environ .copy ()
97
+ env ['GIT_DIR' ] = pjoin (directory , '.git' )
98
+ path = getcwd ()
99
+ chdir (directory )
100
+
101
+ command = ['git' , 'tag' ]
102
+ if annotated :
103
+ command .extend (['-a' , '-m' , 'Some tag' ])
104
+ command .append (tag )
105
+ check_output (command , env = env )
106
+ chdir (path )
107
+
108
+
96
109
def make_test_hg ():
97
110
directory = mkdtemp ()
98
111
path = getcwd ()
0 commit comments