|
| 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 | + |
| 6 | +import logging |
| 7 | +import os |
| 8 | +import sys |
| 9 | +import unittest |
| 10 | + |
| 11 | +if sys.version_info.major == 2: |
| 12 | + import mock |
| 13 | +else: |
| 14 | + from unittest import mock |
| 15 | + |
| 16 | +DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 17 | +sys.path.insert(0, DEPOT_TOOLS_ROOT) |
| 18 | + |
| 19 | +from testing_support import coverage_utils |
| 20 | +import utils |
| 21 | + |
| 22 | + |
| 23 | +class GitCacheTest(unittest.TestCase): |
| 24 | + def setUp(self): |
| 25 | + pass |
| 26 | + |
| 27 | + @mock.patch('subprocess.check_output', lambda x, **kwargs: b'foo') |
| 28 | + def testVersionWithGit(self): |
| 29 | + version = utils.depot_tools_version() |
| 30 | + self.assertEqual(version, 'git-foo') |
| 31 | + |
| 32 | + @mock.patch('subprocess.check_output') |
| 33 | + @mock.patch('os.path.getmtime', lambda x: 42) |
| 34 | + def testVersionWithNoGit(self, mock_subprocess): |
| 35 | + mock_subprocess.side_effect = Exception |
| 36 | + version = utils.depot_tools_version() |
| 37 | + self.assertEqual(version, 'recipes.cfg-42') |
| 38 | + |
| 39 | + @mock.patch('subprocess.check_output') |
| 40 | + @mock.patch('os.path.getmtime') |
| 41 | + def testVersionWithNoGit(self, mock_subprocess, mock_getmtime): |
| 42 | + mock_subprocess.side_effect = Exception |
| 43 | + mock_getmtime.side_effect = Exception |
| 44 | + version = utils.depot_tools_version() |
| 45 | + self.assertEqual(version, 'unknown') |
| 46 | + |
| 47 | + |
| 48 | +if __name__ == '__main__': |
| 49 | + logging.basicConfig( |
| 50 | + level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) |
| 51 | + sys.exit( |
| 52 | + coverage_utils.covered_main( |
| 53 | + (os.path.join(DEPOT_TOOLS_ROOT, 'git_cache.py')), |
| 54 | + required_percentage=0)) |
0 commit comments