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