OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Unit tests for git_cl.py.""" |
| 7 |
| 8 import os |
| 9 import sys |
| 10 import unittest |
| 11 |
| 12 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 13 |
| 14 from testing_support.auto_stub import TestCase |
| 15 |
| 16 import git_cl |
| 17 import subprocess2 |
| 18 |
| 19 |
| 20 class TestGitCl(TestCase): |
| 21 def setUp(self): |
| 22 super(TestGitCl, self).setUp() |
| 23 self.calls = [] |
| 24 self._calls_done = 0 |
| 25 def mock_call(args, **kwargs): |
| 26 expected_args, result = self.calls.pop(0) |
| 27 self.assertEquals( |
| 28 expected_args, |
| 29 args, |
| 30 '@%d Expected: %r Actual: %r' % ( |
| 31 self._calls_done, expected_args, args)) |
| 32 self._calls_done += 1 |
| 33 return result |
| 34 self.mock(subprocess2, 'call', mock_call) |
| 35 self.mock(subprocess2, 'check_call', mock_call) |
| 36 self.mock(subprocess2, 'check_output', mock_call) |
| 37 self.mock(subprocess2, 'communicate', mock_call) |
| 38 self.mock(subprocess2, 'Popen', mock_call) |
| 39 |
| 40 self.mock(git_cl, 'FindCodereviewSettingsFile', lambda: '') |
| 41 |
| 42 class PresubmitMock(object): |
| 43 def __init__(self, *args, **kwargs): |
| 44 self.reviewers = [] |
| 45 @staticmethod |
| 46 def should_continue(): |
| 47 return True |
| 48 self.mock(git_cl.presubmit_support, 'DoPresubmitChecks', PresubmitMock) |
| 49 |
| 50 class RietveldMock(object): |
| 51 def __init__(self, *args, **kwargs): |
| 52 pass |
| 53 self.mock(git_cl.rietveld, 'Rietveld', RietveldMock) |
| 54 |
| 55 self.mock(git_cl.upload, 'RealMain', self.fail) |
| 56 |
| 57 class WatchlistsMock(object): |
| 58 def __init__(self, _): |
| 59 pass |
| 60 @staticmethod |
| 61 def GetWatchersForPaths(_): |
| 62 return ['joe@example.com'] |
| 63 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) |
| 64 # It's important to reset settings to not have inter-tests interference. |
| 65 git_cl.settings = None |
| 66 |
| 67 def tearDown(self): |
| 68 if not self.has_failed(): |
| 69 self.assertEquals([], self.calls) |
| 70 super(TestGitCl, self).tearDown() |
| 71 |
| 72 @staticmethod |
| 73 def _upload_calls(): |
| 74 return [ |
| 75 (['git', 'update-index', '--refresh', '-q'], ''), |
| 76 (['git', 'diff-index', 'HEAD'], ''), |
| 77 (['git', 'config', 'rietveld.server'], 'codereview.example.com'), |
| 78 (['git', 'symbolic-ref', 'HEAD'], 'master'), |
| 79 (['git', 'config', 'branch.master.merge'], 'master'), |
| 80 (['git', 'config', 'branch.master.remote'], 'origin'), |
| 81 (['git', 'rev-parse', '--show-cdup'], ''), |
| 82 (['git', 'rev-parse', 'HEAD'], '12345'), |
| 83 (['git', 'diff', '--name-status', '-r', 'master...', '.'], |
| 84 'M\t.gitignore\n'), |
| 85 (['git', 'rev-parse', '--git-dir'], '.git'), |
| 86 (['git', 'config', 'branch.master.rietveldissue'], ''), |
| 87 (['git', 'config', 'branch.master.rietveldpatchset'], ''), |
| 88 (['git', 'log', '--pretty=format:%s%n%n%b', 'master...'], 'foo'), |
| 89 (['git', 'config', 'user.email'], 'me@example.com'), |
| 90 (['git', 'diff', '--no-ext-diff', '--stat', '-M', 'master...'], '+dat'), |
| 91 (['git', 'log', '--pretty=format:%s\n\n%b', 'master..'], 'desc\n'), |
| 92 (['git', 'config', 'rietveld.cc'], ''), |
| 93 (['git', 'config', '--get-regexp', '^svn-remote\\.'], (('', None), 0)), |
| 94 (['git', 'rev-parse', '--show-cdup'], ''), |
| 95 (['git', 'svn', 'info'], ''), |
| 96 (['git', 'config', 'branch.master.rietveldissue', '1'], ''), |
| 97 (['git', 'config', 'branch.master.rietveldserver', |
| 98 'http://codereview.example.com'], ''), |
| 99 (['git', 'config', 'branch.master.rietveldpatchset', '2'], ''), |
| 100 ] |
| 101 |
| 102 @staticmethod |
| 103 def _cmd_line(description, args): |
| 104 """Returns the upload command line passed to upload.RealMain().""" |
| 105 msg = description.split('\n', 1)[0] |
| 106 return [ |
| 107 'upload', '--assume_yes', '--server', |
| 108 'http://codereview.example.com', |
| 109 '--message', msg, |
| 110 '--description', description |
| 111 ] + args + [ |
| 112 '--cc', 'joe@example.com', |
| 113 'master...' |
| 114 ] |
| 115 |
| 116 def _run_reviewer_test( |
| 117 self, |
| 118 upload_args, |
| 119 expected_description, |
| 120 returned_description, |
| 121 final_description, |
| 122 reviewers): |
| 123 """Generic reviewer test framework.""" |
| 124 self.calls = self._upload_calls() |
| 125 def RunEditor(desc, _): |
| 126 self.assertEquals( |
| 127 '# Enter a description of the change.\n' |
| 128 '# This will displayed on the codereview site.\n' |
| 129 '# The first line will also be used as the subject of the review.\n' + |
| 130 expected_description, |
| 131 desc) |
| 132 return returned_description |
| 133 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
| 134 def check_upload(args): |
| 135 self.assertEquals(self._cmd_line(final_description, reviewers), args) |
| 136 return 1, 2 |
| 137 self.mock(git_cl.upload, 'RealMain', check_upload) |
| 138 git_cl.main(['upload'] + upload_args) |
| 139 |
| 140 def test_no_reviewer(self): |
| 141 self._run_reviewer_test( |
| 142 [], |
| 143 'desc\n\nBUG=\nTEST=\n', |
| 144 '# Blah blah comment.\ndesc\n\nBUG=\nTEST=\n', |
| 145 'desc\n\nBUG=\nTEST=\n', |
| 146 []) |
| 147 |
| 148 def test_reviewers_cmd_line(self): |
| 149 # Reviewer is passed as-is |
| 150 description = 'desc\n\nR=foo@example.com\nBUG=\nTEST=\n' |
| 151 self._run_reviewer_test( |
| 152 ['-r' 'foo@example.com'], |
| 153 description, |
| 154 '\n%s\n' % description, |
| 155 description, |
| 156 ['--reviewers', 'foo@example.com']) |
| 157 |
| 158 def test_reviewer_tbr_overriden(self): |
| 159 # Reviewer is overriden with TBR |
| 160 # Also verifies the regexp work without a trailing LF |
| 161 description = 'Foo Bar\nTBR=reviewer@example.com\n' |
| 162 self._run_reviewer_test( |
| 163 ['-r' 'foo@example.com'], |
| 164 'desc\n\nR=foo@example.com\nBUG=\nTEST=\n', |
| 165 description.strip('\n'), |
| 166 description, |
| 167 ['--reviewers', 'reviewer@example.com']) |
| 168 |
| 169 def test_reviewer_multiple(self): |
| 170 # Handles multiple R= or TBR= lines. |
| 171 description = ( |
| 172 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n') |
| 173 self._run_reviewer_test( |
| 174 [], |
| 175 'desc\n\nBUG=\nTEST=\n', |
| 176 description, |
| 177 description, |
| 178 ['--reviewers', 'reviewer@example.com,another@example.com']) |
| 179 |
| 180 |
| 181 if __name__ == '__main__': |
| 182 unittest.main() |
OLD | NEW |