OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # |
| 3 # Copyright 2008-2009 Google Inc. All Rights Reserved. |
| 4 # |
| 5 # Licensed under the Apache License, Version 2.0 (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 |
| 8 # |
| 9 # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 # |
| 11 # Unless required by applicable law or agreed to in writing, software |
| 12 # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 # See the License for the specific language governing permissions and |
| 15 # limitations under the License. |
| 16 |
| 17 """Unit tests for gclient.py.""" |
| 18 |
| 19 __author__ = 'stephen5.ng@gmail.com (Stephen Ng)' |
| 20 |
| 21 import __builtin__ |
| 22 import copy |
| 23 import os |
| 24 import random |
| 25 import string |
| 26 import subprocess |
| 27 import sys |
| 28 import unittest |
| 29 |
| 30 directory, _file = os.path.split(__file__) |
| 31 if directory: |
| 32 directory += os.sep |
| 33 sys.path.append(os.path.abspath(directory + '../pymox')) |
| 34 |
| 35 import gclient |
| 36 import mox |
| 37 |
| 38 |
| 39 ## Some utilities for generating arbitrary arguments. |
| 40 |
| 41 |
| 42 def String(max_length): |
| 43 return ''.join([random.choice(string.letters) |
| 44 for x in xrange(random.randint(1, max_length))]) |
| 45 |
| 46 |
| 47 def Strings(max_arg_count, max_arg_length): |
| 48 return [String(max_arg_length) for x in xrange(max_arg_count)] |
| 49 |
| 50 |
| 51 def Args(max_arg_count=8, max_arg_length=16): |
| 52 return Strings(max_arg_count, random.randint(1, max_arg_length)) |
| 53 |
| 54 |
| 55 def _DirElts(max_elt_count=4, max_elt_length=8): |
| 56 return os.sep.join(Strings(max_elt_count, max_elt_length)) |
| 57 |
| 58 |
| 59 def Dir(max_elt_count=4, max_elt_length=8): |
| 60 return random.choice((os.sep, '')) + _DirElts(max_elt_count, max_elt_length) |
| 61 |
| 62 def Url(max_elt_count=4, max_elt_length=8): |
| 63 return ('svn://random_host:port/a' + |
| 64 _DirElts(max_elt_count, max_elt_length).replace(os.sep, '/')) |
| 65 |
| 66 def RootDir(max_elt_count=4, max_elt_length=8): |
| 67 return os.sep + _DirElts(max_elt_count, max_elt_length) |
| 68 |
| 69 |
| 70 class BaseTestCase(unittest.TestCase): |
| 71 # Like unittest's assertRaises, but checks for Gclient.Error. |
| 72 def assertRaisesError(self, msg, fn, *args, **kwargs): |
| 73 try: |
| 74 fn(*args, **kwargs) |
| 75 except gclient.Error, e: |
| 76 self.assertEquals(e.args[0], msg) |
| 77 else: |
| 78 self.fail('%s not raised' % msg) |
| 79 |
| 80 def Options(self, *args, **kwargs): |
| 81 return self.OptionsObject(self, *args, **kwargs) |
| 82 |
| 83 def setUp(self): |
| 84 self.mox = mox.Mox() |
| 85 # Mock them to be sure nothing bad happens. |
| 86 self._CaptureSVN = gclient.CaptureSVN |
| 87 gclient.CaptureSVN = self.mox.CreateMockAnything() |
| 88 self._CaptureSVNInfo = gclient.CaptureSVNInfo |
| 89 gclient.CaptureSVNInfo = self.mox.CreateMockAnything() |
| 90 self._CaptureSVNStatus = gclient.CaptureSVNStatus |
| 91 gclient.CaptureSVNStatus = self.mox.CreateMockAnything() |
| 92 self._FileRead = gclient.FileRead |
| 93 gclient.FileRead = self.mox.CreateMockAnything() |
| 94 self._FileWrite = gclient.FileWrite |
| 95 gclient.FileWrite = self.mox.CreateMockAnything() |
| 96 self._RemoveDirectory = gclient.RemoveDirectory |
| 97 gclient.RemoveDirectory = self.mox.CreateMockAnything() |
| 98 self._RunSVN = gclient.RunSVN |
| 99 gclient.RunSVN = self.mox.CreateMockAnything() |
| 100 self._RunSVNAndGetFileList = gclient.RunSVNAndGetFileList |
| 101 gclient.RunSVNAndGetFileList = self.mox.CreateMockAnything() |
| 102 # Doesn't seem to work very well: |
| 103 self._os = gclient.os |
| 104 gclient.os = self.mox.CreateMock(os) |
| 105 self._sys = gclient.sys |
| 106 gclient.sys = self.mox.CreateMock(sys) |
| 107 self._subprocess = gclient.subprocess |
| 108 gclient.subprocess = self.mox.CreateMock(subprocess) |
| 109 |
| 110 def tearDown(self): |
| 111 gclient.CaptureSVN = self._CaptureSVN |
| 112 gclient.CaptureSVNInfo = self._CaptureSVNInfo |
| 113 gclient.CaptureSVNStatus = self._CaptureSVNStatus |
| 114 gclient.FileRead = self._FileRead |
| 115 gclient.FileWrite = self._FileWrite |
| 116 gclient.RemoveDirectory = self._RemoveDirectory |
| 117 gclient.RunSVN = self._RunSVN |
| 118 gclient.RunSVNAndGetFileList = self._RunSVNAndGetFileList |
| 119 # Doesn't seem to work very well: |
| 120 gclient.os = self._os |
| 121 gclient.sys = self._sys |
| 122 gclient.subprocess = self._subprocess |
| 123 |
| 124 |
| 125 class GclientTestCase(BaseTestCase): |
| 126 class OptionsObject(object): |
| 127 def __init__(self, test_case, verbose=False, spec=None, |
| 128 config_filename='a_file_name', |
| 129 entries_filename='a_entry_file_name', |
| 130 deps_file='a_deps_file_name', force=False): |
| 131 self.verbose = verbose |
| 132 self.spec = spec |
| 133 self.config_filename = config_filename |
| 134 self.entries_filename = entries_filename |
| 135 self.deps_file = deps_file |
| 136 self.force = force |
| 137 self.revisions = [] |
| 138 self.manually_grab_svn_rev = True |
| 139 self.deps_os = None |
| 140 self.head = False |
| 141 |
| 142 # Mox |
| 143 self.stdout = test_case.stdout |
| 144 self.path_exists = test_case.path_exists |
| 145 self.platform = test_case.platform |
| 146 self.gclient = test_case.gclient |
| 147 self.scm_wrapper = test_case.scm_wrapper |
| 148 |
| 149 def setUp(self): |
| 150 BaseTestCase.setUp(self) |
| 151 self.stdout = self.mox.CreateMock(sys.stdout) |
| 152 #self.subprocess = self.mox.CreateMock(subprocess) |
| 153 # Stub os.path.exists. |
| 154 self.path_exists = self.mox.CreateMockAnything() |
| 155 self.sys = self.mox.CreateMock(sys) |
| 156 self.platform = 'darwin' |
| 157 |
| 158 self.gclient = self.mox.CreateMock(gclient.GClient) |
| 159 self.scm_wrapper = self.mox.CreateMock(gclient.SCMWrapper) |
| 160 |
| 161 self.args = Args() |
| 162 self.root_dir = Dir() |
| 163 self.url = Url() |
| 164 |
| 165 |
| 166 class GClientCommandsTestCase(BaseTestCase): |
| 167 def testCommands(self): |
| 168 known_commands = [gclient.DoCleanup, gclient.DoConfig, gclient.DoDiff, |
| 169 gclient.DoHelp, gclient.DoStatus, gclient.DoUpdate, |
| 170 gclient.DoRevert, gclient.DoRunHooks, gclient.DoRevInfo] |
| 171 for (k,v) in gclient.gclient_command_map.iteritems(): |
| 172 # If it fails, you need to add a test case for the new command. |
| 173 self.assert_(v in known_commands) |
| 174 self.mox.ReplayAll() |
| 175 self.mox.VerifyAll() |
| 176 |
| 177 class TestDoConfig(GclientTestCase): |
| 178 def setUp(self): |
| 179 GclientTestCase.setUp(self) |
| 180 # pymox has trouble to mock the class object and not a class instance. |
| 181 self.gclient = self.mox.CreateMockAnything() |
| 182 |
| 183 def testMissingArgument(self): |
| 184 exception_msg = "required argument missing; see 'gclient help config'" |
| 185 |
| 186 self.mox.ReplayAll() |
| 187 self.assertRaisesError(exception_msg, gclient.DoConfig, self.Options(), ()) |
| 188 self.mox.VerifyAll() |
| 189 |
| 190 def testExistingClientFile(self): |
| 191 options = self.Options() |
| 192 exception_msg = ('%s file already exists in the current directory' % |
| 193 options.config_filename) |
| 194 self.path_exists(options.config_filename).AndReturn(True) |
| 195 |
| 196 self.mox.ReplayAll() |
| 197 self.assertRaisesError(exception_msg, gclient.DoConfig, options, (1,)) |
| 198 self.mox.VerifyAll() |
| 199 |
| 200 def testFromText(self): |
| 201 options = self.Options(spec='config_source_content') |
| 202 options.path_exists(options.config_filename).AndReturn(False) |
| 203 options.gclient('.', options).AndReturn(options.gclient) |
| 204 options.gclient.SetConfig(options.spec) |
| 205 options.gclient.SaveConfig() |
| 206 |
| 207 self.mox.ReplayAll() |
| 208 gclient.DoConfig(options, (1,),) |
| 209 self.mox.VerifyAll() |
| 210 |
| 211 def testCreateClientFile(self): |
| 212 options = self.Options() |
| 213 options.path_exists(options.config_filename).AndReturn(False) |
| 214 options.gclient('.', options).AndReturn(options.gclient) |
| 215 options.gclient.SetDefaultConfig('the_name', 'http://svn/url/the_name', |
| 216 'other') |
| 217 options.gclient.SaveConfig() |
| 218 |
| 219 self.mox.ReplayAll() |
| 220 gclient.DoConfig(options, |
| 221 ('http://svn/url/the_name', 'other', 'args', 'ignored')) |
| 222 self.mox.VerifyAll() |
| 223 |
| 224 |
| 225 class TestDoHelp(GclientTestCase): |
| 226 def testGetUsage(self): |
| 227 options = self.Options() |
| 228 print >> options.stdout, gclient.COMMAND_USAGE_TEXT['config'] |
| 229 |
| 230 self.mox.ReplayAll() |
| 231 gclient.DoHelp(options, ('config',)) |
| 232 self.mox.VerifyAll() |
| 233 |
| 234 def testTooManyArgs(self): |
| 235 options = self.Options() |
| 236 self.mox.ReplayAll() |
| 237 self.assertRaisesError("unknown subcommand 'config'; see 'gclient help'", |
| 238 gclient.DoHelp, options, ('config', |
| 239 'another argument')) |
| 240 self.mox.VerifyAll() |
| 241 |
| 242 def testUnknownSubcommand(self): |
| 243 options = self.Options() |
| 244 self.mox.ReplayAll() |
| 245 self.assertRaisesError("unknown subcommand 'xyzzy'; see 'gclient help'", |
| 246 gclient.DoHelp, options, ('xyzzy',)) |
| 247 self.mox.VerifyAll() |
| 248 |
| 249 |
| 250 class GenericCommandTestCase(GclientTestCase): |
| 251 def ReturnValue(self, command, function, return_value): |
| 252 options = self.Options() |
| 253 self.gclient.LoadCurrentConfig(options).AndReturn(self.gclient) |
| 254 self.gclient.RunOnDeps(command, self.args).AndReturn(return_value) |
| 255 |
| 256 self.mox.ReplayAll() |
| 257 result = function(options, self.args) |
| 258 self.assertEquals(result, return_value) |
| 259 self.mox.VerifyAll() |
| 260 |
| 261 def BadClient(self, function): |
| 262 options = self.Options() |
| 263 self.gclient.LoadCurrentConfig(options).AndReturn(None) |
| 264 |
| 265 self.mox.ReplayAll() |
| 266 self.assertRaisesError( |
| 267 "client not configured; see 'gclient config'", |
| 268 function, options, self.args) |
| 269 self.mox.VerifyAll() |
| 270 |
| 271 def Verbose(self, command, function): |
| 272 options = self.Options(verbose=True) |
| 273 self.gclient.LoadCurrentConfig(options).AndReturn(self.gclient) |
| 274 text = "# Dummy content\nclient = 'my client'" |
| 275 self.gclient.ConfigContent().AndReturn(text) |
| 276 print >>self.stdout, text |
| 277 self.gclient.RunOnDeps(command, self.args).AndReturn(0) |
| 278 |
| 279 self.mox.ReplayAll() |
| 280 result = function(options, self.args) |
| 281 self.assertEquals(result, 0) |
| 282 self.mox.VerifyAll() |
| 283 |
| 284 class TestDoCleanup(GenericCommandTestCase): |
| 285 def testGoodClient(self): |
| 286 self.ReturnValue('cleanup', gclient.DoCleanup, 0) |
| 287 def testError(self): |
| 288 self.ReturnValue('cleanup', gclient.DoCleanup, 42) |
| 289 def testBadClient(self): |
| 290 self.BadClient(gclient.DoCleanup) |
| 291 |
| 292 class TestDoStatus(GenericCommandTestCase): |
| 293 def testGoodClient(self): |
| 294 self.ReturnValue('status', gclient.DoStatus, 0) |
| 295 def testError(self): |
| 296 self.ReturnValue('status', gclient.DoStatus, 42) |
| 297 def testBadClient(self): |
| 298 self.BadClient(gclient.DoStatus) |
| 299 |
| 300 |
| 301 class TestDoRunHooks(GenericCommandTestCase): |
| 302 def Options(self, verbose=False, *args, **kwargs): |
| 303 return self.OptionsObject(self, verbose=verbose, *args, **kwargs) |
| 304 |
| 305 def testGoodClient(self): |
| 306 self.ReturnValue('runhooks', gclient.DoRunHooks, 0) |
| 307 def testError(self): |
| 308 self.ReturnValue('runhooks', gclient.DoRunHooks, 42) |
| 309 def testBadClient(self): |
| 310 self.BadClient(gclient.DoRunHooks) |
| 311 |
| 312 |
| 313 class TestDoUpdate(GenericCommandTestCase): |
| 314 def Options(self, verbose=False, *args, **kwargs): |
| 315 return self.OptionsObject(self, verbose=verbose, *args, **kwargs) |
| 316 |
| 317 def ReturnValue(self, command, function, return_value): |
| 318 options = self.Options() |
| 319 self.gclient.LoadCurrentConfig(options).AndReturn(self.gclient) |
| 320 self.gclient.GetVar("solutions") |
| 321 self.gclient.RunOnDeps(command, self.args).AndReturn(return_value) |
| 322 |
| 323 self.mox.ReplayAll() |
| 324 result = function(options, self.args) |
| 325 self.assertEquals(result, return_value) |
| 326 self.mox.VerifyAll() |
| 327 |
| 328 def Verbose(self, command, function): |
| 329 options = self.Options(verbose=True) |
| 330 self.gclient.LoadCurrentConfig(options).AndReturn(self.gclient) |
| 331 self.gclient.GetVar("solutions") |
| 332 text = "# Dummy content\nclient = 'my client'" |
| 333 self.gclient.ConfigContent().AndReturn(text) |
| 334 print >>self.stdout, text |
| 335 self.gclient.RunOnDeps(command, self.args).AndReturn(0) |
| 336 |
| 337 self.mox.ReplayAll() |
| 338 result = function(options, self.args) |
| 339 self.assertEquals(result, 0) |
| 340 self.mox.VerifyAll() |
| 341 |
| 342 def Options(self, verbose=False, *args, **kwargs): |
| 343 return self.OptionsObject(self, verbose=verbose, *args, **kwargs) |
| 344 |
| 345 def testBasic(self): |
| 346 self.ReturnValue('update', gclient.DoUpdate, 0) |
| 347 def testError(self): |
| 348 self.ReturnValue('update', gclient.DoUpdate, 42) |
| 349 def testBadClient(self): |
| 350 self.BadClient(gclient.DoUpdate) |
| 351 def testVerbose(self): |
| 352 self.Verbose('update', gclient.DoUpdate) |
| 353 |
| 354 |
| 355 class TestDoDiff(GenericCommandTestCase): |
| 356 def Options(self, *args, **kwargs): |
| 357 return self.OptionsObject(self, *args, **kwargs) |
| 358 |
| 359 def testBasic(self): |
| 360 self.ReturnValue('diff', gclient.DoDiff, 0) |
| 361 def testError(self): |
| 362 self.ReturnValue('diff', gclient.DoDiff, 42) |
| 363 def testBadClient(self): |
| 364 self.BadClient(gclient.DoDiff) |
| 365 def testVerbose(self): |
| 366 self.Verbose('diff', gclient.DoDiff) |
| 367 |
| 368 |
| 369 class TestDoRevert(GenericCommandTestCase): |
| 370 def testBasic(self): |
| 371 self.ReturnValue('revert', gclient.DoRevert, 0) |
| 372 def testError(self): |
| 373 self.ReturnValue('revert', gclient.DoRevert, 42) |
| 374 def testBadClient(self): |
| 375 self.BadClient(gclient.DoRevert) |
| 376 |
| 377 |
| 378 class GClientClassTestCase(GclientTestCase): |
| 379 def testDir(self): |
| 380 members = ['ConfigContent', 'FromImpl', '_VarImpl', '_ParseAllDeps', |
| 381 '_ParseSolutionDeps', 'GetVar', '_LoadConfig', 'LoadCurrentConfig', |
| 382 '_ReadEntries', '_RunHookAction', '_RunHooks', 'RunOnDeps', 'SaveConfig', |
| 383 '_SaveEntries', 'SetConfig', 'SetDefaultConfig', 'supported_commands', |
| 384 'PrintRevInfo'] |
| 385 |
| 386 # If you add a member, be sure to add the relevant test! |
| 387 actual_members = [x for x in sorted(dir(gclient.GClient)) |
| 388 if not x.startswith('__')] |
| 389 self.assertEqual(actual_members, sorted(members)) |
| 390 self.mox.ReplayAll() |
| 391 self.mox.VerifyAll() |
| 392 |
| 393 def testSetConfig_ConfigContent_GetVar_SaveConfig_SetDefaultConfig(self): |
| 394 options = self.Options() |
| 395 text = "# Dummy content\nclient = 'my client'" |
| 396 gclient.FileWrite(os.path.join(self.root_dir, options.config_filename), |
| 397 text) |
| 398 |
| 399 self.mox.ReplayAll() |
| 400 client = gclient.GClient(self.root_dir, options) |
| 401 client.SetConfig(text) |
| 402 self.assertEqual(client.ConfigContent(), text) |
| 403 self.assertEqual(client.GetVar('client'), 'my client') |
| 404 self.assertEqual(client.GetVar('foo'), None) |
| 405 client.SaveConfig() |
| 406 |
| 407 solution_name = 'solution name' |
| 408 solution_url = 'solution url' |
| 409 safesync_url = 'safesync url' |
| 410 default_text = gclient.DEFAULT_CLIENT_FILE_TEXT % (solution_name, |
| 411 solution_url, |
| 412 safesync_url) |
| 413 client.SetDefaultConfig(solution_name, solution_url, safesync_url) |
| 414 self.assertEqual(client.ConfigContent(), default_text) |
| 415 solutions = [{ |
| 416 'name': solution_name, |
| 417 'url': solution_url, |
| 418 'custom_deps': {}, |
| 419 'safesync_url': safesync_url |
| 420 }] |
| 421 self.assertEqual(client.GetVar('solutions'), solutions) |
| 422 self.assertEqual(client.GetVar('foo'), None) |
| 423 self.mox.VerifyAll() |
| 424 |
| 425 def testLoadCurrentConfig(self): |
| 426 # pymox has trouble to mock the class object and not a class instance. |
| 427 self.gclient = self.mox.CreateMockAnything() |
| 428 options = self.Options() |
| 429 path = os.path.realpath(self.root_dir) |
| 430 options.path_exists(os.path.join(path, options.config_filename) |
| 431 ).AndReturn(True) |
| 432 options.gclient(path, options).AndReturn(options.gclient) |
| 433 options.gclient._LoadConfig() |
| 434 |
| 435 self.mox.ReplayAll() |
| 436 client = gclient.GClient.LoadCurrentConfig(options, self.root_dir) |
| 437 self.mox.VerifyAll() |
| 438 |
| 439 def testRunOnDepsNoDeps(self): |
| 440 solution_name = 'testRunOnDepsNoDeps_solution_name' |
| 441 gclient_config = ( |
| 442 "solutions = [ {\n" |
| 443 " 'name': '%s',\n" |
| 444 " 'url': '%s',\n" |
| 445 " 'custom_deps': {},\n" |
| 446 "} ]\n" |
| 447 ) % (solution_name, self.url) |
| 448 |
| 449 entries_content = ( |
| 450 'entries = [\n' |
| 451 ' "%s",\n' |
| 452 ']\n' |
| 453 ) % solution_name |
| 454 |
| 455 self.scm_wrapper = self.mox.CreateMockAnything() |
| 456 scm_wrapper_sol = self.mox.CreateMock(gclient.SCMWrapper) |
| 457 |
| 458 options = self.Options() |
| 459 |
| 460 # Expect a check for the entries file and we say there is not one. |
| 461 options.path_exists(os.path.join(self.root_dir, options.entries_filename) |
| 462 ).AndReturn(False) |
| 463 |
| 464 # An scm will be requested for the solution. |
| 465 options.scm_wrapper(self.url, self.root_dir, solution_name |
| 466 ).AndReturn(scm_wrapper_sol) |
| 467 # Then an update will be performed. |
| 468 scm_wrapper_sol.RunCommand('update', options, self.args, []) |
| 469 # Then an attempt will be made to read its DEPS file. |
| 470 gclient.FileRead(os.path.join(self.root_dir, |
| 471 solution_name, |
| 472 options.deps_file)).AndRaise(IOError(2, 'No DEPS file')) |
| 473 |
| 474 # After everything is done, an attempt is made to write an entries |
| 475 # file. |
| 476 gclient.FileWrite(os.path.join(self.root_dir, options.entries_filename), |
| 477 entries_content) |
| 478 |
| 479 self.mox.ReplayAll() |
| 480 client = gclient.GClient(self.root_dir, options) |
| 481 client.SetConfig(gclient_config) |
| 482 client.RunOnDeps('update', self.args) |
| 483 self.mox.VerifyAll() |
| 484 |
| 485 def testRunOnDepsRelativePaths(self): |
| 486 solution_name = 'testRunOnDepsRelativePaths_solution_name' |
| 487 gclient_config = ( |
| 488 "solutions = [ {\n" |
| 489 " 'name': '%s',\n" |
| 490 " 'url': '%s',\n" |
| 491 " 'custom_deps': {},\n" |
| 492 "} ]\n" |
| 493 ) % (solution_name, self.url) |
| 494 |
| 495 deps = ( |
| 496 "use_relative_paths = True\n" |
| 497 "deps = {\n" |
| 498 " 'src/t': 'svn://scm.t/trunk',\n" |
| 499 "}\n") |
| 500 |
| 501 entries_content = ( |
| 502 'entries = [\n' |
| 503 ' "%s",\n' |
| 504 ' "%s",\n' |
| 505 ']\n' |
| 506 ) % (os.path.join(solution_name, 'src', 't'), solution_name) |
| 507 |
| 508 self.scm_wrapper = self.mox.CreateMockAnything() |
| 509 scm_wrapper_sol = self.mox.CreateMock(gclient.SCMWrapper) |
| 510 scm_wrapper_t = self.mox.CreateMock(gclient.SCMWrapper) |
| 511 |
| 512 options = self.Options() |
| 513 |
| 514 # Expect a check for the entries file and we say there is not one. |
| 515 options.path_exists(os.path.join(self.root_dir, options.entries_filename) |
| 516 ).AndReturn(False) |
| 517 |
| 518 # An scm will be requested for the solution. |
| 519 options.scm_wrapper(self.url, self.root_dir, solution_name |
| 520 ).AndReturn(scm_wrapper_sol) |
| 521 # Then an update will be performed. |
| 522 scm_wrapper_sol.RunCommand('update', options, self.args, []) |
| 523 # Then an attempt will be made to read its DEPS file. |
| 524 gclient.FileRead(os.path.join(self.root_dir, |
| 525 solution_name, |
| 526 options.deps_file)).AndReturn(deps) |
| 527 |
| 528 # Next we expect an scm to be request for dep src/t but it should |
| 529 # use the url specified in deps and the relative path should now |
| 530 # be relative to the DEPS file. |
| 531 options.scm_wrapper( |
| 532 'svn://scm.t/trunk', |
| 533 self.root_dir, |
| 534 os.path.join(solution_name, "src", "t")).AndReturn(scm_wrapper_t) |
| 535 scm_wrapper_t.RunCommand('update', options, self.args, []) |
| 536 |
| 537 # After everything is done, an attempt is made to write an entries |
| 538 # file. |
| 539 gclient.FileWrite(os.path.join(self.root_dir, options.entries_filename), |
| 540 entries_content) |
| 541 |
| 542 self.mox.ReplayAll() |
| 543 client = gclient.GClient(self.root_dir, options) |
| 544 client.SetConfig(gclient_config) |
| 545 client.RunOnDeps('update', self.args) |
| 546 self.mox.VerifyAll() |
| 547 |
| 548 def testRunOnDepsCustomDeps(self): |
| 549 solution_name = 'testRunOnDepsCustomDeps_solution_name' |
| 550 gclient_config = ( |
| 551 "solutions = [ {\n" |
| 552 " 'name': '%s',\n" |
| 553 " 'url': '%s',\n" |
| 554 " 'custom_deps': {\n" |
| 555 " 'src/b': None,\n" |
| 556 " 'src/n': 'svn://custom.n/trunk',\n" |
| 557 " 'src/t': 'svn://custom.t/trunk',\n" |
| 558 " }\n} ]\n" |
| 559 ) % (solution_name, self.url) |
| 560 |
| 561 deps = ( |
| 562 "deps = {\n" |
| 563 " 'src/b': 'svn://original.b/trunk',\n" |
| 564 " 'src/t': 'svn://original.t/trunk',\n" |
| 565 "}\n" |
| 566 ) |
| 567 |
| 568 entries_content = ( |
| 569 'entries = [\n' |
| 570 ' "%s",\n' |
| 571 ' "src/n",\n' |
| 572 ' "src/t",\n' |
| 573 ']\n' |
| 574 ) % solution_name |
| 575 |
| 576 self.scm_wrapper = self.mox.CreateMockAnything() |
| 577 scm_wrapper_sol = self.mox.CreateMock(gclient.SCMWrapper) |
| 578 scm_wrapper_t = self.mox.CreateMock(gclient.SCMWrapper) |
| 579 scm_wrapper_n = self.mox.CreateMock(gclient.SCMWrapper) |
| 580 |
| 581 options = self.Options() |
| 582 |
| 583 # Expect a check for the entries file and we say there is not one. |
| 584 options.path_exists(os.path.join(self.root_dir, options.entries_filename) |
| 585 ).AndReturn(False) |
| 586 |
| 587 # An scm will be requested for the solution. |
| 588 options.scm_wrapper(self.url, self.root_dir, solution_name |
| 589 ).AndReturn(scm_wrapper_sol) |
| 590 # Then an update will be performed. |
| 591 scm_wrapper_sol.RunCommand('update', options, self.args, []) |
| 592 # Then an attempt will be made to read its DEPS file. |
| 593 gclient.FileRead(os.path.join(self.root_dir, |
| 594 solution_name, |
| 595 options.deps_file)).AndReturn(deps) |
| 596 |
| 597 # Next we expect an scm to be request for dep src/n even though it does not |
| 598 # exist in the DEPS file. |
| 599 options.scm_wrapper('svn://custom.n/trunk', |
| 600 self.root_dir, |
| 601 "src/n").AndReturn(scm_wrapper_n) |
| 602 |
| 603 # Next we expect an scm to be request for dep src/t but it should |
| 604 # use the url specified in custom_deps. |
| 605 options.scm_wrapper('svn://custom.t/trunk', |
| 606 self.root_dir, |
| 607 "src/t").AndReturn(scm_wrapper_t) |
| 608 |
| 609 scm_wrapper_n.RunCommand('update', options, self.args, []) |
| 610 scm_wrapper_t.RunCommand('update', options, self.args, []) |
| 611 |
| 612 # NOTE: the dep src/b should not create an scm at all. |
| 613 |
| 614 # After everything is done, an attempt is made to write an entries |
| 615 # file. |
| 616 gclient.FileWrite(os.path.join(self.root_dir, options.entries_filename), |
| 617 entries_content) |
| 618 |
| 619 self.mox.ReplayAll() |
| 620 client = gclient.GClient(self.root_dir, options) |
| 621 client.SetConfig(gclient_config) |
| 622 client.RunOnDeps('update', self.args) |
| 623 self.mox.VerifyAll() |
| 624 |
| 625 # Regression test for Issue #11. |
| 626 # http://code.google.com/p/gclient/issues/detail?id=11 |
| 627 def testRunOnDepsSharedDependency(self): |
| 628 name_a = 'testRunOnDepsSharedDependency_a' |
| 629 name_b = 'testRunOnDepsSharedDependency_b' |
| 630 |
| 631 url_a = self.url + '/a' |
| 632 url_b = self.url + '/b' |
| 633 |
| 634 # config declares two solutions and each has a dependency to place |
| 635 # http://svn.t/trunk at src/t. |
| 636 gclient_config = ( |
| 637 "solutions = [ {\n" |
| 638 " 'name': '%s',\n" |
| 639 " 'url': '%s',\n" |
| 640 " 'custom_deps': {},\n" |
| 641 "}, {\n" |
| 642 " 'name': '%s',\n" |
| 643 " 'url': '%s',\n" |
| 644 " 'custom_deps': {},\n" |
| 645 "}\n]\n") % (name_a, url_a, name_b, url_b) |
| 646 |
| 647 deps_b = deps_a = ( |
| 648 "deps = {\n" |
| 649 " 'src/t' : 'http://svn.t/trunk',\n" |
| 650 "}\n") |
| 651 |
| 652 entries_content = ( |
| 653 'entries = [\n "%s",\n' |
| 654 ' "%s",\n' |
| 655 ' "src/t",\n' |
| 656 ']\n') % (name_a, name_b) |
| 657 |
| 658 self.scm_wrapper = self.mox.CreateMockAnything() |
| 659 scm_wrapper_a = self.mox.CreateMock(gclient.SCMWrapper) |
| 660 scm_wrapper_b = self.mox.CreateMock(gclient.SCMWrapper) |
| 661 scm_wrapper_dep = self.mox.CreateMock(gclient.SCMWrapper) |
| 662 |
| 663 options = self.Options() |
| 664 |
| 665 # Expect a check for the entries file and we say there is not one. |
| 666 options.path_exists(os.path.join(self.root_dir, options.entries_filename) |
| 667 ).AndReturn(False) |
| 668 |
| 669 # An scm will be requested for the first solution. |
| 670 options.scm_wrapper(url_a, self.root_dir, name_a).AndReturn( |
| 671 scm_wrapper_a) |
| 672 # Then an attempt will be made to read it's DEPS file. |
| 673 gclient.FileRead(os.path.join(self.root_dir, name_a, options.deps_file) |
| 674 ).AndReturn(deps_a) |
| 675 # Then an update will be performed. |
| 676 scm_wrapper_a.RunCommand('update', options, self.args, []) |
| 677 |
| 678 # An scm will be requested for the second solution. |
| 679 options.scm_wrapper(url_b, self.root_dir, name_b).AndReturn( |
| 680 scm_wrapper_b) |
| 681 # Then an attempt will be made to read its DEPS file. |
| 682 gclient.FileRead(os.path.join(self.root_dir, name_b, options.deps_file) |
| 683 ).AndReturn(deps_b) |
| 684 # Then an update will be performed. |
| 685 scm_wrapper_b.RunCommand('update', options, self.args, []) |
| 686 |
| 687 # Finally, an scm is requested for the shared dep. |
| 688 options.scm_wrapper('http://svn.t/trunk', self.root_dir, 'src/t' |
| 689 ).AndReturn(scm_wrapper_dep) |
| 690 # And an update is run on it. |
| 691 scm_wrapper_dep.RunCommand('update', options, self.args, []) |
| 692 |
| 693 # After everything is done, an attempt is made to write an entries file. |
| 694 gclient.FileWrite(os.path.join(self.root_dir, options.entries_filename), |
| 695 entries_content) |
| 696 |
| 697 self.mox.ReplayAll() |
| 698 client = gclient.GClient(self.root_dir, options) |
| 699 client.SetConfig(gclient_config) |
| 700 client.RunOnDeps('update', self.args) |
| 701 self.mox.VerifyAll() |
| 702 |
| 703 def testRunOnDepsSuccess(self): |
| 704 # Fake .gclient file. |
| 705 name = 'testRunOnDepsSuccess_solution_name' |
| 706 gclient_config = """solutions = [ { |
| 707 'name': '%s', |
| 708 'url': '%s', |
| 709 'custom_deps': {}, |
| 710 }, ]""" % (name, self.url) |
| 711 |
| 712 # pymox has trouble to mock the class object and not a class instance. |
| 713 self.scm_wrapper = self.mox.CreateMockAnything() |
| 714 options = self.Options() |
| 715 options.path_exists(os.path.join(self.root_dir, options.entries_filename) |
| 716 ).AndReturn(False) |
| 717 options.scm_wrapper(self.url, self.root_dir, name).AndReturn( |
| 718 options.scm_wrapper) |
| 719 options.scm_wrapper.RunCommand('update', options, self.args, []) |
| 720 gclient.FileRead(os.path.join(self.root_dir, name, options.deps_file) |
| 721 ).AndReturn("Boo = 'a'") |
| 722 gclient.FileWrite(os.path.join(self.root_dir, options.entries_filename), |
| 723 'entries = [\n "%s",\n]\n' % name) |
| 724 |
| 725 self.mox.ReplayAll() |
| 726 client = gclient.GClient(self.root_dir, options) |
| 727 client.SetConfig(gclient_config) |
| 728 client.RunOnDeps('update', self.args) |
| 729 self.mox.VerifyAll() |
| 730 |
| 731 def testRunOnDepsRevisions(self): |
| 732 def OptIsRev(options, rev): |
| 733 if not options.revision == str(rev): |
| 734 print "options.revision = %s" % options.revision |
| 735 return options.revision == str(rev) |
| 736 def OptIsRevNone(options): |
| 737 if options.revision: |
| 738 print "options.revision = %s" % options.revision |
| 739 return options.revision == None |
| 740 def OptIsRev42(options): |
| 741 return OptIsRev(options, 42) |
| 742 def OptIsRev123(options): |
| 743 return OptIsRev(options, 123) |
| 744 def OptIsRev333(options): |
| 745 return OptIsRev(options, 333) |
| 746 |
| 747 # Fake .gclient file. |
| 748 gclient_config = """solutions = [ { |
| 749 'name': 'src', |
| 750 'url': '%s', |
| 751 'custom_deps': {}, |
| 752 }, ]""" % self.url |
| 753 # Fake DEPS file. |
| 754 deps_content = """deps = { |
| 755 'src/breakpad/bar': 'http://google-breakpad.googlecode.com/svn/trunk/src@285', |
| 756 'foo/third_party/WebKit': '/trunk/deps/third_party/WebKit', |
| 757 'src/third_party/cygwin': '/trunk/deps/third_party/cygwin@3248', |
| 758 } |
| 759 deps_os = { |
| 760 'win': { |
| 761 'src/foosad/asdf': 'svn://random_server:123/asd/python_24@5580', |
| 762 }, |
| 763 'mac': { |
| 764 'src/third_party/python_24': 'svn://random_server:123/trunk/python_24@5580', |
| 765 }, |
| 766 }""" |
| 767 entries_content = ( |
| 768 'entries = [\n "src",\n' |
| 769 ' "foo/third_party/WebKit",\n' |
| 770 ' "src/third_party/cygwin",\n' |
| 771 ' "src/third_party/python_24",\n' |
| 772 ' "src/breakpad/bar",\n' |
| 773 ']\n') |
| 774 cygwin_path = 'dummy path cygwin' |
| 775 webkit_path = 'dummy path webkit' |
| 776 |
| 777 # pymox has trouble to mock the class object and not a class instance. |
| 778 self.scm_wrapper = self.mox.CreateMockAnything() |
| 779 scm_wrapper_bleh = self.mox.CreateMock(gclient.SCMWrapper) |
| 780 scm_wrapper_src = self.mox.CreateMock(gclient.SCMWrapper) |
| 781 scm_wrapper_src2 = self.mox.CreateMock(gclient.SCMWrapper) |
| 782 scm_wrapper_webkit = self.mox.CreateMock(gclient.SCMWrapper) |
| 783 scm_wrapper_breakpad = self.mox.CreateMock(gclient.SCMWrapper) |
| 784 scm_wrapper_cygwin = self.mox.CreateMock(gclient.SCMWrapper) |
| 785 scm_wrapper_python = self.mox.CreateMock(gclient.SCMWrapper) |
| 786 options = self.Options() |
| 787 options.revisions = [ 'src@123', 'foo/third_party/WebKit@42', |
| 788 'src/third_party/cygwin@333' ] |
| 789 |
| 790 # Also, pymox doesn't verify the order of function calling w.r.t. different |
| 791 # mock objects. Pretty lame. So reorder as we wish to make it clearer. |
| 792 gclient.FileRead(os.path.join(self.root_dir, 'src', options.deps_file) |
| 793 ).AndReturn(deps_content) |
| 794 gclient.FileWrite(os.path.join(self.root_dir, options.entries_filename), |
| 795 entries_content) |
| 796 |
| 797 options.path_exists(os.path.join(self.root_dir, options.entries_filename) |
| 798 ).AndReturn(False) |
| 799 |
| 800 options.scm_wrapper(self.url, self.root_dir, 'src').AndReturn( |
| 801 scm_wrapper_src) |
| 802 scm_wrapper_src.RunCommand('update', mox.Func(OptIsRev123), self.args, []) |
| 803 |
| 804 options.scm_wrapper(self.url, self.root_dir, |
| 805 None).AndReturn(scm_wrapper_src2) |
| 806 scm_wrapper_src2.FullUrlForRelativeUrl('/trunk/deps/third_party/cygwin@3248' |
| 807 ).AndReturn(cygwin_path) |
| 808 |
| 809 options.scm_wrapper(self.url, self.root_dir, |
| 810 None).AndReturn(scm_wrapper_src2) |
| 811 scm_wrapper_src2.FullUrlForRelativeUrl('/trunk/deps/third_party/WebKit' |
| 812 ).AndReturn(webkit_path) |
| 813 |
| 814 options.scm_wrapper(webkit_path, self.root_dir, |
| 815 'foo/third_party/WebKit').AndReturn(scm_wrapper_webkit) |
| 816 scm_wrapper_webkit.RunCommand('update', mox.Func(OptIsRev42), self.args, []) |
| 817 |
| 818 options.scm_wrapper( |
| 819 'http://google-breakpad.googlecode.com/svn/trunk/src@285', |
| 820 self.root_dir, 'src/breakpad/bar').AndReturn(scm_wrapper_breakpad) |
| 821 scm_wrapper_breakpad.RunCommand('update', mox.Func(OptIsRevNone), |
| 822 self.args, []) |
| 823 |
| 824 options.scm_wrapper(cygwin_path, self.root_dir, |
| 825 'src/third_party/cygwin').AndReturn(scm_wrapper_cygwin) |
| 826 scm_wrapper_cygwin.RunCommand('update', mox.Func(OptIsRev333), self.args, |
| 827 []) |
| 828 |
| 829 options.scm_wrapper('svn://random_server:123/trunk/python_24@5580', |
| 830 self.root_dir, |
| 831 'src/third_party/python_24').AndReturn( |
| 832 scm_wrapper_python) |
| 833 scm_wrapper_python.RunCommand('update', mox.Func(OptIsRevNone), self.args, |
| 834 []) |
| 835 |
| 836 self.mox.ReplayAll() |
| 837 client = gclient.GClient(self.root_dir, options) |
| 838 client.SetConfig(gclient_config) |
| 839 client.RunOnDeps('update', self.args) |
| 840 self.mox.VerifyAll() |
| 841 |
| 842 def testRunOnDepsConflictingRevisions(self): |
| 843 # Fake .gclient file. |
| 844 name = 'testRunOnDepsConflictingRevisions_solution_name' |
| 845 gclient_config = """solutions = [ { |
| 846 'name': '%s', |
| 847 'url': '%s', |
| 848 'custom_deps': {}, |
| 849 'custom_vars': {}, |
| 850 }, ]""" % (name, self.url) |
| 851 # Fake DEPS file. |
| 852 deps_content = """deps = { |
| 853 'foo/third_party/WebKit': '/trunk/deps/third_party/WebKit', |
| 854 }""" |
| 855 |
| 856 options = self.Options() |
| 857 options.revisions = [ 'foo/third_party/WebKit@42', |
| 858 'foo/third_party/WebKit@43' ] |
| 859 client = gclient.GClient(self.root_dir, options) |
| 860 client.SetConfig(gclient_config) |
| 861 exception = "Conflicting revision numbers specified." |
| 862 try: |
| 863 client.RunOnDeps('update', self.args) |
| 864 except gclient.Error, e: |
| 865 self.assertEquals(e.args[0], exception) |
| 866 else: |
| 867 self.fail('%s not raised' % exception) |
| 868 |
| 869 def testRunOnDepsSuccessVars(self): |
| 870 # Fake .gclient file. |
| 871 name = 'testRunOnDepsSuccessVars_solution_name' |
| 872 gclient_config = """solutions = [ { |
| 873 'name': '%s', |
| 874 'url': '%s', |
| 875 'custom_deps': {}, |
| 876 'custom_vars': {}, |
| 877 }, ]""" % (name, self.url) |
| 878 # Fake DEPS file. |
| 879 deps_content = """vars = { |
| 880 'webkit': '/trunk/bar/', |
| 881 } |
| 882 deps = { |
| 883 'foo/third_party/WebKit': Var('webkit') + 'WebKit', |
| 884 }""" |
| 885 entries_content = ( |
| 886 'entries = [\n "foo/third_party/WebKit",\n' |
| 887 ' "%s",\n' |
| 888 ']\n') % name |
| 889 webkit_path = 'dummy path webkit' |
| 890 |
| 891 # pymox has trouble to mock the class object and not a class instance. |
| 892 self.scm_wrapper = self.mox.CreateMockAnything() |
| 893 scm_wrapper_webkit = self.mox.CreateMock(gclient.SCMWrapper) |
| 894 scm_wrapper_src = self.mox.CreateMock(gclient.SCMWrapper) |
| 895 |
| 896 options = self.Options() |
| 897 gclient.FileRead(os.path.join(self.root_dir, name, options.deps_file) |
| 898 ).AndReturn(deps_content) |
| 899 gclient.FileWrite(os.path.join(self.root_dir, options.entries_filename), |
| 900 entries_content) |
| 901 |
| 902 options.path_exists(os.path.join(self.root_dir, options.entries_filename) |
| 903 ).AndReturn(False) |
| 904 options.scm_wrapper(self.url, self.root_dir, name).AndReturn( |
| 905 options.scm_wrapper) |
| 906 options.scm_wrapper.RunCommand('update', options, self.args, []) |
| 907 |
| 908 options.scm_wrapper(self.url, self.root_dir, |
| 909 None).AndReturn(scm_wrapper_src) |
| 910 scm_wrapper_src.FullUrlForRelativeUrl('/trunk/bar/WebKit' |
| 911 ).AndReturn(webkit_path) |
| 912 |
| 913 options.scm_wrapper(webkit_path, self.root_dir, |
| 914 'foo/third_party/WebKit').AndReturn(options.scm_wrapper) |
| 915 options.scm_wrapper.RunCommand('update', options, self.args, []) |
| 916 |
| 917 self.mox.ReplayAll() |
| 918 client = gclient.GClient(self.root_dir, options) |
| 919 client.SetConfig(gclient_config) |
| 920 client.RunOnDeps('update', self.args) |
| 921 self.mox.VerifyAll() |
| 922 |
| 923 def testRunOnDepsSuccessCustomVars(self): |
| 924 # Fake .gclient file. |
| 925 name = 'testRunOnDepsSuccessCustomVars_solution_name' |
| 926 gclient_config = """solutions = [ { |
| 927 'name': '%s', |
| 928 'url': '%s', |
| 929 'custom_deps': {}, |
| 930 'custom_vars': {'webkit': '/trunk/bar_custom/'}, |
| 931 }, ]""" % (name, self.url) |
| 932 # Fake DEPS file. |
| 933 deps_content = """vars = { |
| 934 'webkit': '/trunk/bar/', |
| 935 } |
| 936 deps = { |
| 937 'foo/third_party/WebKit': Var('webkit') + 'WebKit', |
| 938 }""" |
| 939 entries_content = ( |
| 940 'entries = [\n "foo/third_party/WebKit",\n' |
| 941 ' "%s",\n' |
| 942 ']\n') % name |
| 943 webkit_path = 'dummy path webkit' |
| 944 |
| 945 # pymox has trouble to mock the class object and not a class instance. |
| 946 self.scm_wrapper = self.mox.CreateMockAnything() |
| 947 scm_wrapper_webkit = self.mox.CreateMock(gclient.SCMWrapper) |
| 948 scm_wrapper_src = self.mox.CreateMock(gclient.SCMWrapper) |
| 949 |
| 950 options = self.Options() |
| 951 gclient.FileRead(os.path.join(self.root_dir, name, options.deps_file) |
| 952 ).AndReturn(deps_content) |
| 953 gclient.FileWrite(os.path.join(self.root_dir, options.entries_filename), |
| 954 entries_content) |
| 955 |
| 956 options.path_exists(os.path.join(self.root_dir, options.entries_filename) |
| 957 ).AndReturn(False) |
| 958 options.scm_wrapper(self.url, self.root_dir, name).AndReturn( |
| 959 options.scm_wrapper) |
| 960 options.scm_wrapper.RunCommand('update', options, self.args, []) |
| 961 |
| 962 options.scm_wrapper(self.url, self.root_dir, |
| 963 None).AndReturn(scm_wrapper_src) |
| 964 scm_wrapper_src.FullUrlForRelativeUrl('/trunk/bar_custom/WebKit' |
| 965 ).AndReturn(webkit_path) |
| 966 |
| 967 options.scm_wrapper(webkit_path, self.root_dir, |
| 968 'foo/third_party/WebKit').AndReturn(options.scm_wrapper) |
| 969 options.scm_wrapper.RunCommand('update', options, self.args, []) |
| 970 |
| 971 self.mox.ReplayAll() |
| 972 client = gclient.GClient(self.root_dir, options) |
| 973 client.SetConfig(gclient_config) |
| 974 client.RunOnDeps('update', self.args) |
| 975 self.mox.VerifyAll() |
| 976 |
| 977 def testRunOnDepsFailueVars(self): |
| 978 # Fake .gclient file. |
| 979 name = 'testRunOnDepsFailureVars_solution_name' |
| 980 gclient_config = """solutions = [ { |
| 981 'name': '%s', |
| 982 'url': '%s', |
| 983 'custom_deps': {}, |
| 984 'custom_vars': {}, |
| 985 }, ]""" % (name, self.url) |
| 986 # Fake DEPS file. |
| 987 deps_content = """deps = { |
| 988 'foo/third_party/WebKit': Var('webkit') + 'WebKit', |
| 989 }""" |
| 990 |
| 991 # pymox has trouble to mock the class object and not a class instance. |
| 992 self.scm_wrapper = self.mox.CreateMockAnything() |
| 993 |
| 994 options = self.Options() |
| 995 gclient.FileRead(os.path.join(self.root_dir, name, options.deps_file) |
| 996 ).AndReturn(deps_content) |
| 997 gclient.FileWrite(os.path.join(self.root_dir, options.entries_filename), |
| 998 'dummy entries content') |
| 999 |
| 1000 options.path_exists(os.path.join(self.root_dir, options.entries_filename) |
| 1001 ).AndReturn(False) |
| 1002 options.scm_wrapper(self.url, self.root_dir, name).AndReturn( |
| 1003 options.scm_wrapper) |
| 1004 options.scm_wrapper.RunCommand('update', options, self.args, []) |
| 1005 |
| 1006 self.mox.ReplayAll() |
| 1007 client = gclient.GClient(self.root_dir, options) |
| 1008 client.SetConfig(gclient_config) |
| 1009 exception = "Var is not defined: webkit" |
| 1010 try: |
| 1011 client.RunOnDeps('update', self.args) |
| 1012 except gclient.Error, e: |
| 1013 self.assertEquals(e.args[0], exception) |
| 1014 else: |
| 1015 self.fail('%s not raised' % exception) |
| 1016 |
| 1017 def testRunOnDepsFailureInvalidCommand(self): |
| 1018 options = self.Options() |
| 1019 |
| 1020 self.mox.ReplayAll() |
| 1021 client = gclient.GClient(self.root_dir, options) |
| 1022 exception = "'foo' is an unsupported command" |
| 1023 self.assertRaisesError(exception, gclient.GClient.RunOnDeps, client, 'foo', |
| 1024 self.args) |
| 1025 self.mox.VerifyAll() |
| 1026 |
| 1027 def testRunOnDepsFailureEmpty(self): |
| 1028 options = self.Options() |
| 1029 |
| 1030 self.mox.ReplayAll() |
| 1031 client = gclient.GClient(self.root_dir, options) |
| 1032 exception = "No solution specified" |
| 1033 self.assertRaisesError(exception, gclient.GClient.RunOnDeps, client, |
| 1034 'update', self.args) |
| 1035 self.mox.VerifyAll() |
| 1036 |
| 1037 def testFromImpl(self): |
| 1038 # TODO(maruel): Test me! |
| 1039 pass |
| 1040 |
| 1041 def test_PrintRevInfo(self): |
| 1042 # TODO(aharper): no test yet for revinfo, lock it down once we've verified |
| 1043 # implementation for Pulse plugin |
| 1044 pass |
| 1045 |
| 1046 # No test for internal functions. |
| 1047 def test_GetAllDeps(self): |
| 1048 pass |
| 1049 def test_GetDefaultSolutionDeps(self): |
| 1050 pass |
| 1051 def test_LoadConfig(self): |
| 1052 pass |
| 1053 def test_ReadEntries(self): |
| 1054 pass |
| 1055 def test_SaveEntries(self): |
| 1056 pass |
| 1057 def test_VarImpl(self): |
| 1058 pass |
| 1059 |
| 1060 |
| 1061 class SCMWrapperTestCase(BaseTestCase): |
| 1062 class OptionsObject(object): |
| 1063 def __init__(self, test_case, verbose=False, revision=None): |
| 1064 self.verbose = verbose |
| 1065 self.revision = revision |
| 1066 self.manually_grab_svn_rev = True |
| 1067 self.deps_os = None |
| 1068 self.force = False |
| 1069 |
| 1070 # Mox |
| 1071 self.stdout = test_case.stdout |
| 1072 self.path_exists = test_case.path_exists |
| 1073 |
| 1074 def setUp(self): |
| 1075 BaseTestCase.setUp(self) |
| 1076 self.root_dir = Dir() |
| 1077 self.args = Args() |
| 1078 self.url = Url() |
| 1079 self.relpath = 'asf' |
| 1080 self.stdout = self.mox.CreateMock(sys.stdout) |
| 1081 # Stub os.path.exists. |
| 1082 self.path_exists = self.mox.CreateMockAnything() |
| 1083 |
| 1084 def testDir(self): |
| 1085 members = ['FullUrlForRelativeUrl', 'RunCommand', |
| 1086 'cleanup', 'diff', 'revert', 'status', 'update'] |
| 1087 |
| 1088 # If you add a member, be sure to add the relevant test! |
| 1089 actual_members = [x for x in sorted(dir(gclient.SCMWrapper)) |
| 1090 if not x.startswith('__')] |
| 1091 self.assertEqual(actual_members, sorted(members)) |
| 1092 self.mox.ReplayAll() |
| 1093 self.mox.VerifyAll() |
| 1094 |
| 1095 def testFullUrlForRelativeUrl(self): |
| 1096 self.url = 'svn://a/b/c/d' |
| 1097 |
| 1098 self.mox.ReplayAll() |
| 1099 scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir, |
| 1100 relpath=self.relpath) |
| 1101 self.assertEqual(scm.FullUrlForRelativeUrl('/crap'), 'svn://a/b/crap') |
| 1102 self.mox.VerifyAll() |
| 1103 |
| 1104 def testRunCommandException(self): |
| 1105 options = self.Options(verbose=False) |
| 1106 options.path_exists(os.path.join(self.root_dir, self.relpath, '.git') |
| 1107 ).AndReturn(False) |
| 1108 |
| 1109 self.mox.ReplayAll() |
| 1110 scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir, |
| 1111 relpath=self.relpath) |
| 1112 exception = "Unsupported argument(s): %s" % ','.join(self.args) |
| 1113 self.assertRaisesError(exception, gclient.SCMWrapper.RunCommand, |
| 1114 scm, 'update', options, self.args) |
| 1115 self.mox.VerifyAll() |
| 1116 |
| 1117 def testRunCommandUnknown(self): |
| 1118 # TODO(maruel): if ever used. |
| 1119 pass |
| 1120 |
| 1121 def testRevertMissing(self): |
| 1122 options = self.Options(verbose=True) |
| 1123 gclient.os.path.isdir = self.mox.CreateMockAnything() |
| 1124 gclient.os.path.isdir(os.path.join(self.root_dir, self.relpath) |
| 1125 ).AndReturn(False) |
| 1126 print >>options.stdout, ("\n_____ %s is missing, can't revert" % |
| 1127 self.relpath) |
| 1128 |
| 1129 self.mox.ReplayAll() |
| 1130 scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir, |
| 1131 relpath=self.relpath) |
| 1132 file_list = [] |
| 1133 scm.revert(options, self.args, file_list) |
| 1134 self.mox.VerifyAll() |
| 1135 gclient.os.path.isdir = os.path.isdir |
| 1136 |
| 1137 def testRevertNone(self): |
| 1138 options = self.Options(verbose=True) |
| 1139 base_path = os.path.join(self.root_dir, self.relpath) |
| 1140 gclient.os.path.isdir = self.mox.CreateMockAnything() |
| 1141 gclient.os.path.isdir(base_path).AndReturn(True) |
| 1142 gclient.CaptureSVNStatus(options, base_path).AndReturn([]) |
| 1143 |
| 1144 self.mox.ReplayAll() |
| 1145 scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir, |
| 1146 relpath=self.relpath) |
| 1147 file_list = [] |
| 1148 scm.revert(options, self.args, file_list) |
| 1149 self.mox.VerifyAll() |
| 1150 gclient.os.path.isdir = os.path.isdir |
| 1151 |
| 1152 def testRevert2Files(self): |
| 1153 options = self.Options(verbose=True) |
| 1154 base_path = os.path.join(self.root_dir, self.relpath) |
| 1155 gclient.os.path.isdir = self.mox.CreateMockAnything() |
| 1156 gclient.os.path.isdir(base_path).AndReturn(True) |
| 1157 items = [ |
| 1158 gclient.FileStatus('a', 'M', ' ', ' '), |
| 1159 gclient.FileStatus('b', 'A', ' ', ' '), |
| 1160 ] |
| 1161 gclient.CaptureSVNStatus(options, base_path).AndReturn(items) |
| 1162 |
| 1163 print >>options.stdout, os.path.join(base_path, 'a') |
| 1164 print >>options.stdout, os.path.join(base_path, 'b') |
| 1165 gclient.RunSVN(options, ['revert', 'a', 'b'], base_path) |
| 1166 |
| 1167 self.mox.ReplayAll() |
| 1168 scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir, |
| 1169 relpath=self.relpath) |
| 1170 file_list = [] |
| 1171 scm.revert(options, self.args, file_list) |
| 1172 self.mox.VerifyAll() |
| 1173 gclient.os.path.isdir = os.path.isdir |
| 1174 |
| 1175 def testStatus(self): |
| 1176 options = self.Options(verbose=True) |
| 1177 base_path = os.path.join(self.root_dir, self.relpath) |
| 1178 gclient.RunSVNAndGetFileList(options, ['status'] + self.args, base_path, |
| 1179 []).AndReturn(None) |
| 1180 |
| 1181 self.mox.ReplayAll() |
| 1182 scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir, |
| 1183 relpath=self.relpath) |
| 1184 file_list = [] |
| 1185 self.assertEqual(scm.status(options, self.args, file_list), None) |
| 1186 self.mox.VerifyAll() |
| 1187 |
| 1188 |
| 1189 # TODO(maruel): TEST REVISIONS!!! |
| 1190 # TODO(maruel): TEST RELOCATE!!! |
| 1191 def testUpdateCheckout(self): |
| 1192 options = self.Options(verbose=True) |
| 1193 base_path = os.path.join(self.root_dir, self.relpath) |
| 1194 file_info = gclient.PrintableObject() |
| 1195 file_info.root = 'blah' |
| 1196 file_info.url = self.url |
| 1197 file_info.uuid = 'ABC' |
| 1198 file_info.revision = 42 |
| 1199 options.path_exists(os.path.join(base_path, '.git')).AndReturn(False) |
| 1200 # Checkout or update. |
| 1201 options.path_exists(base_path).AndReturn(False) |
| 1202 print >>options.stdout, "\n_____ asf at 42" |
| 1203 #print >>options.stdout, "\n________ running 'svn checkout %s %s' in '%s'" %
( |
| 1204 # self.url, base_path, os.path.abspath(self.root_dir)) |
| 1205 |
| 1206 gclient.CaptureSVNInfo(options, os.path.join(base_path, "."), '.' |
| 1207 ).AndReturn(file_info) |
| 1208 # Cheat a bit here. |
| 1209 gclient.CaptureSVNInfo(options, file_info.url, '.').AndReturn(file_info) |
| 1210 files_list = self.mox.CreateMockAnything() |
| 1211 gclient.RunSVNAndGetFileList(options, ['checkout', self.url, base_path], |
| 1212 self.root_dir, files_list) |
| 1213 self.mox.ReplayAll() |
| 1214 scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir, |
| 1215 relpath=self.relpath) |
| 1216 #file_list = [] |
| 1217 scm.update(options, (), files_list) |
| 1218 self.mox.VerifyAll() |
| 1219 |
| 1220 def testUpdateUpdate(self): |
| 1221 options = self.Options(verbose=True) |
| 1222 base_path = os.path.join(self.root_dir, self.relpath) |
| 1223 options.force = True |
| 1224 file_info = gclient.PrintableObject() |
| 1225 file_info.root = 'blah' |
| 1226 file_info.url = self.url |
| 1227 file_info.uuid = 'ABC' |
| 1228 file_info.revision = 42 |
| 1229 options.path_exists(os.path.join(base_path, '.git')).AndReturn(False) |
| 1230 # Checkout or update. |
| 1231 options.path_exists(base_path).AndReturn(True) |
| 1232 gclient.CaptureSVNInfo(options, os.path.join(base_path, "."), '.' |
| 1233 ).AndReturn(file_info) |
| 1234 # Cheat a bit here. |
| 1235 gclient.CaptureSVNInfo(options, file_info.url, '.').AndReturn(file_info) |
| 1236 additional_args = [] |
| 1237 if options.manually_grab_svn_rev: |
| 1238 additional_args = ['--revision', str(file_info.revision)] |
| 1239 files_list = [] |
| 1240 gclient.RunSVNAndGetFileList(options, ['update', base_path] + additional_arg
s, |
| 1241 self.root_dir, files_list) |
| 1242 |
| 1243 self.mox.ReplayAll() |
| 1244 scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir, |
| 1245 relpath=self.relpath) |
| 1246 scm.update(options, (), files_list) |
| 1247 self.mox.VerifyAll() |
| 1248 |
| 1249 def testUpdateGit(self): |
| 1250 options = self.Options(verbose=True) |
| 1251 options.path_exists(os.path.join(self.root_dir, self.relpath, '.git') |
| 1252 ).AndReturn(True) |
| 1253 print >> options.stdout, ( |
| 1254 "________ found .git directory; skipping %s" % self.relpath) |
| 1255 |
| 1256 self.mox.ReplayAll() |
| 1257 scm = gclient.SCMWrapper(url=self.url, root_dir=self.root_dir, |
| 1258 relpath=self.relpath) |
| 1259 file_list = [] |
| 1260 scm.update(options, self.args, file_list) |
| 1261 self.mox.VerifyAll() |
| 1262 |
| 1263 def testCaptureSvnInfo(self): |
| 1264 xml_text = """<?xml version="1.0"?> |
| 1265 <info> |
| 1266 <entry |
| 1267 kind="dir" |
| 1268 path="." |
| 1269 revision="35"> |
| 1270 <url>%s</url> |
| 1271 <repository> |
| 1272 <root>%s</root> |
| 1273 <uuid>7b9385f5-0452-0410-af26-ad4892b7a1fb</uuid> |
| 1274 </repository> |
| 1275 <wc-info> |
| 1276 <schedule>normal</schedule> |
| 1277 <depth>infinity</depth> |
| 1278 </wc-info> |
| 1279 <commit |
| 1280 revision="35"> |
| 1281 <author>maruel</author> |
| 1282 <date>2008-12-04T20:12:19.685120Z</date> |
| 1283 </commit> |
| 1284 </entry> |
| 1285 </info> |
| 1286 """ % (self.url, self.root_dir) |
| 1287 options = self.Options(verbose=True) |
| 1288 gclient.CaptureSVN(options, ['info', '--xml', self.url], |
| 1289 '.').AndReturn(xml_text) |
| 1290 self.mox.ReplayAll() |
| 1291 file_info = self._CaptureSVNInfo(options, self.url, '.') |
| 1292 self.failUnless(file_info.root == self.root_dir) |
| 1293 self.failUnless(file_info.url == self.url) |
| 1294 self.failUnless(file_info.uuid == '7b9385f5-0452-0410-af26-ad4892b7a1fb') |
| 1295 self.failUnless(file_info.revision == 35) |
| 1296 self.mox.VerifyAll() |
| 1297 |
| 1298 |
| 1299 if __name__ == '__main__': |
| 1300 unittest.main() |
| 1301 |
| 1302 # vim: ts=2:sw=2:tw=80:et: |
OLD | NEW |