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

fix: add blank lines to empty hashs - json 2.8.x regression #111

Merged
merged 2 commits into from
Nov 29, 2024
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
2 changes: 2 additions & 0 deletions lib/pact/matchers/unix_diff_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def generate_string diff, target
begin
# Can't think of an elegant way to check if we can pretty generate other than to try it and maybe fail
json = fix_blank_lines_in_empty_hashes JSON.pretty_generate(comparable)
json = add_blank_lines_in_empty_hashes json
json = add_blank_lines_in_empty_arrays json
add_comma_to_end_of_arrays json
rescue JSON::GeneratorError
comparable.to_s
Expand Down
9 changes: 9 additions & 0 deletions lib/pact/shared/jruby_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,14 @@ def fix_blank_lines_in_empty_hashes json
json.gsub(/({\n)\n(\s*})/,'\1\2')
end

# preserve pre json 2.8.x behaviour
# https://github.com/ruby/json/pull/626
def add_blank_lines_in_empty_hashes(json)
json.gsub(/({\s*})/, "{\n }")
end

def add_blank_lines_in_empty_arrays(json)
json.gsub(/\[\s*\]/, "[\n ]")
end
end
end
1 change: 1 addition & 0 deletions spec/lib/pact/matchers/unix_diff_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ module Matchers
end

it "generates the right number of lines, even with ActiveSupport loaded" do
puts subject
expect(line_count).to eq 7 + key_lines_count
end
end
Expand Down