|
| 1 | +import os |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from pre_commit_hooks.check_shebang_scripts_are_executable import \ |
| 6 | + _check_git_filemode |
| 7 | +from pre_commit_hooks.check_shebang_scripts_are_executable import main |
| 8 | +from pre_commit_hooks.util import cmd_output |
| 9 | + |
| 10 | + |
| 11 | +def test_check_git_filemode_passing(tmpdir): |
| 12 | + with tmpdir.as_cwd(): |
| 13 | + cmd_output('git', 'init', '.') |
| 14 | + |
| 15 | + f = tmpdir.join('f') |
| 16 | + f.write('#!/usr/bin/env bash') |
| 17 | + f_path = str(f) |
| 18 | + cmd_output('chmod', '+x', f_path) |
| 19 | + cmd_output('git', 'add', f_path) |
| 20 | + cmd_output('git', 'update-index', '--chmod=+x', f_path) |
| 21 | + |
| 22 | + g = tmpdir.join('g').ensure() |
| 23 | + g_path = str(g) |
| 24 | + cmd_output('git', 'add', g_path) |
| 25 | + |
| 26 | + files = [f_path, g_path] |
| 27 | + assert _check_git_filemode(files) == 0 |
| 28 | + |
| 29 | + # this is the one we should trigger on |
| 30 | + h = tmpdir.join('h') |
| 31 | + h.write('#!/usr/bin/env bash') |
| 32 | + h_path = str(h) |
| 33 | + cmd_output('git', 'add', h_path) |
| 34 | + |
| 35 | + files = [h_path] |
| 36 | + assert _check_git_filemode(files) == 1 |
| 37 | + |
| 38 | + |
| 39 | +def test_check_git_filemode_passing_unusual_characters(tmpdir): |
| 40 | + with tmpdir.as_cwd(): |
| 41 | + cmd_output('git', 'init', '.') |
| 42 | + |
| 43 | + f = tmpdir.join('mañana.txt') |
| 44 | + f.write('#!/usr/bin/env bash') |
| 45 | + f_path = str(f) |
| 46 | + cmd_output('chmod', '+x', f_path) |
| 47 | + cmd_output('git', 'add', f_path) |
| 48 | + cmd_output('git', 'update-index', '--chmod=+x', f_path) |
| 49 | + |
| 50 | + files = (f_path,) |
| 51 | + assert _check_git_filemode(files) == 0 |
| 52 | + |
| 53 | + |
| 54 | +def test_check_git_filemode_failing(tmpdir): |
| 55 | + with tmpdir.as_cwd(): |
| 56 | + cmd_output('git', 'init', '.') |
| 57 | + |
| 58 | + f = tmpdir.join('f').ensure() |
| 59 | + f.write('#!/usr/bin/env bash') |
| 60 | + f_path = str(f) |
| 61 | + cmd_output('git', 'add', f_path) |
| 62 | + |
| 63 | + files = (f_path,) |
| 64 | + assert _check_git_filemode(files) == 1 |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize( |
| 68 | + ('content', 'mode', 'expected'), |
| 69 | + ( |
| 70 | + pytest.param('#!python', '+x', 0, id='shebang with executable'), |
| 71 | + pytest.param('#!python', '-x', 1, id='shebang without executable'), |
| 72 | + pytest.param('', '+x', 0, id='no shebang with executable'), |
| 73 | + pytest.param('', '-x', 0, id='no shebang without executable'), |
| 74 | + ), |
| 75 | +) |
| 76 | +def test_git_executable_shebang(temp_git_dir, content, mode, expected): |
| 77 | + with temp_git_dir.as_cwd(): |
| 78 | + path = temp_git_dir.join('path') |
| 79 | + path.write(content) |
| 80 | + cmd_output('git', 'add', str(path)) |
| 81 | + cmd_output('chmod', mode, str(path)) |
| 82 | + cmd_output('git', 'update-index', f'--chmod={mode}', str(path)) |
| 83 | + |
| 84 | + # simulate how identify chooses that something is executable |
| 85 | + filenames = [path for path in [str(path)] if os.access(path, os.X_OK)] |
| 86 | + |
| 87 | + assert main(filenames) == expected |
0 commit comments