@@ -717,3 +717,36 @@ def test_literal_shebang_invalid_template(self):
717
717
f"{ expect } arg1 { script } " ,
718
718
data ["stdout" ].strip (),
719
719
)
720
+
721
+ def test_shebang_command_in_venv (self ):
722
+ stem = "python-that-is-not-on-path"
723
+
724
+ # First ensure that our test name doesn't exist, and the launcher does
725
+ # not match any installed env
726
+ with self .script (f'#! /usr/bin/env { stem } arg1' ) as script :
727
+ data = self .run_py ([script ], expect_returncode = 103 )
728
+
729
+ with self .fake_venv () as (venv_exe , env ):
730
+ # Put a "normal" Python on PATH as a distraction.
731
+ # The active VIRTUAL_ENV should be preferred when the name isn't an
732
+ # exact match.
733
+ exe = Path (Path (venv_exe ).name ).absolute ()
734
+ exe .touch ()
735
+ self .addCleanup (exe .unlink )
736
+ env ["PATH" ] = f"{ exe .parent } ;{ os .environ ['PATH' ]} "
737
+
738
+ with self .script (f'#! /usr/bin/env { stem } arg1' ) as script :
739
+ data = self .run_py ([script ], env = env )
740
+ self .assertEqual (data ["stdout" ].strip (), f"{ quote (venv_exe )} arg1 { quote (script )} " )
741
+
742
+ with self .script (f'#! /usr/bin/env { exe .stem } arg1' ) as script :
743
+ data = self .run_py ([script ], env = env )
744
+ self .assertEqual (data ["stdout" ].strip (), f"{ quote (exe )} arg1 { quote (script )} " )
745
+
746
+ def test_shebang_executable_extension (self ):
747
+ with self .script ('#! /usr/bin/env python3.12' ) as script :
748
+ data = self .run_py ([script ])
749
+ expect = "# Search PATH for python3.12.exe"
750
+ actual = [line .strip () for line in data ["stderr" ].splitlines ()
751
+ if line .startswith ("# Search PATH" )]
752
+ self .assertEqual ([expect ], actual )
0 commit comments