test_check_kconfigs.py 9.3 KB

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