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 # |
(...skipping 16 matching lines...) Expand all Loading... |
27 import gclient | 27 import gclient |
28 import gclient_scm | 28 import gclient_scm |
29 import gclient_utils | 29 import gclient_utils |
30 import super_mox | 30 import super_mox |
31 from super_mox import mox | 31 from super_mox import mox |
32 | 32 |
33 | 33 |
34 class BaseTestCase(super_mox.SuperMoxTestBase): | 34 class BaseTestCase(super_mox.SuperMoxTestBase): |
35 def setUp(self): | 35 def setUp(self): |
36 super_mox.SuperMoxTestBase.setUp(self) | 36 super_mox.SuperMoxTestBase.setUp(self) |
37 self.mox.StubOutWithMock(gclient.os.path, 'exists') | |
38 self.mox.StubOutWithMock(gclient.os.path, 'isfile') | |
39 self.mox.StubOutWithMock(gclient.os.path, 'isdir') | |
40 self.mox.StubOutWithMock(gclient.os, 'remove') | |
41 self.mox.StubOutWithMock(gclient.sys, 'stdout') | |
42 self.mox.StubOutWithMock(gclient_utils, 'subprocess') | |
43 # These are not tested. | |
44 self.mox.StubOutWithMock(gclient, 'FileRead') | |
45 self.mox.StubOutWithMock(gclient, 'FileWrite') | |
46 self.mox.StubOutWithMock(gclient_utils, 'SubprocessCall') | |
47 self.mox.StubOutWithMock(gclient_utils, 'RemoveDirectory') | |
48 | 37 |
49 # Like unittest's assertRaises, but checks for Gclient.Error. | 38 # Like unittest's assertRaises, but checks for Gclient.Error. |
50 def assertRaisesError(self, msg, fn, *args, **kwargs): | 39 def assertRaisesError(self, msg, fn, *args, **kwargs): |
51 try: | 40 try: |
52 fn(*args, **kwargs) | 41 fn(*args, **kwargs) |
53 except gclient.Error, e: | 42 except gclient.Error, e: |
54 self.assertEquals(e.args[0], msg) | 43 self.assertEquals(e.args[0], msg) |
55 else: | 44 else: |
56 self.fail('%s not raised' % msg) | 45 self.fail('%s not raised' % msg) |
57 | 46 |
58 | 47 |
59 class GClientBaseTestCase(BaseTestCase): | 48 class GClientBaseTestCase(BaseTestCase): |
60 def Options(self, *args, **kwargs): | 49 def Options(self, *args, **kwargs): |
61 return self.OptionsObject(self, *args, **kwargs) | 50 return self.OptionsObject(self, *args, **kwargs) |
62 | 51 |
63 def setUp(self): | 52 def setUp(self): |
64 BaseTestCase.setUp(self) | 53 BaseTestCase.setUp(self) |
| 54 self.mox.StubOutWithMock(gclient.os.path, 'exists') |
| 55 self.mox.StubOutWithMock(gclient.os.path, 'isfile') |
| 56 self.mox.StubOutWithMock(gclient.os.path, 'isdir') |
| 57 self.mox.StubOutWithMock(gclient.os, 'remove') |
| 58 self.mox.StubOutWithMock(gclient.sys, 'stdout') |
| 59 self.mox.StubOutWithMock(gclient_utils, 'subprocess') |
| 60 # These are not tested. |
| 61 self.mox.StubOutWithMock(gclient, 'FileRead') |
| 62 self.mox.StubOutWithMock(gclient, 'FileWrite') |
| 63 self.mox.StubOutWithMock(gclient_utils, 'SubprocessCall') |
| 64 self.mox.StubOutWithMock(gclient_utils, 'RemoveDirectory') |
65 # Mock them to be sure nothing bad happens. | 65 # Mock them to be sure nothing bad happens. |
66 self.mox.StubOutWithMock(gclient_scm, 'CaptureSVN') | 66 self.mox.StubOutWithMock(gclient_scm, 'CaptureSVN') |
67 self._CaptureSVNInfo = gclient_scm.CaptureSVNInfo | 67 self._CaptureSVNInfo = gclient_scm.CaptureSVNInfo |
68 self.mox.StubOutWithMock(gclient_scm, 'CaptureSVNInfo') | 68 self.mox.StubOutWithMock(gclient_scm, 'CaptureSVNInfo') |
69 self.mox.StubOutWithMock(gclient_scm, 'CaptureSVNStatus') | 69 self.mox.StubOutWithMock(gclient_scm, 'CaptureSVNStatus') |
70 self.mox.StubOutWithMock(gclient_scm, 'RunSVN') | 70 self.mox.StubOutWithMock(gclient_scm, 'RunSVN') |
71 self.mox.StubOutWithMock(gclient_scm, 'RunSVNAndGetFileList') | 71 self.mox.StubOutWithMock(gclient_scm, 'RunSVNAndGetFileList') |
72 self._gclient_gclient = gclient.GClient | 72 self._gclient_gclient = gclient.GClient |
73 gclient.GClient = self.mox.CreateMockAnything() | 73 gclient.GClient = self.mox.CreateMockAnything() |
74 self._scm_wrapper = gclient_scm.CreateSCM | 74 self._scm_wrapper = gclient_scm.CreateSCM |
(...skipping 937 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1012 def test_LoadConfig(self): | 1012 def test_LoadConfig(self): |
1013 pass | 1013 pass |
1014 def test_ReadEntries(self): | 1014 def test_ReadEntries(self): |
1015 pass | 1015 pass |
1016 def test_SaveEntries(self): | 1016 def test_SaveEntries(self): |
1017 pass | 1017 pass |
1018 def test_VarImpl(self): | 1018 def test_VarImpl(self): |
1019 pass | 1019 pass |
1020 | 1020 |
1021 | 1021 |
1022 class SCMWrapperTestCase(GClientBaseTestCase): | |
1023 class OptionsObject(object): | |
1024 def __init__(self, test_case, verbose=False, revision=None): | |
1025 self.verbose = verbose | |
1026 self.revision = revision | |
1027 self.manually_grab_svn_rev = True | |
1028 self.deps_os = None | |
1029 self.force = False | |
1030 self.nohooks = False | |
1031 | |
1032 def setUp(self): | |
1033 GClientBaseTestCase.setUp(self) | |
1034 self.root_dir = self.Dir() | |
1035 self.args = self.Args() | |
1036 self.url = self.Url() | |
1037 self.relpath = 'asf' | |
1038 | |
1039 def testDir(self): | |
1040 members = [ | |
1041 'FullUrlForRelativeUrl', 'RunCommand', 'cleanup', 'diff', 'export', | |
1042 'pack', 'relpath', 'revert', 'runhooks', 'scm_name', 'status', | |
1043 'update', 'url', | |
1044 ] | |
1045 | |
1046 # If you add a member, be sure to add the relevant test! | |
1047 self.compareMembers(self._scm_wrapper(), members) | |
1048 | |
1049 def testUnsupportedSCM(self): | |
1050 args = [self.url, self.root_dir, self.relpath] | |
1051 kwargs = {'scm_name' : 'foo'} | |
1052 exception_msg = 'Unsupported scm %(scm_name)s' % kwargs | |
1053 self.assertRaisesError(exception_msg, self._scm_wrapper, *args, **kwargs) | |
1054 | |
1055 def testFullUrlForRelativeUrl(self): | |
1056 self.url = 'svn://a/b/c/d' | |
1057 | |
1058 self.mox.ReplayAll() | |
1059 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, | |
1060 relpath=self.relpath) | |
1061 self.assertEqual(scm.FullUrlForRelativeUrl('/crap'), 'svn://a/b/crap') | |
1062 | |
1063 def testRunCommandException(self): | |
1064 options = self.Options(verbose=False) | |
1065 gclient.os.path.exists(os.path.join(self.root_dir, self.relpath, '.git') | |
1066 ).AndReturn(False) | |
1067 | |
1068 self.mox.ReplayAll() | |
1069 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, | |
1070 relpath=self.relpath) | |
1071 exception = "Unsupported argument(s): %s" % ','.join(self.args) | |
1072 self.assertRaisesError(exception, scm.RunCommand, | |
1073 'update', options, self.args) | |
1074 | |
1075 def testRunCommandUnknown(self): | |
1076 # TODO(maruel): if ever used. | |
1077 pass | |
1078 | |
1079 def testRevertMissing(self): | |
1080 options = self.Options(verbose=True) | |
1081 base_path = os.path.join(self.root_dir, self.relpath) | |
1082 gclient.os.path.isdir(base_path).AndReturn(False) | |
1083 # It'll to a checkout instead. | |
1084 gclient.os.path.exists(os.path.join(base_path, '.git')).AndReturn(False) | |
1085 print("\n_____ %s is missing, synching instead" % self.relpath) | |
1086 # Checkout. | |
1087 gclient.os.path.exists(base_path).AndReturn(False) | |
1088 files_list = self.mox.CreateMockAnything() | |
1089 gclient_scm.RunSVNAndGetFileList(['checkout', self.url, base_path], | |
1090 self.root_dir, files_list) | |
1091 | |
1092 self.mox.ReplayAll() | |
1093 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, | |
1094 relpath=self.relpath) | |
1095 scm.revert(options, self.args, files_list) | |
1096 | |
1097 def testRevertNone(self): | |
1098 options = self.Options(verbose=True) | |
1099 base_path = os.path.join(self.root_dir, self.relpath) | |
1100 gclient.os.path.isdir(base_path).AndReturn(True) | |
1101 gclient_scm.CaptureSVNStatus(base_path).AndReturn([]) | |
1102 gclient_scm.RunSVNAndGetFileList(['update', '--revision', 'BASE'], | |
1103 base_path, mox.IgnoreArg()) | |
1104 | |
1105 self.mox.ReplayAll() | |
1106 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, | |
1107 relpath=self.relpath) | |
1108 file_list = [] | |
1109 scm.revert(options, self.args, file_list) | |
1110 | |
1111 def testRevert2Files(self): | |
1112 options = self.Options(verbose=True) | |
1113 base_path = os.path.join(self.root_dir, self.relpath) | |
1114 gclient.os.path.isdir(base_path).AndReturn(True) | |
1115 items = [ | |
1116 ('M ', 'a'), | |
1117 ('A ', 'b'), | |
1118 ] | |
1119 file_path1 = os.path.join(base_path, 'a') | |
1120 file_path2 = os.path.join(base_path, 'b') | |
1121 gclient_scm.CaptureSVNStatus(base_path).AndReturn(items) | |
1122 gclient_scm.os.path.exists(file_path1).AndReturn(True) | |
1123 gclient_scm.os.path.isfile(file_path1).AndReturn(True) | |
1124 gclient_scm.os.remove(file_path1) | |
1125 gclient_scm.os.path.exists(file_path2).AndReturn(True) | |
1126 gclient_scm.os.path.isfile(file_path2).AndReturn(True) | |
1127 gclient_scm.os.remove(file_path2) | |
1128 gclient_scm.RunSVNAndGetFileList(['update', '--revision', 'BASE'], | |
1129 base_path, mox.IgnoreArg()) | |
1130 print(os.path.join(base_path, 'a')) | |
1131 print(os.path.join(base_path, 'b')) | |
1132 | |
1133 self.mox.ReplayAll() | |
1134 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, | |
1135 relpath=self.relpath) | |
1136 file_list = [] | |
1137 scm.revert(options, self.args, file_list) | |
1138 | |
1139 def testRevertDirectory(self): | |
1140 options = self.Options(verbose=True) | |
1141 base_path = os.path.join(self.root_dir, self.relpath) | |
1142 gclient.os.path.isdir(base_path).AndReturn(True) | |
1143 items = [ | |
1144 ('~ ', 'a'), | |
1145 ] | |
1146 gclient_scm.CaptureSVNStatus(base_path).AndReturn(items) | |
1147 file_path = os.path.join(base_path, 'a') | |
1148 print(file_path) | |
1149 gclient_scm.os.path.exists(file_path).AndReturn(True) | |
1150 gclient_scm.os.path.isfile(file_path).AndReturn(False) | |
1151 gclient_scm.os.path.isdir(file_path).AndReturn(True) | |
1152 gclient_utils.RemoveDirectory(file_path) | |
1153 file_list1 = [] | |
1154 gclient_scm.RunSVNAndGetFileList(['update', '--revision', 'BASE'], base_path
, | |
1155 mox.IgnoreArg()) | |
1156 | |
1157 self.mox.ReplayAll() | |
1158 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, | |
1159 relpath=self.relpath) | |
1160 file_list2 = [] | |
1161 scm.revert(options, self.args, file_list2) | |
1162 | |
1163 def testStatus(self): | |
1164 options = self.Options(verbose=True) | |
1165 base_path = os.path.join(self.root_dir, self.relpath) | |
1166 gclient.os.path.isdir(base_path).AndReturn(True) | |
1167 gclient_scm.RunSVNAndGetFileList(['status'] + self.args, base_path, | |
1168 []).AndReturn(None) | |
1169 | |
1170 self.mox.ReplayAll() | |
1171 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, | |
1172 relpath=self.relpath) | |
1173 file_list = [] | |
1174 self.assertEqual(scm.status(options, self.args, file_list), None) | |
1175 | |
1176 | |
1177 # TODO(maruel): TEST REVISIONS!!! | |
1178 # TODO(maruel): TEST RELOCATE!!! | |
1179 def testUpdateCheckout(self): | |
1180 options = self.Options(verbose=True) | |
1181 base_path = os.path.join(self.root_dir, self.relpath) | |
1182 file_info = gclient_utils.PrintableObject() | |
1183 file_info.root = 'blah' | |
1184 file_info.url = self.url | |
1185 file_info.uuid = 'ABC' | |
1186 file_info.revision = 42 | |
1187 gclient.os.path.exists(os.path.join(base_path, '.git')).AndReturn(False) | |
1188 # Checkout. | |
1189 gclient.os.path.exists(base_path).AndReturn(False) | |
1190 files_list = self.mox.CreateMockAnything() | |
1191 gclient_scm.RunSVNAndGetFileList(['checkout', self.url, base_path], | |
1192 self.root_dir, files_list) | |
1193 self.mox.ReplayAll() | |
1194 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, | |
1195 relpath=self.relpath) | |
1196 scm.update(options, (), files_list) | |
1197 | |
1198 def testUpdateUpdate(self): | |
1199 options = self.Options(verbose=True) | |
1200 base_path = os.path.join(self.root_dir, self.relpath) | |
1201 options.force = True | |
1202 options.nohooks = False | |
1203 file_info = { | |
1204 'Repository Root': 'blah', | |
1205 'URL': self.url, | |
1206 'UUID': 'ABC', | |
1207 'Revision': 42, | |
1208 } | |
1209 gclient.os.path.exists(os.path.join(base_path, '.git')).AndReturn(False) | |
1210 # Checkout or update. | |
1211 gclient.os.path.exists(base_path).AndReturn(True) | |
1212 gclient_scm.CaptureSVNInfo(os.path.join(base_path, "."), '.' | |
1213 ).AndReturn(file_info) | |
1214 # Cheat a bit here. | |
1215 gclient_scm.CaptureSVNInfo(file_info['URL'], '.').AndReturn(file_info) | |
1216 additional_args = [] | |
1217 if options.manually_grab_svn_rev: | |
1218 additional_args = ['--revision', str(file_info['Revision'])] | |
1219 files_list = [] | |
1220 gclient_scm.RunSVNAndGetFileList(['update', base_path] + additional_args, | |
1221 self.root_dir, files_list) | |
1222 | |
1223 self.mox.ReplayAll() | |
1224 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, | |
1225 relpath=self.relpath) | |
1226 scm.update(options, (), files_list) | |
1227 | |
1228 def testUpdateGit(self): | |
1229 options = self.Options(verbose=True) | |
1230 gclient.os.path.exists(os.path.join(self.root_dir, self.relpath, '.git') | |
1231 ).AndReturn(True) | |
1232 print("________ found .git directory; skipping %s" % self.relpath) | |
1233 | |
1234 self.mox.ReplayAll() | |
1235 scm = self._scm_wrapper(url=self.url, root_dir=self.root_dir, | |
1236 relpath=self.relpath) | |
1237 file_list = [] | |
1238 scm.update(options, self.args, file_list) | |
1239 | |
1240 def testGetSVNFileInfo(self): | |
1241 xml_text = r"""<?xml version="1.0"?> | |
1242 <info> | |
1243 <entry kind="file" path="%s" revision="14628"> | |
1244 <url>http://src.chromium.org/svn/trunk/src/chrome/app/d</url> | |
1245 <repository><root>http://src.chromium.org/svn</root></repository> | |
1246 <wc-info> | |
1247 <schedule>add</schedule> | |
1248 <depth>infinity</depth> | |
1249 <copy-from-url>http://src.chromium.org/svn/trunk/src/chrome/app/DEPS</copy-from-
url> | |
1250 <copy-from-rev>14628</copy-from-rev> | |
1251 <checksum>369f59057ba0e6d9017e28f8bdfb1f43</checksum> | |
1252 </wc-info> | |
1253 </entry> | |
1254 </info> | |
1255 """ % self.url | |
1256 gclient_scm.CaptureSVN(['info', '--xml', self.url], | |
1257 '.', True).AndReturn(xml_text) | |
1258 expected = { | |
1259 'URL': 'http://src.chromium.org/svn/trunk/src/chrome/app/d', | |
1260 'UUID': None, | |
1261 'Repository Root': 'http://src.chromium.org/svn', | |
1262 'Schedule': 'add', | |
1263 'Copied From URL': 'http://src.chromium.org/svn/trunk/src/chrome/app/DEPS'
, | |
1264 'Copied From Rev': '14628', | |
1265 'Path': self.url, | |
1266 'Revision': 14628, | |
1267 'Node Kind': 'file', | |
1268 } | |
1269 self.mox.ReplayAll() | |
1270 file_info = self._CaptureSVNInfo(self.url, '.', True) | |
1271 self.assertEquals(sorted(file_info.items()), sorted(expected.items())) | |
1272 | |
1273 def testCaptureSvnInfo(self): | |
1274 xml_text = """<?xml version="1.0"?> | |
1275 <info> | |
1276 <entry | |
1277 kind="dir" | |
1278 path="." | |
1279 revision="35"> | |
1280 <url>%s</url> | |
1281 <repository> | |
1282 <root>%s</root> | |
1283 <uuid>7b9385f5-0452-0410-af26-ad4892b7a1fb</uuid> | |
1284 </repository> | |
1285 <wc-info> | |
1286 <schedule>normal</schedule> | |
1287 <depth>infinity</depth> | |
1288 </wc-info> | |
1289 <commit | |
1290 revision="35"> | |
1291 <author>maruel</author> | |
1292 <date>2008-12-04T20:12:19.685120Z</date> | |
1293 </commit> | |
1294 </entry> | |
1295 </info> | |
1296 """ % (self.url, self.root_dir) | |
1297 gclient_scm.CaptureSVN(['info', '--xml', self.url], | |
1298 '.', True).AndReturn(xml_text) | |
1299 self.mox.ReplayAll() | |
1300 file_info = self._CaptureSVNInfo(self.url, '.', True) | |
1301 expected = { | |
1302 'URL': self.url, | |
1303 'UUID': '7b9385f5-0452-0410-af26-ad4892b7a1fb', | |
1304 'Revision': 35, | |
1305 'Repository Root': self.root_dir, | |
1306 'Schedule': 'normal', | |
1307 'Copied From URL': None, | |
1308 'Copied From Rev': None, | |
1309 'Path': '.', | |
1310 'Node Kind': 'dir', | |
1311 } | |
1312 self.assertEqual(file_info, expected) | |
1313 | |
1314 | |
1315 class RunSVNTestCase(BaseTestCase): | |
1316 def testRunSVN(self): | |
1317 param2 = 'bleh' | |
1318 gclient_utils.SubprocessCall(['svn', 'foo', 'bar'], param2).AndReturn(None) | |
1319 self.mox.ReplayAll() | |
1320 gclient_scm.RunSVN(['foo', 'bar'], param2) | |
1321 | |
1322 | |
1323 class SubprocessCallAndFilterTestCase(BaseTestCase): | 1022 class SubprocessCallAndFilterTestCase(BaseTestCase): |
1324 def setUp(self): | 1023 def setUp(self): |
1325 BaseTestCase.setUp(self) | 1024 BaseTestCase.setUp(self) |
| 1025 self.mox.StubOutWithMock(gclient_utils, 'subprocess') |
1326 self.mox.StubOutWithMock(gclient_scm, 'CaptureSVN') | 1026 self.mox.StubOutWithMock(gclient_scm, 'CaptureSVN') |
1327 | 1027 |
1328 def testSubprocessCallAndFilter(self): | 1028 def testSubprocessCallAndFilter(self): |
1329 command = ['boo', 'foo', 'bar'] | 1029 command = ['boo', 'foo', 'bar'] |
1330 in_directory = 'bleh' | 1030 in_directory = 'bleh' |
1331 fail_status = None | 1031 fail_status = None |
1332 pattern = 'a(.*)b' | 1032 pattern = 'a(.*)b' |
1333 test_string = 'ahah\naccb\nallo\naddb\n' | 1033 test_string = 'ahah\naccb\nallo\naddb\n' |
1334 class Mock(object): | 1034 class Mock(object): |
1335 stdout = StringIO.StringIO(test_string) | 1035 stdout = StringIO.StringIO(test_string) |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1425 """ | 1125 """ |
1426 gclient_scm.CaptureSVN = CaptureSVNMock | 1126 gclient_scm.CaptureSVN = CaptureSVNMock |
1427 info = gclient_scm.CaptureSVNStatus(None) | 1127 info = gclient_scm.CaptureSVNStatus(None) |
1428 self.assertEquals(info, []) | 1128 self.assertEquals(info, []) |
1429 | 1129 |
1430 | 1130 |
1431 if __name__ == '__main__': | 1131 if __name__ == '__main__': |
1432 unittest.main() | 1132 unittest.main() |
1433 | 1133 |
1434 # vim: ts=2:sw=2:tw=80:et: | 1134 # vim: ts=2:sw=2:tw=80:et: |
OLD | NEW |