Skip to content

Read conditional include #1054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Sep 4, 2020
Merged
Prev Previous commit
Next Next commit
Add unit tests
  • Loading branch information
buddly27 committed Sep 3, 2020
commit 3dcb520caa069914f9ab014798ab321730f569cc
99 changes: 99 additions & 0 deletions test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import glob
import io
import os
from unittest import mock

from git import (
GitConfigParser
Expand Down Expand Up @@ -238,6 +240,103 @@ def check_test_value(cr, value):
with GitConfigParser(fpa, read_only=True) as cr:
check_test_value(cr, tv)

@with_rw_directory
def test_conditional_includes_from_git_dir(self, rw_dir):
# Initiate repository path
git_dir = osp.join(rw_dir, "target1", "repo1")
os.makedirs(git_dir)

# Initiate mocked repository
repo = mock.Mock(git_dir=git_dir)

# Initiate config files.
path1 = osp.join(rw_dir, "config1")
path2 = osp.join(rw_dir, "config2")
template = "[includeIf \"{}:{}\"]\n path={}\n"

with open(path1, "w") as stream:
stream.write(template.format("gitdir", git_dir, path2))

# Ensure that config is ignored if no repo is set.
with GitConfigParser(path1) as config:
assert not config._has_includes()
assert config._included_paths() == []

# Ensure that config is included if path is matching git_dir.
with GitConfigParser(path1, repo=repo) as config:
assert config._has_includes()
assert config._included_paths() == [("path", path2)]

# Ensure that config is ignored if case is incorrect.
with open(path1, "w") as stream:
stream.write(template.format("gitdir", git_dir.upper(), path2))

with GitConfigParser(path1, repo=repo) as config:
assert not config._has_includes()
assert config._included_paths() == []

# Ensure that config is included if case is ignored.
with open(path1, "w") as stream:
stream.write(template.format("gitdir/i", git_dir.upper(), path2))

with GitConfigParser(path1, repo=repo) as config:
assert config._has_includes()
assert config._included_paths() == [("path", path2)]

# Ensure that config is included with path using glob pattern.
with open(path1, "w") as stream:
stream.write(template.format("gitdir", "**/repo1", path2))

with GitConfigParser(path1, repo=repo) as config:
assert config._has_includes()
assert config._included_paths() == [("path", path2)]

# Ensure that config is ignored if path is not matching git_dir.
with open(path1, "w") as stream:
stream.write(template.format("gitdir", "incorrect", path2))

with GitConfigParser(path1, repo=repo) as config:
assert not config._has_includes()
assert config._included_paths() == []

@with_rw_directory
def test_conditional_includes_from_branch_name(self, rw_dir):
# Initiate mocked branch
branch = mock.Mock()
type(branch).name = mock.PropertyMock(return_value="/foo/branch")

# Initiate mocked repository
repo = mock.Mock(active_branch=branch)

# Initiate config files.
path1 = osp.join(rw_dir, "config1")
path2 = osp.join(rw_dir, "config2")
template = "[includeIf \"onbranch:{}\"]\n path={}\n"

# Ensure that config is included is branch is correct.
with open(path1, "w") as stream:
stream.write(template.format("/foo/branch", path2))

with GitConfigParser(path1, repo=repo) as config:
assert config._has_includes()
assert config._included_paths() == [("path", path2)]

# Ensure that config is included is branch is incorrect.
with open(path1, "w") as stream:
stream.write(template.format("incorrect", path2))

with GitConfigParser(path1, repo=repo) as config:
assert not config._has_includes()
assert config._included_paths() == []

# Ensure that config is included with branch using glob pattern.
with open(path1, "w") as stream:
stream.write(template.format("/foo/**", path2))

with GitConfigParser(path1, repo=repo) as config:
assert config._has_includes()
assert config._included_paths() == [("path", path2)]

def test_rename(self):
file_obj = self._to_memcache(fixture_path('git_config'))
with GitConfigParser(file_obj, read_only=False, merge_includes=False) as cw:
Expand Down