mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-04 11:16:53 +00:00
check_shebang_scripts_are_executable_test.test_git_executable_shebang manually filtered executable files out before calling check_shebang_scripts_are_executable. This makes sense in check_executables_have_shebangs.test_git_executable_shebang, because the check-executables-have-shebangs hook only runs on executable files. However, check-shebang-scripts-are-executable correctly runs on all text files, so the test shouldn't filter executable files out. The test still passed because when git ls-files is passed no files in particular, it lists all files in the Git repository that satisfy the given filters.
85 lines
2.5 KiB
Python
85 lines
2.5 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from pre_commit_hooks.check_shebang_scripts_are_executable import \
|
|
_check_git_filemode
|
|
from pre_commit_hooks.check_shebang_scripts_are_executable import main
|
|
from pre_commit_hooks.util import cmd_output
|
|
|
|
|
|
def test_check_git_filemode_passing(tmpdir):
|
|
with tmpdir.as_cwd():
|
|
cmd_output('git', 'init', '.')
|
|
|
|
f = tmpdir.join('f')
|
|
f.write('#!/usr/bin/env bash')
|
|
f_path = str(f)
|
|
cmd_output('chmod', '+x', f_path)
|
|
cmd_output('git', 'add', f_path)
|
|
cmd_output('git', 'update-index', '--chmod=+x', f_path)
|
|
|
|
g = tmpdir.join('g').ensure()
|
|
g_path = str(g)
|
|
cmd_output('git', 'add', g_path)
|
|
|
|
files = [f_path, g_path]
|
|
assert _check_git_filemode(files) == 0
|
|
|
|
# this is the one we should trigger on
|
|
h = tmpdir.join('h')
|
|
h.write('#!/usr/bin/env bash')
|
|
h_path = str(h)
|
|
cmd_output('git', 'add', h_path)
|
|
|
|
files = [h_path]
|
|
assert _check_git_filemode(files) == 1
|
|
|
|
|
|
def test_check_git_filemode_passing_unusual_characters(tmpdir):
|
|
with tmpdir.as_cwd():
|
|
cmd_output('git', 'init', '.')
|
|
|
|
f = tmpdir.join('maƱana.txt')
|
|
f.write('#!/usr/bin/env bash')
|
|
f_path = str(f)
|
|
cmd_output('chmod', '+x', f_path)
|
|
cmd_output('git', 'add', f_path)
|
|
cmd_output('git', 'update-index', '--chmod=+x', f_path)
|
|
|
|
files = (f_path,)
|
|
assert _check_git_filemode(files) == 0
|
|
|
|
|
|
def test_check_git_filemode_failing(tmpdir):
|
|
with tmpdir.as_cwd():
|
|
cmd_output('git', 'init', '.')
|
|
|
|
f = tmpdir.join('f').ensure()
|
|
f.write('#!/usr/bin/env bash')
|
|
f_path = str(f)
|
|
cmd_output('git', 'add', f_path)
|
|
|
|
files = (f_path,)
|
|
assert _check_git_filemode(files) == 1
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
('content', 'mode', 'expected'),
|
|
(
|
|
pytest.param('#!python', '+x', 0, id='shebang with executable'),
|
|
pytest.param('#!python', '-x', 1, id='shebang without executable'),
|
|
pytest.param('', '+x', 0, id='no shebang with executable'),
|
|
pytest.param('', '-x', 0, id='no shebang without executable'),
|
|
),
|
|
)
|
|
def test_git_executable_shebang(temp_git_dir, content, mode, expected):
|
|
with temp_git_dir.as_cwd():
|
|
path = temp_git_dir.join('path')
|
|
path.write(content)
|
|
cmd_output('git', 'add', str(path))
|
|
cmd_output('chmod', mode, str(path))
|
|
cmd_output('git', 'update-index', f'--chmod={mode}', str(path))
|
|
|
|
files = (str(path),)
|
|
assert main(files) == expected
|