utils_test.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 shutil
  8. import sys
  9. import tempfile
  10. import unittest
  11. from unittest import mock
  12. DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  13. sys.path.insert(0, DEPOT_TOOLS_ROOT)
  14. from testing_support import coverage_utils
  15. import utils
  16. class GitCacheTest(unittest.TestCase):
  17. def setUp(self):
  18. pass
  19. @mock.patch('subprocess.check_output', lambda x, **kwargs: b'foo')
  20. def testVersionWithGit(self):
  21. version = utils.depot_tools_version()
  22. self.assertEqual(version, 'git-foo')
  23. @mock.patch('subprocess.check_output')
  24. @mock.patch('os.path.getmtime', lambda x: 42)
  25. def testVersionWithNoGit(self, mock_subprocess):
  26. mock_subprocess.side_effect = Exception
  27. version = utils.depot_tools_version()
  28. self.assertEqual(version, 'recipes.cfg-42')
  29. @mock.patch('subprocess.check_output')
  30. @mock.patch('os.path.getmtime')
  31. def testVersionWithNoGit(self, mock_subprocess, mock_getmtime):
  32. mock_subprocess.side_effect = Exception
  33. mock_getmtime.side_effect = Exception
  34. version = utils.depot_tools_version()
  35. self.assertEqual(version, 'unknown')
  36. class ConfigDirTest(unittest.TestCase):
  37. @mock.patch('sys.platform', 'win')
  38. def testWin(self):
  39. self.assertEqual(DEPOT_TOOLS_ROOT, utils.depot_tools_config_dir())
  40. @mock.patch('sys.platform', 'darwin')
  41. def testMac(self):
  42. self.assertEqual(DEPOT_TOOLS_ROOT, utils.depot_tools_config_dir())
  43. @mock.patch('sys.platform', 'foo')
  44. def testOther(self):
  45. self.assertEqual(DEPOT_TOOLS_ROOT, utils.depot_tools_config_dir())
  46. @mock.patch('sys.platform', 'linux')
  47. @mock.patch.dict('os.environ', {})
  48. def testLinuxDefault(self):
  49. self.assertEqual(
  50. os.path.join(os.path.expanduser('~/.config'), 'depot_tools'),
  51. utils.depot_tools_config_dir())
  52. @mock.patch('sys.platform', 'linux')
  53. @mock.patch.dict('os.environ', {'XDG_CONFIG_HOME': '/my/home'})
  54. def testLinuxCustom(self):
  55. self.assertEqual(os.path.join('/my/home', 'depot_tools'),
  56. utils.depot_tools_config_dir())
  57. class ConfigPathTest(unittest.TestCase):
  58. def setUp(self):
  59. self.temp_dir = tempfile.mkdtemp(prefix='utils_test')
  60. self.config_dir = os.path.join(self.temp_dir, 'test_files')
  61. self.isfile = mock.Mock()
  62. self.move = mock.Mock()
  63. mock.patch('os.path.isfile', self.isfile).start()
  64. mock.patch('shutil.move', self.move).start()
  65. mock.patch('utils.depot_tools_config_dir',
  66. lambda: self.config_dir).start()
  67. self.addCleanup(mock.patch.stopall)
  68. self.addCleanup(shutil.rmtree, self.temp_dir)
  69. def testCreatesConfigDir(self):
  70. # Ensure "legacy path" doesn't exist so that nothing gets moved.
  71. def EnsureLegacyPathNotExists(path):
  72. return path != os.path.join(DEPOT_TOOLS_ROOT, 'metrics.cfg')
  73. self.isfile.side_effect = EnsureLegacyPathNotExists
  74. self.assertEqual(os.path.join(self.config_dir, 'metrics.cfg'),
  75. utils.depot_tools_config_path('metrics.cfg'))
  76. self.assertTrue(os.path.exists(self.config_dir))
  77. self.move.assert_not_called()
  78. def testMovesLegacy(self):
  79. # Ensure "legacy path" exists so that it gets moved.
  80. def EnsureLegacyPathExists(path):
  81. return path == os.path.join(DEPOT_TOOLS_ROOT, 'metrics.cfg')
  82. self.isfile.side_effect = EnsureLegacyPathExists
  83. self.assertEqual(os.path.join(self.config_dir, 'metrics.cfg'),
  84. utils.depot_tools_config_path('metrics.cfg'))
  85. self.move.assert_called_once_with(
  86. os.path.join(DEPOT_TOOLS_ROOT, 'metrics.cfg'),
  87. os.path.join(self.config_dir, 'metrics.cfg'))
  88. if __name__ == '__main__':
  89. logging.basicConfig(
  90. level=logging.DEBUG if '-v' in sys.argv else logging.ERROR)
  91. sys.exit(
  92. coverage_utils.covered_main(
  93. (os.path.join(DEPOT_TOOLS_ROOT, 'git_cache.py')),
  94. required_percentage=0))