Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: tests/fake_repos.py

Issue 6029004: Split FakeRepos class in two so it can be reused for commti-queue unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Generate fake repositories for testing.""" 6 """Generate fake repositories for testing."""
7 7
8 import atexit 8 import atexit
9 import errno 9 import errno
10 import logging 10 import logging
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 check_call(['git', 'add', '-A', '-f'], cwd=repo) 188 check_call(['git', 'add', '-A', '-f'], cwd=repo)
189 check_call(['git', 'commit', '-q', '--message', 'foo'], cwd=repo) 189 check_call(['git', 'commit', '-q', '--message', 'foo'], cwd=repo)
190 rev = Popen(['git', 'show-ref', '--head', 'HEAD'], 190 rev = Popen(['git', 'show-ref', '--head', 'HEAD'],
191 cwd=repo).communicate()[0].split(' ', 1)[0] 191 cwd=repo).communicate()[0].split(' ', 1)[0]
192 logging.debug('At revision %s' % rev) 192 logging.debug('At revision %s' % rev)
193 return rev 193 return rev
194 194
195 195
196 _FAKE_LOADED = False 196 _FAKE_LOADED = False
197 197
198 class FakeRepos(object): 198 class FakeReposBase(object):
199 """Generate both svn and git repositories to test gclient functionality. 199 """Generate both svn and git repositories to test gclient functionality.
200 200
201 Many DEPS functionalities need to be tested: Var, File, From, deps_os, hooks, 201 Many DEPS functionalities need to be tested: Var, File, From, deps_os, hooks,
202 use_relative_paths. 202 use_relative_paths.
203 203
204 And types of dependencies: Relative urls, Full urls, both svn and git.""" 204 And types of dependencies: Relative urls, Full urls, both svn and git.
205
206 populateSvn() and populateGit() need to be implemented by the subclass.
207 """
205 208
206 # Should leak the repositories. 209 # Should leak the repositories.
207 SHOULD_LEAK = False 210 SHOULD_LEAK = False
208 # Override if unhappy. 211 # Override if unhappy.
209 TRIAL_DIR = None 212 TRIAL_DIR = None
210 # Hostname 213 # Hostname
211 HOST = '127.0.0.1' 214 HOST = '127.0.0.1'
215 NB_GIT_REPOS = 1
212 216
213 def __init__(self, trial_dir=None, leak=None, host=None): 217 def __init__(self, trial_dir=None, leak=None, host=None):
214 global _FAKE_LOADED 218 global _FAKE_LOADED
215 if _FAKE_LOADED: 219 if _FAKE_LOADED:
216 raise Exception('You can only start one FakeRepos at a time.') 220 raise Exception('You can only start one FakeRepos at a time.')
217 _FAKE_LOADED = True 221 _FAKE_LOADED = True
218 # Quick hack. 222 # Quick hack.
219 if '-v' in sys.argv: 223 if '-v' in sys.argv:
220 logging.basicConfig(level=logging.DEBUG) 224 logging.basicConfig(level=logging.DEBUG)
221 elif leak is not None: 225 elif leak is not None:
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 'user2 = bar\n') 312 'user2 = bar\n')
309 313
310 # Start the daemon. 314 # Start the daemon.
311 cmd = ['svnserve', '-d', '--foreground', '-r', self.repos_dir] 315 cmd = ['svnserve', '-d', '--foreground', '-r', self.repos_dir]
312 if self.HOST == '127.0.0.1': 316 if self.HOST == '127.0.0.1':
313 cmd.append('--listen-host=127.0.0.1') 317 cmd.append('--listen-host=127.0.0.1')
314 self.svnserve = Popen(cmd, cwd=root) 318 self.svnserve = Popen(cmd, cwd=root)
315 self.populateSvn() 319 self.populateSvn()
316 return True 320 return True
317 321
322 def setUpGIT(self):
323 """Creates git repositories and start the servers."""
324 if self.gitdaemon:
325 return True
326 self.setUp()
327 if sys.platform == 'win32':
328 return False
329 for repo in ['repo_%d' % r for r in range(1, self.NB_GIT_REPOS + 1)]:
330 check_call(['git', 'init', '-q', join(self.git_root, repo)])
331 self.git_hashes[repo] = [None]
332 self.populateGit()
333 # Start the daemon.
334 cmd = ['git', 'daemon', '--export-all', '--base-path=' + self.repos_dir]
335 if self.HOST == '127.0.0.1':
336 cmd.append('--listen=127.0.0.1')
337 logging.debug(cmd)
338 self.gitdaemon = Popen(cmd, cwd=self.repos_dir)
339 return True
340
341 def _commit_svn(self, tree):
342 self._genTree(self.svn_root, tree)
343 commit_svn(self.svn_root)
344 if self.svn_revs and self.svn_revs[-1]:
345 new_tree = self.svn_revs[-1].copy()
346 new_tree.update(tree)
347 else:
348 new_tree = tree.copy()
349 self.svn_revs.append(new_tree)
350
351 def _commit_git(self, repo, tree):
352 repo_root = join(self.git_root, repo)
353 self._genTree(repo_root, tree)
354 commit_hash = commit_git(repo_root)
355 if self.git_hashes[repo][-1]:
356 new_tree = self.git_hashes[repo][-1][1].copy()
357 new_tree.update(tree)
358 else:
359 new_tree = tree.copy()
360 self.git_hashes[repo].append((commit_hash, new_tree))
361
362
363 class FakeRepos(FakeReposBase):
364 """Implements populateSvn() and populateGit()."""
365 NB_GIT_REPOS = 4
366
318 def populateSvn(self): 367 def populateSvn(self):
319 """Creates a few revisions of changes including DEPS files.""" 368 """Creates a few revisions of changes including DEPS files."""
320 # Repos 369 # Repos
321 check_call(['svn', 'checkout', 'svn://127.0.0.1/svn', self.svn_root, '-q', 370 check_call(['svn', 'checkout', 'svn://127.0.0.1/svn', self.svn_root, '-q',
322 '--non-interactive', '--no-auth-cache', 371 '--non-interactive', '--no-auth-cache',
323 '--username', 'user1', '--password', 'foo']) 372 '--username', 'user1', '--password', 'foo'])
324 assert os.path.isdir(join(self.svn_root, '.svn')) 373 assert os.path.isdir(join(self.svn_root, '.svn'))
325 def file_system(rev, DEPS): 374 def file_system(rev, DEPS):
326 fs = { 375 fs = {
327 'origin': 'svn@%(rev)d\n', 376 'origin': 'svn@%(rev)d\n',
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 462
414 hooks = [ 463 hooks = [
415 { 464 {
416 'pattern': '.*', 465 'pattern': '.*',
417 'action': ['echo', 'foo'], 466 'action': ['echo', 'foo'],
418 }, 467 },
419 ] 468 ]
420 """ % { 'host': self.HOST } 469 """ % { 'host': self.HOST }
421 self._commit_svn(fs) 470 self._commit_svn(fs)
422 471
423 def setUpGIT(self): 472 def populateGit(self):
424 """Creates git repositories and start the servers."""
425 if self.gitdaemon:
426 return True
427 self.setUp()
428 if sys.platform == 'win32':
429 return False
430 for repo in ['repo_%d' % r for r in range(1, 5)]:
431 check_call(['git', 'init', '-q', join(self.git_root, repo)])
432 self.git_hashes[repo] = [None]
433
434 # Testing: 473 # Testing:
435 # - dependency disapear 474 # - dependency disapear
436 # - dependency renamed 475 # - dependency renamed
437 # - versioned and unversioned reference 476 # - versioned and unversioned reference
438 # - relative and full reference 477 # - relative and full reference
439 # - deps_os 478 # - deps_os
440 # - var 479 # - var
441 # - hooks 480 # - hooks
442 # - From 481 # - From
443 # TODO(maruel): 482 # TODO(maruel):
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 """ % { 562 """ % {
524 'host': self.HOST, 563 'host': self.HOST,
525 # See self.__init__() for the format. Grab's the hash of the first 564 # See self.__init__() for the format. Grab's the hash of the first
526 # commit in repo_2. Only keep the first 7 character because of: 565 # commit in repo_2. Only keep the first 7 character because of:
527 # TODO(maruel): http://crosbug.com/3591 We need to strip the hash.. duh. 566 # TODO(maruel): http://crosbug.com/3591 We need to strip the hash.. duh.
528 'hash': self.git_hashes['repo_2'][1][0][:7] 567 'hash': self.git_hashes['repo_2'][1][0][:7]
529 }, 568 },
530 'origin': 'git/repo_1@2\n', 569 'origin': 'git/repo_1@2\n',
531 }) 570 })
532 571
533 # Start the daemon.
534 cmd = ['git', 'daemon', '--export-all', '--base-path=' + self.repos_dir]
535 if self.HOST == '127.0.0.1':
536 cmd.append('--listen=127.0.0.1')
537 logging.debug(cmd)
538 self.gitdaemon = Popen(cmd, cwd=self.repos_dir)
539 return True
540
541 def _commit_svn(self, tree):
542 self._genTree(self.svn_root, tree)
543 commit_svn(self.svn_root)
544 if self.svn_revs and self.svn_revs[-1]:
545 new_tree = self.svn_revs[-1].copy()
546 new_tree.update(tree)
547 else:
548 new_tree = tree.copy()
549 self.svn_revs.append(new_tree)
550
551 def _commit_git(self, repo, tree):
552 repo_root = join(self.git_root, repo)
553 self._genTree(repo_root, tree)
554 commit_hash = commit_git(repo_root)
555 if self.git_hashes[repo][-1]:
556 new_tree = self.git_hashes[repo][-1][1].copy()
557 new_tree.update(tree)
558 else:
559 new_tree = tree.copy()
560 self.git_hashes[repo].append((commit_hash, new_tree))
561
562 572
563 class FakeReposTestBase(unittest.TestCase): 573 class FakeReposTestBase(unittest.TestCase):
564 """This is vaguely inspired by twisted.""" 574 """This is vaguely inspired by twisted."""
565 575
566 # Replace this in your subclass. 576 # Replace this in your subclass.
567 CLASS_ROOT_DIR = None 577 CLASS_ROOT_DIR = None
568 578
569 # static FakeRepos instance. Lazy loaded. 579 # static FakeRepos instance. Lazy loaded.
570 FAKE_REPOS = None 580 FAKE_REPOS = None
581 # Override if necessary.
582 FAKE_REPOS_CLASS = FakeRepos
571 583
572 def __init__(self, *args, **kwargs): 584 def __init__(self, *args, **kwargs):
573 unittest.TestCase.__init__(self, *args, **kwargs) 585 unittest.TestCase.__init__(self, *args, **kwargs)
574 if not FakeReposTestBase.FAKE_REPOS: 586 if not FakeReposTestBase.FAKE_REPOS:
575 FakeReposTestBase.FAKE_REPOS = FakeRepos() 587 FakeReposTestBase.FAKE_REPOS = self.FAKE_REPOS_CLASS()
576 588
577 def setUp(self): 589 def setUp(self):
578 unittest.TestCase.setUp(self) 590 unittest.TestCase.setUp(self)
579 self.FAKE_REPOS.setUp() 591 self.FAKE_REPOS.setUp()
580 592
581 # Remove left overs and start fresh. 593 # Remove left overs and start fresh.
582 if not self.CLASS_ROOT_DIR: 594 if not self.CLASS_ROOT_DIR:
583 self.CLASS_ROOT_DIR = join(self.FAKE_REPOS.trial_dir(), 'smoke') 595 self.CLASS_ROOT_DIR = join(self.FAKE_REPOS.trial_dir(), 'smoke')
584 self.root_dir = join(self.CLASS_ROOT_DIR, self.id()) 596 self.root_dir = join(self.CLASS_ROOT_DIR, self.id())
585 rmtree(self.root_dir) 597 rmtree(self.root_dir)
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
670 682
671 683
672 # Kind of hack. 684 # Kind of hack.
673 if '-l' in sys.argv: 685 if '-l' in sys.argv:
674 FakeRepos.SHOULD_LEAK = True 686 FakeRepos.SHOULD_LEAK = True
675 sys.argv.remove('-l') 687 sys.argv.remove('-l')
676 688
677 689
678 if __name__ == '__main__': 690 if __name__ == '__main__':
679 sys.exit(main(sys.argv)) 691 sys.exit(main(sys.argv))
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698