OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Unit tests for git_cl.py.""" | 6 """Unit tests for git_cl.py.""" |
7 | 7 |
8 import os | 8 import os |
| 9 import StringIO |
9 import sys | 10 import sys |
10 import unittest | 11 import unittest |
11 | 12 |
12 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 13 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
13 | 14 |
14 from testing_support.auto_stub import TestCase | 15 from testing_support.auto_stub import TestCase |
15 | 16 |
16 import git_cl | 17 import git_cl |
17 import subprocess2 | 18 import subprocess2 |
18 | 19 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 return ['joe@example.com'] | 63 return ['joe@example.com'] |
63 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) | 64 self.mock(git_cl.watchlists, 'Watchlists', WatchlistsMock) |
64 # It's important to reset settings to not have inter-tests interference. | 65 # It's important to reset settings to not have inter-tests interference. |
65 git_cl.settings = None | 66 git_cl.settings = None |
66 | 67 |
67 def tearDown(self): | 68 def tearDown(self): |
68 if not self.has_failed(): | 69 if not self.has_failed(): |
69 self.assertEquals([], self.calls) | 70 self.assertEquals([], self.calls) |
70 super(TestGitCl, self).tearDown() | 71 super(TestGitCl, self).tearDown() |
71 | 72 |
| 73 @classmethod |
| 74 def _upload_calls(cls): |
| 75 return cls._git_base_calls() + cls._git_upload_calls() |
| 76 |
72 @staticmethod | 77 @staticmethod |
73 def _upload_calls(): | 78 def _git_base_calls(): |
74 return [ | 79 return [ |
75 (['git', 'update-index', '--refresh', '-q'], ''), | 80 (['git', 'update-index', '--refresh', '-q'], ''), |
76 (['git', 'diff-index', 'HEAD'], ''), | 81 (['git', 'diff-index', 'HEAD'], ''), |
77 (['git', 'config', 'rietveld.server'], 'codereview.example.com'), | 82 (['git', 'config', 'rietveld.server'], 'codereview.example.com'), |
78 (['git', 'symbolic-ref', 'HEAD'], 'master'), | 83 (['git', 'symbolic-ref', 'HEAD'], 'master'), |
79 (['git', 'config', 'branch.master.merge'], 'master'), | 84 (['git', 'config', 'branch.master.merge'], 'master'), |
80 (['git', 'config', 'branch.master.remote'], 'origin'), | 85 (['git', 'config', 'branch.master.remote'], 'origin'), |
81 (['git', 'rev-parse', '--show-cdup'], ''), | 86 (['git', 'rev-parse', '--show-cdup'], ''), |
82 (['git', 'rev-parse', 'HEAD'], '12345'), | 87 (['git', 'rev-parse', 'HEAD'], '12345'), |
83 (['git', 'diff', '--name-status', '-r', 'master...', '.'], | 88 (['git', 'diff', '--name-status', '-r', 'master...', '.'], |
84 'M\t.gitignore\n'), | 89 'M\t.gitignore\n'), |
85 (['git', 'rev-parse', '--git-dir'], '.git'), | 90 (['git', 'rev-parse', '--git-dir'], '.git'), |
86 (['git', 'config', 'branch.master.rietveldissue'], ''), | 91 (['git', 'config', 'branch.master.rietveldissue'], ''), |
87 (['git', 'config', 'branch.master.rietveldpatchset'], ''), | 92 (['git', 'config', 'branch.master.rietveldpatchset'], ''), |
88 (['git', 'log', '--pretty=format:%s%n%n%b', 'master...'], 'foo'), | 93 (['git', 'log', '--pretty=format:%s%n%n%b', 'master...'], 'foo'), |
89 (['git', 'config', 'user.email'], 'me@example.com'), | 94 (['git', 'config', 'user.email'], 'me@example.com'), |
90 (['git', 'diff', '--no-ext-diff', '--stat', '-M', 'master...'], '+dat'), | 95 (['git', 'diff', '--no-ext-diff', '--stat', '-M', 'master...'], '+dat'), |
91 (['git', 'log', '--pretty=format:%s\n\n%b', 'master..'], 'desc\n'), | 96 (['git', 'log', '--pretty=format:%s\n\n%b', 'master..'], 'desc\n'), |
| 97 ] |
| 98 |
| 99 @staticmethod |
| 100 def _git_upload_calls(): |
| 101 return [ |
92 (['git', 'config', 'rietveld.cc'], ''), | 102 (['git', 'config', 'rietveld.cc'], ''), |
93 (['git', 'config', '--get-regexp', '^svn-remote\\.'], (('', None), 0)), | 103 (['git', 'config', '--get-regexp', '^svn-remote\\.'], (('', None), 0)), |
94 (['git', 'rev-parse', '--show-cdup'], ''), | 104 (['git', 'rev-parse', '--show-cdup'], ''), |
95 (['git', 'svn', 'info'], ''), | 105 (['git', 'svn', 'info'], ''), |
96 (['git', 'config', 'branch.master.rietveldissue', '1'], ''), | 106 (['git', 'config', 'branch.master.rietveldissue', '1'], ''), |
97 (['git', 'config', 'branch.master.rietveldserver', | 107 (['git', 'config', 'branch.master.rietveldserver', |
98 'http://codereview.example.com'], ''), | 108 'http://codereview.example.com'], ''), |
99 (['git', 'config', 'branch.master.rietveldpatchset', '2'], ''), | 109 (['git', 'config', 'branch.master.rietveldpatchset', '2'], ''), |
100 ] | 110 ] |
101 | 111 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 # Handles multiple R= or TBR= lines. | 180 # Handles multiple R= or TBR= lines. |
171 description = ( | 181 description = ( |
172 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n') | 182 'Foo Bar\nTBR=reviewer@example.com\nBUG=\nR=another@example.com\n') |
173 self._run_reviewer_test( | 183 self._run_reviewer_test( |
174 [], | 184 [], |
175 'desc\n\nBUG=\nTEST=\n', | 185 'desc\n\nBUG=\nTEST=\n', |
176 description, | 186 description, |
177 description, | 187 description, |
178 ['--reviewers', 'reviewer@example.com,another@example.com']) | 188 ['--reviewers', 'reviewer@example.com,another@example.com']) |
179 | 189 |
| 190 def test_reviewer_send_mail(self): |
| 191 # --send-mail can be used without -r if R= is used |
| 192 description = 'Foo Bar\nR=reviewer@example.com\n' |
| 193 self._run_reviewer_test( |
| 194 ['--send-mail'], |
| 195 'desc\n\nBUG=\nTEST=\n', |
| 196 description.strip('\n'), |
| 197 description, |
| 198 ['--reviewers', 'reviewer@example.com', '--send_mail']) |
| 199 |
| 200 def test_reviewer_send_mail_no_rev(self): |
| 201 # Fails without a reviewer. |
| 202 class FileMock(object): |
| 203 buf = StringIO.StringIO() |
| 204 def write(self, content): |
| 205 self.buf.write(content) |
| 206 |
| 207 mock = FileMock() |
| 208 try: |
| 209 self.calls = self._git_base_calls() |
| 210 def RunEditor(desc, _): |
| 211 return desc |
| 212 self.mock(git_cl.gclient_utils, 'RunEditor', RunEditor) |
| 213 self.mock(sys, 'stderr', mock) |
| 214 git_cl.main(['upload', '--send-mail']) |
| 215 self.fail() |
| 216 except SystemExit: |
| 217 self.assertEquals( |
| 218 'Must specify reviewers to send email.\n', mock.buf.getvalue()) |
| 219 |
180 | 220 |
181 if __name__ == '__main__': | 221 if __name__ == '__main__': |
182 unittest.main() | 222 unittest.main() |
OLD | NEW |