diff options
Diffstat (limited to 'tests/test_git_superproject.py')
| -rw-r--r-- | tests/test_git_superproject.py | 89 |
1 files changed, 82 insertions, 7 deletions
diff --git a/tests/test_git_superproject.py b/tests/test_git_superproject.py index 4012ec26..d2c2f501 100644 --- a/tests/test_git_superproject.py +++ b/tests/test_git_superproject.py | |||
| @@ -21,6 +21,7 @@ from unittest import mock | |||
| 21 | 21 | ||
| 22 | from error import GitError | 22 | from error import GitError |
| 23 | import git_superproject | 23 | import git_superproject |
| 24 | import manifest_xml | ||
| 24 | import platform_utils | 25 | import platform_utils |
| 25 | 26 | ||
| 26 | 27 | ||
| @@ -31,27 +32,43 @@ class SuperprojectTestCase(unittest.TestCase): | |||
| 31 | """Set up superproject every time.""" | 32 | """Set up superproject every time.""" |
| 32 | self.tempdir = tempfile.mkdtemp(prefix='repo_tests') | 33 | self.tempdir = tempfile.mkdtemp(prefix='repo_tests') |
| 33 | self.repodir = os.path.join(self.tempdir, '.repo') | 34 | self.repodir = os.path.join(self.tempdir, '.repo') |
| 34 | os.mkdir(self.repodir) | ||
| 35 | self._superproject = git_superproject.Superproject(self.repodir) | 35 | self._superproject = git_superproject.Superproject(self.repodir) |
| 36 | self.manifest_file = os.path.join( | ||
| 37 | self.repodir, manifest_xml.MANIFEST_FILE_NAME) | ||
| 38 | os.mkdir(self.repodir) | ||
| 39 | |||
| 40 | # The manifest parsing really wants a git repo currently. | ||
| 41 | gitdir = os.path.join(self.repodir, 'manifests.git') | ||
| 42 | os.mkdir(gitdir) | ||
| 43 | with open(os.path.join(gitdir, 'config'), 'w') as fp: | ||
| 44 | fp.write("""[remote "origin"] | ||
| 45 | url = https://localhost:0/manifest | ||
| 46 | """) | ||
| 36 | 47 | ||
| 37 | def tearDown(self): | 48 | def tearDown(self): |
| 38 | """Tear down superproject every time.""" | 49 | """Tear down superproject every time.""" |
| 39 | platform_utils.rmtree(self.tempdir) | 50 | platform_utils.rmtree(self.tempdir) |
| 40 | 51 | ||
| 52 | def getXmlManifest(self, data): | ||
| 53 | """Helper to initialize a manifest for testing.""" | ||
| 54 | with open(self.manifest_file, 'w') as fp: | ||
| 55 | fp.write(data) | ||
| 56 | return manifest_xml.XmlManifest(self.repodir, self.manifest_file) | ||
| 57 | |||
| 41 | def test_superproject_get_project_shas_no_url(self): | 58 | def test_superproject_get_project_shas_no_url(self): |
| 42 | """Test with no url.""" | 59 | """Test with no url.""" |
| 43 | with self.assertRaises(ValueError): | 60 | with self.assertRaises(ValueError): |
| 44 | self._superproject.GetAllProjectsSHAs(url=None) | 61 | self._superproject._GetAllProjectsSHAs(url=None) |
| 45 | 62 | ||
| 46 | def test_superproject_get_project_shas_invalid_url(self): | 63 | def test_superproject_get_project_shas_invalid_url(self): |
| 47 | """Test with an invalid url.""" | 64 | """Test with an invalid url.""" |
| 48 | with self.assertRaises(GitError): | 65 | with self.assertRaises(GitError): |
| 49 | self._superproject.GetAllProjectsSHAs(url='localhost') | 66 | self._superproject._GetAllProjectsSHAs(url='localhost') |
| 50 | 67 | ||
| 51 | def test_superproject_get_project_shas_invalid_branch(self): | 68 | def test_superproject_get_project_shas_invalid_branch(self): |
| 52 | """Test with an invalid branch.""" | 69 | """Test with an invalid branch.""" |
| 53 | with self.assertRaises(GitError): | 70 | with self.assertRaises(GitError): |
| 54 | self._superproject.GetAllProjectsSHAs( | 71 | self._superproject._GetAllProjectsSHAs( |
| 55 | url='sso://android/platform/superproject', | 72 | url='sso://android/platform/superproject', |
| 56 | branch='junk') | 73 | branch='junk') |
| 57 | 74 | ||
| @@ -59,14 +76,14 @@ class SuperprojectTestCase(unittest.TestCase): | |||
| 59 | """Test with _Clone failing.""" | 76 | """Test with _Clone failing.""" |
| 60 | with self.assertRaises(GitError): | 77 | with self.assertRaises(GitError): |
| 61 | with mock.patch.object(self._superproject, '_Clone', return_value=False): | 78 | with mock.patch.object(self._superproject, '_Clone', return_value=False): |
| 62 | self._superproject.GetAllProjectsSHAs(url='localhost') | 79 | self._superproject._GetAllProjectsSHAs(url='localhost') |
| 63 | 80 | ||
| 64 | def test_superproject_get_project_shas_mock_pull(self): | 81 | def test_superproject_get_project_shas_mock_pull(self): |
| 65 | """Test with _Pull failing.""" | 82 | """Test with _Pull failing.""" |
| 66 | with self.assertRaises(GitError): | 83 | with self.assertRaises(GitError): |
| 67 | with mock.patch.object(self._superproject, '_Clone', return_value=True): | 84 | with mock.patch.object(self._superproject, '_Clone', return_value=True): |
| 68 | with mock.patch.object(self._superproject, '_Pull', return_value=False): | 85 | with mock.patch.object(self._superproject, '_Pull', return_value=False): |
| 69 | self._superproject.GetAllProjectsSHAs(url='localhost') | 86 | self._superproject._GetAllProjectsSHAs(url='localhost') |
| 70 | 87 | ||
| 71 | def test_superproject_get_project_shas_mock_ls_tree(self): | 88 | def test_superproject_get_project_shas_mock_ls_tree(self): |
| 72 | """Test with LsTree being a mock.""" | 89 | """Test with LsTree being a mock.""" |
| @@ -77,13 +94,71 @@ class SuperprojectTestCase(unittest.TestCase): | |||
| 77 | '160000 commit ade9b7a0d874e25fff4bf2552488825c6f111928\tbuild/bazel\x00') | 94 | '160000 commit ade9b7a0d874e25fff4bf2552488825c6f111928\tbuild/bazel\x00') |
| 78 | with mock.patch.object(self._superproject, '_Clone', return_value=True): | 95 | with mock.patch.object(self._superproject, '_Clone', return_value=True): |
| 79 | with mock.patch.object(self._superproject, '_LsTree', return_value=data): | 96 | with mock.patch.object(self._superproject, '_LsTree', return_value=data): |
| 80 | shas = self._superproject.GetAllProjectsSHAs(url='localhost', branch='junk') | 97 | shas = self._superproject._GetAllProjectsSHAs(url='localhost', branch='junk') |
| 81 | self.assertEqual(shas, { | 98 | self.assertEqual(shas, { |
| 82 | 'art': '2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea', | 99 | 'art': '2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea', |
| 83 | 'bootable/recovery': 'e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06', | 100 | 'bootable/recovery': 'e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06', |
| 84 | 'build/bazel': 'ade9b7a0d874e25fff4bf2552488825c6f111928' | 101 | 'build/bazel': 'ade9b7a0d874e25fff4bf2552488825c6f111928' |
| 85 | }) | 102 | }) |
| 86 | 103 | ||
| 104 | def test_superproject_write_manifest_file(self): | ||
| 105 | """Test with writing manifest to a file after setting revisionId.""" | ||
| 106 | manifest = self.getXmlManifest(""" | ||
| 107 | <manifest> | ||
| 108 | <remote name="default-remote" fetch="http://localhost" /> | ||
| 109 | <default remote="default-remote" revision="refs/heads/main" /> | ||
| 110 | <project name="test-name"/> | ||
| 111 | </manifest> | ||
| 112 | """) | ||
| 113 | self.assertEqual(len(manifest.projects), 1) | ||
| 114 | project = manifest.projects[0] | ||
| 115 | project.SetRevisionId('ABCDEF') | ||
| 116 | # Create temporary directory so that it can write the file. | ||
| 117 | os.mkdir(self._superproject._superproject_path) | ||
| 118 | manifest_path = self._superproject._WriteManfiestFile(manifest) | ||
| 119 | self.assertIsNotNone(manifest_path) | ||
| 120 | with open(manifest_path, 'r') as fp: | ||
| 121 | manifest_xml = fp.read() | ||
| 122 | self.assertEqual( | ||
| 123 | manifest_xml, | ||
| 124 | '<?xml version="1.0" ?><manifest>' + | ||
| 125 | '<remote name="default-remote" fetch="http://localhost"/>' + | ||
| 126 | '<default remote="default-remote" revision="refs/heads/main"/>' + | ||
| 127 | '<project name="test-name" revision="ABCDEF"/>' + | ||
| 128 | '</manifest>') | ||
| 129 | |||
| 130 | def test_superproject_update_project_revision_id(self): | ||
| 131 | """Test with LsTree being a mock.""" | ||
| 132 | manifest = self.getXmlManifest(""" | ||
| 133 | <manifest> | ||
| 134 | <remote name="default-remote" fetch="http://localhost" /> | ||
| 135 | <default remote="default-remote" revision="refs/heads/main" /> | ||
| 136 | <project path="art" name="platform/art" /> | ||
| 137 | </manifest> | ||
| 138 | """) | ||
| 139 | self.assertEqual(len(manifest.projects), 1) | ||
| 140 | projects = manifest.projects | ||
| 141 | data = ('160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00' | ||
| 142 | '160000 commit e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06\tbootable/recovery\x00') | ||
| 143 | with mock.patch.object(self._superproject, '_Clone', return_value=True): | ||
| 144 | with mock.patch.object(self._superproject, '_Pull', return_value=True): | ||
| 145 | with mock.patch.object(self._superproject, '_LsTree', return_value=data): | ||
| 146 | # Create temporary directory so that it can write the file. | ||
| 147 | os.mkdir(self._superproject._superproject_path) | ||
| 148 | manifest_path = self._superproject.UpdateProjectsRevisionId( | ||
| 149 | manifest, projects, url='localhost') | ||
| 150 | self.assertIsNotNone(manifest_path) | ||
| 151 | with open(manifest_path, 'r') as fp: | ||
| 152 | manifest_xml = fp.read() | ||
| 153 | self.assertEqual( | ||
| 154 | manifest_xml, | ||
| 155 | '<?xml version="1.0" ?><manifest>' + | ||
| 156 | '<remote name="default-remote" fetch="http://localhost"/>' + | ||
| 157 | '<default remote="default-remote" revision="refs/heads/main"/>' + | ||
| 158 | '<project name="platform/art" path="art" ' + | ||
| 159 | 'revision="2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea"/>' + | ||
| 160 | '</manifest>') | ||
| 161 | |||
| 87 | 162 | ||
| 88 | if __name__ == '__main__': | 163 | if __name__ == '__main__': |
| 89 | unittest.main() | 164 | unittest.main() |
