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

Fix failures with long Python interpreter paths #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion testpath/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ def __init__(self, name, content=None, python=''):
self.command_dir = tempfile.mkdtemp()

if content is None:
# Shebangs have a limited length, so symlink the Python executable
# to a short name that can't conflict with the real command.
tmp_python = os.path.join(self.command_dir, 'python-' + name)
os.symlink(sys.executable, tmp_python)
content = _record_run.format(
python=sys.executable, recording_file=self.recording_file,
python=tmp_python, recording_file=self.recording_file,
extra_code=python,
)
elif python:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os
from subprocess import call, check_output
import unittest
import shutil
import sys
import tempfile

from testpath.commands import *

Expand Down Expand Up @@ -35,6 +38,26 @@ def test_assert_calls_twice(self):
with assert_calls('git'):
pass

def test_assert_calls_long_python_path(self):
current_python = sys.executable
tmp = tempfile.mkdtemp()
try:
tmp_python_dir = tmp
for _ in range(10):
tmp_python_dir = os.path.join(tmp_python_dir, 'looooooooooooooooooooooooooooooooooooooooooong_segment')
os.makedirs(tmp_python_dir)
tmp_python = os.path.join(tmp_python_dir, 'python')
os.symlink(sys.executable, tmp_python)
sys.executable = tmp_python

with assert_calls('i_dont_exist'):
call(['i_dont_exist'])
with assert_calls('git'):
call(['git'])
finally:
sys.executable = current_python
shutil.rmtree(tmp, ignore_errors=True)

def test_fixed_output():
t = 'Sat 24 Apr 17:11:58 BST 2021\n'
with MockCommand.fixed_output('date', t) as mock_date:
Expand Down