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