test_check_kconfigs.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 CONFIG_NAME_MAX_LENGTH, IndentAndNameChecker, InputError, LineRuleChecker, SourceChecker
  18. class ApplyLine(object):
  19. def apply_line(self, string):
  20. self.checker.process_line(string + '\n', 0)
  21. def expect_error(self, string, expect, cleanup=None):
  22. try:
  23. with self.assertRaises(InputError) as cm:
  24. self.apply_line(string)
  25. if expect:
  26. self.assertEqual(cm.exception.suggested_line, expect + '\n')
  27. finally:
  28. if cleanup:
  29. # cleanup of the previous failure
  30. self.apply_line(cleanup)
  31. def expt_success(self, string):
  32. self.apply_line(string)
  33. class TestLineRuleChecker(unittest.TestCase, ApplyLine):
  34. def setUp(self):
  35. self.checker = LineRuleChecker('Kconfig')
  36. def tearDown(self):
  37. pass
  38. def test_tabulators(self):
  39. self.expect_error('\ttest', expect=' test')
  40. self.expect_error('\t test', expect=' test')
  41. self.expect_error(' \ttest', expect=' test')
  42. self.expect_error(' \t test', expect=' test')
  43. self.expt_success(' test')
  44. self.expt_success('test')
  45. def test_trailing_whitespaces(self):
  46. self.expect_error(' ', expect='')
  47. self.expect_error(' ', expect='')
  48. self.expect_error('test ', expect='test')
  49. self.expt_success('test')
  50. self.expt_success('')
  51. def test_line_length(self):
  52. self.expect_error('x' * 120, expect=None)
  53. self.expt_success('x' * 119)
  54. self.expt_success('')
  55. def test_backslashes(self):
  56. self.expect_error('test \\', expect=None)
  57. class TestSourceChecker(unittest.TestCase, ApplyLine):
  58. def setUp(self):
  59. self.checker = SourceChecker('Kconfig')
  60. def tearDown(self):
  61. pass
  62. def test_source_file_name(self):
  63. self.expect_error('source "notKconfig.test"', expect='source "Kconfig.notKconfig.test"')
  64. self.expect_error('source "Kconfig"', expect='source "Kconfig.Kconfig"')
  65. self.expt_success('source "Kconfig.in"')
  66. self.expt_success('source "/tmp/Kconfig.test"')
  67. self.expt_success('source "/tmp/Kconfig.in"')
  68. self.expect_error('source"Kconfig.in"', expect='source "Kconfig.in"')
  69. self.expt_success('source "/tmp/Kconfig.in" # comment')
  70. class TestIndentAndNameChecker(unittest.TestCase, ApplyLine):
  71. def setUp(self):
  72. self.checker = IndentAndNameChecker('Kconfig')
  73. self.checker.min_prefix_length = 4
  74. def tearDown(self):
  75. self.checker.__exit__('Kconfig', None, None)
  76. class TestIndent(TestIndentAndNameChecker):
  77. def setUp(self):
  78. super(TestIndent, self).setUp()
  79. self.checker.min_prefix_length = 0 # prefixes are ignored in this test case
  80. def test_indent_characters(self):
  81. self.expt_success('menu "test"')
  82. self.expect_error(' test', expect=' test')
  83. self.expect_error(' test', expect=' test')
  84. self.expect_error(' test', expect=' test')
  85. self.expect_error(' test', expect=' test')
  86. self.expt_success(' test')
  87. self.expt_success(' test2')
  88. self.expt_success(' config')
  89. self.expect_error(' default', expect=' default')
  90. self.expt_success(' help')
  91. self.expect_error(' text', expect=' text')
  92. self.expt_success(' help text')
  93. self.expt_success(' menu')
  94. self.expt_success(' endmenu')
  95. self.expect_error(' choice', expect=' choice', cleanup=' endchoice')
  96. self.expect_error(' choice', expect=' choice', cleanup=' endchoice')
  97. self.expt_success(' choice')
  98. self.expt_success(' endchoice')
  99. self.expt_success(' config')
  100. self.expt_success('endmenu')
  101. def test_help_content(self):
  102. self.expt_success('menu "test"')
  103. self.expt_success(' config')
  104. self.expt_success(' help')
  105. self.expt_success(' description')
  106. self.expt_success(' config keyword in the help')
  107. self.expt_success(' menu keyword in the help')
  108. self.expt_success(' menuconfig keyword in the help')
  109. self.expt_success(' endmenu keyword in the help')
  110. self.expt_success(' endmenu keyword in the help')
  111. self.expt_success('') # newline in help
  112. self.expt_success(' endmenu keyword in the help')
  113. self.expect_error(' menu "real menu with wrong indent"',
  114. expect=' menu "real menu with wrong indent"', cleanup=' endmenu')
  115. self.expt_success('endmenu')
  116. def test_mainmenu(self):
  117. self.expt_success('mainmenu "test"')
  118. self.expect_error('test', expect=' test')
  119. self.expt_success(' not_a_keyword')
  120. self.expt_success(' config')
  121. self.expt_success(' menuconfig')
  122. self.expect_error('test', expect=' test')
  123. self.expect_error(' test', expect=' test')
  124. self.expt_success(' menu')
  125. self.expt_success(' endmenu')
  126. def test_ifendif(self):
  127. self.expt_success('menu "test"')
  128. self.expt_success(' config')
  129. self.expt_success(' help')
  130. self.expect_error(' if', expect=' if', cleanup=' endif')
  131. self.expt_success(' if')
  132. self.expect_error(' config', expect=' config')
  133. self.expt_success(' config')
  134. self.expt_success(' help')
  135. self.expt_success(' endif')
  136. self.expt_success(' config')
  137. self.expt_success('endmenu')
  138. def test_config_without_menu(self):
  139. self.expt_success('menuconfig')
  140. self.expt_success(' help')
  141. self.expt_success(' text')
  142. self.expt_success('')
  143. self.expt_success(' text')
  144. self.expt_success('config')
  145. self.expt_success(' help')
  146. def test_source_after_config(self):
  147. self.expt_success('menuconfig')
  148. self.expt_success(' help')
  149. self.expt_success(' text')
  150. self.expect_error(' source', expect='source')
  151. self.expt_success('source "Kconfig.in"')
  152. def test_comment_after_config(self):
  153. self.expt_success('menuconfig')
  154. self.expt_success(' # comment')
  155. self.expt_success(' help')
  156. self.expt_success(' text')
  157. self.expect_error('# comment', expect=' # comment')
  158. self.expt_success(' # second not realcomment"')
  159. class TestName(TestIndentAndNameChecker):
  160. def setUp(self):
  161. super(TestName, self).setUp()
  162. self.checker.min_prefix_length = 0 # prefixes are ignored in this test case
  163. def test_name_length(self):
  164. max_length = CONFIG_NAME_MAX_LENGTH
  165. too_long = max_length + 1
  166. self.expt_success('menu "test"')
  167. self.expt_success(' config ABC')
  168. self.expt_success(' config ' + ('X' * max_length))
  169. self.expect_error(' config ' + ('X' * too_long), expect=None)
  170. self.expt_success(' menuconfig ' + ('X' * max_length))
  171. self.expect_error(' menuconfig ' + ('X' * too_long), expect=None)
  172. self.expt_success(' choice ' + ('X' * max_length))
  173. self.expect_error(' choice ' + ('X' * too_long), expect=None)
  174. self.expt_success('endmenu')
  175. class TestPrefix(TestIndentAndNameChecker):
  176. def test_prefix_len(self):
  177. self.expt_success('menu "test"')
  178. self.expt_success(' config ABC_1')
  179. self.expt_success(' config ABC_2')
  180. self.expt_success(' config ABC_DEBUG')
  181. self.expt_success(' config ABC_ANOTHER')
  182. self.expt_success('endmenu')
  183. self.expt_success('menu "test2"')
  184. self.expt_success(' config A')
  185. self.expt_success(' config B')
  186. self.expect_error('endmenu', expect=None)
  187. def test_choices(self):
  188. self.expt_success('menu "test"')
  189. self.expt_success(' choice ASSERTION_LEVEL')
  190. self.expt_success(' config ASSERTION_DEBUG')
  191. self.expt_success(' config ASSERTION_RELEASE')
  192. self.expt_success(' menuconfig ASSERTION_XY')
  193. self.expt_success(' endchoice')
  194. self.expt_success(' choice DEBUG')
  195. self.expt_success(' config DE_1')
  196. self.expt_success(' config DE_2')
  197. self.expect_error(' endchoice', expect=None)
  198. self.expect_error('endmenu', expect=None)
  199. def test_nested_menu(self):
  200. self.expt_success('menu "test"')
  201. self.expt_success(' config DOESNT_MATTER')
  202. self.expt_success(' menu "inner menu"')
  203. self.expt_success(' config MENUOP_1')
  204. self.expt_success(' config MENUOP_2')
  205. self.expt_success(' config MENUOP_3')
  206. self.expt_success(' endmenu')
  207. self.expt_success('endmenu')
  208. def test_nested_ifendif(self):
  209. self.expt_success('menu "test"')
  210. self.expt_success(' config MENUOP_1')
  211. self.expt_success(' if MENUOP_1')
  212. self.expt_success(' config MENUOP_2')
  213. self.expt_success(' endif')
  214. self.expt_success('endmenu')
  215. if __name__ == '__main__':
  216. unittest.main()