test_wrapper.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding:utf-8 -*-
  2. #
  3. # Copyright (C) 2015 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Unittests for the wrapper.py module."""
  17. from __future__ import print_function
  18. import os
  19. import re
  20. import unittest
  21. from pyversion import is_python3
  22. import wrapper
  23. def fixture(*paths):
  24. """Return a path relative to tests/fixtures.
  25. """
  26. return os.path.join(os.path.dirname(__file__), 'fixtures', *paths)
  27. class RepoWrapperTestCase(unittest.TestCase):
  28. """TestCase for the wrapper module."""
  29. def setUp(self):
  30. """Load the wrapper module every time."""
  31. wrapper._wrapper_module = None
  32. self.wrapper = wrapper.Wrapper()
  33. if not is_python3():
  34. self.assertRegex = self.assertRegexpMatches
  35. class RepoWrapperUnitTest(RepoWrapperTestCase):
  36. """Tests helper functions in the repo wrapper
  37. """
  38. def test_get_gitc_manifest_dir_no_gitc(self):
  39. """
  40. Test reading a missing gitc config file
  41. """
  42. self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
  43. val = self.wrapper.get_gitc_manifest_dir()
  44. self.assertEqual(val, '')
  45. def test_get_gitc_manifest_dir(self):
  46. """
  47. Test reading the gitc config file and parsing the directory
  48. """
  49. self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
  50. val = self.wrapper.get_gitc_manifest_dir()
  51. self.assertEqual(val, '/test/usr/local/google/gitc')
  52. def test_gitc_parse_clientdir_no_gitc(self):
  53. """
  54. Test parsing the gitc clientdir without gitc running
  55. """
  56. self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
  57. self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
  58. self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
  59. def test_gitc_parse_clientdir(self):
  60. """
  61. Test parsing the gitc clientdir
  62. """
  63. self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
  64. self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
  65. self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
  66. self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/'), 'test')
  67. self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/extra'), 'test')
  68. self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test'), 'test')
  69. self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/'), 'test')
  70. self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/extra'), 'test')
  71. self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/'), None)
  72. self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/'), None)
  73. class SetGitTrace2ParentSid(RepoWrapperTestCase):
  74. """Check SetGitTrace2ParentSid behavior."""
  75. KEY = 'GIT_TRACE2_PARENT_SID'
  76. VALID_FORMAT = re.compile(r'^repo-[0-9]{8}T[0-9]{6}Z-P[0-9a-f]{8}$')
  77. def test_first_set(self):
  78. """Test env var not yet set."""
  79. env = {}
  80. self.wrapper.SetGitTrace2ParentSid(env)
  81. self.assertIn(self.KEY, env)
  82. value = env[self.KEY]
  83. self.assertRegex(value, self.VALID_FORMAT)
  84. def test_append(self):
  85. """Test env var is appended."""
  86. env = {self.KEY: 'pfx'}
  87. self.wrapper.SetGitTrace2ParentSid(env)
  88. self.assertIn(self.KEY, env)
  89. value = env[self.KEY]
  90. self.assertTrue(value.startswith('pfx/'))
  91. self.assertRegex(value[4:], self.VALID_FORMAT)
  92. def test_global_context(self):
  93. """Check os.environ gets updated by default."""
  94. os.environ.pop(self.KEY, None)
  95. self.wrapper.SetGitTrace2ParentSid()
  96. self.assertIn(self.KEY, os.environ)
  97. value = os.environ[self.KEY]
  98. self.assertRegex(value, self.VALID_FORMAT)
  99. if __name__ == '__main__':
  100. unittest.main()