OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 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 gcl.py.""" | |
7 | |
8 # pylint: disable=E1103,E1101,E1120 | |
9 | |
10 import os | |
11 import sys | |
12 | |
13 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
14 | |
15 from testing_support.super_mox import mox, SuperMoxTestBase | |
16 | |
17 import gcl | |
18 import presubmit_support | |
19 | |
20 | |
21 class GclTestsBase(SuperMoxTestBase): | |
22 """Setups and tear downs the mocks but doesn't test anything as-is.""" | |
23 def setUp(self): | |
24 SuperMoxTestBase.setUp(self) | |
25 self.fake_root_dir = self.RootDir() | |
26 self.mox.StubOutWithMock(gcl, 'RunShell') | |
27 self.mox.StubOutWithMock(gcl.SVN, '_CaptureInfo') | |
28 self.mox.StubOutWithMock(gcl.SVN, 'GetCheckoutRoot') | |
29 self.mox.StubOutWithMock(gcl, 'tempfile') | |
30 self.mox.StubOutWithMock(gcl.upload, 'RealMain') | |
31 self.mox.StubOutWithMock(gcl.gclient_utils, 'FileRead') | |
32 self.mox.StubOutWithMock(gcl.gclient_utils, 'FileWrite') | |
33 gcl.REPOSITORY_ROOT = None | |
34 self.old_review_settings = gcl.CODEREVIEW_SETTINGS | |
35 self.assertEquals(gcl.CODEREVIEW_SETTINGS, {}) | |
36 | |
37 def tearDown(self): | |
38 gcl.CODEREVIEW_SETTINGS = self.old_review_settings | |
39 | |
40 def fakeChange(self, files=None): # pylint: disable=R0201 | |
41 if files == None: | |
42 files = [('A', 'aa'), ('M', 'bb')] | |
43 | |
44 change_info = self.mox.CreateMock(gcl.ChangeInfo) | |
45 change_info.name = 'naame' | |
46 change_info.issue = 1 | |
47 change_info.patchset = 0 | |
48 change_info.description = 'deescription' | |
49 change_info.files = files | |
50 change_info.GetFiles = lambda : change_info.files | |
51 change_info.GetIssueDescription = lambda : change_info.description | |
52 change_info.GetFileNames = lambda : [f[1] for f in change_info.files] | |
53 change_info.GetLocalRoot = lambda : 'proout' | |
54 change_info.patch = None | |
55 change_info.rietveld = 'https://my_server' | |
56 change_info._closed = False | |
57 change_info._deleted = False | |
58 change_info._comments_added = [] | |
59 | |
60 class RpcServer(object): | |
61 # pylint: disable=R0201,W0613 | |
62 def get_issue_properties(self, issue, messages): | |
63 return { 'patchsets': [1337] } | |
64 change_info.RpcServer = RpcServer | |
65 | |
66 def AddComment(comment): | |
67 # pylint: disable=W0212 | |
68 change_info._comments_added.append(comment) | |
69 change_info.AddComment = AddComment | |
70 | |
71 def Delete(): | |
72 change_info._deleted = True | |
73 change_info.Delete = Delete | |
74 | |
75 def CloseIssue(): | |
76 change_info._closed = True | |
77 change_info.CloseIssue = CloseIssue | |
78 | |
79 return change_info | |
80 | |
81 | |
82 class GclUnittest(GclTestsBase): | |
83 """General gcl.py tests.""" | |
84 def tearDown(self): | |
85 gcl.CODEREVIEW_SETTINGS = {} | |
86 | |
87 def testMembersChanged(self): | |
88 self.mox.ReplayAll() | |
89 members = [ | |
90 'CODEREVIEW_SETTINGS', 'CODEREVIEW_SETTINGS_FILE', | |
91 'CODEREVIEW_SETTINGS_FILE_NOT_FOUND', | |
92 'CMDchange', 'CMDchanges', 'CMDcommit', 'CMDdelete', 'CMDdeleteempties', | |
93 'CMDdescription', 'CMDdiff', 'CMDhelp', 'CMDlint', 'CMDnothave', | |
94 'CMDopened', 'CMDpassthru', 'CMDpresubmit', 'CMDrename', 'CMDsettings', | |
95 'CMDstatus', 'CMDtry', 'CMDupload', | |
96 'ChangeInfo', 'Command', 'DEFAULT_LINT_IGNORE_REGEX', | |
97 'DEFAULT_LINT_REGEX', 'CheckHomeForFile', 'DoPresubmitChecks', | |
98 'ErrorExit', 'FILES_CACHE', 'FilterFlag', 'GenUsage', | |
99 'GenerateChangeName', 'GenerateDiff', 'GetCLs', 'GetCacheDir', | |
100 'GetCachedFile', 'GetChangelistInfoFile', 'GetChangesDir', | |
101 'GetCodeReviewSetting', 'GetFilesNotInCL', 'GetInfoDir', | |
102 'GetModifiedFiles', 'GetRepositoryRoot', 'GetTreeStatus', 'ListFiles', | |
103 'LoadChangelistInfoForMultiple', 'MISSING_TEST_MSG', | |
104 'OptionallyDoPresubmitChecks', 'REPOSITORY_ROOT', | |
105 'RunShell', 'RunShellWithReturnCode', 'SVN', | |
106 'TryChange', 'UnknownFiles', 'Warn', | |
107 'attrs', 'auth', 'breakpad', 'defer_attributes', 'fix_encoding', | |
108 'gclient_utils', 'git_cl', 'json', 'main', 'need_change', | |
109 'need_change_and_args', 'no_args', 'optparse', 'os', | |
110 'presubmit_support', 'random', 're', 'rietveld', | |
111 'ssl', 'string', 'subprocess2', 'sys', 'tempfile', 'time', | |
112 'upload', 'urllib2', | |
113 ] | |
114 # If this test fails, you should add the relevant test. | |
115 self.compareMembers(gcl, members) | |
116 | |
117 def testIsSVNMoved(self): | |
118 # TODO(maruel): TEST ME | |
119 pass | |
120 | |
121 def testGetSVNFileProperty(self): | |
122 # TODO(maruel): TEST ME | |
123 pass | |
124 | |
125 def testUnknownFiles(self): | |
126 # TODO(maruel): TEST ME | |
127 pass | |
128 | |
129 def testCheckHomeForFile(self): | |
130 # TODO(maruel): TEST ME | |
131 pass | |
132 | |
133 def testDefaultSettings(self): | |
134 self.assertEquals({}, gcl.CODEREVIEW_SETTINGS) | |
135 | |
136 def testGetCodeReviewSettingOk(self): | |
137 self.mox.StubOutWithMock(gcl, 'GetCachedFile') | |
138 gcl.GetCachedFile(gcl.CODEREVIEW_SETTINGS_FILE).AndReturn( | |
139 'foo:bar\n' | |
140 '# comment\n' | |
141 ' c : d \n\r' | |
142 'e: f') | |
143 self.mox.ReplayAll() | |
144 self.assertEquals('bar', gcl.GetCodeReviewSetting('foo')) | |
145 self.assertEquals('d', gcl.GetCodeReviewSetting('c')) | |
146 self.assertEquals('f', gcl.GetCodeReviewSetting('e')) | |
147 self.assertEquals('', gcl.GetCodeReviewSetting('other')) | |
148 self.assertEquals( | |
149 {'foo': 'bar', 'c': 'd', 'e': 'f', '__just_initialized': None}, | |
150 gcl.CODEREVIEW_SETTINGS) | |
151 | |
152 def testGetCodeReviewSettingFail(self): | |
153 self.mox.StubOutWithMock(gcl, 'GetCachedFile') | |
154 gcl.GetCachedFile(gcl.CODEREVIEW_SETTINGS_FILE).AndReturn( | |
155 'aaa\n' | |
156 ' c : d \n\r' | |
157 'e: f') | |
158 self.mox.ReplayAll() | |
159 try: | |
160 gcl.GetCodeReviewSetting('c') | |
161 self.fail() | |
162 except gcl.gclient_utils.Error: | |
163 pass | |
164 self.assertEquals({}, gcl.CODEREVIEW_SETTINGS) | |
165 | |
166 def testGetRepositoryRootNone(self): | |
167 gcl.os.getcwd().AndReturn(self.fake_root_dir) | |
168 gcl.SVN.GetCheckoutRoot(self.fake_root_dir).AndReturn(None) | |
169 self.mox.ReplayAll() | |
170 self.assertRaises(gcl.gclient_utils.Error, gcl.GetRepositoryRoot) | |
171 | |
172 def testGetRepositoryRootGood(self): | |
173 root_path = gcl.os.path.join('bleh', 'prout', 'pouet') | |
174 gcl.os.getcwd().AndReturn(root_path) | |
175 gcl.SVN.GetCheckoutRoot(root_path).AndReturn(root_path + '.~') | |
176 self.mox.ReplayAll() | |
177 self.assertEquals(gcl.GetRepositoryRoot(), root_path + '.~') | |
178 | |
179 def testHelp(self): | |
180 gcl.sys.stdout.write = lambda x: None | |
181 self.mox.ReplayAll() | |
182 gcl.CMDhelp([]) | |
183 | |
184 | |
185 class ChangeInfoUnittest(GclTestsBase): | |
186 def setUp(self): | |
187 GclTestsBase.setUp(self) | |
188 self.mox.StubOutWithMock(gcl, 'GetChangelistInfoFile') | |
189 self.mox.StubOutWithMock(gcl, 'GetRepositoryRoot') | |
190 | |
191 def testChangeInfoMembers(self): | |
192 self.mox.ReplayAll() | |
193 members = [ | |
194 'AddComment', 'CloseIssue', 'Delete', 'Exists', 'GetFiles', | |
195 'GetApprovingReviewers', 'GetFileNames', 'GetIssueDescription', | |
196 'GetLocalRoot', 'Load', | |
197 'MissingTests', 'NeedsUpload', 'PrimeLint', 'RpcServer', 'Save', | |
198 'SendToRietveld', | |
199 'SEPARATOR', | |
200 'UpdateDescriptionFromIssue', 'UpdateRietveldDescription', | |
201 'append_footer', | |
202 'description', 'force_description', 'get_reviewers', 'issue', 'name', | |
203 'needs_upload', 'patch', 'patchset', 'rietveld', 'update_reviewers', | |
204 ] | |
205 # If this test fails, you should add the relevant test. | |
206 self.compareMembers( | |
207 gcl.ChangeInfo('', 0, 0, '', None, self.fake_root_dir, 'foo', False), | |
208 members) | |
209 | |
210 def testChangeInfoBase(self): | |
211 files = [('M', 'foo'), ('A', 'bar')] | |
212 self.mox.ReplayAll() | |
213 o = gcl.ChangeInfo( | |
214 'name2', | |
215 '42', | |
216 '53', | |
217 'description2', | |
218 files, | |
219 self.fake_root_dir, | |
220 'foo', | |
221 False) | |
222 self.assertEquals(o.name, 'name2') | |
223 self.assertEquals(o.issue, 42) | |
224 self.assertEquals(o.patchset, 53) | |
225 self.assertEquals(o.description, 'description2') | |
226 self.assertEquals(o.patch, None) | |
227 self.assertEquals(o.GetFileNames(), ['foo', 'bar']) | |
228 self.assertEquals(o.GetFiles(), files) | |
229 self.assertEquals(o.GetLocalRoot(), self.fake_root_dir) | |
230 | |
231 def testLoadWithIssue(self): | |
232 self.mox.StubOutWithMock(gcl, 'GetCodeReviewSetting') | |
233 description = ["This is some description.", "force an extra separator."] | |
234 gcl.GetChangelistInfoFile('bleh').AndReturn('bleeeh') | |
235 gcl.os.path.exists('bleeeh').AndReturn(True) | |
236 gcl.gclient_utils.FileRead('bleeeh').AndReturn( | |
237 gcl.ChangeInfo.SEPARATOR.join(["42, 53", "G b.cc"] + description)) | |
238 gcl.GetCodeReviewSetting('CODE_REVIEW_SERVER').AndReturn('foo') | |
239 # Does an upgrade. | |
240 gcl.GetChangelistInfoFile('bleh').AndReturn('bleeeh') | |
241 gcl.gclient_utils.FileWrite('bleeeh', mox.IgnoreArg()) | |
242 self.mox.ReplayAll() | |
243 | |
244 change_info = gcl.ChangeInfo.Load('bleh', self.fake_root_dir, True, False) | |
245 self.assertEquals(change_info.name, 'bleh') | |
246 self.assertEquals(change_info.issue, 42) | |
247 self.assertEquals(change_info.patchset, 53) | |
248 self.assertEquals(change_info.description, | |
249 gcl.ChangeInfo.SEPARATOR.join(description)) | |
250 self.assertEquals(change_info.GetFiles(), [('G ', 'b.cc')]) | |
251 | |
252 def testLoadEmpty(self): | |
253 self.mox.StubOutWithMock(gcl, 'GetCodeReviewSetting') | |
254 gcl.GetChangelistInfoFile('bleh').AndReturn('bleeeh') | |
255 gcl.os.path.exists('bleeeh').AndReturn(True) | |
256 gcl.gclient_utils.FileRead('bleeeh').AndReturn( | |
257 gcl.ChangeInfo.SEPARATOR.join(["", "", ""])) | |
258 gcl.GetCodeReviewSetting('CODE_REVIEW_SERVER').AndReturn('foo') | |
259 # Does an upgrade. | |
260 gcl.GetChangelistInfoFile('bleh').AndReturn('bleeeh') | |
261 gcl.gclient_utils.FileWrite('bleeeh', mox.IgnoreArg()) | |
262 self.mox.ReplayAll() | |
263 | |
264 change_info = gcl.ChangeInfo.Load('bleh', self.fake_root_dir, True, False) | |
265 self.assertEquals(change_info.name, 'bleh') | |
266 self.assertEquals(change_info.issue, 0) | |
267 self.assertEquals(change_info.patchset, 0) | |
268 self.assertEquals(change_info.description, "") | |
269 self.assertEquals(change_info.GetFiles(), []) | |
270 | |
271 def testSaveEmpty(self): | |
272 gcl.GetChangelistInfoFile('').AndReturn('foo') | |
273 values = { | |
274 'description': '', 'patchset': 2, 'issue': 1, | |
275 'files': [], 'needs_upload': False, 'rietveld': 'https://foo'} | |
276 gcl.gclient_utils.FileWrite( | |
277 'foo', gcl.json.dumps(values, sort_keys=True, indent=2)) | |
278 self.mox.ReplayAll() | |
279 | |
280 change_info = gcl.ChangeInfo( | |
281 '', 1, 2, '', None, self.fake_root_dir, 'foo', False) | |
282 change_info.Save() | |
283 | |
284 def testSaveDirty(self): | |
285 gcl.GetChangelistInfoFile('n').AndReturn('foo') | |
286 values = { | |
287 'description': 'des', 'patchset': 0, 'issue': 0, | |
288 'files': [], 'needs_upload': True, 'rietveld': 'https://foo'} | |
289 gcl.gclient_utils.FileWrite( | |
290 'foo', gcl.json.dumps(values, sort_keys=True, indent=2)) | |
291 self.mox.ReplayAll() | |
292 | |
293 change_info = gcl.ChangeInfo('n', 0, 0, 'des', None, self.fake_root_dir, | |
294 'foo', needs_upload=True) | |
295 change_info.Save() | |
296 | |
297 | |
298 class CMDuploadUnittest(GclTestsBase): | |
299 def setUp(self): | |
300 GclTestsBase.setUp(self) | |
301 self.mox.StubOutWithMock(gcl, 'CheckHomeForFile') | |
302 self.mox.StubOutWithMock(gcl, 'DoPresubmitChecks') | |
303 self.mox.StubOutWithMock(gcl, 'GenerateDiff') | |
304 self.mox.StubOutWithMock(gcl, 'GetCodeReviewSetting') | |
305 self.mox.StubOutWithMock(gcl, 'GetRepositoryRoot') | |
306 self.mox.StubOutWithMock(gcl.ChangeInfo, 'SendToRietveld') | |
307 self.mox.StubOutWithMock(gcl, 'TryChange') | |
308 self.mox.StubOutWithMock(gcl.ChangeInfo, 'Load') | |
309 | |
310 def testNew(self): | |
311 change_info = self.mox.CreateMock(gcl.ChangeInfo) | |
312 change_info.name = 'naame' | |
313 change_info.issue = 1 | |
314 change_info.patchset = 0 | |
315 change_info.description = 'deescription\n\nR=foo@bar.com', | |
316 change_info.files = [('A', 'aa'), ('M', 'bb')] | |
317 change_info.patch = None | |
318 change_info.rietveld = 'https://my_server' | |
319 files = [item[1] for item in change_info.files] | |
320 output = presubmit_support.PresubmitOutput() | |
321 gcl.DoPresubmitChecks(change_info, False, True).AndReturn(output) | |
322 #gcl.GetCodeReviewSetting('CODE_REVIEW_SERVER').AndReturn('my_server') | |
323 gcl.os.getcwd().AndReturn('somewhere') | |
324 change_info.GetFiles().AndReturn(change_info.files) | |
325 gcl.os.chdir('proout') | |
326 change_info.get_reviewers().AndReturn('foo@bar.com') | |
327 change_info.GetFileNames().AndReturn(files) | |
328 gcl.GenerateDiff(files) | |
329 gcl.upload.RealMain(['upload.py', '-y', '--server=https://my_server', | |
330 '-r', 'georges@example.com', | |
331 '--issue=1', '--title= '], | |
332 change_info.patch).AndReturn(("1", | |
333 "2")) | |
334 change_info.GetLocalRoot().AndReturn('proout') | |
335 change_info.Save() | |
336 change_info.PrimeLint() | |
337 gcl.os.chdir('somewhere') | |
338 gcl.sys.stdout.write("*** Upload does not submit a try; use gcl try to" | |
339 " submit a try. ***") | |
340 gcl.sys.stdout.write("\n") | |
341 gcl.GetRepositoryRoot().AndReturn(self.fake_root_dir) | |
342 gcl.ChangeInfo.Load('naame', self.fake_root_dir, True, True | |
343 ).AndReturn(change_info) | |
344 self.mox.ReplayAll() | |
345 | |
346 gcl.CMDupload(['naame', '-r', 'georges@example.com']) | |
347 self.checkstdout('*** Upload does not submit a try; use gcl try to submit ' | |
348 'a try. ***\n' | |
349 '*** Upload does not submit a try; use gcl try to submit a try. ***\n') | |
350 | |
351 def testServerOverride(self): | |
352 change_info = gcl.ChangeInfo( | |
353 'naame', | |
354 0, | |
355 0, | |
356 'deescription', | |
357 [('A', 'aa'), ('M', 'bb')], | |
358 self.fake_root_dir, | |
359 'my_server', | |
360 False) | |
361 self.mox.StubOutWithMock(change_info, 'Save') | |
362 change_info.Save() | |
363 output = presubmit_support.PresubmitOutput() | |
364 gcl.DoPresubmitChecks(change_info, False, True).AndReturn(output) | |
365 gcl.tempfile.mkstemp(text=True).AndReturn((42, 'descfile')) | |
366 gcl.os.write(42, change_info.description) | |
367 gcl.os.close(42) | |
368 gcl.GetCodeReviewSetting('CC_LIST') | |
369 gcl.GetCodeReviewSetting('PRIVATE') | |
370 gcl.GetCodeReviewSetting('PROJECT') | |
371 gcl.os.getcwd().AndReturn('somewhere') | |
372 gcl.os.chdir(change_info.GetLocalRoot()) | |
373 gcl.GenerateDiff(change_info.GetFileNames()) | |
374 gcl.upload.RealMain( | |
375 [ 'upload.py', '-y', '--server=https://my_server', '--server=a', | |
376 '--file=descfile'], | |
377 change_info.patch).AndReturn(("1", "2")) | |
378 gcl.os.remove('descfile') | |
379 change_info.SendToRietveld("/lint/issue%s_%s" % ('1', '2'), timeout=60) | |
380 gcl.os.chdir('somewhere') | |
381 gcl.sys.stdout.write("*** Upload does not submit a try; use gcl try to" | |
382 " submit a try. ***") | |
383 gcl.sys.stdout.write("\n") | |
384 gcl.GetRepositoryRoot().AndReturn(self.fake_root_dir) | |
385 gcl.ChangeInfo.Load('naame', self.fake_root_dir, True, True | |
386 ).AndReturn(change_info) | |
387 self.mox.ReplayAll() | |
388 | |
389 gcl.CMDupload(['naame', '--server=a', '--no_watchlists']) | |
390 self.checkstdout('*** Upload does not submit a try; use gcl try to submit ' | |
391 'a try. ***\n' | |
392 '*** Upload does not submit a try; use gcl try to submit a try. ***\n') | |
393 | |
394 def testNormal(self): | |
395 change_info = gcl.ChangeInfo( | |
396 'naame', | |
397 0, | |
398 0, | |
399 'deescription', | |
400 [('A', 'aa'), ('M', 'bb')], | |
401 self.fake_root_dir, | |
402 'my_server', | |
403 False) | |
404 self.mox.StubOutWithMock(change_info, 'Save') | |
405 change_info.Save() | |
406 output = presubmit_support.PresubmitOutput() | |
407 gcl.DoPresubmitChecks(change_info, False, True).AndReturn(output) | |
408 gcl.tempfile.mkstemp(text=True).AndReturn((42, 'descfile')) | |
409 gcl.os.write(42, change_info.description) | |
410 gcl.os.close(42) | |
411 gcl.GetCodeReviewSetting('CC_LIST') | |
412 gcl.GetCodeReviewSetting('PRIVATE') | |
413 gcl.GetCodeReviewSetting('PROJECT') | |
414 gcl.os.getcwd().AndReturn('somewhere') | |
415 gcl.os.chdir(change_info.GetLocalRoot()) | |
416 gcl.GenerateDiff(change_info.GetFileNames()) | |
417 gcl.upload.RealMain( | |
418 ['upload.py', '-y', '--server=https://my_server', "--file=descfile" ], | |
419 change_info.patch).AndReturn(("1", "2")) | |
420 gcl.os.remove('descfile') | |
421 change_info.SendToRietveld("/lint/issue%s_%s" % ('1', '2'), timeout=60) | |
422 gcl.os.chdir('somewhere') | |
423 gcl.sys.stdout.write("*** Upload does not submit a try; use gcl try to" | |
424 " submit a try. ***") | |
425 gcl.sys.stdout.write("\n") | |
426 gcl.GetRepositoryRoot().AndReturn(self.fake_root_dir) | |
427 gcl.ChangeInfo.Load('naame', self.fake_root_dir, True, True | |
428 ).AndReturn(change_info) | |
429 self.mox.ReplayAll() | |
430 | |
431 gcl.CMDupload(['naame', '--no_watchlists']) | |
432 self.assertEquals(change_info.issue, 1) | |
433 self.assertEquals(change_info.patchset, 2) | |
434 self.checkstdout('*** Upload does not submit a try; use gcl try to submit ' | |
435 'a try. ***\n' | |
436 '*** Upload does not submit a try; use gcl try to submit a try. ***\n') | |
437 | |
438 def testNoServer(self): | |
439 self.mox.StubOutWithMock(gcl.sys, 'stderr') | |
440 gcl.sys.stderr.write( | |
441 'Don\'t use the -s flag, fix codereview.settings instead') | |
442 gcl.sys.stderr.write('\n') | |
443 gcl.GetRepositoryRoot().AndReturn(self.fake_root_dir) | |
444 gcl.ChangeInfo.Load('naame', self.fake_root_dir, True, True | |
445 ).AndReturn(1) | |
446 self.mox.ReplayAll() | |
447 | |
448 try: | |
449 gcl.CMDupload(['naame', '-s', 'foo']) | |
450 self.fail() | |
451 except SystemExit: | |
452 pass | |
453 | |
454 def testReviewersInDescription(self): | |
455 change_info = self.mox.CreateMock(gcl.ChangeInfo) | |
456 change_info.name = 'naame' | |
457 change_info.issue = 1 | |
458 change_info.patchset = 0 | |
459 change_info.description = 'deescription\n\nR=georges@example.com', | |
460 change_info.files = [('A', 'aa'), ('M', 'bb')] | |
461 change_info.patch = None | |
462 change_info.rietveld = 'https://my_server' | |
463 files = [item[1] for item in change_info.files] | |
464 output = presubmit_support.PresubmitOutput() | |
465 gcl.DoPresubmitChecks(change_info, False, True).AndReturn(output) | |
466 #gcl.GetCodeReviewSetting('CODE_REVIEW_SERVER').AndReturn('my_server') | |
467 gcl.os.getcwd().AndReturn('somewhere') | |
468 change_info.GetFiles().AndReturn(change_info.files) | |
469 change_info.get_reviewers().AndReturn(['georges@example.com']) | |
470 change_info.GetFileNames().AndReturn(files) | |
471 change_info.GetLocalRoot().AndReturn('proout') | |
472 gcl.os.chdir('proout') | |
473 gcl.GenerateDiff(files) | |
474 gcl.upload.RealMain(['upload.py', '-y', '--server=https://my_server', | |
475 '--reviewers=georges@example.com', | |
476 '--issue=1', '--title= '], | |
477 change_info.patch).AndReturn(("1", "2")) | |
478 change_info.Save() | |
479 change_info.PrimeLint() | |
480 gcl.os.chdir('somewhere') | |
481 gcl.sys.stdout.write("*** Upload does not submit a try; use gcl try to" | |
482 " submit a try. ***") | |
483 gcl.sys.stdout.write("\n") | |
484 gcl.GetRepositoryRoot().AndReturn(self.fake_root_dir) | |
485 gcl.ChangeInfo.Load('naame', self.fake_root_dir, True, True | |
486 ).AndReturn(change_info) | |
487 self.mox.ReplayAll() | |
488 | |
489 gcl.CMDupload(['naame']) | |
490 self.checkstdout('*** Upload does not submit a try; use gcl try to submit ' | |
491 'a try. ***\n' | |
492 '*** Upload does not submit a try; use gcl try to submit a try. ***\n') | |
493 | |
494 def testSuggestReviewers(self): | |
495 change_info = self.fakeChange() | |
496 output = presubmit_support.PresubmitOutput() | |
497 output.reviewers = ['foo@example.com', 'bar@example.com'] | |
498 gcl.DoPresubmitChecks(change_info, False, True).AndReturn(output) | |
499 #gcl.GetCodeReviewSetting('CODE_REVIEW_SERVER').AndReturn('my_server') | |
500 gcl.os.getcwd().AndReturn('somewhere') | |
501 gcl.os.chdir('proout') | |
502 gcl.GenerateDiff(change_info.GetFileNames()) | |
503 gcl.upload.RealMain(['upload.py', '-y', '--server=https://my_server', | |
504 '--reviewers=foo@example.com,bar@example.com', | |
505 '--issue=1', '--title= '], | |
506 change_info.patch).AndReturn(("1", "2")) | |
507 change_info.get_reviewers().AndReturn(['foo@example.com,bar@example.com']) | |
508 change_info.Save() | |
509 change_info.PrimeLint() | |
510 gcl.os.chdir('somewhere') | |
511 gcl.sys.stdout.write("*** Upload does not submit a try; use gcl try to" | |
512 " submit a try. ***") | |
513 gcl.sys.stdout.write("\n") | |
514 gcl.GetRepositoryRoot().AndReturn(self.fake_root_dir) | |
515 gcl.ChangeInfo.Load('naame', self.fake_root_dir, True, True | |
516 ).AndReturn(change_info) | |
517 self.mox.ReplayAll() | |
518 | |
519 gcl.CMDupload(['naame']) | |
520 self.checkstdout('*** Upload does not submit a try; use gcl try to submit ' | |
521 'a try. ***\n' | |
522 '*** Upload does not submit a try; use gcl try to submit a try. ***\n') | |
523 | |
524 | |
525 class CMDCommitUnittest(GclTestsBase): | |
526 def mockLoad(self, files=None): | |
527 self.mox.StubOutWithMock(gcl, 'GetRepositoryRoot') | |
528 self.mox.StubOutWithMock(gcl.ChangeInfo, 'Load') | |
529 gcl.GetRepositoryRoot().AndReturn(self.fake_root_dir) | |
530 change_info = self.fakeChange(files) | |
531 gcl.ChangeInfo.Load('naame', self.fake_root_dir, True, True | |
532 ).AndReturn(change_info) | |
533 return change_info | |
534 | |
535 def mockPresubmit(self, change_info, fail): | |
536 self.mox.StubOutWithMock(gcl, 'OptionallyDoPresubmitChecks') | |
537 output = presubmit_support.PresubmitOutput() | |
538 if fail: | |
539 output.fail() | |
540 gcl.OptionallyDoPresubmitChecks(change_info, True, []).AndReturn(output) | |
541 | |
542 def mockCommit(self, change_info, commit_message, shell_output): | |
543 gcl.tempfile.mkstemp(text=True).AndReturn((42, 'commit')) | |
544 gcl.os.write(42, commit_message) | |
545 gcl.os.close(42) | |
546 gcl.tempfile.mkstemp(text=True).AndReturn((43, 'files')) | |
547 gcl.os.write(43, '\n'.join(change_info.GetFileNames())) | |
548 gcl.os.close(43) | |
549 | |
550 gcl.RunShell(['svn', 'commit', '--file=commit', '--targets=files'], | |
551 True).AndReturn(shell_output) | |
552 if 'Committed' in shell_output: | |
553 self.mox.StubOutWithMock(gcl, 'GetCodeReviewSetting') | |
554 gcl.GetCodeReviewSetting('VIEW_VC').AndReturn('http://view/') | |
555 | |
556 gcl.os.remove('commit') | |
557 gcl.os.remove('files') | |
558 | |
559 def testPresubmitEmpty(self): | |
560 self.mockLoad(files=[]) | |
561 self.mox.ReplayAll() | |
562 | |
563 retval = gcl.CMDcommit(['naame']) | |
564 | |
565 self.assertEquals(retval, 1) | |
566 | |
567 def testPresubmitFails(self): | |
568 change_info = self.mockLoad() | |
569 self.mockPresubmit(change_info, fail=True) | |
570 self.mox.ReplayAll() | |
571 | |
572 retval = gcl.CMDcommit(['naame']) | |
573 | |
574 self.assertEquals(retval, 1) | |
575 | |
576 def testPresubmitSucceeds(self): | |
577 change_info = self.mockLoad() | |
578 self.mockPresubmit(change_info, fail=False) | |
579 self.mockCommit( | |
580 change_info, 'deescription\n\nReview URL: https://my_server/1', '') | |
581 change_info.UpdateDescriptionFromIssue() | |
582 change_info.GetApprovingReviewers().AndReturn(['a@c']) | |
583 change_info.update_reviewers(['a@c']) | |
584 self.mox.ReplayAll() | |
585 | |
586 retval = gcl.CMDcommit(['naame']) | |
587 | |
588 self.assertEquals(retval, 0) | |
589 self.assertEquals(change_info.description, 'deescription') | |
590 # pylint: disable=W0212 | |
591 self.assertFalse(change_info._deleted) | |
592 self.assertFalse(change_info._closed) | |
593 | |
594 def testPresubmitSucceedsWithCommittedMessage(self): | |
595 change_info = self.mockLoad() | |
596 self.mockPresubmit(change_info, fail=False) | |
597 self.mockCommit( | |
598 change_info, | |
599 'deescription\n\nReview URL: https://my_server/1', | |
600 '\nCommitted revision 12345') | |
601 change_info.UpdateDescriptionFromIssue() | |
602 change_info.GetApprovingReviewers().AndReturn(['a@c']) | |
603 change_info.update_reviewers(['a@c']) | |
604 change_info.append_footer('Committed: http://view/12345') | |
605 self.mox.ReplayAll() | |
606 | |
607 retval = gcl.CMDcommit(['naame']) | |
608 self.assertEquals(retval, 0) | |
609 # This is because append_footer is mocked. | |
610 self.assertEquals(change_info.description, 'deescription') | |
611 # pylint: disable=W0212 | |
612 self.assertTrue(change_info._deleted) | |
613 self.assertTrue(change_info._closed) | |
614 self.assertEqual( | |
615 change_info._comments_added, | |
616 ["Committed patchset #1 (id:1337) manually as r12345 (presubmit " | |
617 "successful)."]) | |
618 | |
619 | |
620 if __name__ == '__main__': | |
621 import unittest | |
622 unittest.main() | |
OLD | NEW |