Isolate tests from user/system config

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
This commit is contained in:
Cedric Brandily 2014-11-16 15:00:27 +01:00
parent e31646b3f2
commit abe76bfe28
3 changed files with 36 additions and 4 deletions

View File

@ -51,6 +51,7 @@ else:
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")
@ -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:
@ -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

View File

@ -145,6 +145,10 @@ class BaseGitReviewTestCase(testtools.TestCase, GerritHelpers):
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()

View File

@ -26,7 +26,28 @@ import textwrap
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):
@ -56,7 +77,7 @@ class GitReviewConsole(testtools.TestCase):
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,