Skip to content

Commit 3dcb520

Browse files
committed
Add unit tests
1 parent f37011d commit 3dcb520

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

‎test/test_config.py

+99
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import glob
88
import io
9+
import os
10+
from unittest import mock
911

1012
from git import (
1113
GitConfigParser
@@ -238,6 +240,103 @@ def check_test_value(cr, value):
238240
with GitConfigParser(fpa, read_only=True) as cr:
239241
check_test_value(cr, tv)
240242

243+
@with_rw_directory
244+
def test_conditional_includes_from_git_dir(self, rw_dir):
245+
# Initiate repository path
246+
git_dir = osp.join(rw_dir, "target1", "repo1")
247+
os.makedirs(git_dir)
248+
249+
# Initiate mocked repository
250+
repo = mock.Mock(git_dir=git_dir)
251+
252+
# Initiate config files.
253+
path1 = osp.join(rw_dir, "config1")
254+
path2 = osp.join(rw_dir, "config2")
255+
template = "[includeIf \"{}:{}\"]\n path={}\n"
256+
257+
with open(path1, "w") as stream:
258+
stream.write(template.format("gitdir", git_dir, path2))
259+
260+
# Ensure that config is ignored if no repo is set.
261+
with GitConfigParser(path1) as config:
262+
assert not config._has_includes()
263+
assert config._included_paths() == []
264+
265+
# Ensure that config is included if path is matching git_dir.
266+
with GitConfigParser(path1, repo=repo) as config:
267+
assert config._has_includes()
268+
assert config._included_paths() == [("path", path2)]
269+
270+
# Ensure that config is ignored if case is incorrect.
271+
with open(path1, "w") as stream:
272+
stream.write(template.format("gitdir", git_dir.upper(), path2))
273+
274+
with GitConfigParser(path1, repo=repo) as config:
275+
assert not config._has_includes()
276+
assert config._included_paths() == []
277+
278+
# Ensure that config is included if case is ignored.
279+
with open(path1, "w") as stream:
280+
stream.write(template.format("gitdir/i", git_dir.upper(), path2))
281+
282+
with GitConfigParser(path1, repo=repo) as config:
283+
assert config._has_includes()
284+
assert config._included_paths() == [("path", path2)]
285+
286+
# Ensure that config is included with path using glob pattern.
287+
with open(path1, "w") as stream:
288+
stream.write(template.format("gitdir", "**/repo1", path2))
289+
290+
with GitConfigParser(path1, repo=repo) as config:
291+
assert config._has_includes()
292+
assert config._included_paths() == [("path", path2)]
293+
294+
# Ensure that config is ignored if path is not matching git_dir.
295+
with open(path1, "w") as stream:
296+
stream.write(template.format("gitdir", "incorrect", path2))
297+
298+
with GitConfigParser(path1, repo=repo) as config:
299+
assert not config._has_includes()
300+
assert config._included_paths() == []
301+
302+
@with_rw_directory
303+
def test_conditional_includes_from_branch_name(self, rw_dir):
304+
# Initiate mocked branch
305+
branch = mock.Mock()
306+
type(branch).name = mock.PropertyMock(return_value="/foo/branch")
307+
308+
# Initiate mocked repository
309+
repo = mock.Mock(active_branch=branch)
310+
311+
# Initiate config files.
312+
path1 = osp.join(rw_dir, "config1")
313+
path2 = osp.join(rw_dir, "config2")
314+
template = "[includeIf \"onbranch:{}\"]\n path={}\n"
315+
316+
# Ensure that config is included is branch is correct.
317+
with open(path1, "w") as stream:
318+
stream.write(template.format("/foo/branch", path2))
319+
320+
with GitConfigParser(path1, repo=repo) as config:
321+
assert config._has_includes()
322+
assert config._included_paths() == [("path", path2)]
323+
324+
# Ensure that config is included is branch is incorrect.
325+
with open(path1, "w") as stream:
326+
stream.write(template.format("incorrect", path2))
327+
328+
with GitConfigParser(path1, repo=repo) as config:
329+
assert not config._has_includes()
330+
assert config._included_paths() == []
331+
332+
# Ensure that config is included with branch using glob pattern.
333+
with open(path1, "w") as stream:
334+
stream.write(template.format("/foo/**", path2))
335+
336+
with GitConfigParser(path1, repo=repo) as config:
337+
assert config._has_includes()
338+
assert config._included_paths() == [("path", path2)]
339+
241340
def test_rename(self):
242341
file_obj = self._to_memcache(fixture_path('git_config'))
243342
with GitConfigParser(file_obj, read_only=False, merge_includes=False) as cw:

0 commit comments

Comments
 (0)