From 22a9cf54c9d62999ac15d77e16f779f646649121 Mon Sep 17 00:00:00 2001 From: Andrii Shyshkalov Date: Thu, 13 Jul 2017 14:23:58 +0200 Subject: [PATCH] git_footers: fix bug stdin-based message input. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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/570030 Reviewed-by: Paweł Hajdan Jr. Commit-Queue: Paweł Hajdan Jr. --- git_footers.py | 8 ++++---- tests/git_footers_test.py | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/git_footers.py b/git_footers.py index 9ce733d46..3f12d1735 100755 --- a/git_footers.py +++ b/git_footers.py @@ -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) diff --git a/tests/git_footers_test.py b/tests/git_footers_test.py index 1d537cee3..5aa3734df 100755 --- a/tests/git_footers_test.py +++ b/tests/git_footers_test.py @@ -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__':