diff options
Diffstat (limited to 'tests/test_git_superproject.py')
| -rw-r--r-- | tests/test_git_superproject.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/tests/test_git_superproject.py b/tests/test_git_superproject.py new file mode 100644 index 00000000..67a75a17 --- /dev/null +++ b/tests/test_git_superproject.py | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | # Copyright (C) 2021 The Android Open Source Project | ||
| 2 | # | ||
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 4 | # you may not use this file except in compliance with the License. | ||
| 5 | # You may obtain a copy of the License at | ||
| 6 | # | ||
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 8 | # | ||
| 9 | # Unless required by applicable law or agreed to in writing, software | ||
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 12 | # See the License for the specific language governing permissions and | ||
| 13 | # limitations under the License. | ||
| 14 | |||
| 15 | """Unittests for the git_superproject.py module.""" | ||
| 16 | |||
| 17 | import os | ||
| 18 | import tempfile | ||
| 19 | import unittest | ||
| 20 | from unittest import mock | ||
| 21 | |||
| 22 | from error import GitError | ||
| 23 | import git_superproject | ||
| 24 | import platform_utils | ||
| 25 | |||
| 26 | |||
| 27 | class SuperprojectTestCase(unittest.TestCase): | ||
| 28 | """TestCase for the Superproject module.""" | ||
| 29 | |||
| 30 | def setUp(self): | ||
| 31 | """Set up superproject every time.""" | ||
| 32 | self.tempdir = tempfile.mkdtemp(prefix='repo_tests') | ||
| 33 | self.repodir = os.path.join(self.tempdir, '.repo') | ||
| 34 | os.mkdir(self.repodir) | ||
| 35 | self._superproject = git_superproject.Superproject(self.repodir) | ||
| 36 | |||
| 37 | def tearDown(self): | ||
| 38 | """Tear down superproject every time.""" | ||
| 39 | platform_utils.rmtree(self.tempdir) | ||
| 40 | |||
| 41 | def test_superproject_get_project_shas_no_url(self): | ||
| 42 | """Test with no url.""" | ||
| 43 | with self.assertRaises(ValueError): | ||
| 44 | self._superproject.GetAllProjectsSHAs(url=None) | ||
| 45 | |||
| 46 | def test_superproject_get_project_shas_invalid_url(self): | ||
| 47 | """Test with an invalid url.""" | ||
| 48 | with self.assertRaises(GitError): | ||
| 49 | self._superproject.GetAllProjectsSHAs(url='localhost') | ||
| 50 | |||
| 51 | def test_superproject_get_project_shas_invalid_branch(self): | ||
| 52 | """Test with an invalid branch.""" | ||
| 53 | with self.assertRaises(GitError): | ||
| 54 | self._superproject.GetAllProjectsSHAs( | ||
| 55 | url='sso://android/platform/superproject', | ||
| 56 | branch='junk') | ||
| 57 | |||
| 58 | def test_superproject_get_project_shas_mock_clone(self): | ||
| 59 | """Test with _Clone failing.""" | ||
| 60 | with self.assertRaises(GitError): | ||
| 61 | with mock.patch.object(self._superproject, '_Clone', return_value=False): | ||
| 62 | self._superproject.GetAllProjectsSHAs(url='localhost') | ||
| 63 | |||
| 64 | def test_superproject_get_project_shas_mock_ls_tree(self): | ||
| 65 | """Test with LsTree being a mock.""" | ||
| 66 | data = ('120000 blob 158258bdf146f159218e2b90f8b699c4d85b5804\tAndroid.bp\x00' | ||
| 67 | '160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00' | ||
| 68 | '160000 commit e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06\tbootable/recovery\x00' | ||
| 69 | '120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00' | ||
| 70 | '160000 commit ade9b7a0d874e25fff4bf2552488825c6f111928\tbuild/bazel\x00') | ||
| 71 | with mock.patch.object(self._superproject, '_Clone', return_value=True): | ||
| 72 | with mock.patch.object(self._superproject, '_LsTree', return_value=data): | ||
| 73 | shas = self._superproject.GetAllProjectsSHAs(url='localhost', branch='junk') | ||
| 74 | self.assertEqual(shas, { | ||
| 75 | 'art': '2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea', | ||
| 76 | 'bootable/recovery': 'e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06', | ||
| 77 | 'build/bazel': 'ade9b7a0d874e25fff4bf2552488825c6f111928' | ||
| 78 | }) | ||
| 79 | |||
| 80 | |||
| 81 | if __name__ == '__main__': | ||
| 82 | unittest.main() | ||
