szager@chromium.org | 66c8b85 | 2015-09-22 23:19:07 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2015 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 | """Unit tests for git_cache.py""" |
| 7 | |
| 8 | import os |
| 9 | import shutil |
| 10 | import sys |
| 11 | import tempfile |
| 12 | import unittest |
| 13 | |
| 14 | DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 15 | sys.path.insert(0, DEPOT_TOOLS_ROOT) |
| 16 | |
| 17 | from testing_support import coverage_utils |
| 18 | import git_cache |
| 19 | |
| 20 | class GitCacheTest(unittest.TestCase): |
| 21 | @classmethod |
| 22 | def setUpClass(cls): |
| 23 | cls.cache_dir = tempfile.mkdtemp(prefix='git_cache_test_') |
| 24 | git_cache.Mirror.SetCachePath(cls.cache_dir) |
| 25 | |
| 26 | @classmethod |
| 27 | def tearDownClass(cls): |
| 28 | shutil.rmtree(cls.cache_dir, ignore_errors=True) |
| 29 | |
| 30 | def testParseFetchSpec(self): |
| 31 | testData = [ |
| 32 | ([], []), |
| 33 | (['master'], [('+refs/heads/master:refs/heads/master', |
| 34 | r'\+refs/heads/master:.*')]), |
| 35 | (['master/'], [('+refs/heads/master:refs/heads/master', |
| 36 | r'\+refs/heads/master:.*')]), |
| 37 | (['+master'], [('+refs/heads/master:refs/heads/master', |
| 38 | r'\+refs/heads/master:.*')]), |
| 39 | (['refs/heads/*'], [('+refs/heads/*:refs/heads/*', |
| 40 | r'\+refs/heads/\*:.*')]), |
| 41 | (['foo/bar/*', 'baz'], [('+refs/heads/foo/bar/*:refs/heads/foo/bar/*', |
| 42 | r'\+refs/heads/foo/bar/\*:.*'), |
| 43 | ('+refs/heads/baz:refs/heads/baz', |
| 44 | r'\+refs/heads/baz:.*')]), |
| 45 | (['refs/foo/*:refs/bar/*'], [('+refs/foo/*:refs/bar/*', |
| 46 | r'\+refs/foo/\*:.*')]) |
| 47 | ] |
| 48 | |
| 49 | mirror = git_cache.Mirror('test://phony.example.biz') |
| 50 | for fetch_specs, expected in testData: |
| 51 | mirror = git_cache.Mirror('test://phony.example.biz', refs=fetch_specs) |
| 52 | self.assertItemsEqual(mirror.fetch_specs, expected) |
| 53 | |
Robert Iannucci | a19649b | 2018-06-29 16:31:45 +0000 | [diff] [blame^] | 54 | |
| 55 | class GitCacheDirTest(unittest.TestCase): |
| 56 | def setUp(self): |
| 57 | try: |
| 58 | delattr(git_cache.Mirror, 'cachepath') |
| 59 | except AttributeError: |
| 60 | pass |
| 61 | super(GitCacheDirTest, self).setUp() |
| 62 | |
| 63 | def tearDown(self): |
| 64 | try: |
| 65 | delattr(git_cache.Mirror, 'cachepath') |
| 66 | except AttributeError: |
| 67 | pass |
| 68 | super(GitCacheDirTest, self).tearDown() |
| 69 | |
| 70 | def test_git_config_read(self): |
| 71 | (fd, tmpFile) = tempfile.mkstemp() |
| 72 | old = git_cache.Mirror._GIT_CONFIG_LOCATION |
| 73 | try: |
| 74 | try: |
| 75 | os.write(fd, '[cache]\n cachepath="hello world"\n') |
| 76 | finally: |
| 77 | os.close(fd) |
| 78 | |
| 79 | git_cache.Mirror._GIT_CONFIG_LOCATION = ['-f', tmpFile] |
| 80 | |
| 81 | self.assertEqual(git_cache.Mirror.GetCachePath(), 'hello world') |
| 82 | finally: |
| 83 | git_cache.Mirror._GIT_CONFIG_LOCATION = old |
| 84 | os.remove(tmpFile) |
| 85 | |
| 86 | def test_environ_read(self): |
| 87 | path = os.environ.get('GIT_CACHE_PATH') |
| 88 | config = os.environ.get('GIT_CONFIG') |
| 89 | try: |
| 90 | os.environ['GIT_CACHE_PATH'] = 'hello world' |
| 91 | os.environ['GIT_CONFIG'] = 'disabled' |
| 92 | |
| 93 | self.assertEqual(git_cache.Mirror.GetCachePath(), 'hello world') |
| 94 | finally: |
| 95 | for name, val in zip(('GIT_CACHE_PATH', 'GIT_CONFIG'), (path, config)): |
| 96 | if val is None: |
| 97 | os.environ.pop(name, None) |
| 98 | else: |
| 99 | os.environ[name] = val |
| 100 | |
| 101 | def test_manual_set(self): |
| 102 | git_cache.Mirror.SetCachePath('hello world') |
| 103 | self.assertEqual(git_cache.Mirror.GetCachePath(), 'hello world') |
| 104 | |
| 105 | def test_unconfigured(self): |
| 106 | path = os.environ.get('GIT_CACHE_PATH') |
| 107 | config = os.environ.get('GIT_CONFIG') |
| 108 | try: |
| 109 | os.environ.pop('GIT_CACHE_PATH', None) |
| 110 | os.environ['GIT_CONFIG'] = 'disabled' |
| 111 | |
| 112 | with self.assertRaisesRegexp(RuntimeError, 'cache\.cachepath'): |
| 113 | git_cache.Mirror.GetCachePath() |
| 114 | |
| 115 | # negatively cached value still raises |
| 116 | with self.assertRaisesRegexp(RuntimeError, 'cache\.cachepath'): |
| 117 | git_cache.Mirror.GetCachePath() |
| 118 | finally: |
| 119 | for name, val in zip(('GIT_CACHE_PATH', 'GIT_CONFIG'), (path, config)): |
| 120 | if val is None: |
| 121 | os.environ.pop(name, None) |
| 122 | else: |
| 123 | os.environ[name] = val |
| 124 | |
| 125 | |
szager@chromium.org | 66c8b85 | 2015-09-22 23:19:07 +0000 | [diff] [blame] | 126 | if __name__ == '__main__': |
| 127 | sys.exit(coverage_utils.covered_main(( |
| 128 | os.path.join(DEPOT_TOOLS_ROOT, 'git_cache.py') |
| 129 | ), required_percentage=0)) |