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 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 Args: | 249 Args: |
250 schema - An instance of GitRepoSchema | 250 schema - An instance of GitRepoSchema |
251 """ | 251 """ |
252 self.repo_path = tempfile.mkdtemp(dir=self.BASE_TEMP_DIR) | 252 self.repo_path = tempfile.mkdtemp(dir=self.BASE_TEMP_DIR) |
253 self.commit_map = {} | 253 self.commit_map = {} |
254 self._date = datetime.datetime(1970, 1, 1) | 254 self._date = datetime.datetime(1970, 1, 1) |
255 | 255 |
256 self.git('init') | 256 self.git('init') |
257 for commit in schema.walk(): | 257 for commit in schema.walk(): |
258 self._add_schema_commit(commit, schema.data_for(commit.name)) | 258 self._add_schema_commit(commit, schema.data_for(commit.name)) |
| 259 self.last_commit = self[commit.name] |
259 if schema.master: | 260 if schema.master: |
260 self.git('update-ref', 'master', self[schema.master]) | 261 self.git('update-ref', 'master', self[schema.master]) |
261 | 262 |
262 def __getitem__(self, commit_name): | 263 def __getitem__(self, commit_name): |
263 """Gets the hash of a commit by its schema name. | 264 """Gets the hash of a commit by its schema name. |
264 | 265 |
265 >>> r = GitRepo(GitRepoSchema('A B C')) | 266 >>> r = GitRepo(GitRepoSchema('A B C')) |
266 >>> r['B'] | 267 >>> r['B'] |
267 '7381febe1da03b09da47f009963ab7998a974935' | 268 '7381febe1da03b09da47f009963ab7998a974935' |
268 """ | 269 """ |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 if mode and not deleted: | 315 if mode and not deleted: |
315 os.chmod(path, mode) | 316 os.chmod(path, mode) |
316 | 317 |
317 self.git('add', fname) | 318 self.git('add', fname) |
318 | 319 |
319 rslt = self.git('commit', '--allow-empty', '-m', commit.name, env=env) | 320 rslt = self.git('commit', '--allow-empty', '-m', commit.name, env=env) |
320 assert rslt.retcode == 0, 'Failed to commit %s' % str(commit) | 321 assert rslt.retcode == 0, 'Failed to commit %s' % str(commit) |
321 self.commit_map[commit.name] = self.git('rev-parse', 'HEAD').stdout.strip() | 322 self.commit_map[commit.name] = self.git('rev-parse', 'HEAD').stdout.strip() |
322 self.git('tag', 'tag_%s' % commit.name, self[commit.name]) | 323 self.git('tag', 'tag_%s' % commit.name, self[commit.name]) |
323 if commit.is_branch: | 324 if commit.is_branch: |
324 self.git('update-ref', 'branch_%s' % commit.name, self[commit.name]) | 325 self.git('branch', '-f', 'branch_%s' % commit.name, self[commit.name]) |
325 | 326 |
326 def git(self, *args, **kwargs): | 327 def git(self, *args, **kwargs): |
327 """Runs a git command specified by |args| in this repo.""" | 328 """Runs a git command specified by |args| in this repo.""" |
328 assert self.repo_path is not None | 329 assert self.repo_path is not None |
329 try: | 330 try: |
330 with open(os.devnull, 'wb') as devnull: | 331 with open(os.devnull, 'wb') as devnull: |
331 output = subprocess.check_output( | 332 output = subprocess.check_output( |
332 ('git',) + args, cwd=self.repo_path, stderr=devnull, **kwargs) | 333 ('git',) + args, cwd=self.repo_path, stderr=devnull, **kwargs) |
333 return self.COMMAND_OUTPUT(0, output) | 334 return self.COMMAND_OUTPUT(0, output) |
334 except subprocess.CalledProcessError as e: | 335 except subprocess.CalledProcessError as e: |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 for the duration of all the tests in the subclass. | 388 for the duration of all the tests in the subclass. |
388 """ | 389 """ |
389 REPO = None | 390 REPO = None |
390 | 391 |
391 @classmethod | 392 @classmethod |
392 def setUpClass(cls): | 393 def setUpClass(cls): |
393 super(GitRepoReadOnlyTestBase, cls).setUpClass() | 394 super(GitRepoReadOnlyTestBase, cls).setUpClass() |
394 assert cls.REPO is not None | 395 assert cls.REPO is not None |
395 cls.repo = cls.r_schema.reify() | 396 cls.repo = cls.r_schema.reify() |
396 | 397 |
| 398 def setUp(self): |
| 399 self.repo.git('checkout', '-f', self.repo.last_commit) |
| 400 |
397 @classmethod | 401 @classmethod |
398 def tearDownClass(cls): | 402 def tearDownClass(cls): |
399 cls.repo.nuke() | 403 cls.repo.nuke() |
400 super(GitRepoReadOnlyTestBase, cls).tearDownClass() | 404 super(GitRepoReadOnlyTestBase, cls).tearDownClass() |
401 | 405 |
402 | 406 |
403 class GitRepoReadWriteTestBase(GitRepoSchemaTestBase): | 407 class GitRepoReadWriteTestBase(GitRepoSchemaTestBase): |
404 """Injects a GitRepo object given the schema and content from | 408 """Injects a GitRepo object given the schema and content from |
405 GitRepoSchemaTestBase into TestCase classes which subclass this. | 409 GitRepoSchemaTestBase into TestCase classes which subclass this. |
406 | 410 |
407 This GitRepo will appear as self.repo, and will be deleted and recreated for | 411 This GitRepo will appear as self.repo, and will be deleted and recreated for |
408 each test function in the subclass. | 412 each test function in the subclass. |
409 """ | 413 """ |
410 REPO = None | 414 REPO = None |
411 | 415 |
412 def setUp(self): | 416 def setUp(self): |
413 super(GitRepoReadWriteTestBase, self).setUp() | 417 super(GitRepoReadWriteTestBase, self).setUp() |
414 self.repo = self.r_schema.reify() | 418 self.repo = self.r_schema.reify() |
415 | 419 |
416 def tearDown(self): | 420 def tearDown(self): |
417 self.repo.nuke() | 421 self.repo.nuke() |
418 super(GitRepoReadWriteTestBase, self).tearDown() | 422 super(GitRepoReadWriteTestBase, self).tearDown() |
OLD | NEW |