2
1

test_tool.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import os
  2. import pytest
  3. import re
  4. import tempfile
  5. import checkpackagelib.tool as m
  6. workdir_regex = re.compile(r'/tmp/tmp[^/]*-checkpackagelib-test-tool')
  7. def check_file(tool, filename, string, permissions=None):
  8. with tempfile.TemporaryDirectory(suffix='-checkpackagelib-test-tool') as workdir:
  9. script = os.path.join(workdir, filename)
  10. with open(script, 'wb') as f:
  11. f.write(string.encode())
  12. if permissions:
  13. os.chmod(script, permissions)
  14. obj = tool(script)
  15. result = obj.run()
  16. if result is None:
  17. return []
  18. return [workdir_regex.sub('dir', r) for r in result]
  19. NotExecutable = [
  20. ('664',
  21. 'package.mk',
  22. 0o664,
  23. '',
  24. []),
  25. ('775',
  26. 'package.mk',
  27. 0o775,
  28. '',
  29. ["dir/package.mk:0: This file does not need to be executable"]),
  30. ]
  31. @pytest.mark.parametrize('testname,filename,permissions,string,expected', NotExecutable)
  32. def test_NotExecutable(testname, filename, permissions, string, expected):
  33. warnings = check_file(m.NotExecutable, filename, string, permissions)
  34. assert warnings == expected
  35. NotExecutable_hint = [
  36. ('no hint',
  37. "",
  38. 'sh-shebang.sh',
  39. 0o775,
  40. '#!/bin/sh',
  41. ["dir/sh-shebang.sh:0: This file does not need to be executable"]),
  42. ('hint',
  43. ", very special hint",
  44. 'sh-shebang.sh',
  45. 0o775,
  46. '#!/bin/sh',
  47. ["dir/sh-shebang.sh:0: This file does not need to be executable, very special hint"]),
  48. ]
  49. @pytest.mark.parametrize('testname,hint,filename,permissions,string,expected', NotExecutable_hint)
  50. def test_NotExecutable_hint(testname, hint, filename, permissions, string, expected):
  51. class NotExecutable(m.NotExecutable):
  52. def hint(self):
  53. return hint
  54. warnings = check_file(NotExecutable, filename, string, permissions)
  55. assert warnings == expected
  56. Flake8 = [
  57. ('empty',
  58. 'empty.py',
  59. '',
  60. []),
  61. ('W391',
  62. 'blank-line.py',
  63. '\n',
  64. ["dir/blank-line.py:0: run 'flake8' and fix the warnings",
  65. "dir/blank-line.py:1:1: W391 blank line at end of file"]),
  66. ('more than one warning',
  67. 'file',
  68. 'import os\n'
  69. 'import re\n'
  70. '\n',
  71. ["dir/file:0: run 'flake8' and fix the warnings",
  72. "dir/file:1:1: F401 'os' imported but unused\n"
  73. "dir/file:2:1: F401 're' imported but unused\n"
  74. 'dir/file:3:1: W391 blank line at end of file']),
  75. ]
  76. @pytest.mark.parametrize('testname,filename,string,expected', Flake8)
  77. def test_Flake8(testname, filename, string, expected):
  78. warnings = check_file(m.Flake8, filename, string)
  79. assert warnings == expected
  80. Shellcheck = [
  81. ('missing shebang',
  82. 'empty.sh',
  83. '',
  84. ["dir/empty.sh:0: run 'shellcheck' and fix the warnings",
  85. "In dir/empty.sh line 1:\n"
  86. "^-- SC2148 (error): Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.\n"
  87. "For more information:\n"
  88. " https://www.shellcheck.net/wiki/SC2148 -- Tips depend on target shell and y..."]),
  89. ('sh shebang',
  90. 'sh-shebang.sh',
  91. '#!/bin/sh',
  92. []),
  93. ('bash shebang',
  94. 'bash-shebang.sh',
  95. '#!/bin/bash',
  96. []),
  97. ('2 warnings',
  98. 'unused.sh',
  99. 'unused=""',
  100. ["dir/unused.sh:0: run 'shellcheck' and fix the warnings",
  101. "In dir/unused.sh line 1:\n"
  102. 'unused=""\n'
  103. "^-- SC2148 (error): Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.\n"
  104. "^----^ SC2034 (warning): unused appears unused. Verify use (or export if used externally).\n"
  105. "For more information:\n"
  106. " https://www.shellcheck.net/wiki/SC2148 -- Tips depend on target shell and y...\n"
  107. " https://www.shellcheck.net/wiki/SC2034 -- unused appears unused. Verify use..."]),
  108. ('tab',
  109. 'tab.sh',
  110. '\t#!/bin/sh',
  111. ["dir/tab.sh:0: run 'shellcheck' and fix the warnings",
  112. "In dir/tab.sh line 1:\n"
  113. '\t#!/bin/sh\n'
  114. "^-- SC1114 (error): Remove leading spaces before the shebang.\n"
  115. "For more information:\n"
  116. " https://www.shellcheck.net/wiki/SC1114 -- Remove leading spaces before the ..."]),
  117. ]
  118. @pytest.mark.parametrize('testname,filename,string,expected', Shellcheck)
  119. def test_Shellcheck(testname, filename, string, expected):
  120. warnings = check_file(m.Shellcheck, filename, string)
  121. assert warnings == expected