-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcl_unittest.py
executable file
·366 lines (316 loc) · 12.9 KB
/
gcl_unittest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/python
# Copyright (c) 2009 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Unit tests for gcl.py."""
import unittest
# Local imports
import gcl
import super_mox
from super_mox import mox
class GclTestsBase(super_mox.SuperMoxTestBase):
"""Setups and tear downs the mocks but doesn't test anything as-is."""
def setUp(self):
super_mox.SuperMoxTestBase.setUp(self)
self.fake_root_dir = self.RootDir()
self.mox.StubOutWithMock(gcl, 'RunShell')
self.mox.StubOutWithMock(gcl.gclient_scm, 'CaptureSVNInfo')
self.mox.StubOutWithMock(gcl.os, 'getcwd')
self.mox.StubOutWithMock(gcl.os, 'chdir')
self.mox.StubOutWithMock(gcl.os, 'close')
self.mox.StubOutWithMock(gcl.os, 'remove')
self.mox.StubOutWithMock(gcl.os, 'write')
self.mox.StubOutWithMock(gcl.os.path, 'exists')
self.mox.StubOutWithMock(gcl.os.path, 'isdir')
self.mox.StubOutWithMock(gcl.os.path, 'isfile')
self.mox.StubOutWithMock(gcl, 'tempfile')
self.mox.StubOutWithMock(gcl.upload, 'RealMain')
# These are not tested.
self.mox.StubOutWithMock(gcl, 'ReadFile')
self.mox.StubOutWithMock(gcl, 'WriteFile')
class GclUnittest(GclTestsBase):
"""General gcl.py tests."""
def testMembersChanged(self):
self.mox.ReplayAll()
members = [
'CODEREVIEW_SETTINGS', 'CODEREVIEW_SETTINGS_FILE', 'CPP_EXTENSIONS',
'Change', 'ChangeInfo', 'Changes', 'DeleteEmptyChangeLists', 'Commit',
'DoPresubmitChecks',
'ErrorExit', 'FILES_CACHE', 'FilterFlag', 'GenerateChangeName',
'GenerateDiff',
'GetCacheDir', 'GetCachedFile', 'GetChangesDir', 'GetCLs',
'GetChangelistInfoFile', 'GetCodeReviewSetting', 'GetEditor',
'GetFilesNotInCL', 'GetInfoDir', 'GetIssueDescription',
'GetModifiedFiles', 'GetRepositoryRoot',
'GetSVNFileProperty', 'Help', 'IGNORE_PATHS', 'IsSVNMoved',
'Lint', 'LoadChangelistInfoForMultiple',
'MISSING_TEST_MSG', 'Opened', 'OptionallyDoPresubmitChecks',
'PresubmitCL', 'ReadFile', 'REPOSITORY_ROOT', 'RunShell',
'RunShellWithReturnCode', 'SendToRietveld', 'TryChange',
'UnknownFiles', 'UploadCL', 'Warn', 'WriteFile',
'gclient_scm', 'gclient_utils', 'getpass', 'main', 'os', 'random', 're',
'shutil', 'string', 'subprocess', 'sys', 'tempfile',
'upload', 'urllib2', 'xml',
]
# If this test fails, you should add the relevant test.
self.compareMembers(gcl, members)
def testIsSVNMoved(self):
# TODO(maruel): TEST ME
pass
def testGetSVNFileProperty(self):
# TODO(maruel): TEST ME
pass
def testUnknownFiles(self):
# TODO(maruel): TEST ME
pass
def testGetRepositoryRootNone(self):
gcl.REPOSITORY_ROOT = None
gcl.os.getcwd().AndReturn("/bleh/prout")
result = {
"Repository Root": ""
}
gcl.gclient_scm.CaptureSVNInfo("/bleh/prout", print_error=False).AndReturn(
result)
self.mox.ReplayAll()
self.assertRaises(Exception, gcl.GetRepositoryRoot)
def testGetRepositoryRootGood(self):
gcl.REPOSITORY_ROOT = None
root_path = gcl.os.path.join('bleh', 'prout', 'pouet')
gcl.os.getcwd().AndReturn(root_path)
result1 = { "Repository Root": "Some root" }
gcl.gclient_scm.CaptureSVNInfo(root_path,
print_error=False).AndReturn(result1)
gcl.os.getcwd().AndReturn(root_path)
results2 = { "Repository Root": "A different root" }
gcl.gclient_scm.CaptureSVNInfo(
gcl.os.path.dirname(root_path),
print_error=False).AndReturn(results2)
self.mox.ReplayAll()
self.assertEquals(gcl.GetRepositoryRoot(), root_path)
def testGetCachedFile(self):
# TODO(maruel): TEST ME
pass
def testGetCodeReviewSetting(self):
# TODO(maruel): TEST ME
pass
def testGetChangelistInfoFile(self):
# TODO(maruel): TEST ME
pass
def testLoadChangelistInfoForMultiple(self):
# TODO(maruel): TEST ME
pass
def testGetModifiedFiles(self):
# TODO(maruel): TEST ME
pass
def testGetFilesNotInCL(self):
# TODO(maruel): TEST ME
pass
def testSendToRietveld(self):
# TODO(maruel): TEST ME
pass
def testOpened(self):
# TODO(maruel): TEST ME
pass
def testHelp(self):
self.mox.StubOutWithMock(gcl.sys, 'stdout')
gcl.sys.stdout.write(mox.StrContains('GCL is a wrapper for Subversion'))
gcl.sys.stdout.write('\n')
self.mox.ReplayAll()
gcl.Help()
def testGenerateDiff(self):
# TODO(maruel): TEST ME
pass
def testPresubmitCL(self):
# TODO(maruel): TEST ME
pass
def testTryChange(self):
# TODO(maruel): TEST ME
pass
def testCommit(self):
# TODO(maruel): TEST ME
pass
def testChange(self):
# TODO(maruel): TEST ME
pass
def testLint(self):
# TODO(maruel): TEST ME
pass
def testDoPresubmitChecks(self):
# TODO(maruel): TEST ME
pass
class ChangeInfoUnittest(GclTestsBase):
def setUp(self):
GclTestsBase.setUp(self)
self.mox.StubOutWithMock(gcl, 'GetChangelistInfoFile')
self.mox.StubOutWithMock(gcl, 'GetRepositoryRoot')
def testChangeInfoMembers(self):
self.mox.ReplayAll()
members = [
'CloseIssue', 'Delete', 'GetFiles', 'GetFileNames', 'GetLocalRoot',
'Exists', 'Load', 'MissingTests', 'Save', 'UpdateRietveldDescription',
'description', 'issue', 'name',
'patch', 'patchset',
]
# If this test fails, you should add the relevant test.
self.compareMembers(gcl.ChangeInfo('', 0, 0, '', None, self.fake_root_dir),
members)
def testChangeInfoBase(self):
files = [('M', 'foo'), ('A', 'bar')]
self.mox.ReplayAll()
o = gcl.ChangeInfo('name2', '42', '53', 'description2', files,
self.fake_root_dir)
self.assertEquals(o.name, 'name2')
self.assertEquals(o.issue, 42)
self.assertEquals(o.patchset, 53)
self.assertEquals(o.description, 'description2')
self.assertEquals(o.patch, None)
self.assertEquals(o.GetFileNames(), ['foo', 'bar'])
self.assertEquals(o.GetFiles(), files)
self.assertEquals(o.GetLocalRoot(), self.fake_root_dir)
def testLoadWithIssue(self):
description = ["This is some description.", "force an extra separator."]
gcl.GetChangelistInfoFile('bleh').AndReturn('bleeeh')
gcl.os.path.exists('bleeeh').AndReturn(True)
gcl.ReadFile('bleeeh').AndReturn(
gcl.ChangeInfo._SEPARATOR.join(["42,53", "G b.cc"] + description))
self.mox.ReplayAll()
change_info = gcl.ChangeInfo.Load('bleh', self.fake_root_dir, True, False)
self.assertEquals(change_info.name, 'bleh')
self.assertEquals(change_info.issue, 42)
self.assertEquals(change_info.patchset, 53)
self.assertEquals(change_info.description,
gcl.ChangeInfo._SEPARATOR.join(description))
self.assertEquals(change_info.GetFiles(), [('G ', 'b.cc')])
def testLoadEmpty(self):
gcl.GetChangelistInfoFile('bleh').AndReturn('bleeeh')
gcl.os.path.exists('bleeeh').AndReturn(True)
gcl.ReadFile('bleeeh').AndReturn(
gcl.ChangeInfo._SEPARATOR.join(["", "", ""]))
self.mox.ReplayAll()
change_info = gcl.ChangeInfo.Load('bleh', self.fake_root_dir, True, False)
self.assertEquals(change_info.name, 'bleh')
self.assertEquals(change_info.issue, 0)
self.assertEquals(change_info.patchset, 0)
self.assertEquals(change_info.description, "")
self.assertEquals(change_info.GetFiles(), [])
def testSaveEmpty(self):
gcl.GetChangelistInfoFile('').AndReturn('foo')
gcl.WriteFile('foo', gcl.ChangeInfo._SEPARATOR.join(['0, 0', '', '']))
self.mox.ReplayAll()
change_info = gcl.ChangeInfo('', 0, 0, '', None, self.fake_root_dir)
change_info.Save()
class UploadCLUnittest(GclTestsBase):
def setUp(self):
GclTestsBase.setUp(self)
self.mox.StubOutWithMock(gcl, 'DoPresubmitChecks')
self.mox.StubOutWithMock(gcl, 'GenerateDiff')
self.mox.StubOutWithMock(gcl, 'GetCodeReviewSetting')
self.mox.StubOutWithMock(gcl, 'GetRepositoryRoot')
self.mox.StubOutWithMock(gcl, 'SendToRietveld')
self.mox.StubOutWithMock(gcl, 'TryChange')
def testNew(self):
change_info = self.mox.CreateMock(gcl.ChangeInfo)
change_info.name = 'naame'
change_info.issue = 1
change_info.patchset = 0
change_info.description = 'deescription',
change_info.files = [('A', 'aa'), ('M', 'bb')]
change_info.patch = None
files = [item[1] for item in change_info.files]
args = ['--foo=bar']
gcl.DoPresubmitChecks(change_info, False, True).AndReturn(True)
gcl.GetCodeReviewSetting('CODE_REVIEW_SERVER').AndReturn('my_server')
gcl.os.getcwd().AndReturn('somewhere')
change_info.GetFiles().AndReturn(change_info.files)
change_info.GetLocalRoot().AndReturn('proout')
gcl.os.chdir('proout')
change_info.GetFileNames().AndReturn(files)
gcl.GenerateDiff(files)
gcl.upload.RealMain(['upload.py', '-y', '--server=my_server', '--foo=bar',
"--message=''", '--issue=1'], change_info.patch).AndReturn(("1",
"2"))
gcl.SendToRietveld("/lint/issue%s_%s" % ('1', '2'), timeout=0.5)
gcl.GetCodeReviewSetting('TRY_ON_UPLOAD').AndReturn('True')
gcl.TryChange(change_info, [], swallow_exception=True)
gcl.os.chdir('somewhere')
change_info.Save()
self.mox.ReplayAll()
gcl.UploadCL(change_info, args)
def testServerOverride(self):
change_info = gcl.ChangeInfo('naame', 0, 0, 'deescription',
[('A', 'aa'), ('M', 'bb')],
self.fake_root_dir)
self.mox.StubOutWithMock(change_info, 'Save')
args = ['--server=a', '--no_watchlists']
change_info.Save()
gcl.DoPresubmitChecks(change_info, False, True).AndReturn(True)
gcl.GetCodeReviewSetting('CODE_REVIEW_SERVER').AndReturn('my_server')
gcl.tempfile.mkstemp(text=True).AndReturn((42, 'descfile'))
gcl.os.write(42, change_info.description)
gcl.os.close(42)
gcl.GetCodeReviewSetting('CC_LIST')
gcl.os.getcwd().AndReturn('somewhere')
gcl.os.chdir(change_info.GetLocalRoot())
gcl.GenerateDiff(change_info.GetFileNames())
gcl.upload.RealMain(['upload.py', '-y', '--server=my_server', '--server=a',
"--description_file=descfile",
"--message=deescription"], change_info.patch).AndReturn(("1", "2"))
gcl.os.remove('descfile')
gcl.SendToRietveld("/lint/issue%s_%s" % ('1', '2'), timeout=0.5)
gcl.os.chdir('somewhere')
self.mox.ReplayAll()
gcl.UploadCL(change_info, args)
def testNoTry(self):
change_info = gcl.ChangeInfo('naame', 0, 0, 'deescription',
[('A', 'aa'), ('M', 'bb')],
self.fake_root_dir)
change_info.Save = self.mox.CreateMockAnything()
args = ['--no-try', '--no_watchlists']
change_info.Save()
gcl.DoPresubmitChecks(change_info, False, True).AndReturn(True)
gcl.GetCodeReviewSetting('CODE_REVIEW_SERVER').AndReturn('my_server')
gcl.tempfile.mkstemp(text=True).AndReturn((42, 'descfile'))
gcl.os.write(42, change_info.description)
gcl.os.close(42)
gcl.GetCodeReviewSetting('CC_LIST')
gcl.os.getcwd().AndReturn('somewhere')
gcl.os.chdir(change_info.GetLocalRoot())
gcl.GenerateDiff(change_info.GetFileNames())
gcl.upload.RealMain(['upload.py', '-y', '--server=my_server',
"--description_file=descfile",
"--message=deescription"], change_info.patch).AndReturn(("1", "2"))
gcl.os.remove('descfile')
gcl.SendToRietveld("/lint/issue%s_%s" % ('1', '2'), timeout=0.5)
gcl.os.chdir('somewhere')
self.mox.ReplayAll()
gcl.UploadCL(change_info, args)
def testNormal(self):
change_info = gcl.ChangeInfo('naame', 0, 0, 'deescription',
[('A', 'aa'), ('M', 'bb')],
self.fake_root_dir)
self.mox.StubOutWithMock(change_info, 'Save')
args = ['--no_watchlists']
change_info.Save()
gcl.DoPresubmitChecks(change_info, False, True).AndReturn(True)
gcl.GetCodeReviewSetting('CODE_REVIEW_SERVER').AndReturn('my_server')
gcl.tempfile.mkstemp(text=True).AndReturn((42, 'descfile'))
gcl.os.write(42, change_info.description)
gcl.os.close(42)
gcl.GetCodeReviewSetting('CC_LIST')
gcl.os.getcwd().AndReturn('somewhere')
gcl.os.chdir(change_info.GetLocalRoot())
gcl.GenerateDiff(change_info.GetFileNames())
gcl.upload.RealMain(['upload.py', '-y', '--server=my_server',
"--description_file=descfile",
"--message=deescription"], change_info.patch).AndReturn(("1", "2"))
gcl.os.remove('descfile')
gcl.SendToRietveld("/lint/issue%s_%s" % ('1', '2'), timeout=0.5)
gcl.GetCodeReviewSetting('TRY_ON_UPLOAD').AndReturn('True')
gcl.TryChange(change_info, [], swallow_exception=True)
gcl.os.chdir('somewhere')
self.mox.ReplayAll()
gcl.UploadCL(change_info, args)
self.assertEquals(change_info.issue, 1)
self.assertEquals(change_info.patchset, 2)
if __name__ == '__main__':
unittest.main()