diff --git a/Library/Homebrew/test/language/python/shebang_spec.rb b/Library/Homebrew/test/language/python/shebang_spec.rb index 33a553a4d7..4e430cc093 100644 --- a/Library/Homebrew/test/language/python/shebang_spec.rb +++ b/Library/Homebrew/test/language/python/shebang_spec.rb @@ -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