Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 from testing_support import auto_stub | 5 from testing_support import auto_stub |
| 6 | 6 |
| 7 from testing_support.git.repo import GitRepo | 7 from testing_support.git.repo import GitRepo |
| 8 from testing_support.git.schema import GitRepoSchema | 8 from testing_support.git.schema import GitRepoSchema |
| 9 | 9 |
| 10 | 10 |
| 11 class GitRepoSchemaTestBase(auto_stub.TestCase): | 11 class GitRepoTestBase(auto_stub.TestCase): |
| 12 """A TestCase with a built-in GitRepoSchema. | 12 """A TestCase with a built-in GitRepoSchema. |
| 13 | 13 |
| 14 Expects a class variable REPO_SCHEMA to be a GitRepoSchema string in the form | 14 Expects a class variable REPO_SCHEMA to be a GitRepoSchema string in the form |
| 15 described by that class. | 15 described by that class. |
| 16 | 16 |
| 17 You may also set class variables in the form COMMIT_%(commit_name)s, which | 17 You may also set class variables in the form COMMIT_%(commit_name)s, which |
| 18 provide the content for the given commit_name commits. | 18 provide the content for the given commit_name commits. |
| 19 | 19 |
| 20 You probably will end up using either GitRepoReadOnlyTestBase or | 20 You probably will end up using either GitRepoReadOnlyTestBase or |
| 21 GitRepoReadWriteTestBase for real tests. | 21 GitRepoReadWriteTestBase for real tests. |
| 22 | |
| 23 Injects a GitRepo object given the schema and content from | |
| 24 GitRepoSchemaTestBase into TestCase classes which subclass this. | |
|
Vadim Sh.
2014/08/17 04:39:59
GitRepoSchemaTestBase?
iannucci
2014/08/17 08:47:04
Fixed this whole docstring
| |
| 25 | |
| 26 This GitRepo will appear as self.repo, and will be deleted and recreated for | |
| 27 each test function in the subclass. | |
| 22 """ | 28 """ |
| 23 REPO_SCHEMA = None | 29 REPO_SCHEMA = None |
| 24 | 30 |
| 25 @classmethod | 31 @classmethod |
| 26 def getRepoContent(cls, commit): | 32 def getRepoContent(cls, commit): |
| 27 return getattr(cls, 'COMMIT_%s' % commit, None) | 33 return getattr(cls, 'COMMIT_%s' % commit, None) |
| 28 | 34 |
| 29 @classmethod | |
| 30 def setUpClass(cls): | |
| 31 super(GitRepoSchemaTestBase, cls).setUpClass() | |
| 32 assert cls.REPO_SCHEMA is not None | |
| 33 cls.r_schema = GitRepoSchema(cls.REPO_SCHEMA, cls.getRepoContent) | |
| 34 | |
| 35 | |
| 36 class GitRepoReadOnlyTestBase(GitRepoSchemaTestBase): | |
| 37 """Injects a GitRepo object given the schema and content from | |
| 38 GitRepoSchemaTestBase into TestCase classes which subclass this. | |
| 39 | |
| 40 This GitRepo will appear as self.repo, and will be deleted and recreated once | |
| 41 for the duration of all the tests in the subclass. | |
| 42 """ | |
| 43 REPO_SCHEMA = None | |
| 44 | |
| 45 @classmethod | |
| 46 def setUpClass(cls): | |
| 47 super(GitRepoReadOnlyTestBase, cls).setUpClass() | |
| 48 assert cls.REPO_SCHEMA is not None | |
| 49 cls.repo = GitRepo(cls.r_schema) | |
| 50 | |
| 51 def setUp(self): | |
| 52 self.repo.git('checkout', '-f', self.repo.last_commit) | |
| 53 | |
| 54 @classmethod | |
| 55 def tearDownClass(cls): | |
| 56 cls.repo.nuke() | |
| 57 super(GitRepoReadOnlyTestBase, cls).tearDownClass() | |
| 58 | |
| 59 | |
| 60 class GitRepoReadWriteTestBase(GitRepoSchemaTestBase): | |
| 61 """Injects a GitRepo object given the schema and content from | |
| 62 GitRepoSchemaTestBase into TestCase classes which subclass this. | |
| 63 | |
| 64 This GitRepo will appear as self.repo, and will be deleted and recreated for | |
| 65 each test function in the subclass. | |
| 66 """ | |
| 67 REPO_SCHEMA = None | |
| 68 | |
| 69 def setUp(self): | |
| 70 super(GitRepoReadWriteTestBase, self).setUp() | |
| 71 self.repo = GitRepo(self.r_schema) | |
| 72 | |
| 73 def tearDown(self): | |
| 74 self.repo.nuke() | |
| 75 super(GitRepoReadWriteTestBase, self).tearDown() | |
| 76 | |
| 77 def assertSchema(self, schema_string): | 35 def assertSchema(self, schema_string): |
| 78 self.assertEqual(GitRepoSchema(schema_string).simple_graph(), | 36 self.assertEqual(GitRepoSchema(schema_string).simple_graph(), |
| 79 self.repo.to_schema().simple_graph()) | 37 self.repo.to_schema().simple_graph()) |
| 80 | 38 |
| 39 def setUp(self): | |
| 40 super(GitRepoTestBase, self).setUp() | |
| 41 assert self.REPO_SCHEMA is not None | |
| 42 # TODO(iannucci): r_schema could be established at class-load time, using | |
| 43 # a metaclass. This would enable us to only parse the schema once per | |
| 44 # derived test case class. | |
| 45 self.r_schema = GitRepoSchema(self.REPO_SCHEMA, self.getRepoContent) | |
|
Vadim Sh.
2014/08/17 04:39:59
is r_schema used outside of setUp here?
iannucci
2014/08/17 08:47:04
It can be/is accessed by the tests
| |
| 46 self.repo = GitRepo(self.r_schema) | |
| 47 self.repo.git('checkout', '-f', self.repo.last_commit) | |
| 48 | |
| 49 def tearDown(self): | |
| 50 self.repo.nuke() | |
|
Vadim Sh.
2014/08/17 04:39:59
try:
...
finally:
super(GitRepoTestBase, self)
iannucci
2014/08/17 08:47:04
Fair enough. I haven't seen it blow up in practice
| |
| 51 super(GitRepoTestBase, self).tearDown() | |
| 52 | |
| 53 | |
| 54 # The read-only version used to set up and tear down repo on a setUpClass basis, | |
| 55 # and the read-write version used to do it on a setUp basis. This is no longer | |
| 56 # the case. Keep the names around, but they're deprecated. | |
| 57 GitRepoReadWriteTestBase = GitRepoTestBase | |
|
Vadim Sh.
2014/08/17 04:39:59
I assume you'll commit this, roll deps, get rid of
iannucci
2014/08/17 08:47:04
yep
| |
| 58 GitRepoReadOnlyTestBase = GitRepoTestBase | |
| 59 | |
| 60 # Additionally, there was a schema-only version, but it was pretty useless | |
| 61 GitRepoSchemaTestBase = GitRepoTestBase | |
| OLD | NEW |