Merge pull request #19563 from botantony/python-shebang
shebangs: fix broken shebangs like `#!python`
This commit is contained in:
commit
6f04ee4cff
@ -96,7 +96,7 @@ module Language
|
|||||||
module_function
|
module_function
|
||||||
|
|
||||||
# A regex to match potential shebang permutations.
|
# A regex to match potential shebang permutations.
|
||||||
NODE_SHEBANG_REGEX = %r{^#! ?/usr/bin/(?:env )?node( |$)}
|
NODE_SHEBANG_REGEX = %r{^#! ?(?:/usr/bin/(?:env )?)?node( |$)}
|
||||||
|
|
||||||
# The length of the longest shebang matching `SHEBANG_REGEX`.
|
# The length of the longest shebang matching `SHEBANG_REGEX`.
|
||||||
NODE_SHEBANG_MAX_LENGTH = T.let("#! /usr/bin/env node ".length, Integer)
|
NODE_SHEBANG_MAX_LENGTH = T.let("#! /usr/bin/env node ".length, Integer)
|
||||||
|
@ -15,7 +15,7 @@ module Language
|
|||||||
module_function
|
module_function
|
||||||
|
|
||||||
# A regex to match potential shebang permutations.
|
# A regex to match potential shebang permutations.
|
||||||
PERL_SHEBANG_REGEX = %r{^#! ?/usr/bin/(?:env )?perl( |$)}
|
PERL_SHEBANG_REGEX = %r{^#! ?(?:/usr/bin/(?:env )?)?perl( |$)}
|
||||||
|
|
||||||
# The length of the longest shebang matching `SHEBANG_REGEX`.
|
# The length of the longest shebang matching `SHEBANG_REGEX`.
|
||||||
PERL_SHEBANG_MAX_LENGTH = T.let("#! /usr/bin/env perl ".length, Integer)
|
PERL_SHEBANG_MAX_LENGTH = T.let("#! /usr/bin/env perl ".length, Integer)
|
||||||
|
@ -112,7 +112,7 @@ module Language
|
|||||||
module_function
|
module_function
|
||||||
|
|
||||||
# A regex to match potential shebang permutations.
|
# A regex to match potential shebang permutations.
|
||||||
PYTHON_SHEBANG_REGEX = %r{^#! ?/usr/bin/(?:env )?python(?:[23](?:\.\d{1,2})?)?( |$)}
|
PYTHON_SHEBANG_REGEX = %r{^#! ?(?:/usr/bin/(?:env )?)?python(?:[23](?:\.\d{1,2})?)?( |$)}
|
||||||
|
|
||||||
# The length of the longest shebang matching `SHEBANG_REGEX`.
|
# The length of the longest shebang matching `SHEBANG_REGEX`.
|
||||||
PYTHON_SHEBANG_MAX_LENGTH = T.let("#! /usr/bin/env pythonx.yyy ".length, Integer)
|
PYTHON_SHEBANG_MAX_LENGTH = T.let("#! /usr/bin/env pythonx.yyy ".length, Integer)
|
||||||
|
@ -5,6 +5,7 @@ require "utils/shebang"
|
|||||||
|
|
||||||
RSpec.describe Language::Node::Shebang do
|
RSpec.describe Language::Node::Shebang do
|
||||||
let(:file) { Tempfile.new("node-shebang") }
|
let(:file) { Tempfile.new("node-shebang") }
|
||||||
|
let(:broken_file) { Tempfile.new("node-shebang") }
|
||||||
let(:f) do
|
let(:f) do
|
||||||
f = {}
|
f = {}
|
||||||
|
|
||||||
@ -40,9 +41,16 @@ RSpec.describe Language::Node::Shebang do
|
|||||||
c
|
c
|
||||||
EOS
|
EOS
|
||||||
file.flush
|
file.flush
|
||||||
|
broken_file.write <<~EOS
|
||||||
|
#!node
|
||||||
|
a
|
||||||
|
b
|
||||||
|
c
|
||||||
|
EOS
|
||||||
|
broken_file.flush
|
||||||
end
|
end
|
||||||
|
|
||||||
after { file.unlink }
|
after { [file, broken_file].each(&:unlink) }
|
||||||
|
|
||||||
describe "#detected_node_shebang" do
|
describe "#detected_node_shebang" do
|
||||||
it "can be used to replace Node shebangs" do
|
it "can be used to replace Node shebangs" do
|
||||||
@ -57,6 +65,18 @@ RSpec.describe Language::Node::Shebang do
|
|||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "can fix broken shebang like `#!node`" do
|
||||||
|
allow(Formulary).to receive(:factory).with(f[:node18].name).and_return(f[:node18])
|
||||||
|
Utils::Shebang.rewrite_shebang described_class.detected_node_shebang(f[:versioned_node_dep]), broken_file.path
|
||||||
|
|
||||||
|
expect(File.read(broken_file)).to eq <<~EOS
|
||||||
|
#!#{HOMEBREW_PREFIX/"opt/node@18/bin/node"}
|
||||||
|
a
|
||||||
|
b
|
||||||
|
c
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
it "errors if formula doesn't depend on node" do
|
it "errors if formula doesn't depend on node" do
|
||||||
expect { Utils::Shebang.rewrite_shebang described_class.detected_node_shebang(f[:no_deps]), file.path }
|
expect { Utils::Shebang.rewrite_shebang described_class.detected_node_shebang(f[:no_deps]), file.path }
|
||||||
.to raise_error(ShebangDetectionError, "Cannot detect Node shebang: formula does not depend on Node.")
|
.to raise_error(ShebangDetectionError, "Cannot detect Node shebang: formula does not depend on Node.")
|
||||||
|
@ -5,6 +5,7 @@ require "utils/shebang"
|
|||||||
|
|
||||||
RSpec.describe Language::Perl::Shebang do
|
RSpec.describe Language::Perl::Shebang do
|
||||||
let(:file) { Tempfile.new("perl-shebang") }
|
let(:file) { Tempfile.new("perl-shebang") }
|
||||||
|
let(:broken_file) { Tempfile.new("perl-shebang") }
|
||||||
let(:f) do
|
let(:f) do
|
||||||
f = {}
|
f = {}
|
||||||
|
|
||||||
@ -39,9 +40,16 @@ RSpec.describe Language::Perl::Shebang do
|
|||||||
c
|
c
|
||||||
EOS
|
EOS
|
||||||
file.flush
|
file.flush
|
||||||
|
broken_file.write <<~EOS
|
||||||
|
#!perl
|
||||||
|
a
|
||||||
|
b
|
||||||
|
c
|
||||||
|
EOS
|
||||||
|
broken_file.flush
|
||||||
end
|
end
|
||||||
|
|
||||||
after { file.unlink }
|
after { [file, broken_file].each(&:unlink) }
|
||||||
|
|
||||||
describe "#detected_perl_shebang" do
|
describe "#detected_perl_shebang" do
|
||||||
it "can be used to replace Perl shebangs when depends_on \"perl\" is used" do
|
it "can be used to replace Perl shebangs when depends_on \"perl\" is used" do
|
||||||
@ -74,6 +82,24 @@ RSpec.describe Language::Perl::Shebang do
|
|||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "can fix broken shebang like `#!perl`" do
|
||||||
|
allow(Formulary).to receive(:factory).with(f[:perl].name).and_return(f[:perl])
|
||||||
|
Utils::Shebang.rewrite_shebang described_class.detected_perl_shebang(f[:uses_from_macos]), broken_file.path
|
||||||
|
|
||||||
|
expected_shebang = if OS.mac?
|
||||||
|
"/usr/bin/perl#{MacOS.preferred_perl_version}"
|
||||||
|
else
|
||||||
|
HOMEBREW_PREFIX/"opt/perl/bin/perl"
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(File.read(broken_file)).to eq <<~EOS
|
||||||
|
#!#{expected_shebang}
|
||||||
|
a
|
||||||
|
b
|
||||||
|
c
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
it "errors if formula doesn't depend on perl" do
|
it "errors if formula doesn't depend on perl" do
|
||||||
expect { Utils::Shebang.rewrite_shebang described_class.detected_perl_shebang(f[:no_deps]), file.path }
|
expect { Utils::Shebang.rewrite_shebang described_class.detected_perl_shebang(f[:no_deps]), file.path }
|
||||||
.to raise_error(ShebangDetectionError, "Cannot detect Perl shebang: formula does not depend on Perl.")
|
.to raise_error(ShebangDetectionError, "Cannot detect Perl shebang: formula does not depend on Perl.")
|
||||||
|
@ -5,6 +5,7 @@ require "utils/shebang"
|
|||||||
|
|
||||||
RSpec.describe Language::Python::Shebang do
|
RSpec.describe Language::Python::Shebang do
|
||||||
let(:file) { Tempfile.new("python-shebang") }
|
let(:file) { Tempfile.new("python-shebang") }
|
||||||
|
let(:broken_file) { Tempfile.new("python-shebang") }
|
||||||
let(:f) do
|
let(:f) do
|
||||||
f = {}
|
f = {}
|
||||||
|
|
||||||
@ -40,9 +41,16 @@ RSpec.describe Language::Python::Shebang do
|
|||||||
c
|
c
|
||||||
EOS
|
EOS
|
||||||
file.flush
|
file.flush
|
||||||
|
broken_file.write <<~EOS
|
||||||
|
#!python
|
||||||
|
a
|
||||||
|
b
|
||||||
|
c
|
||||||
|
EOS
|
||||||
|
broken_file.flush
|
||||||
end
|
end
|
||||||
|
|
||||||
after { file.unlink }
|
after { [file, broken_file].each(&:unlink) }
|
||||||
|
|
||||||
describe "#detected_python_shebang" do
|
describe "#detected_python_shebang" do
|
||||||
it "can be used to replace Python shebangs" do
|
it "can be used to replace Python shebangs" do
|
||||||
@ -72,6 +80,20 @@ RSpec.describe Language::Python::Shebang do
|
|||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "can fix broken shebang line `#!python`" do
|
||||||
|
Utils::Shebang.rewrite_shebang(
|
||||||
|
described_class.detected_python_shebang(f[:versioned_python_dep],
|
||||||
|
use_python_from_path: true), broken_file.path
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(File.read(broken_file)).to eq <<~EOS
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
a
|
||||||
|
b
|
||||||
|
c
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
it "errors if formula doesn't depend on python" do
|
it "errors if formula doesn't depend on python" do
|
||||||
expect do
|
expect do
|
||||||
Utils::Shebang.rewrite_shebang(
|
Utils::Shebang.rewrite_shebang(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user