Skip to content

Commit

Permalink
Add warning for extra keys.
Browse files Browse the repository at this point in the history
Also use progress format.
  • Loading branch information
agustinrhcp committed Oct 27, 2023
1 parent e0efd5d commit 84a12ab
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/pact/cli/run_pact_verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class RunPactVerification
attr_reader :options

def initialize options
@options = options
@options = options.merge(format: 'progress')
end

def self.call options
Expand Down
28 changes: 28 additions & 0 deletions lib/pact/provider/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,39 @@ def describe_response expected_response, interaction_context
end
end

def warn_if_actual_has_extra_keys(actual, expected)
extra_keys = extract_keys(Array(actual).first) - extract_keys(Array(expected).first)

if extra_keys.any?
pending "Extra keys: #{extra_keys.join(", ")}"
expect(extra_keys).to be_empty
else
expect(true).to be true
end
end

def extract_keys(hash, prefix: '')
return extract_keys(hash.first, prefix: "#{prefix}[]") if hash.is_a?(Array) && hash.first.is_a?(Hash)
return [] if hash.is_a?(Array)

hash.reduce([]) do |acc, (k, v)|
new_acc = acc.concat(["#{prefix}#{k}"])
next new_acc.concat(extract_keys(v, prefix: "#{prefix}#{k}.")) if v.is_a?(Hash)

new_acc
end
end

if expected_response.body
it "has a matching body" do | example |
set_metadata(example, :pact_actual_body, response_body)
@response_body = response_body
expect(response_body).to match_term expected_response_body, diff_options, example
end

it 'does not have extra keys' do
warn_if_actual_has_extra_keys(parse_body_from_response(interaction_context.last_response), expected_response.body)
end
end
end
end
Expand Down

0 comments on commit 84a12ab

Please sign in to comment.