gerrit_client_test.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/env vpython3
  2. # coding=utf-8
  3. # Copyright 2020 The Chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. """Unit tests for gerrit_client.py."""
  7. import logging
  8. import os
  9. import sys
  10. import unittest
  11. if sys.version_info.major == 2:
  12. from StringIO import StringIO
  13. import mock
  14. BUILTIN_OPEN = '__builtin__.open'
  15. else:
  16. from io import StringIO
  17. from unittest import mock
  18. BUILTIN_OPEN = 'builtins.open'
  19. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  20. import gerrit_client
  21. import gerrit_util
  22. class TestGerritClient(unittest.TestCase):
  23. @mock.patch('gerrit_util.GetGerritBranch', return_value='')
  24. def test_branch_info(self, util_mock):
  25. gerrit_client.main([
  26. 'branchinfo', '--host', 'https://example.org/foo', '--project',
  27. 'projectname', '--branch', 'branchname'
  28. ])
  29. util_mock.assert_called_once_with('example.org', 'projectname',
  30. 'branchname')
  31. @mock.patch('gerrit_util.CreateGerritBranch', return_value='')
  32. def test_branch(self, util_mock):
  33. gerrit_client.main([
  34. 'branch', '--host', 'https://example.org/foo', '--project',
  35. 'projectname', '--branch', 'branchname', '--commit', 'commitname'
  36. ])
  37. util_mock.assert_called_once_with('example.org', 'projectname',
  38. 'branchname', 'commitname')
  39. @mock.patch('gerrit_util.QueryChanges', return_value='')
  40. def test_changes(self, util_mock):
  41. gerrit_client.main([
  42. 'changes', '--host', 'https://example.org/foo', '-p', 'foo=bar', '-p',
  43. 'baz=qux', '--limit', '10', '--start', '20', '-o', 'op1', '-o', 'op2'
  44. ])
  45. util_mock.assert_called_once_with('example.org', [('foo', 'bar'),
  46. ('baz', 'qux')],
  47. first_param=None,
  48. limit=10,
  49. start=20,
  50. o_params=['op1', 'op2'])
  51. @mock.patch('gerrit_util.QueryChanges', return_value='')
  52. def test_changes_query(self, util_mock):
  53. gerrit_client.main([
  54. 'changes',
  55. '--host',
  56. 'https://example.org/foo',
  57. '--query',
  58. 'is:owner is:open',
  59. '--limit',
  60. '10',
  61. '--start',
  62. '20',
  63. ])
  64. util_mock.assert_called_once_with('example.org', [],
  65. first_param='is:owner is:open',
  66. limit=10,
  67. start=20,
  68. o_params=None)
  69. @mock.patch('gerrit_util.QueryChanges', return_value='')
  70. def test_changes_params_query(self, util_mock):
  71. gerrit_client.main([
  72. 'changes',
  73. '--host',
  74. 'https://example.org/foo',
  75. '--query',
  76. 'is:owner is:open',
  77. '-p',
  78. 'foo=bar',
  79. '--limit',
  80. '10',
  81. '--start',
  82. '20',
  83. ])
  84. util_mock.assert_called_once_with('example.org', [('foo', 'bar')],
  85. first_param='is:owner is:open',
  86. limit=10,
  87. start=20,
  88. o_params=None)
  89. @mock.patch('gerrit_util.GetRelatedChanges', return_value='')
  90. def test_relatedchanges(self, util_mock):
  91. gerrit_client.main([
  92. 'relatedchanges', '--host', 'https://example.org/foo', '--change',
  93. 'foo-change-id', '--revision', 'foo-revision-id'
  94. ])
  95. util_mock.assert_called_once_with('example.org',
  96. change='foo-change-id',
  97. revision='foo-revision-id')
  98. @mock.patch('gerrit_util.CreateChange', return_value={})
  99. def test_createchange(self, util_mock):
  100. gerrit_client.main([
  101. 'createchange', '--host', 'https://example.org/foo', '--project',
  102. 'project', '--branch', 'main', '--subject', 'subject', '-p',
  103. 'work_in_progress=true'
  104. ])
  105. util_mock.assert_called_once_with('example.org',
  106. 'project',
  107. branch='main',
  108. subject='subject',
  109. params=[('work_in_progress', 'true')])
  110. @mock.patch(BUILTIN_OPEN, mock.mock_open())
  111. @mock.patch('gerrit_util.ChangeEdit', return_value='')
  112. def test_changeedit(self, util_mock):
  113. open().read.return_value = 'test_data'
  114. gerrit_client.main([
  115. 'changeedit', '--host', 'https://example.org/foo', '--change', '1',
  116. '--path', 'path/to/file', '--file', '/my/foo'
  117. ])
  118. util_mock.assert_called_once_with('example.org', 1, 'path/to/file',
  119. 'test_data')
  120. @mock.patch('gerrit_util.PublishChangeEdit', return_value='')
  121. def test_publishchangeedit(self, util_mock):
  122. gerrit_client.main([
  123. 'publishchangeedit', '--host', 'https://example.org/foo', '--change',
  124. '1', '--notify', 'yes'
  125. ])
  126. util_mock.assert_called_once_with('example.org', 1, 'yes')
  127. @mock.patch('gerrit_util.AbandonChange', return_value='')
  128. def test_abandon(self, util_mock):
  129. gerrit_client.main([
  130. 'abandon', '--host', 'https://example.org/foo', '-c', '1', '-m', 'bar'
  131. ])
  132. util_mock.assert_called_once_with('example.org', 1, 'bar')
  133. @mock.patch('gerrit_util.SetReview', return_value='')
  134. def test_setlabel(self, util_mock):
  135. gerrit_client.main([
  136. 'setlabel',
  137. '--host',
  138. 'https://example.org/foo',
  139. '-c',
  140. '1',
  141. '-l',
  142. 'some-label',
  143. '-2',
  144. ])
  145. util_mock.assert_called_once_with('example.org',
  146. 1,
  147. labels={'some-label': '-2'})
  148. if __name__ == '__main__':
  149. logging.basicConfig(
  150. level=logging.DEBUG if '-v' in sys.argv else logging.ERROR)
  151. unittest.main()