forked from espressif/ESP8266_RTOS_SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_check_kconfigs.py
executable file
·258 lines (220 loc) · 9.94 KB
/
test_check_kconfigs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python
#
# Copyright 2018 Espressif Systems (Shanghai) PTE LTD
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from check_kconfigs import LineRuleChecker
from check_kconfigs import SourceChecker
from check_kconfigs import InputError
from check_kconfigs import IndentAndNameChecker
from check_kconfigs import CONFIG_NAME_MAX_LENGTH
class ApplyLine(object):
def apply_line(self, string):
self.checker.process_line(string + '\n', 0)
def expect_error(self, string, expect, cleanup=None):
try:
with self.assertRaises(InputError) as cm:
self.apply_line(string)
if expect:
self.assertEqual(cm.exception.suggested_line, expect + '\n')
finally:
if cleanup:
# cleanup of the previous failure
self.apply_line(cleanup)
def expt_success(self, string):
self.apply_line(string)
class TestLineRuleChecker(unittest.TestCase, ApplyLine):
def setUp(self):
self.checker = LineRuleChecker('Kconfig')
def tearDown(self):
pass
def test_tabulators(self):
self.expect_error('\ttest', expect=' test')
self.expect_error('\t test', expect=' test')
self.expect_error(' \ttest', expect=' test')
self.expect_error(' \t test', expect=' test')
self.expt_success(' test')
self.expt_success('test')
def test_trailing_whitespaces(self):
self.expect_error(' ', expect='')
self.expect_error(' ', expect='')
self.expect_error('test ', expect='test')
self.expt_success('test')
self.expt_success('')
def test_line_length(self):
self.expect_error('x' * 120, expect=None)
self.expt_success('x' * 119)
self.expt_success('')
def test_backslashes(self):
self.expect_error('test \\', expect=None)
class TestSourceChecker(unittest.TestCase, ApplyLine):
def setUp(self):
self.checker = SourceChecker('Kconfig')
def tearDown(self):
pass
def test_source_file_name(self):
self.expect_error('source "Kconfig.test"', expect='source "Kconfig.in"')
self.expect_error('source "/tmp/Kconfig.test"', expect='source "/tmp/Kconfig.in"')
self.expect_error('source "Kconfig"', expect='source "Kconfig.in"')
self.expt_success('source "Kconfig.in"')
self.expt_success('source "/tmp/Kconfig.in"')
self.expect_error('source"Kconfig.in"', expect='source "Kconfig.in"')
self.expt_success('source "/tmp/Kconfig.in" # comment')
class TestIndentAndNameChecker(unittest.TestCase, ApplyLine):
def setUp(self):
self.checker = IndentAndNameChecker('Kconfig')
self.checker.min_prefix_length = 4
def tearDown(self):
self.checker.__exit__('Kconfig', None, None)
class TestIndent(TestIndentAndNameChecker):
def setUp(self):
super(TestIndent, self).setUp()
self.checker.min_prefix_length = 0 # prefixes are ignored in this test case
def test_indent_characters(self):
self.expt_success('menu "test"')
self.expect_error(' test', expect=' test')
self.expect_error(' test', expect=' test')
self.expect_error(' test', expect=' test')
self.expect_error(' test', expect=' test')
self.expt_success(' test')
self.expt_success(' test2')
self.expt_success(' config')
self.expect_error(' default', expect=' default')
self.expt_success(' help')
self.expect_error(' text', expect=' text')
self.expt_success(' help text')
self.expt_success(' menu')
self.expt_success(' endmenu')
self.expect_error(' choice', expect=' choice', cleanup=' endchoice')
self.expect_error(' choice', expect=' choice', cleanup=' endchoice')
self.expt_success(' choice')
self.expt_success(' endchoice')
self.expt_success(' config')
self.expt_success('endmenu')
def test_help_content(self):
self.expt_success('menu "test"')
self.expt_success(' config')
self.expt_success(' help')
self.expt_success(' description')
self.expt_success(' config keyword in the help')
self.expt_success(' menu keyword in the help')
self.expt_success(' menuconfig keyword in the help')
self.expt_success(' endmenu keyword in the help')
self.expt_success(' endmenu keyword in the help')
self.expt_success('') # newline in help
self.expt_success(' endmenu keyword in the help')
self.expect_error(' menu "real menu with wrong indent"',
expect=' menu "real menu with wrong indent"', cleanup=' endmenu')
self.expt_success('endmenu')
def test_mainmenu(self):
self.expt_success('mainmenu "test"')
self.expect_error('test', expect=' test')
self.expt_success(' not_a_keyword')
self.expt_success(' config')
self.expt_success(' menuconfig')
self.expect_error('test', expect=' test')
self.expect_error(' test', expect=' test')
self.expt_success(' menu')
self.expt_success(' endmenu')
def test_ifendif(self):
self.expt_success('menu "test"')
self.expt_success(' config')
self.expt_success(' help')
self.expect_error(' if', expect=' if', cleanup=' endif')
self.expt_success(' if')
self.expect_error(' config', expect=' config')
self.expt_success(' config')
self.expt_success(' help')
self.expt_success(' endif')
self.expt_success(' config')
self.expt_success('endmenu')
def test_config_without_menu(self):
self.expt_success('menuconfig')
self.expt_success(' help')
self.expt_success(' text')
self.expt_success('')
self.expt_success(' text')
self.expt_success('config')
self.expt_success(' help')
def test_source_after_config(self):
self.expt_success('menuconfig')
self.expt_success(' help')
self.expt_success(' text')
self.expect_error(' source', expect='source')
self.expt_success('source "Kconfig.in"')
def test_comment_after_config(self):
self.expt_success('menuconfig')
self.expt_success(' # comment')
self.expt_success(' help')
self.expt_success(' text')
self.expect_error('# comment', expect=' # comment')
self.expt_success(' # second not realcomment"')
class TestName(TestIndentAndNameChecker):
def setUp(self):
super(TestName, self).setUp()
self.checker.min_prefix_length = 0 # prefixes are ignored in this test case
def test_name_length(self):
max_length = CONFIG_NAME_MAX_LENGTH
too_long = max_length + 1
self.expt_success('menu "test"')
self.expt_success(' config ABC')
self.expt_success(' config ' + ('X' * max_length))
self.expect_error(' config ' + ('X' * too_long), expect=None)
self.expt_success(' menuconfig ' + ('X' * max_length))
self.expect_error(' menuconfig ' + ('X' * too_long), expect=None)
self.expt_success(' choice ' + ('X' * max_length))
self.expect_error(' choice ' + ('X' * too_long), expect=None)
self.expt_success('endmenu')
class TestPrefix(TestIndentAndNameChecker):
def test_prefix_len(self):
self.expt_success('menu "test"')
self.expt_success(' config ABC_1')
self.expt_success(' config ABC_2')
self.expt_success(' config ABC_DEBUG')
self.expt_success(' config ABC_ANOTHER')
self.expt_success('endmenu')
self.expt_success('menu "test2"')
self.expt_success(' config A')
self.expt_success(' config B')
self.expect_error('endmenu', expect=None)
def test_choices(self):
self.expt_success('menu "test"')
self.expt_success(' choice ASSERTION_LEVEL')
self.expt_success(' config ASSERTION_DEBUG')
self.expt_success(' config ASSERTION_RELEASE')
self.expt_success(' menuconfig ASSERTION_XY')
self.expt_success(' endchoice')
self.expt_success(' choice DEBUG')
self.expt_success(' config DE_1')
self.expt_success(' config DE_2')
self.expect_error(' endchoice', expect=None)
self.expect_error('endmenu', expect=None)
def test_nested_menu(self):
self.expt_success('menu "test"')
self.expt_success(' config DOESNT_MATTER')
self.expt_success(' menu "inner menu"')
self.expt_success(' config MENUOP_1')
self.expt_success(' config MENUOP_2')
self.expt_success(' config MENUOP_3')
self.expt_success(' endmenu')
self.expt_success('endmenu')
def test_nested_ifendif(self):
self.expt_success('menu "test"')
self.expt_success(' config MENUOP_1')
self.expt_success(' if MENUOP_1')
self.expt_success(' config MENUOP_2')
self.expt_success(' endif')
self.expt_success('endmenu')
if __name__ == '__main__':
unittest.main()