Ensure parent options retained for exec'ed finish
Retrieve parent parser arguments set by user, and ensure they are made available as part of the default script cmdline. This allows the logging from the exec'ed finish command called as the last step by git-rebase to log to the same file automatically. Change-Id: Ia79a54afe8ace7ade2aa5a29ad16855c9883bd54 Closes-Bug: #1625864
This commit is contained in:
parent
3b5e2415c3
commit
d8bb479880
|
@ -148,6 +148,11 @@ def main(argv=None):
|
|||
logger.fatal("Git-Upstream requires git version 1.7.5 or later")
|
||||
sys.exit(1)
|
||||
|
||||
for arg in argv:
|
||||
if arg in args.subcommands:
|
||||
break
|
||||
args.script_cmdline.append(arg)
|
||||
|
||||
try:
|
||||
args.cmd.run(args)
|
||||
except GitUpstreamError as e:
|
||||
|
|
|
@ -16,9 +16,18 @@
|
|||
"""Tests for the 'commands' module"""
|
||||
|
||||
from argparse import ArgumentParser
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
import mock
|
||||
import testtools
|
||||
from testtools import content
|
||||
from testtools import matchers
|
||||
|
||||
from git_upstream import commands as c
|
||||
from git_upstream import main
|
||||
from git_upstream.tests import base
|
||||
|
||||
|
||||
class TestGetSubcommands(testtools.TestCase):
|
||||
|
@ -34,3 +43,90 @@ class TestGetSubcommands(testtools.TestCase):
|
|||
len(subcommands.keys()))
|
||||
for command in subcommands.keys():
|
||||
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"))
|
||||
|
|
Loading…
Reference in New Issue