Skip to content
Open
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
3 changes: 0 additions & 3 deletions chefspec.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,4 @@ Gem::Specification.new do |s|
# this needs to be remedied before Ruby 3.3
s.add_dependency "logger", "< 1.6"
s.add_development_dependency "cookstyle", "~> 8.4"

# temporary restriction to a version of rspec-expectations that includes the
# `RSpec::Matchers::ExpectedsForMultipleDiffs` class (renamed in 3.12.4)
end
25 changes: 22 additions & 3 deletions lib/chefspec/matchers/resource_matcher.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# require "rspec/matchers/expecteds_for_multiple_diffs" This is now a private class and will throw errors when the require statement is executed
require "rspec/expectations/fail_with"

module ChefSpec::Matchers
Expand Down Expand Up @@ -66,8 +65,7 @@
"\n\n" \
" " + unmatched_parameters.collect { |parameter, h|
msg = "#{parameter} #{h[:expected].inspect}, was #{h[:actual].inspect}"
diff = ::RSpec::Matchers::ExpectedsForMultipleDiffs.from(h[:expected]) \
.message_with_diff(message, ::RSpec::Expectations.differ, h[:actual])
diff = diff_for(h[:expected], h[:actual], message)
msg += diff if diff
msg
}.join("\n ")
Expand All @@ -77,7 +75,7 @@
" with action :#{@expected_action} to be in Chef run." \
" Other #{@resource_name} resources:" \
"\n\n" \
" " + similar_resources.map(&:to_s).join("\n ") + "\n "

Check failure on line 78 in lib/chefspec/matchers/resource_matcher.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/MapJoin: Remove redundant map(&:to_s) before join.
end
end

Expand All @@ -95,6 +93,27 @@

private

#
# Build the diff text for a mismatched parameter using rspec-expectations'
# private diff helper. That helper was renamed and its signature changed in
# rspec-expectations 3.12.4 (`ExpectedsForMultipleDiffs.from(expected)` with
# `message_with_diff(message, differ, actual)` became
# `MultiMatcherDiff.from(expected, actual)` with
# `message_with_diff(message, differ)`), so support both shapes.
#
# @return [String, nil]
#
def diff_for(expected, actual, message)
differ = ::RSpec::Expectations.differ
if defined?(::RSpec::Matchers::MultiMatcherDiff)
::RSpec::Matchers::MultiMatcherDiff.from(expected, actual)
.message_with_diff(message, differ)
else
::RSpec::Matchers::ExpectedsForMultipleDiffs.from(expected)
.message_with_diff(message, differ, actual)
end
end

def unmatched_parameters
return @_unmatched_parameters if @_unmatched_parameters

Expand Down
27 changes: 26 additions & 1 deletion spec/unit/matchers/resource_matcher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
require "spec_helper"

describe ChefSpec::Matchers::ResourceMatcher do
pending
subject { described_class.new(:template, :create, "/etc/foo") }

describe "#failure_message" do
context "when a resource is found but has unmatched parameters" do
before do
# Pretend we found a matching resource with a mismatched, multi-line
# parameter so that #failure_message renders a diff. This is the code
# path that depends on rspec-expectations' (private) diff helper class.
allow(subject).to receive(:resource).and_return("template[/etc/foo]")
allow(subject).to receive(:unmatched_parameters).and_return(
content: { expected: "line one\nline two\n", actual: "line one\nline three\n" }
)
end

it "renders a diff without raising" do
expect { subject.failure_message }.not_to raise_error
end

it "includes the parameter name and a diff in the message" do
message = subject.failure_message
expect(message).to include("expected \"template[/etc/foo]\" to have parameters")
expect(message).to include("content")
expect(message).to include("line three")
end
end
end
end
Loading