mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-04 19:26:52 +00:00
Support checking executable bit without Git.
The check-shebang-scripts-are-executable hook already avoided false negatives in a Git repository by looking up the Git file mode rather than relying on the file mode in the file system. Git already automatically probes the file system for executable bit support. Use the file mode in the file system when we are not in a Git clone or it is trusted by Git according to its core.fileMode config variable.
This commit is contained in:
parent
9245e07a3e
commit
314fa5366d
2 changed files with 80 additions and 4 deletions
|
|
@ -1,5 +1,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
from pre_commit_hooks.check_shebang_scripts_are_executable import \
|
||||
|
|
@ -7,6 +9,62 @@ from pre_commit_hooks.check_shebang_scripts_are_executable import \
|
|||
from pre_commit_hooks.check_shebang_scripts_are_executable import main
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
|
||||
skip_win32 = pytest.mark.xfail(
|
||||
sys.platform == 'win32',
|
||||
reason="non-git checks aren't relevant on windows",
|
||||
)
|
||||
|
||||
|
||||
@skip_win32 # pragma: win32 no cover
|
||||
@pytest.mark.parametrize(
|
||||
'content', (
|
||||
b'#!/bin/bash\nhello world\n',
|
||||
b'#!/usr/bin/env python3.10',
|
||||
b'#!python',
|
||||
'#!☃'.encode(),
|
||||
),
|
||||
)
|
||||
def test_executable_shebang(content, tmpdir):
|
||||
path = tmpdir.join('path')
|
||||
path.write(content, 'wb')
|
||||
cmd_output('chmod', '+x', path)
|
||||
assert main((str(path),)) == 0
|
||||
|
||||
|
||||
@skip_win32 # pragma: win32 no cover
|
||||
@pytest.mark.parametrize(
|
||||
'content', (
|
||||
b'#!/bin/bash\nhello world\n',
|
||||
b'#!/usr/bin/env python3.10',
|
||||
b'#!python',
|
||||
'#!☃'.encode(),
|
||||
),
|
||||
)
|
||||
def test_not_executable_shebang(content, tmpdir, capsys):
|
||||
path = tmpdir.join('path')
|
||||
path.write(content, 'wb')
|
||||
assert main((str(path),)) == 1
|
||||
_, stderr = capsys.readouterr()
|
||||
assert stderr.startswith(
|
||||
f'{path}: has a shebang but is not marked executable!',
|
||||
)
|
||||
|
||||
|
||||
@skip_win32 # pragma: win32 no cover
|
||||
@pytest.mark.parametrize(
|
||||
'content', (
|
||||
b'',
|
||||
b' #!python\n',
|
||||
b'\n#!python\n',
|
||||
b'python\n',
|
||||
'☃'.encode(),
|
||||
),
|
||||
)
|
||||
def test_not_executable_no_shebang(content, tmpdir, capsys):
|
||||
path = tmpdir.join('path')
|
||||
path.write(content, 'wb')
|
||||
assert main((str(path),)) == 0
|
||||
|
||||
|
||||
def test_check_git_filemode_passing(tmpdir):
|
||||
with tmpdir.as_cwd():
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue