diff --git a/git_review/cmd.py b/git_review/cmd.py index 28cf862..19f7e34 100755 --- a/git_review/cmd.py +++ b/git_review/cmd.py @@ -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") @@ -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 diff --git a/git_review/tests/__init__.py b/git_review/tests/__init__.py index b945170..e606398 100644 --- a/git_review/tests/__init__.py +++ b/git_review/tests/__init__.py @@ -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() diff --git a/git_review/tests/test_unit.py b/git_review/tests/test_unit.py index c20149e..39f3bc8 100644 --- a/git_review/tests/test_unit.py +++ b/git_review/tests/test_unit.py @@ -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): @@ -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,