Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f8a958

Browse files
committed
Be more timid about choosing a shebang
1 parent cc1af1d commit 4f8a958

2 files changed

Lines changed: 23 additions & 12 deletions

File tree

pre_commit/commands/install_uninstall.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
from __future__ import unicode_literals
33

44
import io
5+
import itertools
56
import logging
67
import os.path
78
import sys
89

9-
import pre_commit.constants as C
1010
from pre_commit import git
1111
from pre_commit import output
1212
from pre_commit.clientlib import load_config
13-
from pre_commit.languages import python
1413
from pre_commit.repository import all_hooks
1514
from pre_commit.repository import install_hook_envs
1615
from pre_commit.util import cmd_output
@@ -51,8 +50,19 @@ def shebang():
5150
if sys.platform == 'win32':
5251
py = 'python'
5352
else:
54-
py = python.get_default_version()
55-
if py == C.DEFAULT:
53+
# Homebrew/homebrew-core#35825: be more timid about appropriate `PATH`
54+
path_choices = [p for p in os.defpath.split(os.pathsep) if p]
55+
exe_choices = [
56+
'python{}'.format('.'.join(
57+
str(v) for v in sys.version_info[:i]
58+
))
59+
for i in range(3)
60+
]
61+
for path, exe in itertools.product(path_choices, exe_choices):
62+
if os.path.exists(os.path.join(path, exe)):
63+
py = exe
64+
break
65+
else:
5666
py = 'python'
5767
return '#!/usr/bin/env {}'.format(py)
5868

tests/commands/install_uninstall_test.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from pre_commit.commands.install_uninstall import PRIOR_HASHES
1919
from pre_commit.commands.install_uninstall import shebang
2020
from pre_commit.commands.install_uninstall import uninstall
21-
from pre_commit.languages import python
2221
from pre_commit.util import cmd_output
2322
from pre_commit.util import make_executable
2423
from pre_commit.util import mkdirp
@@ -51,17 +50,19 @@ def test_shebang_windows():
5150
assert shebang() == '#!/usr/bin/env python'
5251

5352

54-
def test_shebang_otherwise():
53+
def test_shebang_posix_not_on_path():
5554
with mock.patch.object(sys, 'platform', 'posix'):
56-
assert C.DEFAULT not in shebang()
55+
with mock.patch.object(os, 'defpath', ''):
56+
assert shebang() == '#!/usr/bin/env python'
57+
5758

59+
def test_shebang_posix_on_path(tmpdir):
60+
tmpdir.join('python{}'.format(sys.version_info[0])).ensure()
5861

59-
def test_shebang_returns_default():
6062
with mock.patch.object(sys, 'platform', 'posix'):
61-
with mock.patch.object(
62-
python, 'get_default_version', return_value=C.DEFAULT,
63-
):
64-
assert shebang() == '#!/usr/bin/env python'
63+
with mock.patch.object(os, 'defpath', tmpdir.strpath):
64+
expected = '#!/usr/bin/env python{}'.format(sys.version_info[0])
65+
assert shebang() == expected
6566

6667

6768
def test_install_pre_commit(in_git_dir, store):

0 commit comments

Comments
 (0)