Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork966
Expand file tree
/
Copy pathtest_git.py
More file actions
384 lines (319 loc) · 15.4 KB
/
test_git.py
File metadata and controls
384 lines (319 loc) · 15.4 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# -*- 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: https://opensource.org/license/bsd-3-clause/
importinspect
importlogging
importos
importos.pathasosp
importre
importshutil
importsubprocess
importsys
fromtempfileimportTemporaryDirectory,TemporaryFile
fromunittestimportskipUnless
ifsys.version_info>= (3,8):
fromunittestimportmock
else:
importmock# To be able to examine call_args.kwargs on a mock.
importddt
fromgitimportGit,refresh,GitCommandError,GitCommandNotFound,Repo,cmd
fromgit.compatimportis_win
fromgit.utilimportcwd,finalize_process
fromtest.libimportTestBase,fixture_path,with_rw_directory
@ddt.ddt
classTestGit(TestBase):
@classmethod
defsetUpClass(cls):
super(TestGit,cls).setUpClass()
cls.git=Git(cls.rorepo.working_dir)
deftearDown(self):
importgc
gc.collect()
def_assert_logged_for_popen(self,log_watcher,name,value):
re_name=re.escape(name)
re_value=re.escape(str(value))
re_line=re.compile(rf"DEBUG:git.cmd:Popen\(.*\b{re_name}={re_value}[,)]")
match_attempts= [re_line.match(message)formessageinlog_watcher.output]
self.assertTrue(any(match_attempts),repr(log_watcher.output))
@mock.patch.object(Git,"execute")
deftest_call_process_calls_execute(self,git):
git.return_value=""
self.git.version()
self.assertTrue(git.called)
self.assertEqual(git.call_args, ((["git","version"],), {}))
deftest_call_unpack_args_unicode(self):
args=Git._unpack_args("Unicode€™")
mangled_value="Unicode\u20ac\u2122"
self.assertEqual(args, [mangled_value])
deftest_call_unpack_args(self):
args=Git._unpack_args(["git","log","--","Unicode€™"])
mangled_value="Unicode\u20ac\u2122"
self.assertEqual(args, ["git","log","--",mangled_value])
deftest_it_raises_errors(self):
self.assertRaises(GitCommandError,self.git.this_does_not_exist)
deftest_it_transforms_kwargs_into_git_command_arguments(self):
self.assertEqual(["-s"],self.git.transform_kwargs(**{"s":True}))
self.assertEqual(["-s","5"],self.git.transform_kwargs(**{"s":5}))
self.assertEqual([],self.git.transform_kwargs(**{"s":None}))
self.assertEqual(["--max-count"],self.git.transform_kwargs(**{"max_count":True}))
self.assertEqual(["--max-count=5"],self.git.transform_kwargs(**{"max_count":5}))
self.assertEqual(["--max-count=0"],self.git.transform_kwargs(**{"max_count":0}))
self.assertEqual([],self.git.transform_kwargs(**{"max_count":None}))
# Multiple args are supported by using lists/tuples
self.assertEqual(
["-L","1-3","-L","12-18"],
self.git.transform_kwargs(**{"L": ("1-3","12-18")}),
)
self.assertEqual(["-C","-C"],self.git.transform_kwargs(**{"C": [True,True,None,False]}))
# order is undefined
res=self.git.transform_kwargs(**{"s":True,"t":True})
self.assertEqual({"-s","-t"},set(res))
_shell_cases= (
# value_in_call, value_from_class, expected_popen_arg
(None,False,False),
(None,True,True),
(False,True,False),
(False,False,False),
(True,False,True),
(True,True,True),
)
def_do_shell_combo(self,value_in_call,value_from_class):
withmock.patch.object(Git,"USE_SHELL",value_from_class):
# git.cmd gets Popen via a "from" import, so patch it there.
withmock.patch.object(cmd,"Popen",wraps=cmd.Popen)asmock_popen:
# Use a command with no arguments (besides the program name), so it runs
# with or without a shell, on all OSes, with the same effect.
self.git.execute(["git"],with_exceptions=False,shell=value_in_call)
returnmock_popen
@ddt.idata(_shell_cases)
deftest_it_uses_shell_or_not_as_specified(self,case):
"""A bool passed as ``shell=`` takes precedence over `Git.USE_SHELL`."""
value_in_call,value_from_class,expected_popen_arg=case
mock_popen=self._do_shell_combo(value_in_call,value_from_class)
mock_popen.assert_called_once()
self.assertIs(mock_popen.call_args.kwargs["shell"],expected_popen_arg)
@ddt.idata(full_case[:2]forfull_casein_shell_cases)
deftest_it_logs_if_it_uses_a_shell(self,case):
"""``shell=`` in the log message agrees with what is passed to `Popen`."""
value_in_call,value_from_class=case
withself.assertLogs(cmd.log,level=logging.DEBUG)aslog_watcher:
mock_popen=self._do_shell_combo(value_in_call,value_from_class)
self._assert_logged_for_popen(log_watcher,"shell",mock_popen.call_args.kwargs["shell"])
@ddt.data(
("None",None),
("<valid stream>",subprocess.PIPE),
)
deftest_it_logs_istream_summary_for_stdin(self,case):
expected_summary,istream_argument=case
withself.assertLogs(cmd.log,level=logging.DEBUG)aslog_watcher:
self.git.execute(["git","version"],istream=istream_argument)
self._assert_logged_for_popen(log_watcher,"stdin",expected_summary)
deftest_it_executes_git_and_returns_result(self):
self.assertRegex(self.git.execute(["git","version"]),r"^git version [\d\.]{2}.*$")
deftest_it_executes_git_not_from_cwd(self):
withTemporaryDirectory()astmpdir:
ifis_win:
# Copy an actual binary executable that is not git.
other_exe_path=os.path.join(os.getenv("WINDIR"),"system32","hostname.exe")
impostor_path=os.path.join(tmpdir,"git.exe")
shutil.copy(other_exe_path,impostor_path)
else:
# Create a shell script that doesn't do anything.
impostor_path=os.path.join(tmpdir,"git")
withopen(impostor_path,mode="w",encoding="utf-8")asfile:
print("#!/bin/sh",file=file)
os.chmod(impostor_path,0o755)
withcwd(tmpdir):
self.assertRegex(self.git.execute(["git","version"]),r"^git version\b")
@skipUnless(is_win,"The regression only affected Windows, and this test logic is OS-specific.")
deftest_it_avoids_upcasing_unrelated_environment_variable_names(self):
old_name="28f425ca_d5d8_4257_b013_8d63166c8158"
ifold_name==old_name.upper():
raiseRuntimeError("test bug or strange locale: old_name invariant under upcasing")
# Step 1: Set the environment variable in this parent process. Because os.putenv is a thin
# wrapper around a system API, os.environ never sees the variable in this parent
# process, so the name is not upcased even on Windows.
os.putenv(old_name,"1")
# Step 2: Create the child process that inherits the environment variable. The child uses
# GitPython, and we are testing that it passes the variable with the exact original
# name to its own child process (the grandchild).
cmdline= [
sys.executable,
fixture_path("env_case.py"),# Contains steps 3 and 4.
self.rorepo.working_dir,
old_name,
]
pair_text=subprocess.check_output(cmdline,shell=False,text=True)# Run steps 3 and 4.
new_name=pair_text.split("=")[0]
self.assertEqual(new_name,old_name)
deftest_it_accepts_stdin(self):
filename=fixture_path("cat_file_blob")
withopen(filename,"r")asfh:
self.assertEqual(
"70c379b63ffa0795fdbfbc128e5a2818397b7ef8",
self.git.hash_object(istream=fh,stdin=True),
)
@mock.patch.object(Git,"execute")
deftest_it_ignores_false_kwargs(self,git):
# this_should_not_be_ignored=False implies it *should* be ignored
self.git.version(pass_this_kwarg=False)
self.assertTrue("pass_this_kwarg"notingit.call_args[1])
deftest_it_raises_proper_exception_with_output_stream(self):
tmp_file=TemporaryFile()
self.assertRaises(
GitCommandError,
self.git.checkout,
"non-existent-branch",
output_stream=tmp_file,
)
deftest_it_accepts_environment_variables(self):
filename=fixture_path("ls_tree_empty")
withopen(filename,"r")asfh:
tree=self.git.mktree(istream=fh)
env= {
"GIT_AUTHOR_NAME":"Author Name",
"GIT_AUTHOR_EMAIL":"author@example.com",
"GIT_AUTHOR_DATE":"1400000000+0000",
"GIT_COMMITTER_NAME":"Committer Name",
"GIT_COMMITTER_EMAIL":"committer@example.com",
"GIT_COMMITTER_DATE":"1500000000+0000",
}
commit=self.git.commit_tree(tree,m="message",env=env)
self.assertEqual(commit,"4cfd6b0314682d5a58f80be39850bad1640e9241")
deftest_persistent_cat_file_command(self):
# read header only
hexsha="b2339455342180c7cc1e9bba3e9f181f7baa5167"
g=self.git.cat_file(batch_check=True,istream=subprocess.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=subprocess.PIPE,as_process=True)
g.stdin.write(b"b2339455342180c7cc1e9bba3e9f181f7baa5167\n")
g.stdin.flush()
obj_info_two=g.stdout.readline()
self.assertEqual(obj_info,obj_info_two)
# read data - have to read it in one large chunk
size=int(obj_info.split()[2])
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()
self.assertEqual(g.stdout.readline(),obj_info)
# same can be achieved using the respective command functions
hexsha,typename,size=self.git.get_object_header(hexsha)
hexsha,typename_two,size_two,_=self.git.get_object_data(hexsha)
self.assertEqual(typename,typename_two)
self.assertEqual(size,size_two)
deftest_version(self):
v=self.git.version_info
self.assertIsInstance(v,tuple)
forninv:
self.assertIsInstance(n,int)
# END verify number types
deftest_cmd_override(self):
withmock.patch.object(
type(self.git),
"GIT_PYTHON_GIT_EXECUTABLE",
osp.join("some","path","which","doesn't","exist","gitbinary"),
):
self.assertRaises(GitCommandNotFound,self.git.version)
deftest_refresh(self):
# test a bad git path refresh
self.assertRaises(GitCommandNotFound,refresh,"yada")
# test a good path refresh
which_cmd="where"ifis_winelse"command -v"
path=os.popen("{0} git".format(which_cmd)).read().strip().split("\n")[0]
refresh(path)
deftest_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.assertEqual(git_version,git_command_version)
deftest_persistent_options(self):
git_command_version=self.git.version()
# analog to test_options_are_passed_to_git
self.git.set_persistent_git_options(version=True)
git_version=self.git.NoOp()
self.assertEqual(git_version,git_command_version)
# subsequent calls keep this option:
git_version_2=self.git.NoOp()
self.assertEqual(git_version_2,git_command_version)
# reset to empty:
self.git.set_persistent_git_options()
self.assertRaises(GitCommandError,self.git.NoOp)
deftest_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.assertEqual(input_value,output_value)
deftest_change_to_transform_kwargs_does_not_break_command_options(self):
self.git.log(n=1)
deftest_insert_after_kwarg_raises(self):
# This isn't a complete add command, which doesn't matter here
self.assertRaises(ValueError,self.git.remote,"add",insert_kwargs_after="foo")
deftest_env_vars_passed_to_git(self):
editor="non_existent_editor"
withmock.patch.dict(os.environ, {"GIT_EDITOR":editor}):
self.assertEqual(self.git.var("GIT_EDITOR"),editor)
@with_rw_directory
deftest_environment(self,rw_dir):
# sanity check
self.assertEqual(self.git.environment(), {})
# make sure the context manager works and cleans up after itself
withself.git.custom_environment(PWD="/tmp"):
self.assertEqual(self.git.environment(), {"PWD":"/tmp"})
self.assertEqual(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'.
self.assertEqual(old_env, {"VARKEY":None})
self.assertEqual(self.git.environment(), {"VARKEY":"VARVALUE"})
new_env=self.git.update_environment(**old_env)
self.assertEqual(new_env, {"VARKEY":"VARVALUE"})
self.assertEqual(self.git.environment(), {})
path=osp.join(rw_dir,"failing-script.sh")
withopen(path,"wt")asstream:
stream.write("#!/usr/bin/env sh\n""echo FOO\n")
os.chmod(path,0o777)
rw_repo=Repo.init(osp.join(rw_dir,"repo"))
remote=rw_repo.create_remote("ssh-origin","ssh://git@server/foo")
withrw_repo.git.custom_environment(GIT_SSH=path):
try:
remote.fetch()
exceptGitCommandErroraserr:
self.assertIn("FOO",str(err))
deftest_handle_process_output(self):
fromgit.cmdimporthandle_process_output
line_count=5002
count= [None,0,0]
defcounter_stdout(line):
count[1]+=1
defcounter_stderr(line):
count[2]+=1
cmdline= [
sys.executable,
fixture_path("cat_file.py"),
str(fixture_path("issue-301_stderr")),
]
proc=subprocess.Popen(
cmdline,
stdin=None,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=False,
creationflags=cmd.PROC_CREATIONFLAGS,
)
handle_process_output(proc,counter_stdout,counter_stderr,finalize_process)
self.assertEqual(count[1],line_count)
self.assertEqual(count[2],line_count)
deftest_execute_kwargs_set_agrees_with_method(self):
parameter_names=inspect.signature(cmd.Git.execute).parameters.keys()
self_param,command_param,*most_params,extra_kwargs_param=parameter_names
self.assertEqual(self_param,"self")
self.assertEqual(command_param,"command")
self.assertEqual(set(most_params),cmd.execute_kwargs)# Most important.
self.assertEqual(extra_kwargs_param,"subprocess_kwargs")