2021-01-31 14:50:29 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "language/python"
|
|
|
|
require "utils/shebang"
|
|
|
|
|
2024-02-18 15:11:11 -08:00
|
|
|
RSpec.describe Language::Python::Shebang do
|
2021-01-31 14:50:29 -05:00
|
|
|
let(:file) { Tempfile.new("python-shebang") }
|
2023-08-14 20:27:16 -04:00
|
|
|
let(:f) do
|
|
|
|
f = {}
|
|
|
|
|
|
|
|
f[:python311] = formula "python@3.11" do
|
2021-01-31 14:50:29 -05:00
|
|
|
url "https://brew.sh/python-1.0.tgz"
|
|
|
|
end
|
2023-08-14 20:27:16 -04:00
|
|
|
|
|
|
|
f[:versioned_python_dep] = formula "foo" do
|
2021-01-31 14:50:29 -05:00
|
|
|
url "https://brew.sh/foo-1.0.tgz"
|
|
|
|
|
2022-08-09 22:38:05 +08:00
|
|
|
depends_on "python@3.11"
|
2021-01-31 14:50:29 -05:00
|
|
|
end
|
2023-08-14 20:27:16 -04:00
|
|
|
|
|
|
|
f[:no_deps] = formula "foo" do
|
|
|
|
url "https://brew.sh/foo-1.0.tgz"
|
|
|
|
end
|
|
|
|
|
|
|
|
f[:multiple_deps] = formula "foo" do
|
|
|
|
url "https://brew.sh/foo-1.0.tgz"
|
|
|
|
|
|
|
|
depends_on "python"
|
|
|
|
depends_on "python@3.11"
|
|
|
|
end
|
|
|
|
|
|
|
|
f
|
2021-01-31 14:50:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
file.write <<~EOS
|
2022-08-13 01:11:09 +08:00
|
|
|
#!/usr/bin/python2
|
2021-01-31 14:50:29 -05:00
|
|
|
a
|
|
|
|
b
|
|
|
|
c
|
|
|
|
EOS
|
|
|
|
file.flush
|
|
|
|
end
|
|
|
|
|
|
|
|
after { file.unlink }
|
|
|
|
|
|
|
|
describe "#detected_python_shebang" do
|
|
|
|
it "can be used to replace Python shebangs" do
|
2022-11-09 14:45:43 +00:00
|
|
|
allow(Formulary).to receive(:factory)
|
2023-08-14 20:27:16 -04:00
|
|
|
allow(Formulary).to receive(:factory).with(f[:python311].name).and_return(f[:python311])
|
2023-04-17 11:18:51 -07:00
|
|
|
Utils::Shebang.rewrite_shebang(
|
2023-08-14 20:27:16 -04:00
|
|
|
described_class.detected_python_shebang(f[:versioned_python_dep], use_python_from_path: false), file.path
|
2023-04-17 11:18:51 -07:00
|
|
|
)
|
2021-01-31 14:50:29 -05:00
|
|
|
|
|
|
|
expect(File.read(file)).to eq <<~EOS
|
2022-08-09 22:38:05 +08:00
|
|
|
#!#{HOMEBREW_PREFIX}/opt/python@3.11/bin/python3.11
|
2021-01-31 14:50:29 -05:00
|
|
|
a
|
|
|
|
b
|
|
|
|
c
|
|
|
|
EOS
|
|
|
|
end
|
2022-08-13 01:11:09 +08:00
|
|
|
|
|
|
|
it "can be pointed to a `python3` in PATH" do
|
2023-04-17 11:18:51 -07:00
|
|
|
Utils::Shebang.rewrite_shebang(
|
2023-08-14 20:27:16 -04:00
|
|
|
described_class.detected_python_shebang(f[:versioned_python_dep], use_python_from_path: true), file.path
|
2023-04-17 11:18:51 -07:00
|
|
|
)
|
2022-08-13 01:11:09 +08:00
|
|
|
|
|
|
|
expect(File.read(file)).to eq <<~EOS
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
a
|
|
|
|
b
|
|
|
|
c
|
|
|
|
EOS
|
|
|
|
end
|
2023-08-14 20:27:16 -04:00
|
|
|
|
|
|
|
it "errors if formula doesn't depend on python" do
|
|
|
|
expect do
|
|
|
|
Utils::Shebang.rewrite_shebang(
|
|
|
|
described_class.detected_python_shebang(f[:no_deps], use_python_from_path: false),
|
|
|
|
file.path,
|
|
|
|
)
|
|
|
|
end.to raise_error(ShebangDetectionError, "Cannot detect Python shebang: formula does not depend on Python.")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "errors if formula depends on more than one python" do
|
|
|
|
expect do
|
|
|
|
Utils::Shebang.rewrite_shebang(
|
|
|
|
described_class.detected_python_shebang(f[:multiple_deps], use_python_from_path: false),
|
|
|
|
file.path,
|
|
|
|
)
|
|
|
|
end.to raise_error(
|
|
|
|
ShebangDetectionError,
|
|
|
|
"Cannot detect Python shebang: formula has multiple Python dependencies.",
|
|
|
|
)
|
|
|
|
end
|
2021-01-31 14:50:29 -05:00
|
|
|
end
|
|
|
|
end
|