Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sh.py should not try to execute a folder #189

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ def get_rc_exc(rc):

def which(program):
def is_exe(fpath):
return os.path.exists(fpath) and os.access(fpath, os.X_OK)
return (os.path.exists(fpath) and
os.access(fpath, os.X_OK) and
os.path.isfile(os.path.realpath(fpath)))

fpath, fname = os.path.split(program)
if fpath:
Expand Down
29 changes: 29 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,35 @@ def test_command_wrapper_equivalence(self):

self.assertEqual(Command(which("ls")), ls)

def test_doesnt_execute_directories(self):
import tempfile
save_path = os.environ['PATH']
bin_dir1 = tempfile.mkdtemp()
bin_dir2 = tempfile.mkdtemp()
gcc_dir1 = os.path.join(bin_dir1, 'gcc')
gcc_file2 = os.path.join(bin_dir2, 'gcc')
try:
os.environ['PATH'] = os.pathsep.join((bin_dir1, bin_dir2))
# a folder named 'gcc', its executable, but should not be
# discovered by internal which(1)-clone
os.makedirs(gcc_dir1)
# an executable named gcc -- only this should be executed
bunk_header = u'#!/bin/sh\necho $*'.encode('ascii')
open(gcc_file2, 'w').write(bunk_header)
os.chmod(gcc_file2, 0755)
from sh import gcc
self.assertEqual(gcc._path, gcc_file2)
self.assertEqual(gcc('no-error').stdout.strip(), u'no-error')
finally:
os.environ['PATH'] = save_path
if os.path.exists(gcc_file2):
os.unlink(gcc_file2)
if os.path.exists(gcc_dir1):
os.rmdir(gcc_dir1)
if os.path.exists(bin_dir1):
os.rmdir(bin_dir1)
if os.path.exists(bin_dir1):
os.rmdir(bin_dir2)

def test_multiple_args_short_option(self):
py = create_tmp_test("""
Expand Down