mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
parent
de177e8850
commit
42b0a263a6
3 changed files with 2 additions and 69 deletions
|
|
@ -1,4 +1,3 @@
|
||||||
import itertools
|
|
||||||
import logging
|
import logging
|
||||||
import os.path
|
import os.path
|
||||||
import shlex
|
import shlex
|
||||||
|
|
@ -31,10 +30,6 @@ PRIOR_HASHES = (
|
||||||
CURRENT_HASH = b'138fd403232d2ddd5efb44317e38bf03'
|
CURRENT_HASH = b'138fd403232d2ddd5efb44317e38bf03'
|
||||||
TEMPLATE_START = '# start templated\n'
|
TEMPLATE_START = '# start templated\n'
|
||||||
TEMPLATE_END = '# end templated\n'
|
TEMPLATE_END = '# end templated\n'
|
||||||
# Homebrew/homebrew-core#35825: be more timid about appropriate `PATH`
|
|
||||||
# #1312 os.defpath is too restrictive on BSD
|
|
||||||
POSIX_SEARCH_PATH = ('/usr/local/bin', '/usr/bin', '/bin')
|
|
||||||
SYS_EXE = os.path.basename(os.path.realpath(sys.executable))
|
|
||||||
|
|
||||||
|
|
||||||
def _hook_paths(
|
def _hook_paths(
|
||||||
|
|
@ -54,26 +49,6 @@ def is_our_script(filename: str) -> bool:
|
||||||
return any(h in contents for h in (CURRENT_HASH,) + PRIOR_HASHES)
|
return any(h in contents for h in (CURRENT_HASH,) + PRIOR_HASHES)
|
||||||
|
|
||||||
|
|
||||||
def shebang() -> str:
|
|
||||||
if sys.platform == 'win32':
|
|
||||||
py, _ = os.path.splitext(SYS_EXE)
|
|
||||||
else:
|
|
||||||
exe_choices = [
|
|
||||||
f'python{sys.version_info[0]}.{sys.version_info[1]}',
|
|
||||||
f'python{sys.version_info[0]}',
|
|
||||||
]
|
|
||||||
# avoid searching for bare `python` as it's likely to be python 2
|
|
||||||
if SYS_EXE != 'python':
|
|
||||||
exe_choices.append(SYS_EXE)
|
|
||||||
for path, exe in itertools.product(POSIX_SEARCH_PATH, exe_choices):
|
|
||||||
if os.access(os.path.join(path, exe), os.X_OK):
|
|
||||||
py = exe
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
py = SYS_EXE
|
|
||||||
return f'#!/usr/bin/env {py}'
|
|
||||||
|
|
||||||
|
|
||||||
def _install_hook_script(
|
def _install_hook_script(
|
||||||
config_file: str,
|
config_file: str,
|
||||||
hook_type: str,
|
hook_type: str,
|
||||||
|
|
|
||||||
|
|
@ -275,7 +275,6 @@ def _run_hooks(
|
||||||
hooks: Sequence[Hook],
|
hooks: Sequence[Hook],
|
||||||
skips: Set[str],
|
skips: Set[str],
|
||||||
args: argparse.Namespace,
|
args: argparse.Namespace,
|
||||||
environ: MutableMapping[str, str],
|
|
||||||
) -> int:
|
) -> int:
|
||||||
"""Actually run the hooks."""
|
"""Actually run the hooks."""
|
||||||
cols = _compute_cols(hooks)
|
cols = _compute_cols(hooks)
|
||||||
|
|
@ -416,7 +415,7 @@ def run(
|
||||||
to_install = [hook for hook in hooks if hook.id not in skips]
|
to_install = [hook for hook in hooks if hook.id not in skips]
|
||||||
install_hook_envs(to_install, store)
|
install_hook_envs(to_install, store)
|
||||||
|
|
||||||
return _run_hooks(config, hooks, skips, args, environ)
|
return _run_hooks(config, hooks, skips, args)
|
||||||
|
|
||||||
# https://github.com/python/mypy/issues/7726
|
# https://github.com/python/mypy/issues/7726
|
||||||
raise AssertionError('unreachable')
|
raise AssertionError('unreachable')
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,15 @@
|
||||||
import os.path
|
import os.path
|
||||||
import re
|
import re
|
||||||
import sys
|
|
||||||
from unittest import mock
|
|
||||||
|
|
||||||
import re_assert
|
import re_assert
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
from pre_commit import git
|
from pre_commit import git
|
||||||
from pre_commit.commands import install_uninstall
|
|
||||||
from pre_commit.commands.install_uninstall import CURRENT_HASH
|
from pre_commit.commands.install_uninstall import CURRENT_HASH
|
||||||
from pre_commit.commands.install_uninstall import install
|
from pre_commit.commands.install_uninstall import install
|
||||||
from pre_commit.commands.install_uninstall import install_hooks
|
from pre_commit.commands.install_uninstall import install_hooks
|
||||||
from pre_commit.commands.install_uninstall import is_our_script
|
from pre_commit.commands.install_uninstall import is_our_script
|
||||||
from pre_commit.commands.install_uninstall import PRIOR_HASHES
|
from pre_commit.commands.install_uninstall import PRIOR_HASHES
|
||||||
from pre_commit.commands.install_uninstall import shebang
|
|
||||||
from pre_commit.commands.install_uninstall import uninstall
|
from pre_commit.commands.install_uninstall import uninstall
|
||||||
from pre_commit.parse_shebang import find_executable
|
from pre_commit.parse_shebang import find_executable
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
|
|
@ -43,43 +39,6 @@ def test_is_previous_pre_commit(tmpdir):
|
||||||
assert is_our_script(f.strpath)
|
assert is_our_script(f.strpath)
|
||||||
|
|
||||||
|
|
||||||
def patch_platform(platform):
|
|
||||||
return mock.patch.object(sys, 'platform', platform)
|
|
||||||
|
|
||||||
|
|
||||||
def patch_lookup_path(path):
|
|
||||||
return mock.patch.object(install_uninstall, 'POSIX_SEARCH_PATH', path)
|
|
||||||
|
|
||||||
|
|
||||||
def patch_sys_exe(exe):
|
|
||||||
return mock.patch.object(install_uninstall, 'SYS_EXE', exe)
|
|
||||||
|
|
||||||
|
|
||||||
def test_shebang_windows():
|
|
||||||
with patch_platform('win32'), patch_sys_exe('python'):
|
|
||||||
assert shebang() == '#!/usr/bin/env python'
|
|
||||||
|
|
||||||
|
|
||||||
def test_shebang_windows_drop_ext():
|
|
||||||
with patch_platform('win32'), patch_sys_exe('python.exe'):
|
|
||||||
assert shebang() == '#!/usr/bin/env python'
|
|
||||||
|
|
||||||
|
|
||||||
def test_shebang_posix_not_on_path():
|
|
||||||
with patch_platform('posix'), patch_lookup_path(()):
|
|
||||||
with patch_sys_exe('python3.6'):
|
|
||||||
assert shebang() == '#!/usr/bin/env python3.6'
|
|
||||||
|
|
||||||
|
|
||||||
def test_shebang_posix_on_path(tmpdir):
|
|
||||||
exe = tmpdir.join(f'python{sys.version_info[0]}').ensure()
|
|
||||||
make_executable(exe)
|
|
||||||
|
|
||||||
with patch_platform('posix'), patch_lookup_path((tmpdir.strpath,)):
|
|
||||||
with patch_sys_exe('python'):
|
|
||||||
assert shebang() == f'#!/usr/bin/env python{sys.version_info[0]}'
|
|
||||||
|
|
||||||
|
|
||||||
def test_install_pre_commit(in_git_dir, store):
|
def test_install_pre_commit(in_git_dir, store):
|
||||||
assert not install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
|
assert not install(C.CONFIG_FILE, store, hook_types=['pre-commit'])
|
||||||
assert os.access(in_git_dir.join('.git/hooks/pre-commit').strpath, os.X_OK)
|
assert os.access(in_git_dir.join('.git/hooks/pre-commit').strpath, os.X_OK)
|
||||||
|
|
@ -336,7 +295,7 @@ EXISTING_COMMIT_RUN = re_assert.Matches(
|
||||||
def _write_legacy_hook(path):
|
def _write_legacy_hook(path):
|
||||||
os.makedirs(os.path.join(path, '.git/hooks'), exist_ok=True)
|
os.makedirs(os.path.join(path, '.git/hooks'), exist_ok=True)
|
||||||
with open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
|
with open(os.path.join(path, '.git/hooks/pre-commit'), 'w') as f:
|
||||||
f.write(f'{shebang()}\nprint("legacy hook")\n')
|
f.write('#!/usr/bin/env bash\necho legacy hook\n')
|
||||||
make_executable(f.name)
|
make_executable(f.name)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue