OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # | 2 # |
3 # Copyright 2008-2009 Google Inc. All Rights Reserved. | 3 # Copyright 2008-2009 Google Inc. All Rights Reserved. |
4 # | 4 # |
5 # Licensed under the Apache License, Version 2.0 (the "License"); | 5 # Licensed under the Apache License, Version 2.0 (the "License"); |
6 # you may not use this file except in compliance with the License. | 6 # you may not use this file except in compliance with the License. |
7 # You may obtain a copy of the License at | 7 # You may obtain a copy of the License at |
8 # | 8 # |
9 # http://www.apache.org/licenses/LICENSE-2.0 | 9 # http://www.apache.org/licenses/LICENSE-2.0 |
10 # | 10 # |
11 # Unless required by applicable law or agreed to in writing, software | 11 # Unless required by applicable law or agreed to in writing, software |
12 # distributed under the License is distributed on an "AS IS" BASIS, | 12 # distributed under the License is distributed on an "AS IS" BASIS, |
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 # See the License for the specific language governing permissions and | 14 # See the License for the specific language governing permissions and |
15 # limitations under the License. | 15 # limitations under the License. |
16 | 16 |
17 """Unit tests for gclient.py.""" | 17 """Unit tests for gclient.py.""" |
18 | 18 |
19 __author__ = 'stephen5.ng@gmail.com (Stephen Ng)' | 19 __author__ = 'stephen5.ng@gmail.com (Stephen Ng)' |
20 | 20 |
21 import __builtin__ | 21 import __builtin__ |
22 import StringIO | 22 import StringIO |
23 | 23 |
24 import gclient | 24 import gclient |
25 from super_mox import mox, SuperMoxTestBase | 25 from super_mox import mox, IsOneOf, SuperMoxTestBase |
26 | |
27 | |
28 class IsOneOf(mox.Comparator): | |
29 def __init__(self, keys): | |
30 self._keys = keys | |
31 | |
32 def equals(self, rhs): | |
33 return rhs in self._keys | |
34 | |
35 def __repr__(self): | |
36 return '<sequence or map containing \'%s\'>' % str(self._keys) | |
37 | 26 |
38 | 27 |
39 class BaseTestCase(SuperMoxTestBase): | 28 class BaseTestCase(SuperMoxTestBase): |
40 def setUp(self): | |
41 SuperMoxTestBase.setUp(self) | |
42 | |
43 # Like unittest's assertRaises, but checks for Gclient.Error. | 29 # Like unittest's assertRaises, but checks for Gclient.Error. |
44 def assertRaisesError(self, msg, fn, *args, **kwargs): | 30 def assertRaisesError(self, msg, fn, *args, **kwargs): |
45 try: | 31 try: |
46 fn(*args, **kwargs) | 32 fn(*args, **kwargs) |
47 except gclient.gclient_utils.Error, e: | 33 except gclient.gclient_utils.Error, e: |
48 self.assertEquals(e.args[0], msg) | 34 self.assertEquals(e.args[0], msg) |
49 else: | 35 else: |
50 self.fail('%s not raised' % msg) | 36 self.fail('%s not raised' % msg) |
51 | 37 |
52 | 38 |
53 class GClientBaseTestCase(BaseTestCase): | 39 class GClientBaseTestCase(BaseTestCase): |
54 def Options(self, *args, **kwargs): | 40 def Options(self, *args, **kwargs): |
55 return self.OptionsObject(self, *args, **kwargs) | 41 return self.OptionsObject(self, *args, **kwargs) |
56 | 42 |
57 def setUp(self): | 43 def setUp(self): |
58 BaseTestCase.setUp(self) | 44 BaseTestCase.setUp(self) |
59 self.mox.StubOutWithMock(gclient.os.path, 'exists') | |
60 self.mox.StubOutWithMock(gclient.os.path, 'isfile') | |
61 self.mox.StubOutWithMock(gclient.os.path, 'isdir') | |
62 self.mox.StubOutWithMock(gclient.os, 'remove') | |
63 self.mox.StubOutWithMock(gclient.sys, 'stdout') | |
64 self.mox.StubOutWithMock(gclient.gclient_utils, 'subprocess') | |
65 # These are not tested. | 45 # These are not tested. |
66 self.mox.StubOutWithMock(gclient.gclient_utils, 'FileRead') | 46 self.mox.StubOutWithMock(gclient.gclient_utils, 'FileRead') |
67 self.mox.StubOutWithMock(gclient.gclient_utils, 'FileWrite') | 47 self.mox.StubOutWithMock(gclient.gclient_utils, 'FileWrite') |
68 self.mox.StubOutWithMock(gclient.gclient_utils, 'SubprocessCall') | 48 self.mox.StubOutWithMock(gclient.gclient_utils, 'SubprocessCall') |
69 self.mox.StubOutWithMock(gclient.gclient_utils, 'RemoveDirectory') | 49 self.mox.StubOutWithMock(gclient.gclient_utils, 'RemoveDirectory') |
70 # Mock them to be sure nothing bad happens. | 50 # Mock them to be sure nothing bad happens. |
71 self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVN') | 51 self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVN') |
72 self._CaptureSVNInfo = gclient.gclient_scm.CaptureSVNInfo | |
73 self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVNInfo') | 52 self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVNInfo') |
74 self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVNStatus') | 53 self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVNStatus') |
75 self.mox.StubOutWithMock(gclient.gclient_scm, 'RunSVN') | 54 self.mox.StubOutWithMock(gclient.gclient_scm, 'RunSVN') |
76 self.mox.StubOutWithMock(gclient.gclient_scm, 'RunSVNAndGetFileList') | 55 self.mox.StubOutWithMock(gclient.gclient_scm, 'RunSVNAndGetFileList') |
77 self._gclient_gclient = gclient.GClient | 56 self._gclient_gclient = gclient.GClient |
78 gclient.GClient = self.mox.CreateMockAnything() | 57 gclient.GClient = self.mox.CreateMockAnything() |
79 self._scm_wrapper = gclient.gclient_scm.CreateSCM | 58 self._scm_wrapper = gclient.gclient_scm.CreateSCM |
80 gclient.gclient_scm.CreateSCM = self.mox.CreateMockAnything() | 59 gclient.gclient_scm.CreateSCM = self.mox.CreateMockAnything() |
81 | 60 |
82 def tearDown(self): | 61 def tearDown(self): |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 'name': solution_name, | 352 'name': solution_name, |
374 'url': solution_url, | 353 'url': solution_url, |
375 'custom_deps': {}, | 354 'custom_deps': {}, |
376 'safesync_url': safesync_url | 355 'safesync_url': safesync_url |
377 }] | 356 }] |
378 self.assertEqual(client.GetVar('solutions'), solutions) | 357 self.assertEqual(client.GetVar('solutions'), solutions) |
379 self.assertEqual(client.GetVar('foo'), None) | 358 self.assertEqual(client.GetVar('foo'), None) |
380 | 359 |
381 def testLoadCurrentConfig(self): | 360 def testLoadCurrentConfig(self): |
382 options = self.Options() | 361 options = self.Options() |
383 path = gclient.os.path.realpath(self.root_dir) | 362 gclient.os.path.realpath(self.root_dir).AndReturn(self.root_dir) |
384 gclient.os.path.exists(gclient.os.path.join(path, options.config_filename) | 363 gclient.os.path.exists( |
| 364 gclient.os.path.join(self.root_dir, options.config_filename) |
385 ).AndReturn(True) | 365 ).AndReturn(True) |
386 gclient.GClient(path, options).AndReturn(gclient.GClient) | 366 gclient.GClient(self.root_dir, options).AndReturn(gclient.GClient) |
387 gclient.GClient._LoadConfig() | 367 gclient.GClient._LoadConfig() |
388 | 368 |
389 self.mox.ReplayAll() | 369 self.mox.ReplayAll() |
390 client = self._gclient_gclient.LoadCurrentConfig(options, self.root_dir) | 370 client = self._gclient_gclient.LoadCurrentConfig(options, self.root_dir) |
391 | 371 |
392 def testRunOnDepsNoDeps(self): | 372 def testRunOnDepsNoDeps(self): |
393 solution_name = 'testRunOnDepsNoDeps_solution_name' | 373 solution_name = 'testRunOnDepsNoDeps_solution_name' |
394 gclient_config = ( | 374 gclient_config = ( |
395 "solutions = [ {\n" | 375 "solutions = [ {\n" |
396 " 'name': '%s',\n" | 376 " 'name': '%s',\n" |
(...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1082 pass | 1062 pass |
1083 def test_SaveEntries(self): | 1063 def test_SaveEntries(self): |
1084 pass | 1064 pass |
1085 def test_VarImpl(self): | 1065 def test_VarImpl(self): |
1086 pass | 1066 pass |
1087 | 1067 |
1088 | 1068 |
1089 class SubprocessCallAndFilterTestCase(BaseTestCase): | 1069 class SubprocessCallAndFilterTestCase(BaseTestCase): |
1090 def setUp(self): | 1070 def setUp(self): |
1091 BaseTestCase.setUp(self) | 1071 BaseTestCase.setUp(self) |
1092 self.mox.StubOutWithMock(gclient.gclient_utils, 'subprocess') | |
1093 self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVN') | 1072 self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVN') |
1094 | 1073 |
1095 def testSubprocessCallAndFilter(self): | 1074 def testSubprocessCallAndFilter(self): |
1096 command = ['boo', 'foo', 'bar'] | 1075 command = ['boo', 'foo', 'bar'] |
1097 in_directory = 'bleh' | 1076 in_directory = 'bleh' |
1098 fail_status = None | 1077 fail_status = None |
1099 pattern = 'a(.*)b' | 1078 pattern = 'a(.*)b' |
1100 test_string = 'ahah\naccb\nallo\naddb\n' | 1079 test_string = 'ahah\naccb\nallo\naddb\n' |
1101 class Mock(object): | 1080 class Mock(object): |
1102 stdout = StringIO.StringIO(test_string) | 1081 stdout = StringIO.StringIO(test_string) |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1191 self.mox.ReplayAll() | 1170 self.mox.ReplayAll() |
1192 info = gclient.gclient_scm.CaptureSVNStatus(None) | 1171 info = gclient.gclient_scm.CaptureSVNStatus(None) |
1193 self.assertEquals(info, []) | 1172 self.assertEquals(info, []) |
1194 | 1173 |
1195 | 1174 |
1196 if __name__ == '__main__': | 1175 if __name__ == '__main__': |
1197 import unittest | 1176 import unittest |
1198 unittest.main() | 1177 unittest.main() |
1199 | 1178 |
1200 # vim: ts=2:sw=2:tw=80:et: | 1179 # vim: ts=2:sw=2:tw=80:et: |
OLD | NEW |