From 9149c51d58fdfffa854ea1da4ce5938a2be93b8c Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Thu, 16 Oct 2014 18:51:28 +0100 Subject: [PATCH] Basic tests for import and fix for additional branches Some simple tests to ensure the basic command line behaviour of the import sub-command is correct and also fix the failure to detect the dropped additional branch on next import. Change rebaseeditor script to ensure that exceptions from running git-var are ignored. Change-Id: I23f7e0798130335cac5882f4357eb8094f72f7af --- git_upstream/commands/import.py | 10 +- git_upstream/lib/rebaseeditor.py | 4 +- git_upstream/tests/test_import_cmd.py | 160 ++++++++++++++++++++++++++ test-requirements.txt | 2 +- 4 files changed, 169 insertions(+), 7 deletions(-) create mode 100644 git_upstream/tests/test_import_cmd.py diff --git a/git_upstream/commands/import.py b/git_upstream/commands/import.py index 22293ea..2bd276a 100644 --- a/git_upstream/commands/import.py +++ b/git_upstream/commands/import.py @@ -589,12 +589,12 @@ def do_import(args): # merged. prev_import_merge = strategy[-1] if len(prev_import_merge.parents) > 1: - idx = next((idx for idx, commit in enumerate(prev_import_merge.parents) - if commit.hexsha == strategy.searcher.commit.hexsha), None) + idxs = [idx for idx, commit in enumerate(prev_import_merge.parents) + if commit.hexsha != strategy.searcher.commit.hexsha] - if idx: - additional_commits = prev_import_merge.parents[idx + 1:] - if additional_commits and not args.branches: + if idxs: + additional_commits = [prev_import_merge.parents[i] for i in idxs] + if additional_commits and len(args.branches) == 0: logger.warning("""\ **************** WARNING **************** Previous import merged additional branches but non have diff --git a/git_upstream/lib/rebaseeditor.py b/git_upstream/lib/rebaseeditor.py index 645b2a3..0427c11 100644 --- a/git_upstream/lib/rebaseeditor.py +++ b/git_upstream/lib/rebaseeditor.py @@ -193,4 +193,6 @@ class RebaseEditor(GitMixin, LogDedentMixin): @property def git_editor(self): - return os.environ.get("GIT_EDITOR", self.git.var("GIT_EDITOR")) + return os.environ.get("GIT_EDITOR", + self.git.var("GIT_EDITOR", + with_exceptions=False)) diff --git a/git_upstream/tests/test_import_cmd.py b/git_upstream/tests/test_import_cmd.py new file mode 100644 index 0000000..1f76ea6 --- /dev/null +++ b/git_upstream/tests/test_import_cmd.py @@ -0,0 +1,160 @@ +# Copyright (c) 2012, 2013, 2014 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Tests for the 'import' command""" + +import inspect + +import mock +from testtools.matchers import Equals + +from git_upstream import main +from git_upstream.tests.base import BaseTestCase + +from string import lower + + +class SubstringMatcher(): + def __init__(self, containing): + self.containing = lower(containing) + + def __eq__(self, other): + return lower(other).find(self.containing) > -1 + + def __unicode__(self): + return 'a string containing "%s"' % self.containing + + def __str__(self): + return unicode(self).encode('utf-8') + + __repr__ = __unicode__ + + +class TestImportCommand(BaseTestCase): + + commands, parser = main.get_parser() + + def test_basic(self): + """Test that default behaviour and options work + + Repository layout being checked (assumed already replayed) + + C---D local/master + / + A---B---E---F upstream/master + + """ + + tree = [ + ('A', []), + ('B', ['A']), + ('C', ['B']), + ('D', ['C']), + ('E', ['B']), + ('F', ['E']) + ] + + branches = { + 'head': ('master', 'D'), + 'upstream': ('upstream/master', 'F') + } + + self._build_git_tree(tree, branches.values()) + self.git.tag(inspect.currentframe().f_code.co_name, 'upstream/master') + args = self.parser.parse_args(['-q', 'import', 'upstream/master']) + self.assertThat(args.func(args), Equals(True), + "import command failed to complete succesfully") + + def test_basic_additional(self): + """Test that default behaviour and options work + + Repository layout being checked (assumed already replayed) + + C---D packaging/master + \ + E---F local/master + / + A---B---G---H upstream/master + + """ + + tree = [ + ('A', []), + ('B', ['A']), + ('C', []), + ('D', ['C']), + ('E', ['B', 'D']), + ('F', ['E']), + ('G', ['B']), + ('H', ['G']) + ] + + branches = { + 'head': ('master', 'F'), + 'upstream': ('upstream/master', 'H'), + 'packaging': ('packaging/master', 'D') + } + + self._build_git_tree(tree, branches.values()) + self.git.tag(inspect.currentframe().f_code.co_name, 'upstream/master') + args = self.parser.parse_args(['-q', 'import', 'upstream/master', + 'packaging/master']) + self.assertThat(args.func(args), Equals(True), + "import command failed to complete succesfully") + + def test_basic_additional_missed(self): + """Test that forgetting an additional branch that was previously + included results in a warning to the user. + + Repository layout being checked (assumed already replayed) + + C---D packaging/master + \ + E---F local/master + / + A---B---G---H upstream/master + + """ + + tree = [ + ('A', []), + ('B', ['A']), + ('C', []), + ('D', ['C']), + ('E', ['B', 'D']), + ('F', ['E']), + ('G', ['B']), + ('H', ['G']) + ] + + branches = { + 'head': ('master', 'F'), + 'upstream': ('upstream/master', 'H'), + 'packaging': ('packaging/master', 'D') + } + + self._build_git_tree(tree, branches.values()) + self.git.tag(inspect.currentframe().f_code.co_name, 'upstream/master') + args = self.parser.parse_args(['import', 'upstream/master']) + + mock_logger = mock.MagicMock() + with mock.patch('git_upstream.log.get_logger', + return_value=mock_logger): + self.assertThat(args.func(args), Equals(True), + "import command failed to complete succesfully") + + mock_logger.warning.assert_called_with( + SubstringMatcher( + containing="Previous import merged additional")) diff --git a/test-requirements.txt b/test-requirements.txt index c483483..f2efc91 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,5 +1,5 @@ hacking>=0.5.6,<0.8 - +mock Sphinx>=1.1.2,<1.2 discover fixtures>=0.3.14