Commit 22a9cf54 authored by Andrii Shyshkalov's avatar Andrii Shyshkalov Committed by Commit Bot

git_footers: fix bug stdin-based message input.

Also adds tests for the bug and for --json output.

R=agable@chromium.org, phajdan@chromium.org

Change-Id: I4e2208fdad8e23d48d27d0a354470336a7b86180
Reviewed-on: https://chromium-review.googlesource.com/570030Reviewed-by: 's avatarPaweł Hajdan Jr. <phajdan.jr@chromium.org>
Commit-Queue: Paweł Hajdan Jr. <phajdan.jr@chromium.org>
parent 02e59414
......@@ -192,8 +192,8 @@ def main(args):
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('ref', nargs='?', help="Git ref to retrieve footers from."
" Omit to parse stdin.")
parser.add_argument('ref', nargs='?', help='Git ref to retrieve footers from.'
' Omit to parse stdin.')
g = parser.add_mutually_exclusive_group()
g.add_argument('--key', metavar='KEY',
......@@ -202,14 +202,14 @@ def main(args):
g.add_argument('--position', action='store_true')
g.add_argument('--position-ref', action='store_true')
g.add_argument('--position-num', action='store_true')
g.add_argument('--json', help="filename to dump JSON serialized headers to.")
g.add_argument('--json', help='filename to dump JSON serialized footers to.')
opts = parser.parse_args(args)
if opts.ref:
message = git.run('log', '-1', '--format=%B', opts.ref)
else:
message = '\n'.join(l for l in sys.stdin)
message = sys.stdin.read()
footers = parse_footers(message)
......
......@@ -2,9 +2,11 @@
"""Tests for git_footers."""
import json
import os
import StringIO
import sys
import tempfile
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
......@@ -198,14 +200,22 @@ My commit message is my best friend. It is my life. I must master it.
def testReadStdin(self):
self.mock(git_footers.sys, 'stdin', StringIO.StringIO(
'line\r\notherline\r\n\r\n\r\nFoo: baz'))
'line\r\notherline\r\n\r\n\r\nFoo: baz\r\nStill: footer'))
stdout = StringIO.StringIO()
self.mock(git_footers.sys, 'stdout', stdout)
self.assertEqual(git_footers.main([]), 0)
self.assertEqual(stdout.getvalue(), "Foo: baz\n")
self.assertEqual(stdout.getvalue(), 'Still: footer\nFoo: baz\n')
def testToJson(self):
self.mock(git_footers.sys, 'stdin', StringIO.StringIO(
'line\r\nany spaces\r\n\r\n\r\nFoo: 1\nBar: 2\nFoo: 3'))
with tempfile.NamedTemporaryFile() as tmp:
self.assertEqual(git_footers.main(['--json', tmp.name]), 0)
js = json.load(open(tmp.name))
self.assertEqual(js, {'Foo': ['3', '1'], 'Bar': ['2']})
if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment