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