mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Be more timid about choosing a shebang
This commit is contained in:
parent
cc1af1da06
commit
4f8a9580aa
2 changed files with 25 additions and 14 deletions
|
|
@ -2,15 +2,14 @@ from __future__ import print_function
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import io
|
import io
|
||||||
|
import itertools
|
||||||
import logging
|
import logging
|
||||||
import os.path
|
import os.path
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import pre_commit.constants as C
|
|
||||||
from pre_commit import git
|
from pre_commit import git
|
||||||
from pre_commit import output
|
from pre_commit import output
|
||||||
from pre_commit.clientlib import load_config
|
from pre_commit.clientlib import load_config
|
||||||
from pre_commit.languages import python
|
|
||||||
from pre_commit.repository import all_hooks
|
from pre_commit.repository import all_hooks
|
||||||
from pre_commit.repository import install_hook_envs
|
from pre_commit.repository import install_hook_envs
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
|
|
@ -51,8 +50,19 @@ def shebang():
|
||||||
if sys.platform == 'win32':
|
if sys.platform == 'win32':
|
||||||
py = 'python'
|
py = 'python'
|
||||||
else:
|
else:
|
||||||
py = python.get_default_version()
|
# Homebrew/homebrew-core#35825: be more timid about appropriate `PATH`
|
||||||
if py == C.DEFAULT:
|
path_choices = [p for p in os.defpath.split(os.pathsep) if p]
|
||||||
|
exe_choices = [
|
||||||
|
'python{}'.format('.'.join(
|
||||||
|
str(v) for v in sys.version_info[:i]
|
||||||
|
))
|
||||||
|
for i in range(3)
|
||||||
|
]
|
||||||
|
for path, exe in itertools.product(path_choices, exe_choices):
|
||||||
|
if os.path.exists(os.path.join(path, exe)):
|
||||||
|
py = exe
|
||||||
|
break
|
||||||
|
else:
|
||||||
py = 'python'
|
py = 'python'
|
||||||
return '#!/usr/bin/env {}'.format(py)
|
return '#!/usr/bin/env {}'.format(py)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ 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 shebang
|
||||||
from pre_commit.commands.install_uninstall import uninstall
|
from pre_commit.commands.install_uninstall import uninstall
|
||||||
from pre_commit.languages import python
|
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
from pre_commit.util import make_executable
|
from pre_commit.util import make_executable
|
||||||
from pre_commit.util import mkdirp
|
from pre_commit.util import mkdirp
|
||||||
|
|
@ -51,19 +50,21 @@ def test_shebang_windows():
|
||||||
assert shebang() == '#!/usr/bin/env python'
|
assert shebang() == '#!/usr/bin/env python'
|
||||||
|
|
||||||
|
|
||||||
def test_shebang_otherwise():
|
def test_shebang_posix_not_on_path():
|
||||||
with mock.patch.object(sys, 'platform', 'posix'):
|
with mock.patch.object(sys, 'platform', 'posix'):
|
||||||
assert C.DEFAULT not in shebang()
|
with mock.patch.object(os, 'defpath', ''):
|
||||||
|
|
||||||
|
|
||||||
def test_shebang_returns_default():
|
|
||||||
with mock.patch.object(sys, 'platform', 'posix'):
|
|
||||||
with mock.patch.object(
|
|
||||||
python, 'get_default_version', return_value=C.DEFAULT,
|
|
||||||
):
|
|
||||||
assert shebang() == '#!/usr/bin/env python'
|
assert shebang() == '#!/usr/bin/env python'
|
||||||
|
|
||||||
|
|
||||||
|
def test_shebang_posix_on_path(tmpdir):
|
||||||
|
tmpdir.join('python{}'.format(sys.version_info[0])).ensure()
|
||||||
|
|
||||||
|
with mock.patch.object(sys, 'platform', 'posix'):
|
||||||
|
with mock.patch.object(os, 'defpath', tmpdir.strpath):
|
||||||
|
expected = '#!/usr/bin/env python{}'.format(sys.version_info[0])
|
||||||
|
assert shebang() == expected
|
||||||
|
|
||||||
|
|
||||||
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)
|
assert not install(C.CONFIG_FILE, store)
|
||||||
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)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue