Skip to content
This repository has been archived by the owner on Aug 1, 2019. It is now read-only.

Commit

Permalink
Isolate tests from user/system config
Browse files Browse the repository at this point in the history
git-review behavior depends on git local, global and system configs.
tests have the same dependencies against them but only manage git local
config which implies the config expected by tests could not be the real
one because of git global and system configs.

The same trouble concerns gitreview global and system config files.

This change allows to run git-review in local mode (only using git and
gitreview local config files) by defining the environment variable
"GITREVIEW_LOCAL_MODE". This mode is used by tests to isolate them from
git and gitreview global and system configs. Mocking can not be used as
functional tests call not git-review code but git-review system command.

Change-Id: I598a284aec9e55f9618ab026c55ef0435e370d69
Closes-Bug: #1393192
  • Loading branch information
ZZelle committed Nov 17, 2014
1 parent e31646b commit abe76bf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
11 changes: 9 additions & 2 deletions git_review/cmd.py
Expand Up @@ -51,6 +51,7 @@

VERBOSE = False
UPDATE = False
LOCAL_MODE = 'GITREVIEW_LOCAL_MODE' in os.environ
CONFIGDIR = os.path.expanduser("~/.config/git-review")
GLOBAL_CONFIG = "/etc/git-review/git-review.conf"
USER_CONFIG = os.path.join(CONFIGDIR, "git-review.conf")
Expand Down Expand Up @@ -228,6 +229,9 @@ def git_config_get_value(section, option, default=None, as_bool=False):
cmd = ["git", "config", "--get", "%s.%s" % (section, option)]
if as_bool:
cmd.insert(2, "--bool")
if LOCAL_MODE:
__, git_dir = git_directories()
cmd[2:2] = ['-f', os.path.join(git_dir, 'config')]
try:
return run_command_exc(GitConfigException, *cmd).strip()
except GitConfigException as exc:
Expand Down Expand Up @@ -522,8 +526,11 @@ def get_config(config_file=None):
with the narrowest scope wins.
"""
config = DEFAULTS.copy()
for filename in (GLOBAL_CONFIG, USER_CONFIG, config_file):
if filename is not None and os.path.exists(filename):
filenames = [] if LOCAL_MODE else [GLOBAL_CONFIG, USER_CONFIG]
if config_file:
filenames.append(config_file)
for filename in filenames:
if os.path.exists(filename):
config.update(load_config_file(filename))
return config

Expand Down
4 changes: 4 additions & 0 deletions git_review/tests/__init__.py
Expand Up @@ -145,6 +145,10 @@ def setUp(self):
self.useFixture(fixtures.Timeout(2 * 60, True))
BaseGitReviewTestCase._test_counter += 1

# ensures git-review command runs in local mode (for functional tests)
self.useFixture(
fixtures.EnvironmentVariable('GITREVIEW_LOCAL_MODE', ''))

self.init_dirs()
ssh_addr, ssh_port, http_addr, http_port, self.site_dir = \
self._pick_gerrit_port_and_dir()
Expand Down
25 changes: 23 additions & 2 deletions git_review/tests/test_unit.py
Expand Up @@ -26,7 +26,28 @@
import mock
import testtools

import git_review
from git_review import cmd


class ConfigTestCase(testtools.TestCase):
"""Class testing config behavior."""

@mock.patch('git_review.cmd.LOCAL_MODE',
mock.PropertyMock(return_value=True))
@mock.patch('git_review.cmd.git_directories', return_value=['', 'fake'])
@mock.patch('git_review.cmd.run_command_exc')
def test_git_local_mode(self, run_mock, dir_mock):
cmd.git_config_get_value('abc', 'def')
run_mock.assert_called_once_with(
cmd.GitConfigException,
'git', 'config', '-f', 'fake/config', '--get', 'abc.def')

@mock.patch('git_review.cmd.LOCAL_MODE',
mock.PropertyMock(return_value=True))
@mock.patch('os.path.exists', return_value=False)
def test_gitreview_local_mode(self, exists_mock):
cmd.get_config()
self.assertFalse(exists_mock.called)


class GitReviewConsole(testtools.TestCase):
Expand Down Expand Up @@ -56,7 +77,7 @@ def test_list_reviews_no_blanks(self, mock_query):

mock_query.return_value = self.reviews
with mock.patch('sys.stdout', new_callable=io.StringIO) as output:
git_review.cmd.list_reviews(None)
cmd.list_reviews(None)
console_output = output.getvalue().split('\n')

wrapper = textwrap.TextWrapper(replace_whitespace=False,
Expand Down

0 comments on commit abe76bf

Please sign in to comment.