FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
GitPython/git/test/test_git.py at master · moben/GitPython · GitHub
moben
GitPython
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
GitPython
/
git
/
test
/
test_git.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
201 lines (166 loc) · 7.17 KB
Breadcrumbs
GitPython
/
git
/
test
/
test_git.py
Copy path
File metadata and controls
201 lines (166 loc) · 7.17 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#-*-coding:utf-8-*-
# test_git.py
# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
#
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
import
os
import
mock
from
git
.
test
.
lib
import
(
TestBase
,
patch
,
raises
,
assert_equal
,
assert_true
,
assert_match
,
fixture_path
)
from
git
import
(
Git
,
GitCommandError
,
GitCommandNotFound
,
Repo
)
from
gitdb
.
test
.
lib
import
with_rw_directory
from
git
.
compat
import
PY3
class
TestGit
(
TestBase
):
@
classmethod
def
setUpClass
(
cls
):
super
(
TestGit
,
cls
).
setUpClass
()
cls
.
git
=
Git
(
cls
.
rorepo
.
working_dir
)
@
patch
.
object
(
Git
,
'execute'
)
def
test_call_process_calls_execute
(
self
,
git
):
git
.
return_value
=
''
self
.
git
.
version
()
assert_true
(
git
.
called
)
assert_equal
(
git
.
call_args
, (([
'git'
,
'version'
],), {}))
def
test_call_unpack_args_unicode
(
self
):
args
=
Git
.
_Git__unpack_args
(
u'Unicode€™'
)
if
PY3
:
mangled_value
=
'Unicode
\u20ac
\u2122
'
else
:
mangled_value
=
'Unicode
\xe2
\x82
\xac
\xe2
\x84
\xa2
'
assert_equal
(
args
, [
mangled_value
])
def
test_call_unpack_args
(
self
):
args
=
Git
.
_Git__unpack_args
([
'git'
,
'log'
,
'--'
,
u'Unicode€™'
])
if
PY3
:
mangled_value
=
'Unicode
\u20ac
\u2122
'
else
:
mangled_value
=
'Unicode
\xe2
\x82
\xac
\xe2
\x84
\xa2
'
assert_equal
(
args
, [
'git'
,
'log'
,
'--'
,
mangled_value
])
@
raises
(
GitCommandError
)
def
test_it_raises_errors
(
self
):
self
.
git
.
this_does_not_exist
()
def
test_it_transforms_kwargs_into_git_command_arguments
(
self
):
assert_equal
([
"-s"
],
self
.
git
.
transform_kwargs
(
**
{
's'
:
True
}))
assert_equal
([
"-s5"
],
self
.
git
.
transform_kwargs
(
**
{
's'
:
5
}))
assert_equal
([
"--max-count"
],
self
.
git
.
transform_kwargs
(
**
{
'max_count'
:
True
}))
assert_equal
([
"--max-count=5"
],
self
.
git
.
transform_kwargs
(
**
{
'max_count'
:
5
}))
# order is undefined
res
=
self
.
git
.
transform_kwargs
(
**
{
's'
:
True
,
't'
:
True
})
assert
[
'-s'
,
'-t'
]
==
res
or
[
'-t'
,
'-s'
]
==
res
def
test_it_executes_git_to_shell_and_returns_result
(
self
):
assert_match
(
'^git version [\d\.]{2}.*$'
,
self
.
git
.
execute
([
"git"
,
"version"
]))
def
test_it_accepts_stdin
(
self
):
filename
=
fixture_path
(
"cat_file_blob"
)
fh
=
open
(
filename
,
'r'
)
assert_equal
(
"70c379b63ffa0795fdbfbc128e5a2818397b7ef8"
,
self
.
git
.
hash_object
(
istream
=
fh
,
stdin
=
True
))
fh
.
close
()
@
patch
.
object
(
Git
,
'execute'
)
def
test_it_ignores_false_kwargs
(
self
,
git
):
# this_should_not_be_ignored=False implies it *should* be ignored
self
.
git
.
version
(
pass_this_kwarg
=
False
)
assert_true
(
"pass_this_kwarg"
not
in
git
.
call_args
[
1
])
def
test_persistent_cat_file_command
(
self
):
# read header only
import
subprocess
as
sp
hexsha
=
"b2339455342180c7cc1e9bba3e9f181f7baa5167"
g
=
self
.
git
.
cat_file
(
batch_check
=
True
,
istream
=
sp
.
PIPE
,
as_process
=
True
)
g
.
stdin
.
write
(
b"b2339455342180c7cc1e9bba3e9f181f7baa5167
\n
"
)
g
.
stdin
.
flush
()
obj_info
=
g
.
stdout
.
readline
()
# read header + data
g
=
self
.
git
.
cat_file
(
batch
=
True
,
istream
=
sp
.
PIPE
,
as_process
=
True
)
g
.
stdin
.
write
(
b"b2339455342180c7cc1e9bba3e9f181f7baa5167
\n
"
)
g
.
stdin
.
flush
()
obj_info_two
=
g
.
stdout
.
readline
()
assert
obj_info
==
obj_info_two
# read data - have to read it in one large chunk
size
=
int
(
obj_info
.
split
()[
2
])
data
=
g
.
stdout
.
read
(
size
)
g
.
stdout
.
read
(
1
)
# now we should be able to read a new object
g
.
stdin
.
write
(
b"b2339455342180c7cc1e9bba3e9f181f7baa5167
\n
"
)
g
.
stdin
.
flush
()
assert
g
.
stdout
.
readline
()
==
obj_info
# same can be achived using the respective command functions
hexsha
,
typename
,
size
=
self
.
git
.
get_object_header
(
hexsha
)
hexsha
,
typename_two
,
size_two
,
data
=
self
.
git
.
get_object_data
(
hexsha
)
assert
typename
==
typename_two
and
size
==
size_two
def
test_version
(
self
):
v
=
self
.
git
.
version_info
assert
isinstance
(
v
,
tuple
)
for
n
in
v
:
assert
isinstance
(
n
,
int
)
# END verify number types
def
test_cmd_override
(
self
):
prev_cmd
=
self
.
git
.
GIT_PYTHON_GIT_EXECUTABLE
exc
=
GitCommandNotFound
try
:
# set it to something that doens't exist, assure it raises
type(
self
.
git
).
GIT_PYTHON_GIT_EXECUTABLE
=
os
.
path
.
join
(
"some"
,
"path"
,
"which"
,
"doesn't"
,
"exist"
,
"gitbinary"
)
self
.
failUnlessRaises
(
exc
,
self
.
git
.
version
)
finally
:
type(
self
.
git
).
GIT_PYTHON_GIT_EXECUTABLE
=
prev_cmd
# END undo adjustment
def
test_options_are_passed_to_git
(
self
):
# This work because any command after git --version is ignored
git_version
=
self
.
git
(
version
=
True
).
NoOp
()
git_command_version
=
self
.
git
.
version
()
self
.
assertEquals
(
git_version
,
git_command_version
)
def
test_single_char_git_options_are_passed_to_git
(
self
):
input_value
=
'TestValue'
output_value
=
self
.
git
(
c
=
'user.name=%s'
%
input_value
).
config
(
'--get'
,
'user.name'
)
self
.
assertEquals
(
input_value
,
output_value
)
def
test_change_to_transform_kwargs_does_not_break_command_options
(
self
):
self
.
git
.
log
(
n
=
1
)
def
test_insert_after_kwarg_raises
(
self
):
# This isn't a complete add command, which doesn't matter here
self
.
failUnlessRaises
(
ValueError
,
self
.
git
.
remote
,
'add'
,
insert_kwargs_after
=
'foo'
)
def
test_env_vars_passed_to_git
(
self
):
editor
=
'non_existant_editor'
with
mock
.
patch
.
dict
(
'os.environ'
, {
'GIT_EDITOR'
:
editor
}):
assert
self
.
git
.
var
(
"GIT_EDITOR"
)
==
editor
@
with_rw_directory
def
test_environment
(
self
,
rw_dir
):
# sanity check
assert
self
.
git
.
environment
()
==
{}
# make sure the context manager works and cleans up after itself
with
self
.
git
.
custom_environment
(
PWD
=
'/tmp'
):
assert
self
.
git
.
environment
()
==
{
'PWD'
:
'/tmp'
}
assert
self
.
git
.
environment
()
==
{}
old_env
=
self
.
git
.
update_environment
(
VARKEY
=
'VARVALUE'
)
# The returned dict can be used to revert the change, hence why it has
# an entry with value 'None'.
assert
old_env
==
{
'VARKEY'
:
None
}
assert
self
.
git
.
environment
()
==
{
'VARKEY'
:
'VARVALUE'
}
new_env
=
self
.
git
.
update_environment
(
**
old_env
)
assert
new_env
==
{
'VARKEY'
:
'VARVALUE'
}
assert
self
.
git
.
environment
()
==
{}
path
=
os
.
path
.
join
(
rw_dir
,
'failing-script.sh'
)
stream
=
open
(
path
,
'wt'
)
stream
.
write
(
"#!/usr/bin/env sh
\n
"
+
"echo FOO
\n
"
)
stream
.
close
()
os
.
chmod
(
path
,
0o555
)
rw_repo
=
Repo
.
init
(
os
.
path
.
join
(
rw_dir
,
'repo'
))
remote
=
rw_repo
.
create_remote
(
'ssh-origin'
,
"ssh://git@server/foo"
)
with
rw_repo
.
git
.
custom_environment
(
GIT_SSH
=
path
):
try
:
remote
.
fetch
()
except
GitCommandError
as
err
:
assert
'FOO'
in
str
(
err
)
# end
# end
Back
|
FazBrowse Home
|
New Git URL