utils_test.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env vpython3
  2. # Copyright 2022 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import logging
  6. import os
  7. import sys
  8. import unittest
  9. from unittest import mock
  10. DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  11. sys.path.insert(0, DEPOT_TOOLS_ROOT)
  12. from testing_support import coverage_utils
  13. import utils
  14. class GitCacheTest(unittest.TestCase):
  15. def setUp(self):
  16. pass
  17. @mock.patch('subprocess.check_output', lambda x, **kwargs: b'foo')
  18. def testVersionWithGit(self):
  19. version = utils.depot_tools_version()
  20. self.assertEqual(version, 'git-foo')
  21. @mock.patch('subprocess.check_output')
  22. @mock.patch('os.path.getmtime', lambda x: 42)
  23. def testVersionWithNoGit(self, mock_subprocess):
  24. mock_subprocess.side_effect = Exception
  25. version = utils.depot_tools_version()
  26. self.assertEqual(version, 'recipes.cfg-42')
  27. @mock.patch('subprocess.check_output')
  28. @mock.patch('os.path.getmtime')
  29. def testVersionWithNoGit(self, mock_subprocess, mock_getmtime):
  30. mock_subprocess.side_effect = Exception
  31. mock_getmtime.side_effect = Exception
  32. version = utils.depot_tools_version()
  33. self.assertEqual(version, 'unknown')
  34. if __name__ == '__main__':
  35. logging.basicConfig(
  36. level=logging.DEBUG if '-v' in sys.argv else logging.ERROR)
  37. sys.exit(
  38. coverage_utils.covered_main(
  39. (os.path.join(DEPOT_TOOLS_ROOT, 'git_cache.py')),
  40. required_percentage=0))