| 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 and a GitRepo based on that |
| 13 schema. |
| 13 | 14 |
| 14 Expects a class variable REPO_SCHEMA to be a GitRepoSchema string in the form | 15 Expects a class variable REPO_SCHEMA to be a GitRepoSchema string in the form |
| 15 described by that class. | 16 described by that class. |
| 16 | 17 |
| 17 You may also set class variables in the form COMMIT_%(commit_name)s, which | 18 You may also set class variables in the form COMMIT_%(commit_name)s, which |
| 18 provide the content for the given commit_name commits. | 19 provide the content for the given commit_name commits (this behavior can be |
| 20 overridden by overriding getRepoContent()). |
| 19 | 21 |
| 20 You probably will end up using either GitRepoReadOnlyTestBase or | 22 This GitRepo will appear as self.repo, and will be deleted and recreated for |
| 21 GitRepoReadWriteTestBase for real tests. | 23 each test method in the subclass. |
| 22 """ | 24 """ |
| 23 REPO_SCHEMA = None | 25 REPO_SCHEMA = None |
| 24 | 26 |
| 25 @classmethod | 27 @classmethod |
| 26 def getRepoContent(cls, commit): | 28 def getRepoContent(cls, commit): |
| 27 return getattr(cls, 'COMMIT_%s' % commit, None) | 29 return getattr(cls, 'COMMIT_%s' % commit, None) |
| 28 | 30 |
| 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): | 31 def assertSchema(self, schema_string): |
| 78 self.assertEqual(GitRepoSchema(schema_string).simple_graph(), | 32 self.assertEqual(GitRepoSchema(schema_string).simple_graph(), |
| 79 self.repo.to_schema().simple_graph()) | 33 self.repo.to_schema().simple_graph()) |
| 80 | 34 |
| 35 def setUp(self): |
| 36 super(GitRepoTestBase, self).setUp() |
| 37 assert self.REPO_SCHEMA is not None |
| 38 # TODO(iannucci): r_schema could be established at class-load time, using |
| 39 # a metaclass. This would enable us to only parse the schema once per |
| 40 # derived test case class. |
| 41 self.r_schema = GitRepoSchema(self.REPO_SCHEMA, self.getRepoContent) |
| 42 self.repo = GitRepo(self.r_schema) |
| 43 self.repo.git('checkout', '-f', self.repo.last_commit) |
| 44 |
| 45 def tearDown(self): |
| 46 try: |
| 47 self.repo.nuke() |
| 48 finally: |
| 49 super(GitRepoTestBase, self).tearDown() |
| 50 |
| 51 |
| 52 # The read-only version used to set up and tear down repo on a setUpClass basis, |
| 53 # and the read-write version used to do it on a setUp basis. This is no longer |
| 54 # the case. Keep the names around, but they're deprecated. |
| 55 GitRepoReadWriteTestBase = GitRepoTestBase |
| 56 GitRepoReadOnlyTestBase = GitRepoTestBase |
| 57 |
| 58 # Additionally, there was a schema-only version, but it was pretty useless |
| 59 GitRepoSchemaTestBase = GitRepoTestBase |
| OLD | NEW |