Skip to content

Commit 1b90a4b

Browse files
committed
python/shebang: fix broken shebangs like #!python
Signed-off-by: botantony <[email protected]>
1 parent b337351 commit 1b90a4b

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

Library/Homebrew/language/python.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ module Shebang
112112
module_function
113113

114114
# A regex to match potential shebang permutations.
115-
PYTHON_SHEBANG_REGEX = %r{^#! ?/usr/bin/(?:env )?python(?:[23](?:\.\d{1,2})?)?( |$)}
115+
PYTHON_SHEBANG_REGEX = %r{^#! ?(?:/usr/bin/(?:env )?)?python(?:[23](?:\.\d{1,2})?)?( |$)}
116116

117117
# The length of the longest shebang matching `SHEBANG_REGEX`.
118118
PYTHON_SHEBANG_MAX_LENGTH = T.let("#! /usr/bin/env pythonx.yyy ".length, Integer)

Library/Homebrew/test/language/python/shebang_spec.rb

+24-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
RSpec.describe Language::Python::Shebang do
77
let(:file) { Tempfile.new("python-shebang") }
8+
let(:broken_file) { Tempfile.new("python-shebang") }
89
let(:f) do
910
f = {}
1011

@@ -40,9 +41,17 @@
4041
c
4142
EOS
4243
file.flush
44+
45+
broken_file.write <<~EOS
46+
#!python
47+
a
48+
b
49+
c
50+
EOS
51+
broken_file.flush
4352
end
4453

45-
after { file.unlink }
54+
after { [file, broken_file].each(&:unlink) }
4655

4756
describe "#detected_python_shebang" do
4857
it "can be used to replace Python shebangs" do
@@ -72,6 +81,20 @@
7281
EOS
7382
end
7483

84+
it "can replace `#!python` shebang" do
85+
Utils::Shebang.rewrite_shebang(
86+
described_class.detected_python_shebang(f[:versioned_python_dep],
87+
use_python_from_path: true), broken_file.path
88+
)
89+
90+
expect(File.read(broken_file)).to eq <<~EOS
91+
#!/usr/bin/env python3
92+
a
93+
b
94+
c
95+
EOS
96+
end
97+
7598
it "errors if formula doesn't depend on python" do
7699
expect do
77100
Utils::Shebang.rewrite_shebang(

0 commit comments

Comments
 (0)