gerrit_client_test.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. from unittest import mock
  12. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  13. import gerrit_client
  14. class TestGerritClient(unittest.TestCase):
  15. @mock.patch('gerrit_util.GetGerritBranch', return_value='')
  16. def test_branch_info(self, util_mock):
  17. gerrit_client.main([
  18. 'branchinfo', '--host', 'https://example.org/foo', '--project',
  19. 'projectname', '--branch', 'branchname'
  20. ])
  21. util_mock.assert_called_once_with('example.org', 'projectname',
  22. 'branchname')
  23. @mock.patch('gerrit_util.CreateGerritBranch', return_value='')
  24. @mock.patch('gerrit_util.GetGerritBranch', return_value='')
  25. def test_branch(self, _, CreateGerritBranch_mock):
  26. gerrit_client.main([
  27. 'branch', '--host', 'https://example.org/foo', '--project',
  28. 'projectname', '--branch', 'branchname', '--commit', 'commitname'
  29. ])
  30. CreateGerritBranch_mock.assert_called_once_with('example.org',
  31. 'projectname',
  32. 'branchname',
  33. 'commitname')
  34. @mock.patch('gerrit_util.QueryChanges', return_value='')
  35. def test_changes(self, util_mock):
  36. gerrit_client.main([
  37. 'changes', '--host', 'https://example.org/foo', '-p', 'foo=bar',
  38. '-p', 'baz=qux', '--limit', '10', '--start', '20', '-o', 'op1',
  39. '-o', 'op2'
  40. ])
  41. util_mock.assert_called_once_with('example.org', [('foo', 'bar'),
  42. ('baz', 'qux')],
  43. first_param=None,
  44. limit=10,
  45. start=20,
  46. o_params=['op1', 'op2'])
  47. @mock.patch('gerrit_util.QueryChanges', return_value='')
  48. def test_changes_query(self, util_mock):
  49. gerrit_client.main([
  50. 'changes',
  51. '--host',
  52. 'https://example.org/foo',
  53. '--query',
  54. 'is:owner is:open',
  55. '--limit',
  56. '10',
  57. '--start',
  58. '20',
  59. ])
  60. util_mock.assert_called_once_with('example.org', [],
  61. first_param='is:owner is:open',
  62. limit=10,
  63. start=20,
  64. o_params=None)
  65. @mock.patch('gerrit_util.QueryChanges', return_value='')
  66. def test_changes_params_query(self, util_mock):
  67. gerrit_client.main([
  68. 'changes',
  69. '--host',
  70. 'https://example.org/foo',
  71. '--query',
  72. 'is:owner is:open',
  73. '-p',
  74. 'foo=bar',
  75. '--limit',
  76. '10',
  77. '--start',
  78. '20',
  79. ])
  80. util_mock.assert_called_once_with('example.org', [('foo', 'bar')],
  81. first_param='is:owner is:open',
  82. limit=10,
  83. start=20,
  84. o_params=None)
  85. @mock.patch('gerrit_util.GetRelatedChanges', return_value='')
  86. def test_relatedchanges(self, util_mock):
  87. gerrit_client.main([
  88. 'relatedchanges', '--host', 'https://example.org/foo', '--change',
  89. 'foo-change-id', '--revision', 'foo-revision-id'
  90. ])
  91. util_mock.assert_called_once_with('example.org',
  92. change='foo-change-id',
  93. revision='foo-revision-id')
  94. @mock.patch('gerrit_util.CreateChange', return_value={})
  95. def test_createchange(self, util_mock):
  96. gerrit_client.main([
  97. 'createchange', '--host', 'https://example.org/foo', '--project',
  98. 'project', '--branch', 'main', '--subject', 'subject', '-p',
  99. 'work_in_progress=true'
  100. ])
  101. util_mock.assert_called_once_with('example.org',
  102. 'project',
  103. branch='main',
  104. subject='subject',
  105. params=[('work_in_progress', 'true')])
  106. @mock.patch('builtins.open', mock.mock_open())
  107. @mock.patch('gerrit_util.ChangeEdit', return_value='')
  108. def test_changeedit(self, util_mock):
  109. open().read.return_value = 'test_data'
  110. gerrit_client.main([
  111. 'changeedit', '--host', 'https://example.org/foo', '--change', '1',
  112. '--path', 'path/to/file', '--file', '/my/foo'
  113. ])
  114. util_mock.assert_called_once_with('example.org', 1, 'path/to/file',
  115. 'test_data')
  116. @mock.patch('gerrit_util.PublishChangeEdit', return_value='')
  117. def test_publishchangeedit(self, util_mock):
  118. gerrit_client.main([
  119. 'publishchangeedit', '--host', 'https://example.org/foo',
  120. '--change', '1', '--notify', 'yes'
  121. ])
  122. util_mock.assert_called_once_with('example.org', 1, 'yes')
  123. @mock.patch('gerrit_util.AbandonChange', return_value='')
  124. def test_abandon(self, util_mock):
  125. gerrit_client.main([
  126. 'abandon', '--host', 'https://example.org/foo', '-c', '1', '-m',
  127. 'bar'
  128. ])
  129. util_mock.assert_called_once_with('example.org', 1, 'bar')
  130. @mock.patch('gerrit_util.SetReview', return_value='')
  131. def test_setlabel(self, util_mock):
  132. gerrit_client.main([
  133. 'setlabel',
  134. '--host',
  135. 'https://example.org/foo',
  136. '-c',
  137. '1',
  138. '-l',
  139. 'some-label',
  140. '-2',
  141. ])
  142. util_mock.assert_called_once_with('example.org',
  143. 1,
  144. labels={'some-label': '-2'})
  145. @mock.patch('gerrit_util.SetReview', return_value='')
  146. def test_addmessage(self, util_mock):
  147. gerrit_client.main([
  148. 'addmessage',
  149. '--host',
  150. 'https://example.org/foo',
  151. '-c',
  152. '1',
  153. '-r',
  154. '2',
  155. '-m',
  156. 'This is a message',
  157. ])
  158. util_mock.assert_called_once_with('example.org',
  159. 1,
  160. revision='2',
  161. msg='This is a message',
  162. automatic_attention_set_update=None)
  163. if __name__ == '__main__':
  164. logging.basicConfig(
  165. level=logging.DEBUG if '-v' in sys.argv else logging.ERROR)
  166. unittest.main()