Kuang-che Wu | 385279d | 2017-09-27 14:48:28 +0800 | [diff] [blame] | 1 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | """Configure module.""" |
| 5 | |
| 6 | from __future__ import print_function |
| 7 | import contextlib |
| 8 | import os |
| 9 | import shutil |
| 10 | import tempfile |
| 11 | import textwrap |
| 12 | import unittest |
| 13 | |
| 14 | import mock |
| 15 | |
| 16 | from bisect_kit import configure |
| 17 | |
| 18 | |
| 19 | @contextlib.contextmanager |
| 20 | def scoped_file(folder, filename, content=''): |
| 21 | """Helper to create file and auto delete after use. |
| 22 | |
| 23 | This is with-context manager. It creates file with given content when at |
| 24 | start. After the context finished, it will delete the created file. |
| 25 | |
| 26 | Args: |
| 27 | folder: where to put the file. |
| 28 | filename: filename to create. |
| 29 | content: file content. |
| 30 | |
| 31 | Yields: |
| 32 | full path of created file. |
| 33 | """ |
| 34 | full_path = os.path.join(folder, filename) |
| 35 | with file(full_path, 'w') as f: |
| 36 | f.write(content) |
| 37 | yield full_path |
| 38 | os.unlink(full_path) |
| 39 | |
| 40 | |
| 41 | class TestConfigure(unittest.TestCase): |
| 42 | """Test configure.Configure class.""" |
| 43 | |
| 44 | def setUp(self): |
| 45 | assert configure.CONFIG_ENV_NAME not in os.environ, ( |
| 46 | '"%s" should not be set during unittest' % configure.CONFIG_ENV_NAME) |
| 47 | self.oldcwd = os.getcwd() |
| 48 | self.cwddir = tempfile.mkdtemp() |
| 49 | self.progdir = tempfile.mkdtemp() |
| 50 | # Make sure current working directory and is clean. |
| 51 | os.chdir(self.cwddir) |
| 52 | |
| 53 | def tearDown(self): |
| 54 | os.chdir(self.oldcwd) |
| 55 | shutil.rmtree(self.progdir) |
| 56 | shutil.rmtree(self.cwddir) |
| 57 | |
| 58 | def test_scoped_file(self): |
| 59 | with scoped_file(self.cwddir, 'foo', 'abc') as path: |
| 60 | assert os.path.exists(path) |
| 61 | self.assertEqual(file(path).read(), 'abc') |
| 62 | assert not os.path.exists(path) |
| 63 | |
| 64 | def test_search_config_file(self): |
| 65 | args = [os.path.join(self.progdir, 'foo.py')] |
| 66 | |
| 67 | self.assertEqual(configure.search_config_file(args), None) |
| 68 | |
| 69 | with scoped_file(self.cwddir, 'foo') as path: |
| 70 | self.assertEqual( |
| 71 | configure.search_config_file(args + ['--rc', path]), path) |
| 72 | |
| 73 | with scoped_file(self.cwddir, 'bar') as path: |
| 74 | with mock.patch.dict(os.environ, {configure.CONFIG_ENV_NAME: path}): |
| 75 | self.assertEqual(configure.search_config_file(args), path) |
| 76 | |
| 77 | with scoped_file(self.cwddir, configure.DEFAULT_CONFIG_FILENAME) as path: |
| 78 | self.assertEqual(configure.search_config_file(args), path) |
| 79 | |
| 80 | with scoped_file(self.progdir, configure.DEFAULT_CONFIG_FILENAME) as path: |
| 81 | self.assertEqual(configure.search_config_file(args), path) |
| 82 | |
| 83 | def test_search_config_file_precedence(self): |
| 84 | args = [os.path.join(self.progdir, 'foo.py')] |
| 85 | |
| 86 | self.assertEqual(configure.search_config_file(args), None) |
| 87 | |
| 88 | with scoped_file(self.progdir, configure.DEFAULT_CONFIG_FILENAME) as path1: |
| 89 | self.assertEqual(configure.search_config_file(args), path1) |
| 90 | |
| 91 | with scoped_file(self.cwddir, configure.DEFAULT_CONFIG_FILENAME) as path2: |
| 92 | self.assertEqual(configure.search_config_file(args), path2) |
| 93 | |
| 94 | with scoped_file(self.cwddir, 'bar') as path3: |
| 95 | with mock.patch.dict(os.environ, {configure.CONFIG_ENV_NAME: 'none'}): |
| 96 | self.assertEqual(configure.search_config_file(args), None) |
| 97 | |
| 98 | with mock.patch.dict(os.environ, {configure.CONFIG_ENV_NAME: path3}): |
| 99 | self.assertEqual(configure.search_config_file(args), path3) |
| 100 | |
| 101 | with scoped_file(self.cwddir, 'foo') as path4: |
| 102 | self.assertEqual( |
| 103 | configure.search_config_file(args + ['--rc', path4]), path4) |
| 104 | self.assertEqual( |
| 105 | configure.search_config_file(args + ['--rc', 'none']), None) |
| 106 | |
| 107 | def test_search_config_file_not_found(self): |
| 108 | args = [os.path.join(self.progdir, 'foo.py')] |
| 109 | path = os.path.join(self.cwddir, 'foo') |
| 110 | |
| 111 | with self.assertRaises(ValueError): |
| 112 | configure.search_config_file(args + ['--rc', path]) |
| 113 | |
| 114 | with self.assertRaises(ValueError): |
| 115 | with mock.patch.dict(os.environ, {configure.CONFIG_ENV_NAME: path}): |
| 116 | configure.search_config_file(args) |
| 117 | |
| 118 | def test_include(self): |
| 119 | subdir = os.path.join(self.progdir, 'subdir') |
| 120 | os.mkdir(subdir) |
| 121 | with scoped_file(self.progdir, 'a', ''' |
| 122 | {"include":["subdir/b"]} |
| 123 | ''') as path, \ |
| 124 | scoped_file(subdir, 'b', ''' |
| 125 | {"options": {"foo": "bar"}}'''): |
| 126 | config = configure.ConfigStore(path) |
| 127 | config.load_config() |
| 128 | self.assertEqual(config.get_option('foo'), 'bar') |
| 129 | |
| 130 | def test_plugins(self): |
| 131 | subdir = os.path.join(self.progdir, 'subdir') |
| 132 | os.mkdir(subdir) |
| 133 | |
| 134 | with scoped_file(self.progdir, 'a', ''' |
| 135 | {"include": ["subdir/b.json"], "plugins": ["a.py"] } |
| 136 | ''') as path, \ |
| 137 | scoped_file(self.progdir, 'a.py', textwrap.dedent(''' |
| 138 | from bisect_kit import configure_test |
| 139 | |
| 140 | def loaded(): |
| 141 | configure_test.aaa = 111 |
| 142 | ''')), \ |
| 143 | scoped_file(subdir, 'b.json', ''' |
| 144 | {"plugins": ["b.py"]} |
| 145 | '''), \ |
| 146 | scoped_file(subdir, 'b.py', textwrap.dedent(''' |
| 147 | from bisect_kit import configure_test |
| 148 | |
| 149 | def loaded(): |
| 150 | configure_test.bbb = 222 |
| 151 | ''')): |
| 152 | config = configure.ConfigStore(path) |
| 153 | config.load_config() |
| 154 | config.load_plugins() |
| 155 | self.assertEqual(globals().get('aaa'), 111) |
| 156 | self.assertEqual(globals().get('bbb'), 222) |
| 157 | |
| 158 | |
| 159 | if __name__ == '__main__': |
| 160 | unittest.main() |