python: expand and refactor tests

This brings coverage of `Language::Python::Shebang` to 100%.
This commit is contained in:
Sam Ford 2023-08-14 20:27:16 -04:00
parent 87935f8d0f
commit 4314daa2cf
No known key found for this signature in database
GPG Key ID: 7AF5CBEE1DD6F76D

View File

@ -5,17 +5,31 @@ require "utils/shebang"
describe Language::Python::Shebang do describe Language::Python::Shebang do
let(:file) { Tempfile.new("python-shebang") } let(:file) { Tempfile.new("python-shebang") }
let(:python_f) do let(:f) do
formula "python@3.11" do f = {}
f[:python311] = formula "python@3.11" do
url "https://brew.sh/python-1.0.tgz" url "https://brew.sh/python-1.0.tgz"
end end
end
let(:f) do f[:versioned_python_dep] = formula "foo" do
formula "foo" do
url "https://brew.sh/foo-1.0.tgz" url "https://brew.sh/foo-1.0.tgz"
depends_on "python@3.11" depends_on "python@3.11"
end end
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
end end
before do before do
@ -33,9 +47,9 @@ describe Language::Python::Shebang do
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
allow(Formulary).to receive(:factory) allow(Formulary).to receive(:factory)
allow(Formulary).to receive(:factory).with(python_f.name).and_return(python_f) allow(Formulary).to receive(:factory).with(f[:python311].name).and_return(f[:python311])
Utils::Shebang.rewrite_shebang( Utils::Shebang.rewrite_shebang(
described_class.detected_python_shebang(f, use_python_from_path: false), file.path described_class.detected_python_shebang(f[:versioned_python_dep], use_python_from_path: false), file.path
) )
expect(File.read(file)).to eq <<~EOS expect(File.read(file)).to eq <<~EOS
@ -48,7 +62,7 @@ describe Language::Python::Shebang do
it "can be pointed to a `python3` in PATH" do it "can be pointed to a `python3` in PATH" do
Utils::Shebang.rewrite_shebang( Utils::Shebang.rewrite_shebang(
described_class.detected_python_shebang(f, use_python_from_path: true), file.path described_class.detected_python_shebang(f[:versioned_python_dep], use_python_from_path: true), file.path
) )
expect(File.read(file)).to eq <<~EOS expect(File.read(file)).to eq <<~EOS
@ -58,5 +72,26 @@ describe Language::Python::Shebang do
c c
EOS EOS
end end
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
end end
end end