mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-09 21:04:17 +00:00
Merge 314fa5366d into c03226380c
This commit is contained in:
commit
e6285b9867
2 changed files with 81 additions and 9 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import os
|
||||||
import shlex
|
import shlex
|
||||||
import sys
|
import sys
|
||||||
from typing import Sequence
|
from typing import Sequence
|
||||||
|
|
@ -9,13 +10,30 @@ from typing import Sequence
|
||||||
from pre_commit_hooks.check_executables_have_shebangs import EXECUTABLE_VALUES
|
from pre_commit_hooks.check_executables_have_shebangs import EXECUTABLE_VALUES
|
||||||
from pre_commit_hooks.check_executables_have_shebangs import git_ls_files
|
from pre_commit_hooks.check_executables_have_shebangs import git_ls_files
|
||||||
from pre_commit_hooks.check_executables_have_shebangs import has_shebang
|
from pre_commit_hooks.check_executables_have_shebangs import has_shebang
|
||||||
|
from pre_commit_hooks.util import cmd_output
|
||||||
|
|
||||||
|
|
||||||
def check_shebangs(paths: list[str]) -> int:
|
def check_shebangs(paths: list[str]) -> int:
|
||||||
# Cannot optimize on non-executability here if we intend this check to
|
fs_tracks_executable_bit = cmd_output(
|
||||||
# work on win32 -- and that's where problems caused by non-executability
|
'git', 'config', 'core.fileMode', retcode=None,
|
||||||
# (elsewhere) are most likely to arise from.
|
).strip()
|
||||||
return _check_git_filemode(paths)
|
return (
|
||||||
|
_check_git_filemode(paths)
|
||||||
|
if fs_tracks_executable_bit == 'false'
|
||||||
|
else _check_fs_filemode(paths)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _check_fs_filemode(
|
||||||
|
paths: list[str],
|
||||||
|
) -> int: # pragma: win32 no cover
|
||||||
|
retv = 0
|
||||||
|
for path in paths:
|
||||||
|
if not os.access(path, os.X_OK) and has_shebang(path):
|
||||||
|
_message(path)
|
||||||
|
retv = 1
|
||||||
|
|
||||||
|
return retv
|
||||||
|
|
||||||
|
|
||||||
def _check_git_filemode(paths: Sequence[str]) -> int:
|
def _check_git_filemode(paths: Sequence[str]) -> int:
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import sys
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
@ -9,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.check_shebang_scripts_are_executable import main
|
||||||
from pre_commit_hooks.util import cmd_output
|
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):
|
def test_check_git_filemode_passing(tmpdir):
|
||||||
with tmpdir.as_cwd():
|
with tmpdir.as_cwd():
|
||||||
|
|
@ -83,7 +139,5 @@ def test_git_executable_shebang(temp_git_dir, content, mode, expected):
|
||||||
cmd_output('chmod', mode, str(path))
|
cmd_output('chmod', mode, str(path))
|
||||||
cmd_output('git', 'update-index', f'--chmod={mode}', str(path))
|
cmd_output('git', 'update-index', f'--chmod={mode}', str(path))
|
||||||
|
|
||||||
# simulate how identify chooses that something is executable
|
files = (str(path),)
|
||||||
filenames = [path for path in [str(path)] if os.access(path, os.X_OK)]
|
assert main(files) == expected
|
||||||
|
|
||||||
assert main(filenames) == expected
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue