brew/Library/Homebrew/test/exceptions_spec.rb

245 lines
7.2 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
2017-02-14 17:45:20 +01:00
require "exceptions"
describe "Exception" do
describe MultipleVersionsInstalledError do
subject do
described_class.new <<~EOS
foo has multiple installed versions
Run `brew uninstall --force foo` to remove all versions.
EOS
end
2017-02-14 17:45:20 +01:00
its(:to_s) do
is_expected.to eq <<~EOS
foo has multiple installed versions
Run `brew uninstall --force foo` to remove all versions.
EOS
end
end
describe NoSuchKegError do
subject { described_class.new("foo") }
2017-02-14 17:45:20 +01:00
its(:to_s) { is_expected.to eq("No such keg: #{HOMEBREW_CELLAR}/foo") }
end
describe FormulaValidationError do
subject(:error) { described_class.new("foo", "sha257", "magic") }
2017-02-14 17:45:20 +01:00
its(:to_s) do
expect(error.to_s).to eq(%q(invalid attribute for formula 'foo': sha257 ("magic")))
end
end
2017-02-14 17:45:20 +01:00
describe TapFormulaOrCaskUnavailableError do
subject(:error) { described_class.new(tap, "foo") }
2023-01-22 17:06:48 -08:00
let(:tap) { instance_double(Tap, user: "u", repo: "r", to_s: "u/r", installed?: false) }
its(:to_s) { is_expected.to match(%r{Please tap it and then try again: brew tap u/r}) }
end
describe FormulaUnavailableError do
subject(:error) { described_class.new("foo") }
2017-02-14 17:45:20 +01:00
describe "#dependent_s" do
it "returns nil if there is no dependent" do
expect(error.dependent_s).to be_nil
end
it "returns nil if it depended on by itself" do
error.dependent = "foo"
expect(error.dependent_s).to be_nil
end
it "returns a string if there is a dependent" do
error.dependent = "foobar"
expect(error.dependent_s).to eq(" (dependency of foobar)")
end
2017-02-14 17:45:20 +01:00
end
context "without a dependent" do
its(:to_s) { is_expected.to eq('No available formula with the name "foo".') }
2017-02-14 17:45:20 +01:00
end
context "with a dependent" do
before do
error.dependent = "foobar"
end
2017-02-14 17:45:20 +01:00
its(:to_s) do
expect(error.to_s).to eq('No available formula with the name "foo" (dependency of foobar).')
end
2017-02-14 17:45:20 +01:00
end
end
describe TapFormulaUnavailableError do
subject { described_class.new(tap, "foo") }
2023-01-22 17:06:48 -08:00
let(:tap) { instance_double(Tap, user: "u", repo: "r", to_s: "u/r", installed?: false) }
its(:to_s) { is_expected.to match(%r{Please tap it and then try again: brew tap u/r}) }
end
2017-02-14 17:45:20 +01:00
describe FormulaClassUnavailableError do
subject(:error) { described_class.new("foo", "foo.rb", "Foo", list) }
let(:mod) do
Module.new do
2023-01-24 20:07:22 -08:00
# These are defined within an anonymous module to avoid polluting the global namespace.
# rubocop:disable RSpec/LeakyConstantDeclaration,Lint/ConstantDefinitionInBlock
class Bar < Requirement; end
2020-11-30 16:27:49 +01:00
class Baz < Formula; end
# rubocop:enable RSpec/LeakyConstantDeclaration,Lint/ConstantDefinitionInBlock
end
2017-02-14 17:45:20 +01:00
end
context "when there are no classes" do
let(:list) { [] }
its(:to_s) do
expect(error.to_s).to match(/Expected to find class Foo, but found no classes\./)
end
end
2017-02-14 17:45:20 +01:00
context "when the class is not derived from Formula" do
let(:list) { [mod.const_get(:Bar)] }
its(:to_s) do
expect(error.to_s).to match(/Expected to find class Foo, but only found: Bar \(not derived from Formula!\)\./)
end
end
2017-02-14 17:45:20 +01:00
context "when the class is derived from Formula" do
let(:list) { [mod.const_get(:Baz)] }
its(:to_s) { is_expected.to match(/Expected to find class Foo, but only found: Baz\./) }
end
2017-02-14 17:45:20 +01:00
end
describe FormulaUnreadableError do
subject { described_class.new("foo", formula_error) }
let(:formula_error) { LoadError.new("bar") }
its(:to_s) { is_expected.to eq("foo: bar") }
end
2017-02-14 17:45:20 +01:00
describe TapUnavailableError do
subject { described_class.new("foo") }
its(:to_s) { is_expected.to eq("No available tap foo.\n") }
end
2017-02-14 17:45:20 +01:00
describe TapAlreadyTappedError do
subject { described_class.new("foo") }
its(:to_s) { is_expected.to eq("Tap foo already tapped.\n") }
end
2017-02-14 17:45:20 +01:00
describe BuildError do
subject { described_class.new(formula, "badprg", %w[arg1 arg2], {}) }
2023-01-22 17:06:48 -08:00
let(:formula) { instance_double(Formula, name: "foo") }
its(:to_s) { is_expected.to eq("Failed executing: badprg arg1 arg2") }
end
2017-02-14 17:45:20 +01:00
describe OperationInProgressError do
subject { described_class.new("foo") }
its(:to_s) { is_expected.to match(/Operation already in progress for foo/) }
end
2017-02-14 17:45:20 +01:00
describe FormulaInstallationAlreadyAttemptedError do
subject { described_class.new(formula) }
2023-01-22 17:06:48 -08:00
let(:formula) { instance_double(Formula, full_name: "foo/bar") }
its(:to_s) { is_expected.to eq("Formula installation already attempted: foo/bar") }
end
2017-02-14 17:45:20 +01:00
describe FormulaConflictError do
subject { described_class.new(formula, [conflict]) }
2023-01-22 17:06:48 -08:00
let(:formula) { instance_double(Formula, full_name: "foo/qux") }
let(:conflict) { instance_double(FormulaConflict, name: "bar", reason: "I decided to") }
its(:to_s) { is_expected.to match(/Please `brew unlink bar` before continuing\./) }
end
2017-02-14 17:45:20 +01:00
describe CompilerSelectionError do
subject { described_class.new(formula) }
2023-01-22 17:06:48 -08:00
let(:formula) { instance_double(Formula, full_name: "foo") }
its(:to_s) { is_expected.to match(/foo cannot be built with any available compilers\./) }
end
2017-02-14 17:45:20 +01:00
describe CurlDownloadStrategyError do
context "when the file does not exist" do
subject { described_class.new("file:///tmp/foo") }
its(:to_s) { is_expected.to eq("File does not exist: /tmp/foo") }
end
2017-02-14 17:45:20 +01:00
context "when the download failed" do
subject { described_class.new("https://brew.sh") }
its(:to_s) { is_expected.to eq("Download failed: https://brew.sh") }
end
2017-02-14 17:45:20 +01:00
end
describe ErrorDuringExecution do
subject { described_class.new(["badprg", "arg1", "arg2"], status: status) }
2018-09-20 09:07:56 +01:00
let(:status) { instance_double(Process::Status, exitstatus: 17, termsig: nil) }
its(:to_s) { is_expected.to eq("Failure while executing; `badprg arg1 arg2` exited with 17.") }
end
2017-02-14 17:45:20 +01:00
describe ChecksumMismatchError do
subject { described_class.new("/file.tar.gz", hash1, hash2) }
2023-01-22 17:06:48 -08:00
let(:hash1) { instance_double(Checksum, to_s: "deadbeef") }
let(:hash2) { instance_double(Checksum, to_s: "deadcafe") }
its(:to_s) { is_expected.to match(/SHA256 mismatch/) }
end
2017-02-14 17:45:20 +01:00
describe ResourceMissingError do
subject { described_class.new(formula, resource) }
2023-01-22 17:06:48 -08:00
let(:formula) { instance_double(Formula, full_name: "bar") }
let(:resource) { instance_double(Resource, inspect: "<resource foo>") }
its(:to_s) { is_expected.to eq("bar does not define resource <resource foo>") }
end
2017-02-14 17:45:20 +01:00
describe DuplicateResourceError do
subject { described_class.new(resource) }
2023-01-22 17:06:48 -08:00
let(:resource) { instance_double(Resource, inspect: "<resource foo>") }
its(:to_s) { is_expected.to eq("Resource <resource foo> is defined more than once") }
end
2017-02-14 17:45:20 +01:00
describe BottleFormulaUnavailableError do
subject { described_class.new("/foo.bottle.tar.gz", "foo/1.0/.brew/foo.rb") }
2023-01-22 17:06:48 -08:00
let(:formula) { instance_double(Formula, full_name: "foo") }
its(:to_s) { is_expected.to match(/This bottle does not contain the formula file/) }
end
2019-10-11 09:34:43 +02:00
describe BuildFlagsError do
subject { described_class.new(["-s"]) }
2019-10-11 09:34:43 +02:00
its(:to_s) { is_expected.to match(/flag:\s+-s\nrequires building tools/) }
end
2019-10-11 09:34:43 +02:00
end