diff --git a/gerrit_client.py b/gerrit_client.py index ccf78b56a..6b3e9dee3 100755 --- a/gerrit_client.py +++ b/gerrit_client.py @@ -16,8 +16,13 @@ import logging import optparse import subcommand import sys -import urllib -import urlparse + +if sys.version_info.major == 2: + import urlparse + from urllib import quote_plus +else: + from urllib.parse import quote_plus + import urllib.parse as urlparse import fix_encoding import gerrit_util @@ -38,8 +43,8 @@ def CMDbranchinfo(parser, args): (opt, args) = parser.parse_args(args) host = urlparse.urlparse(opt.host).netloc - project = urllib.quote_plus(opt.project) - branch = urllib.quote_plus(opt.branch) + project = quote_plus(opt.project) + branch = quote_plus(opt.branch) result = gerrit_util.GetGerritBranch(host, project, branch) logging.info(result) write_result(result, opt) @@ -51,11 +56,14 @@ def CMDbranch(parser, args): parser.add_option('--commit', dest='commit', help='commit hash') (opt, args) = parser.parse_args(args) + assert opt.project, "--project not defined" + assert opt.branch, "--branch not defined" + assert opt.commit, "--commit not defined" - project = urllib.quote_plus(opt.project) + project = quote_plus(opt.project) host = urlparse.urlparse(opt.host).netloc - branch = urllib.quote_plus(opt.branch) - commit = urllib.quote_plus(opt.commit) + branch = quote_plus(opt.branch) + commit = quote_plus(opt.commit) result = gerrit_util.CreateGerritBranch(host, project, branch, commit) logging.info(result) write_result(result, opt) @@ -92,6 +100,7 @@ def CMDabandon(parser, args): parser.add_option('-m', '--message', default='', help='reason for abandoning') (opt, args) = parser.parse_args(args) + assert opt.change, "-c not defined" result = gerrit_util.AbandonChange( urlparse.urlparse(opt.host).netloc, opt.change, opt.message) @@ -102,8 +111,7 @@ def CMDabandon(parser, args): class OptionParser(optparse.OptionParser): """Creates the option parse and add --verbose support.""" def __init__(self, *args, **kwargs): - optparse.OptionParser.__init__( - self, *args, prog='git cl', version=__version__, **kwargs) + optparse.OptionParser.__init__(self, *args, version=__version__, **kwargs) self.add_option( '--verbose', action='count', default=0, help='Use 2 times for more debugging info') @@ -114,6 +122,8 @@ class OptionParser(optparse.OptionParser): def parse_args(self, args=None, values=None): options, args = optparse.OptionParser.parse_args(self, args, values) + # Host is always required + assert options.host, "--host not defined." levels = [logging.WARNING, logging.INFO, logging.DEBUG] logging.basicConfig(level=levels[min(options.verbose, len(levels) - 1)]) return options, args diff --git a/tests/gerrit_client_test.py b/tests/gerrit_client_test.py new file mode 100755 index 000000000..90b0a8963 --- /dev/null +++ b/tests/gerrit_client_test.py @@ -0,0 +1,68 @@ +#!/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 gerrit_client.py.""" + +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 gerrit_client +import gerrit_util + + +class TestGerritClient(unittest.TestCase): + @mock.patch('gerrit_util.GetGerritBranch', return_value='') + def test_branch_info(self, util_mock): + gerrit_client.main([ + 'branchinfo', '--host', 'https://example.org/foo', '--project', + 'projectname', '--branch', 'branchname' + ]) + util_mock.assert_called_once_with('example.org', 'projectname', + 'branchname') + + @mock.patch('gerrit_util.CreateGerritBranch', return_value='') + def test_branch(self, util_mock): + gerrit_client.main([ + 'branch', '--host', 'https://example.org/foo', '--project', + 'projectname', '--branch', 'branchname', '--commit', 'commitname' + ]) + util_mock.assert_called_once_with('example.org', 'projectname', + 'branchname', 'commitname') + + @mock.patch('gerrit_util.QueryChanges', return_value='') + def test_changes(self, util_mock): + gerrit_client.main([ + 'changes', '--host', 'https://example.org/foo', '-p', 'foo=bar', '-p', + 'baz=qux', '--limit', '10', '--start', '20', '-o', 'op1', '-o', 'op2' + ]) + util_mock.assert_called_once_with( + 'example.org', [('foo', 'bar'), ('baz', 'qux')], + limit=10, + start=20, + o_params=['op1', 'op2']) + + @mock.patch('gerrit_util.AbandonChange', return_value='') + def test_abandon(self, util_mock): + gerrit_client.main([ + 'abandon', '--host', 'https://example.org/foo', '-c', '1', '-m', 'bar' + ]) + util_mock.assert_called_once_with('example.org', 1, 'bar') + + +if __name__ == '__main__': + logging.basicConfig( + level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) + unittest.main()