test_git_superproject.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. """Unittests for the git_superproject.py module."""
  15. import os
  16. import tempfile
  17. import unittest
  18. from unittest import mock
  19. from error import GitError
  20. import git_superproject
  21. import platform_utils
  22. class SuperprojectTestCase(unittest.TestCase):
  23. """TestCase for the Superproject module."""
  24. def setUp(self):
  25. """Set up superproject every time."""
  26. self.tempdir = tempfile.mkdtemp(prefix='repo_tests')
  27. self.repodir = os.path.join(self.tempdir, '.repo')
  28. os.mkdir(self.repodir)
  29. self._superproject = git_superproject.Superproject(self.repodir)
  30. def tearDown(self):
  31. """Tear down superproject every time."""
  32. platform_utils.rmtree(self.tempdir)
  33. def test_superproject_get_project_shas_no_url(self):
  34. """Test with no url."""
  35. with self.assertRaises(ValueError):
  36. self._superproject.GetAllProjectsSHAs(url=None)
  37. def test_superproject_get_project_shas_invalid_url(self):
  38. """Test with an invalid url."""
  39. with self.assertRaises(GitError):
  40. self._superproject.GetAllProjectsSHAs(url='localhost')
  41. def test_superproject_get_project_shas_invalid_branch(self):
  42. """Test with an invalid branch."""
  43. with self.assertRaises(GitError):
  44. self._superproject.GetAllProjectsSHAs(
  45. url='sso://android/platform/superproject',
  46. branch='junk')
  47. def test_superproject_get_project_shas_mock_clone(self):
  48. """Test with _Clone failing."""
  49. with self.assertRaises(GitError):
  50. with mock.patch.object(self._superproject, '_Clone', return_value=False):
  51. self._superproject.GetAllProjectsSHAs(url='localhost')
  52. def test_superproject_get_project_shas_mock_ls_tree(self):
  53. """Test with LsTree being a mock."""
  54. data = ('120000 blob 158258bdf146f159218e2b90f8b699c4d85b5804\tAndroid.bp\x00'
  55. '160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00'
  56. '160000 commit e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06\tbootable/recovery\x00'
  57. '120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00'
  58. '160000 commit ade9b7a0d874e25fff4bf2552488825c6f111928\tbuild/bazel\x00')
  59. with mock.patch.object(self._superproject, '_Clone', return_value=True):
  60. with mock.patch.object(self._superproject, '_LsTree', return_value=data):
  61. shas = self._superproject.GetAllProjectsSHAs(url='localhost', branch='junk')
  62. self.assertEqual(shas, {
  63. 'art': '2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea',
  64. 'bootable/recovery': 'e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06',
  65. 'build/bazel': 'ade9b7a0d874e25fff4bf2552488825c6f111928'
  66. })
  67. if __name__ == '__main__':
  68. unittest.main()