| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import atexit | 5 import atexit |
| 6 import collections | 6 import collections |
| 7 import copy | 7 import copy |
| 8 import datetime | 8 import datetime |
| 9 import hashlib | 9 import hashlib |
| 10 import os | 10 import os |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 nxt[1] = prev | 86 nxt[1] = prev |
| 87 | 87 |
| 88 def pop(self, last=True): # pylint: disable=W0221 | 88 def pop(self, last=True): # pylint: disable=W0221 |
| 89 if not self: | 89 if not self: |
| 90 raise KeyError('set is empty') | 90 raise KeyError('set is empty') |
| 91 key = self.end[1][0] if last else self.end[2][0] | 91 key = self.end[1][0] if last else self.end[2][0] |
| 92 self.discard(key) | 92 self.discard(key) |
| 93 return key | 93 return key |
| 94 | 94 |
| 95 | 95 |
| 96 class UTC(datetime.tzinfo): |
| 97 """UTC time zone. |
| 98 |
| 99 from https://docs.python.org/2/library/datetime.html#tzinfo-objects |
| 100 """ |
| 101 def utcoffset(self, dt): |
| 102 return datetime.timedelta(0) |
| 103 |
| 104 def tzname(self, dt): |
| 105 return "UTC" |
| 106 |
| 107 def dst(self, dt): |
| 108 return datetime.timedelta(0) |
| 109 |
| 110 |
| 111 UTC = UTC() |
| 112 |
| 113 |
| 96 class GitRepoSchema(object): | 114 class GitRepoSchema(object): |
| 97 """A declarative git testing repo. | 115 """A declarative git testing repo. |
| 98 | 116 |
| 99 Pass a schema to __init__ in the form of: | 117 Pass a schema to __init__ in the form of: |
| 100 A B C D | 118 A B C D |
| 101 B E D | 119 B E D |
| 102 | 120 |
| 103 This is the repo | 121 This is the repo |
| 104 | 122 |
| 105 A - B - C - D | 123 A - B - C - D |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 Automatically creates a temp folder under GitRepo.BASE_TEMP_DIR. It's | 278 Automatically creates a temp folder under GitRepo.BASE_TEMP_DIR. It's |
| 261 recommended that you clean this repo up by calling nuke() on it, but if not, | 279 recommended that you clean this repo up by calling nuke() on it, but if not, |
| 262 GitRepo will automatically clean up all allocated repos at the exit of the | 280 GitRepo will automatically clean up all allocated repos at the exit of the |
| 263 program (assuming a normal exit like with sys.exit) | 281 program (assuming a normal exit like with sys.exit) |
| 264 | 282 |
| 265 Args: | 283 Args: |
| 266 schema - An instance of GitRepoSchema | 284 schema - An instance of GitRepoSchema |
| 267 """ | 285 """ |
| 268 self.repo_path = tempfile.mkdtemp(dir=self.BASE_TEMP_DIR) | 286 self.repo_path = tempfile.mkdtemp(dir=self.BASE_TEMP_DIR) |
| 269 self.commit_map = {} | 287 self.commit_map = {} |
| 270 self._date = datetime.datetime(1970, 1, 1) | 288 self._date = datetime.datetime(1970, 1, 1, tzinfo=UTC) |
| 271 | 289 |
| 272 self.to_schema_refs = ['--branches'] | 290 self.to_schema_refs = ['--branches'] |
| 273 | 291 |
| 274 self.git('init') | 292 self.git('init') |
| 275 self.git('config', 'user.name', 'testcase') | 293 self.git('config', 'user.name', 'testcase') |
| 276 self.git('config', 'user.email', 'testcase@example.com') | 294 self.git('config', 'user.email', 'testcase@example.com') |
| 277 for commit in schema.walk(): | 295 for commit in schema.walk(): |
| 278 self._add_schema_commit(commit, schema.data_for(commit.name)) | 296 self._add_schema_commit(commit, schema.data_for(commit.name)) |
| 279 self.last_commit = self[commit.name] | 297 self.last_commit = self[commit.name] |
| 280 if schema.master: | 298 if schema.master: |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 """Runs a git command specified by |args| in this repo.""" | 371 """Runs a git command specified by |args| in this repo.""" |
| 354 assert self.repo_path is not None | 372 assert self.repo_path is not None |
| 355 try: | 373 try: |
| 356 with open(os.devnull, 'wb') as devnull: | 374 with open(os.devnull, 'wb') as devnull: |
| 357 output = subprocess.check_output( | 375 output = subprocess.check_output( |
| 358 ('git',) + args, cwd=self.repo_path, stderr=devnull, **kwargs) | 376 ('git',) + args, cwd=self.repo_path, stderr=devnull, **kwargs) |
| 359 return self.COMMAND_OUTPUT(0, output) | 377 return self.COMMAND_OUTPUT(0, output) |
| 360 except subprocess.CalledProcessError as e: | 378 except subprocess.CalledProcessError as e: |
| 361 return self.COMMAND_OUTPUT(e.returncode, e.output) | 379 return self.COMMAND_OUTPUT(e.returncode, e.output) |
| 362 | 380 |
| 381 def show_commit(self, commit_name, format_string): |
| 382 """Shows a commit (by its schema name) with a given format string.""" |
| 383 return self.git('show', '-q', '--pretty=format:%s' % format_string, |
| 384 self[commit_name]).stdout |
| 385 |
| 363 def git_commit(self, message): | 386 def git_commit(self, message): |
| 364 return self.git('commit', '-am', message, env=self.get_git_commit_env()) | 387 return self.git('commit', '-am', message, env=self.get_git_commit_env()) |
| 365 | 388 |
| 366 def nuke(self): | 389 def nuke(self): |
| 367 """Obliterates the git repo on disk. | 390 """Obliterates the git repo on disk. |
| 368 | 391 |
| 369 Causes this GitRepo to be unusable. | 392 Causes this GitRepo to be unusable. |
| 370 """ | 393 """ |
| 371 shutil.rmtree(self.repo_path) | 394 shutil.rmtree(self.repo_path) |
| 372 self.repo_path = None | 395 self.repo_path = None |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 super(GitRepoReadWriteTestBase, self).setUp() | 520 super(GitRepoReadWriteTestBase, self).setUp() |
| 498 self.repo = self.r_schema.reify() | 521 self.repo = self.r_schema.reify() |
| 499 | 522 |
| 500 def tearDown(self): | 523 def tearDown(self): |
| 501 self.repo.nuke() | 524 self.repo.nuke() |
| 502 super(GitRepoReadWriteTestBase, self).tearDown() | 525 super(GitRepoReadWriteTestBase, self).tearDown() |
| 503 | 526 |
| 504 def assertSchema(self, schema_string): | 527 def assertSchema(self, schema_string): |
| 505 self.assertEqual(GitRepoSchema(schema_string).simple_graph(), | 528 self.assertEqual(GitRepoSchema(schema_string).simple_graph(), |
| 506 self.repo.to_schema().simple_graph()) | 529 self.repo.to_schema().simple_graph()) |
| OLD | NEW |