blob: 4a482ffaf72e449ef5cb40caf8eac30145bbfc7d [file] [log] [blame]
Kuang-che Wu6e4beca2018-06-27 17:45:02 +08001# -*- coding: utf-8 -*-
Kuang-che Wu385279d2017-09-27 14:48:28 +08002# 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
7from __future__ import print_function
8import contextlib
9import os
10import shutil
11import tempfile
12import textwrap
13import unittest
14
15import mock
16
17from bisect_kit import configure
18
19
20@contextlib.contextmanager
21def 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
42class 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 Wuab72da72018-01-16 18:07:54 +0800119 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 Wu385279d2017-09-27 14:48:28 +0800127 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
168if __name__ == '__main__':
169 unittest.main()