Merge "Ensure parent options retained for exec'ed finish"

This commit is contained in:
Jenkins
2016-09-29 16:07:39 +00:00
committed by Gerrit Code Review
2 changed files with 101 additions and 0 deletions

View File

@@ -148,6 +148,11 @@ def main(argv=None):
logger.fatal("Git-Upstream requires git version 1.7.5 or later") logger.fatal("Git-Upstream requires git version 1.7.5 or later")
sys.exit(1) sys.exit(1)
for arg in argv:
if arg in args.subcommands:
break
args.script_cmdline.append(arg)
try: try:
args.cmd.run(args) args.cmd.run(args)
except GitUpstreamError as e: except GitUpstreamError as e:

View File

@@ -16,9 +16,18 @@
"""Tests for the 'commands' module""" """Tests for the 'commands' module"""
from argparse import ArgumentParser from argparse import ArgumentParser
import os
import subprocess
import tempfile
import mock
import testtools import testtools
from testtools import content
from testtools import matchers
from git_upstream import commands as c from git_upstream import commands as c
from git_upstream import main
from git_upstream.tests import base
class TestGetSubcommands(testtools.TestCase): class TestGetSubcommands(testtools.TestCase):
@@ -34,3 +43,90 @@ class TestGetSubcommands(testtools.TestCase):
len(subcommands.keys())) len(subcommands.keys()))
for command in subcommands.keys(): for command in subcommands.keys():
self.assertIn(command, TestGetSubcommands._available_subcommands) self.assertIn(command, TestGetSubcommands._available_subcommands)
class TestMainParser(testtools.TestCase):
"""Test cases for main parser"""
def test_logging_options_kept(self):
argv = ["-v", "--log-level", "debug", "import"]
with mock.patch(
'git_upstream.commands.GitUpstreamCommand.run') as mock_import:
main.main(argv)
self.assertThat(mock_import.call_args[0][0].script_cmdline[2:],
matchers.Equals(argv[:3]))
class TestLoggingOptions(base.BaseTestCase):
"""Test cases for logging options behaviour"""
def setUp(self):
_, self.parser = main.build_parsers()
script_cmdline = self.parser.get_default('script_cmdline')
script_cmdline[-1] = os.path.join(os.getcwd(), main.__file__)
self.parser.set_defaults(script_cmdline=script_cmdline)
# builds the tree to be tested
super(TestLoggingOptions, self).setUp()
def test_logfile_contains_finish(self):
"""Confirm that logger calls in 'finish' phase recorded
Repository layout being checked
B---C local/master
/
A---D---E upstream/master
"""
tree = [
('A', []),
('B', ['A']),
('C', ['B']),
('D', ['A']),
('E', ['D']),
]
branches = {
'head': ('master', 'C'),
'upstream': ('upstream/master', 'E'),
}
self.gittree = base.BuildTree(self.testrepo, tree, branches.values())
cmdline = self.parser.get_default('script_cmdline')
tfile = None
try:
tfile = tempfile.NamedTemporaryFile(delete=False)
# need to close to allow reopen to write
tfile.close()
cmdline.extend(['-v', '--log-file', tfile.name, '--log-level',
'debug', 'import', '--into', 'master',
'upstream/master'])
try:
output = subprocess.check_output(cmdline,
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as cpe:
self.addDetail(
'subprocess-output',
content.text_content(cpe.output.decode('utf-8')))
raise
self.addDetail(
'subprocess-output',
content.text_content(output.decode('utf-8')))
logfile_contents = open(tfile.name, 'r').read()
finally:
if tfile and os.path.exists(tfile.name):
os.remove(tfile.name)
self.assertThat(
logfile_contents,
matchers.Contains("Merging by inverting the 'ours' strategy"))
self.assertThat(
logfile_contents,
matchers.Contains("Replacing tree contents with those from"))