From 1850bf6d1781fe078eb1570c4a9c0fb387de941a Mon Sep 17 00:00:00 2001 From: Josip Sokcevic Date: Wed, 18 Mar 2020 16:33:58 +0000 Subject: [PATCH] Add unit tests for git_find_releases R=ehmaldonado@chromium.org Change-Id: Ib11ada368fda13bbef62c8be26ddb9c78a528a92 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2107839 Reviewed-by: Edward Lesmes Commit-Queue: Josip Sokcevic --- git_find_releases.py | 6 ++-- tests/git_find_releases_test.py | 63 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100755 tests/git_find_releases_test.py diff --git a/git_find_releases.py b/git_find_releases.py index cab723dd35..8da917af6d 100755 --- a/git_find_releases.py +++ b/git_find_releases.py @@ -35,9 +35,9 @@ def GetMergesForCommit(sha1): 'cherry picked from commit %s' % sha1).splitlines()] -def main(): +def main(args): parser = optparse.OptionParser(usage=sys.modules[__name__].__doc__) - _, args = parser.parse_args() + _, args = parser.parse_args(args) if len(args) == 0: parser.error('Need at least one commit.') @@ -61,7 +61,7 @@ def main(): if __name__ == '__main__': try: - sys.exit(main()) + sys.exit(main(sys.argv[1:])) except KeyboardInterrupt: sys.stderr.write('interrupted\n') sys.exit(1) diff --git a/tests/git_find_releases_test.py b/tests/git_find_releases_test.py new file mode 100755 index 0000000000..30440bdd10 --- /dev/null +++ b/tests/git_find_releases_test.py @@ -0,0 +1,63 @@ +#!/usr/bin/env vpython3 +# coding=utf-8 +# Copyright 2020 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. +"""Unit tests for git_find_releases.py.""" + +from __future__ import print_function +from __future__ import unicode_literals + +import logging +import os +import sys +import unittest + +if sys.version_info.major == 2: + from StringIO import StringIO + import mock +else: + from io import StringIO + from unittest import mock + +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +import git_find_releases +import git_common + + +class TestGitFindReleases(unittest.TestCase): + @mock.patch('sys.stdout', StringIO()) + @mock.patch('git_common.run', return_value='') + def test_invalid_commit(self, git_run): + result = git_find_releases.main(['foo']) + self.assertEqual(1, result) + self.assertEqual('foo not found', sys.stdout.getvalue().strip()) + git_run.assert_called_once_with('name-rev', '--tags', '--name-only', 'foo') + + @mock.patch('sys.stdout', StringIO()) + @mock.patch('git_common.run') + def test_no_merge(self, git_run): + def git_run_function(*args): + assert len(args) > 1 + if args[0] == 'name-rev' and args[1] == '--tags': + return 'undefined' + elif args[0] == 'name-rev' and args[1] == '--refs': + return '1.0.0' + elif args[0] == 'log': + return '' + assert False, "Unexpected arguments for git.run" + + git_run.side_effect = git_run_function + result = git_find_releases.main(['foo']) + self.assertEqual(0, result) + stdout = sys.stdout.getvalue().strip() + self.assertIn('commit foo was', stdout) + self.assertIn('No merges found', stdout) + self.assertEqual(3, git_run.call_count) + + +if __name__ == '__main__': + logging.basicConfig( + level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) + unittest.main() \ No newline at end of file