| OLD | NEW |
| 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 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 import scm | 22 import scm |
| 23 | 23 |
| 24 ## Utility functions | 24 ## Utility functions |
| 25 | 25 |
| 26 | 26 |
| 27 def addKill(): | 27 def addKill(): |
| 28 """Add kill() method to subprocess.Popen for python <2.6""" | 28 """Add kill() method to subprocess.Popen for python <2.6""" |
| 29 if getattr(subprocess.Popen, 'kill', None): | 29 if getattr(subprocess.Popen, 'kill', None): |
| 30 return | 30 return |
| 31 # Unable to import 'module' |
| 32 # pylint: disable=F0401 |
| 31 if sys.platform == 'win32': | 33 if sys.platform == 'win32': |
| 32 def kill_win(process): | 34 def kill_win(process): |
| 33 import win32process | 35 import win32process |
| 36 # Access to a protected member _handle of a client class |
| 37 # pylint: disable=W0212 |
| 34 return win32process.TerminateProcess(process._handle, -1) | 38 return win32process.TerminateProcess(process._handle, -1) |
| 35 subprocess.Popen.kill = kill_win | 39 subprocess.Popen.kill = kill_win |
| 36 else: | 40 else: |
| 37 def kill_nix(process): | 41 def kill_nix(process): |
| 38 import signal | 42 import signal |
| 39 return os.kill(process.pid, signal.SIGKILL) | 43 return os.kill(process.pid, signal.SIGKILL) |
| 40 subprocess.Popen.kill = kill_nix | 44 subprocess.Popen.kill = kill_nix |
| 41 | 45 |
| 42 | 46 |
| 43 def rmtree(*path): | 47 def rmtree(*path): |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 elif status[0] == '!': | 166 elif status[0] == '!': |
| 163 to_remove.append(filepath) | 167 to_remove.append(filepath) |
| 164 if to_add: | 168 if to_add: |
| 165 check_call(['svn', 'add', '--no-auto-props', '-q'] + to_add, cwd=repo) | 169 check_call(['svn', 'add', '--no-auto-props', '-q'] + to_add, cwd=repo) |
| 166 if to_remove: | 170 if to_remove: |
| 167 check_call(['svn', 'remove', '-q'] + to_remove, cwd=repo) | 171 check_call(['svn', 'remove', '-q'] + to_remove, cwd=repo) |
| 168 proc = Popen(['svn', 'commit', repo, '-m', 'foo', '--non-interactive', | 172 proc = Popen(['svn', 'commit', repo, '-m', 'foo', '--non-interactive', |
| 169 '--no-auth-cache', '--username', 'user1', '--password', 'foo'], | 173 '--no-auth-cache', '--username', 'user1', '--password', 'foo'], |
| 170 cwd=repo) | 174 cwd=repo) |
| 171 out, err = proc.communicate() | 175 out, err = proc.communicate() |
| 172 last_line = out.splitlines()[-1] | |
| 173 match = re.search(r'(\d+)', out) | 176 match = re.search(r'(\d+)', out) |
| 174 if not match: | 177 if not match: |
| 175 raise Exception('Commit failed', out, err, proc.returncode) | 178 raise Exception('Commit failed', out, err, proc.returncode) |
| 176 rev = match.group(1) | 179 rev = match.group(1) |
| 177 st = Popen(['svn', 'status'], cwd=repo).communicate()[0] | 180 st = Popen(['svn', 'status'], cwd=repo).communicate()[0] |
| 178 assert len(st) == 0, st | 181 assert len(st) == 0, st |
| 179 logging.debug('At revision %s' % rev) | 182 logging.debug('At revision %s' % rev) |
| 180 return rev | 183 return rev |
| 181 | 184 |
| 182 | 185 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 self.svnserve.kill() | 263 self.svnserve.kill() |
| 261 self.svnserve = None | 264 self.svnserve = None |
| 262 if self.gitdaemon: | 265 if self.gitdaemon: |
| 263 logging.debug('Killing git-daemon pid %s' % self.gitdaemon.pid) | 266 logging.debug('Killing git-daemon pid %s' % self.gitdaemon.pid) |
| 264 self.gitdaemon.kill() | 267 self.gitdaemon.kill() |
| 265 self.gitdaemon = None | 268 self.gitdaemon = None |
| 266 if not self.SHOULD_LEAK: | 269 if not self.SHOULD_LEAK: |
| 267 logging.debug('Removing %s' % self.trial_dir()) | 270 logging.debug('Removing %s' % self.trial_dir()) |
| 268 rmtree(self.trial_dir()) | 271 rmtree(self.trial_dir()) |
| 269 | 272 |
| 270 def _genTree(self, root, tree_dict): | 273 @staticmethod |
| 274 def _genTree(root, tree_dict): |
| 271 """For a dictionary of file contents, generate a filesystem.""" | 275 """For a dictionary of file contents, generate a filesystem.""" |
| 272 if not os.path.isdir(root): | 276 if not os.path.isdir(root): |
| 273 os.makedirs(root) | 277 os.makedirs(root) |
| 274 for (k, v) in tree_dict.iteritems(): | 278 for (k, v) in tree_dict.iteritems(): |
| 275 k_os = k.replace('/', os.sep) | 279 k_os = k.replace('/', os.sep) |
| 276 k_arr = k_os.split(os.sep) | 280 k_arr = k_os.split(os.sep) |
| 277 if len(k_arr) > 1: | 281 if len(k_arr) > 1: |
| 278 p = os.sep.join([root] + k_arr[:-1]) | 282 p = os.sep.join([root] + k_arr[:-1]) |
| 279 if not os.path.isdir(p): | 283 if not os.path.isdir(p): |
| 280 os.makedirs(p) | 284 os.makedirs(p) |
| 281 if v is None: | 285 if v is None: |
| 282 os.remove(join(root, k)) | 286 os.remove(join(root, k)) |
| 283 else: | 287 else: |
| 284 write(join(root, k), v) | 288 write(join(root, k), v) |
| 285 | 289 |
| 286 def setUpSVN(self): | 290 def setUpSVN(self): |
| 287 """Creates subversion repositories and start the servers.""" | 291 """Creates subversion repositories and start the servers.""" |
| 288 if self.svnserve: | 292 if self.svnserve: |
| 289 return True | 293 return True |
| 290 self.setUp() | 294 self.setUp() |
| 291 root = join(self.repos_dir, 'svn') | 295 root = join(self.repos_dir, 'svn') |
| 292 try: | 296 try: |
| 293 check_call(['svnadmin', 'create', root]) | 297 check_call(['svnadmin', 'create', root]) |
| 294 except OSError: | 298 except OSError: |
| 295 self.svn_enabled = False | |
| 296 return False | 299 return False |
| 297 write(join(root, 'conf', 'svnserve.conf'), | 300 write(join(root, 'conf', 'svnserve.conf'), |
| 298 '[general]\n' | 301 '[general]\n' |
| 299 'anon-access = read\n' | 302 'anon-access = read\n' |
| 300 'auth-access = write\n' | 303 'auth-access = write\n' |
| 301 'password-db = passwd\n') | 304 'password-db = passwd\n') |
| 302 write(join(root, 'conf', 'passwd'), | 305 write(join(root, 'conf', 'passwd'), |
| 303 '[users]\n' | 306 '[users]\n' |
| 304 'user1 = foo\n' | 307 'user1 = foo\n' |
| 305 'user2 = bar\n') | 308 'user2 = bar\n') |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 if self.svn_revs and self.svn_revs[-1]: | 544 if self.svn_revs and self.svn_revs[-1]: |
| 542 new_tree = self.svn_revs[-1].copy() | 545 new_tree = self.svn_revs[-1].copy() |
| 543 new_tree.update(tree) | 546 new_tree.update(tree) |
| 544 else: | 547 else: |
| 545 new_tree = tree.copy() | 548 new_tree = tree.copy() |
| 546 self.svn_revs.append(new_tree) | 549 self.svn_revs.append(new_tree) |
| 547 | 550 |
| 548 def _commit_git(self, repo, tree): | 551 def _commit_git(self, repo, tree): |
| 549 repo_root = join(self.git_root, repo) | 552 repo_root = join(self.git_root, repo) |
| 550 self._genTree(repo_root, tree) | 553 self._genTree(repo_root, tree) |
| 551 hash = commit_git(repo_root) | 554 commit_hash = commit_git(repo_root) |
| 552 if self.git_hashes[repo][-1]: | 555 if self.git_hashes[repo][-1]: |
| 553 new_tree = self.git_hashes[repo][-1][1].copy() | 556 new_tree = self.git_hashes[repo][-1][1].copy() |
| 554 new_tree.update(tree) | 557 new_tree.update(tree) |
| 555 else: | 558 else: |
| 556 new_tree = tree.copy() | 559 new_tree = tree.copy() |
| 557 self.git_hashes[repo].append((hash, new_tree)) | 560 self.git_hashes[repo].append((commit_hash, new_tree)) |
| 558 | 561 |
| 559 | 562 |
| 560 class FakeReposTestBase(unittest.TestCase): | 563 class FakeReposTestBase(unittest.TestCase): |
| 561 """This is vaguely inspired by twisted.""" | 564 """This is vaguely inspired by twisted.""" |
| 562 | 565 |
| 563 # Replace this in your subclass. | 566 # Replace this in your subclass. |
| 564 CLASS_ROOT_DIR = None | 567 CLASS_ROOT_DIR = None |
| 565 | 568 |
| 566 # static FakeRepos instance. Lazy loaded. | 569 # static FakeRepos instance. Lazy loaded. |
| 567 FAKE_REPOS = None | 570 FAKE_REPOS = None |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 667 | 670 |
| 668 | 671 |
| 669 # Kind of hack. | 672 # Kind of hack. |
| 670 if '-l' in sys.argv: | 673 if '-l' in sys.argv: |
| 671 FakeRepos.SHOULD_LEAK = True | 674 FakeRepos.SHOULD_LEAK = True |
| 672 sys.argv.remove('-l') | 675 sys.argv.remove('-l') |
| 673 | 676 |
| 674 | 677 |
| 675 if __name__ == '__main__': | 678 if __name__ == '__main__': |
| 676 sys.exit(main(sys.argv)) | 679 sys.exit(main(sys.argv)) |
| OLD | NEW |