Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for shebang mixins #15267

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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") }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let(:file) { File.open("#{TEST_TMPDIR}/perl-shebang", "w") }
let(:file) { Pathname.new("perl-shebang") }

as this was technically a Pathname not File. Similarly, I think TEST_TMPDIR may be implicit.

Generally it's considered 👎🏻 to use File.open without the block form.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have mktmpdir which creates a directory that is automatically cleaned after a test.

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 Language::Perl::Shebang # rubocop:disable RSpec/DescribedClass (does not resolve in this context)
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 # rubocop:disable RSpec/DescribedClass (does not resolve in this context)
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 # rubocop:disable RSpec/DescribedClass (does not resolve in this context)
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