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

Skip to content

Commit c2e4040

Browse files
committed
Improve not found error with script paths (./exe)
1 parent 8f6fe8c commit c2e4040

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

pre_commit/parse_shebang.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,21 @@ def find_executable(exe, _environ=None):
4242
return None
4343

4444

45-
def normexe(orig_exe):
46-
if os.sep not in orig_exe:
47-
exe = find_executable(orig_exe)
45+
def normexe(orig):
46+
def _error(msg):
47+
raise ExecutableNotFoundError('Executable `{}` {}'.format(orig, msg))
48+
49+
if os.sep not in orig and (not os.altsep or os.altsep not in orig):
50+
exe = find_executable(orig)
4851
if exe is None:
49-
raise ExecutableNotFoundError(
50-
'Executable `{}` not found'.format(orig_exe),
51-
)
52+
_error('not found')
5253
return exe
54+
elif not os.access(orig, os.X_OK):
55+
_error('not found')
56+
elif os.path.isdir(orig):
57+
_error('is a directory')
5358
else:
54-
return orig_exe
59+
return orig
5560

5661

5762
def normalize_cmd(cmd):

tests/parse_shebang_test.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ def test_normexe_does_not_exist():
8585
assert excinfo.value.args == ('Executable `i-dont-exist-lol` not found',)
8686

8787

88+
def test_normexe_does_not_exist_sep():
89+
with pytest.raises(OSError) as excinfo:
90+
parse_shebang.normexe('./i-dont-exist-lol')
91+
assert excinfo.value.args == ('Executable `./i-dont-exist-lol` not found',)
92+
93+
94+
def test_normexe_is_a_directory(tmpdir):
95+
with tmpdir.as_cwd():
96+
tmpdir.join('exe').ensure_dir()
97+
exe = os.path.join('.', 'exe')
98+
with pytest.raises(OSError) as excinfo:
99+
parse_shebang.normexe(exe)
100+
msg, = excinfo.value.args
101+
assert msg == 'Executable `{}` is a directory'.format(exe)
102+
103+
88104
def test_normexe_already_full_path():
89105
assert parse_shebang.normexe(sys.executable) == sys.executable
90106

@@ -107,14 +123,14 @@ def test_normalize_cmd_PATH():
107123

108124

109125
def test_normalize_cmd_shebang(in_tmpdir):
110-
python = distutils.spawn.find_executable('python')
111-
path = write_executable(python.replace(os.sep, '/'))
126+
python = distutils.spawn.find_executable('python').replace(os.sep, '/')
127+
path = write_executable(python)
112128
assert parse_shebang.normalize_cmd((path,)) == (python, path)
113129

114130

115131
def test_normalize_cmd_PATH_shebang_full_path(in_tmpdir):
116-
python = distutils.spawn.find_executable('python')
117-
path = write_executable(python.replace(os.sep, '/'))
132+
python = distutils.spawn.find_executable('python').replace(os.sep, '/')
133+
path = write_executable(python)
118134
with bin_on_path():
119135
ret = parse_shebang.normalize_cmd(('run',))
120136
assert ret == (python, os.path.abspath(path))

0 commit comments

Comments
 (0)