gh-83180: Made launcher treat shebang 'python' tags as low priority so that active virtual environments are preferred (GH-108101)

This commit is contained in:
Steve Dower 2023-10-02 13:22:55 +01:00 committed by GitHub
parent 6139bf5e0c
commit 1b3bc610fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 12 deletions

View file

@ -717,3 +717,25 @@ class TestLauncher(unittest.TestCase, RunPyMixin):
f"{expect} arg1 {script}",
data["stdout"].strip(),
)
def test_shebang_command_in_venv(self):
stem = "python-that-is-not-on-path"
# First ensure that our test name doesn't exist, and the launcher does
# not match any installed env
with self.script(f'#! /usr/bin/env {stem} arg1') as script:
data = self.run_py([script], expect_returncode=103)
with self.fake_venv() as (venv_exe, env):
# Put a real Python (ourselves) on PATH as a distraction.
# The active VIRTUAL_ENV should be preferred when the name isn't an
# exact match.
env["PATH"] = f"{Path(sys.executable).parent};{os.environ['PATH']}"
with self.script(f'#! /usr/bin/env {stem} arg1') as script:
data = self.run_py([script], env=env)
self.assertEqual(data["stdout"].strip(), f"{venv_exe} arg1 {script}")
with self.script(f'#! /usr/bin/env {Path(sys.executable).stem} arg1') as script:
data = self.run_py([script], env=env)
self.assertEqual(data["stdout"].strip(), f"{sys.executable} arg1 {script}")