test_check_kconfigs.py 9.9 KB

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