test_lib.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import pytest
  2. import checkpackagelib.test_util as util
  3. import checkpackagelib.lib as m
  4. ConsecutiveEmptyLines = [
  5. ('1 line (no newline)',
  6. 'any',
  7. '',
  8. []),
  9. ('1 line',
  10. 'any',
  11. '\n',
  12. []),
  13. ('2 lines',
  14. 'any',
  15. '\n'
  16. '\n',
  17. [['any:2: consecutive empty lines']]),
  18. ('more than 2 consecutive',
  19. 'any',
  20. '\n'
  21. '\n'
  22. '\n',
  23. [['any:2: consecutive empty lines'],
  24. ['any:3: consecutive empty lines']]),
  25. ('ignore whitespace 1',
  26. 'any',
  27. '\n'
  28. ' ',
  29. [['any:2: consecutive empty lines']]),
  30. ('ignore whitespace 2',
  31. 'any',
  32. ' \n'
  33. '\t\n',
  34. [['any:2: consecutive empty lines']]),
  35. ]
  36. @pytest.mark.parametrize('testname,filename,string,expected', ConsecutiveEmptyLines)
  37. def test_ConsecutiveEmptyLines(testname, filename, string, expected):
  38. warnings = util.check_file(m.ConsecutiveEmptyLines, filename, string)
  39. assert warnings == expected
  40. EmptyLastLine = [
  41. ('ignore empty file',
  42. 'any',
  43. '',
  44. []),
  45. ('empty line (newline)',
  46. 'any',
  47. '\n',
  48. [['any:1: empty line at end of file']]),
  49. ('empty line (space, newline)',
  50. 'any',
  51. ' \n',
  52. [['any:1: empty line at end of file']]),
  53. ('empty line (space, no newline)',
  54. 'any',
  55. ' ',
  56. [['any:1: empty line at end of file']]),
  57. ('warn for the last of 2',
  58. 'any',
  59. '\n'
  60. '\n',
  61. [['any:2: empty line at end of file']]),
  62. ('warn for the last of 3',
  63. 'any',
  64. '\n'
  65. '\n'
  66. '\n',
  67. [['any:3: empty line at end of file']]),
  68. ('ignore whitespace',
  69. 'any',
  70. ' \n'
  71. '\t\n',
  72. [['any:2: empty line at end of file']]),
  73. ]
  74. @pytest.mark.parametrize('testname,filename,string,expected', EmptyLastLine)
  75. def test_EmptyLastLine(testname, filename, string, expected):
  76. warnings = util.check_file(m.EmptyLastLine, filename, string)
  77. assert warnings == expected
  78. NewlineAtEof = [
  79. ('good',
  80. 'any',
  81. 'text\n',
  82. []),
  83. ('text (bad)',
  84. 'any',
  85. '\n'
  86. 'text',
  87. [['any:2: missing newline at end of file',
  88. 'text']]),
  89. ('space (bad)',
  90. 'any',
  91. '\n'
  92. ' ',
  93. [['any:2: missing newline at end of file',
  94. ' ']]),
  95. ('tab (bad)',
  96. 'any',
  97. '\n'
  98. '\t',
  99. [['any:2: missing newline at end of file',
  100. '\t']]),
  101. ('even for file with one line',
  102. 'any',
  103. ' ',
  104. [['any:1: missing newline at end of file',
  105. ' ']]),
  106. ]
  107. @pytest.mark.parametrize('testname,filename,string,expected', NewlineAtEof)
  108. def test_NewlineAtEof(testname, filename, string, expected):
  109. warnings = util.check_file(m.NewlineAtEof, filename, string)
  110. assert warnings == expected
  111. TrailingSpace = [
  112. ('good',
  113. 'any',
  114. 'text\n',
  115. []),
  116. ('ignore missing newline',
  117. 'any',
  118. '\n'
  119. 'text',
  120. []),
  121. ('spaces',
  122. 'any',
  123. 'text \n',
  124. [['any:1: line contains trailing whitespace',
  125. 'text \n']]),
  126. ('tabs after text',
  127. 'any',
  128. 'text\t\t\n',
  129. [['any:1: line contains trailing whitespace',
  130. 'text\t\t\n']]),
  131. ('mix of tabs and spaces',
  132. 'any',
  133. ' \n'
  134. ' ',
  135. [['any:1: line contains trailing whitespace',
  136. ' \n'],
  137. ['any:2: line contains trailing whitespace',
  138. ' ']]),
  139. ('blank line with tabs',
  140. 'any',
  141. '\n'
  142. '\t',
  143. [['any:2: line contains trailing whitespace',
  144. '\t']]),
  145. ]
  146. @pytest.mark.parametrize('testname,filename,string,expected', TrailingSpace)
  147. def test_TrailingSpace(testname, filename, string, expected):
  148. warnings = util.check_file(m.TrailingSpace, filename, string)
  149. assert warnings == expected
  150. Utf8Characters = [
  151. ('usual',
  152. 'any',
  153. 'text\n',
  154. []),
  155. ('acceptable character',
  156. 'any',
  157. '\x60',
  158. []),
  159. ('unacceptable character',
  160. 'any',
  161. '\x81',
  162. [['any:1: line contains UTF-8 characters',
  163. '\x81']]),
  164. ('2 warnings',
  165. 'any',
  166. 'text\n'
  167. 'text \xc8 text\n'
  168. '\xc9\n',
  169. [['any:2: line contains UTF-8 characters',
  170. 'text \xc8 text\n'],
  171. ['any:3: line contains UTF-8 characters',
  172. '\xc9\n']]),
  173. ]
  174. @pytest.mark.parametrize('testname,filename,string,expected', Utf8Characters)
  175. def test_Utf8Characters(testname, filename, string, expected):
  176. warnings = util.check_file(m.Utf8Characters, filename, string)
  177. assert warnings == expected
  178. def test_all_check_functions_are_used():
  179. import inspect
  180. import checkpackagelib.lib_config as lib_config
  181. import checkpackagelib.lib_hash as lib_hash
  182. import checkpackagelib.lib_mk as lib_mk
  183. import checkpackagelib.lib_patch as lib_patch
  184. c_config = [c[0] for c in inspect.getmembers(lib_config, inspect.isclass)]
  185. c_hash = [c[0] for c in inspect.getmembers(lib_hash, inspect.isclass)]
  186. c_mk = [c[0] for c in inspect.getmembers(lib_mk, inspect.isclass)]
  187. c_patch = [c[0] for c in inspect.getmembers(lib_patch, inspect.isclass)]
  188. c_all = c_config + c_hash + c_mk + c_patch
  189. c_common = [c[0] for c in inspect.getmembers(m, inspect.isclass)]
  190. assert set(c_common) <= set(c_all)