Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make exception string representations also the message #2

Merged
merged 1 commit into from
Oct 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/cookbook-omnifetch/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class NotACookbook < OmnifetchError
# the path to the thing that is not a cookbook
def initialize(path)
@path = File.expand_path(path) rescue path
super(to_s)
end

def to_s
Expand All @@ -68,6 +69,7 @@ class CookbookValidationFailure < OmnifetchError
def initialize(dependency, cached_cookbook)
@dependency = dependency
@cached_cookbook = cached_cookbook
super(to_s)
end

def to_s
Expand All @@ -85,6 +87,7 @@ class MismatchedCookbookName < OmnifetchError
def initialize(dependency, cached_cookbook)
@dependency = dependency
@cached_cookbook = cached_cookbook
super(to_s)
end

def to_s
Expand All @@ -98,7 +101,7 @@ def to_s
out << "\n"
out << "NOTE: If you do not explicitly set the 'name' attribute in the "
out << "metadata, the name of the directory will be used instead. This "
out << "is often a cause of confusion for dependency solving."
out << "is often a cause of confusion for dependency solving.\n"
out
end
end
Expand Down
87 changes: 87 additions & 0 deletions spec/unit/exceptions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'spec_helper'
require 'cookbook-omnifetch/exceptions'

module CookbookOmnifetch
describe "Exceptions" do

describe NotACookbook do

subject(:exception) do
described_class.new("/path/to/cookbook")
end

let(:message) do
"The resource at '/path/to/cookbook' does not appear to be a valid cookbook. Does it have a metadata.rb?"
end

it "creates an informative error in #message" do
expect(exception.message).to eq(message)
end

it "creates an informative error in #to_s" do
expect(exception.to_s).to eq(message)
end

end

describe CookbookValidationFailure do

let(:dependency) { instance_double("ChefDK::Policyfile::CookbookLocationSpecification", to_s: "apt ~> 1.2.3") }

# The exception class itself doesn't use this
let(:cached_cookbook) { Object.new }

subject(:exception) do
described_class.new(dependency, cached_cookbook)
end

let(:message) do
"The cookbook downloaded for apt ~> 1.2.3 did not satisfy the constraint."
end

it "creates an informative error in #message" do
expect(exception.message).to eq(message)
end

it "creates an informative error in #to_s" do
expect(exception.to_s).to eq(message)
end

end

describe MismatchedCookbookName do

let(:dependency) { instance_double("ChefDK::Policyfile::CookbookLocationSpecification", name: "apt") }

let(:cached_cookbook) { instance_double("Chef::Cookbook", cookbook_name: "apt") }

subject(:exception) do
described_class.new(dependency, cached_cookbook)
end

let(:message) do
<<-EOM
In your Berksfile, you have:

cookbook 'apt'

But that cookbook is actually named 'apt'

This can cause potentially unwanted side-effects in the future.

NOTE: If you do not explicitly set the 'name' attribute in the metadata, the name of the directory will be used instead. This is often a cause of confusion for dependency solving.
EOM
end

it "creates an informative error in #message" do
expect(exception.message).to eq(message)
end

it "creates an informative error in #to_s" do
expect(exception.to_s).to eq(message)
end

end

end
end