Skip to content

Commit

Permalink
Add tests for shebang mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
dduugg committed Apr 19, 2023
1 parent bdc55b4 commit a37e5bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
22 changes: 11 additions & 11 deletions Library/Homebrew/test/language/perl/shebang_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,12 @@
require "utils/shebang"

describe Language::Perl::Shebang do
let(:file) { Tempfile.new("perl-shebang") }
let(:file) { File.open("#{TEST_TMPDIR}/perl-shebang", "w") }
let(:perl_f) do
formula "perl" do
url "https://brew.sh/perl-1.0.tgz"
end
end
let(:f) do
formula "foo" do
url "https://brew.sh/foo-1.0.tgz"

uses_from_macos "perl"
end
end

before do
file.write <<~EOS
Expand All @@ -29,13 +22,20 @@
file.flush
end

after { file.unlink }
after { FileUtils.rm file }

describe "#detected_perl_shebang" do
it "can be used to replace Perl shebangs" do
allow(Formulary).to receive(:factory)
allow(Formulary).to receive(:factory).with(perl_f.name).and_return(perl_f)
Utils::Shebang.rewrite_shebang described_class.detected_perl_shebang(f), file.path
formula "foo" do
include described_class
url "https://brew.sh/foo-1.0.tgz"
uses_from_macos "perl"

def install
rewrite_shebang detected_perl_shebang, Pathname("#{TEST_TMPDIR}/perl-shebang")
end
end.install

expected_shebang = if OS.mac?
"/usr/bin/perl#{MacOS.preferred_perl_version}"
Expand Down
38 changes: 22 additions & 16 deletions Library/Homebrew/test/language/python/shebang_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,12 @@
require "utils/shebang"

describe Language::Python::Shebang do
let(:file) { Tempfile.new("python-shebang") }
let(:file) { File.open("#{TEST_TMPDIR}/python-shebang", "w") }
let(:python_f) do
formula "python@3.11" do
url "https://brew.sh/python-1.0.tgz"
end
end
let(:f) do
formula "foo" do
url "https://brew.sh/foo-1.0.tgz"

depends_on "python@3.11"
end
end

before do
file.write <<~EOS
Expand All @@ -29,15 +22,21 @@
file.flush
end

after { file.unlink }
after { FileUtils.rm file }

describe "#detected_python_shebang" do
it "can be used to replace Python shebangs" do
allow(Formulary).to receive(:factory)
allow(Formulary).to receive(:factory).with(python_f.name).and_return(python_f)
Utils::Shebang.rewrite_shebang(
described_class.detected_python_shebang(f, use_python_from_path: false), file.path
)
formula "foo" do
include Language::Python::Shebang
url "https://brew.sh/foo-1.0.tgz"

depends_on "python@3.11"
def install
rewrite_shebang detected_python_shebang(use_python_from_path: false),
Pathname("#{TEST_TMPDIR}/python-shebang")
end
end.install

expect(File.read(file)).to eq <<~EOS
#!#{HOMEBREW_PREFIX}/opt/python@3.11/bin/python3.11
Expand All @@ -48,9 +47,16 @@
end

it "can be pointed to a `python3` in PATH" do
Utils::Shebang.rewrite_shebang(
described_class.detected_python_shebang(f, use_python_from_path: true), file.path
)
formula "foo" do
include Language::Python::Shebang
url "https://brew.sh/foo-1.0.tgz"

depends_on "python@3.11"
def install
rewrite_shebang detected_python_shebang(use_python_from_path: true),
Pathname("#{TEST_TMPDIR}/python-shebang")
end
end.install

expect(File.read(file)).to eq <<~EOS
#!/usr/bin/env python3
Expand Down

0 comments on commit a37e5bd

Please sign in to comment.