2
1

test_tool.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. Shellcheck = [
  57. ('missing shebang',
  58. 'empty.sh',
  59. '',
  60. ["dir/empty.sh:0: run 'shellcheck' and fix the warnings",
  61. "In dir/empty.sh line 1:",
  62. "^-- SC2148: Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.",
  63. "For more information:",
  64. " https://www.shellcheck.net/wiki/SC2148 -- Tips depend on target shell and y..."]),
  65. ('sh shebang',
  66. 'sh-shebang.sh',
  67. '#!/bin/sh',
  68. []),
  69. ('bash shebang',
  70. 'bash-shebang.sh',
  71. '#!/bin/bash',
  72. []),
  73. ('2 warnings',
  74. 'unused.sh',
  75. 'unused=""',
  76. ["dir/unused.sh:0: run 'shellcheck' and fix the warnings",
  77. "In dir/unused.sh line 1:",
  78. 'unused=""',
  79. "^-- SC2148: Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.",
  80. "^----^ SC2034: unused appears unused. Verify use (or export if used externally).",
  81. "For more information:",
  82. " https://www.shellcheck.net/wiki/SC2148 -- Tips depend on target shell and y...",
  83. " https://www.shellcheck.net/wiki/SC2034 -- unused appears unused. Verify use..."]),
  84. ('tab',
  85. 'tab.sh',
  86. '\t#!/bin/sh',
  87. ["dir/tab.sh:0: run 'shellcheck' and fix the warnings",
  88. "In dir/tab.sh line 1:",
  89. '\t#!/bin/sh',
  90. "^-- SC1114: Remove leading spaces before the shebang.",
  91. "For more information:",
  92. " https://www.shellcheck.net/wiki/SC1114 -- Remove leading spaces before the ..."]),
  93. ]
  94. @pytest.mark.parametrize('testname,filename,string,expected', Shellcheck)
  95. def test_Shellcheck(testname, filename, string, expected):
  96. warnings = check_file(m.Shellcheck, filename, string)
  97. assert warnings == expected