Skip to content

Commit

Permalink
feat: print a new line between JSON documents when using --format json
Browse files Browse the repository at this point in the history
This allows the stream to be parsed as jsonl

Fixes: pact-foundation/pact-go#88
  • Loading branch information
bethesque committed Jul 12, 2018
1 parent 707d3bb commit dc90d8e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
7 changes: 7 additions & 0 deletions lib/pact/provider_verifier/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def call
def setup
print_deprecation_note
set_environment_variables
require_rspec_monkeypatch_for_jsonl
require_pact_project_pact_helper # Beth: not sure if this is needed, hangover from pact-provider-proxy?
end

Expand Down Expand Up @@ -127,6 +128,12 @@ def require_pact_project_pact_helper
require ENV['PACT_PROJECT_PACT_HELPER'] if ENV.fetch('PACT_PROJECT_PACT_HELPER','') != ''
end

def require_rspec_monkeypatch_for_jsonl
if options.format == 'json'
require 'pact/provider_verifier/rspec_json_formatter_monkeypatch'
end
end

def custom_provider_headers_for_env_var
if options.custom_provider_header && options.custom_provider_header.any?
options.custom_provider_header.join("\n")
Expand Down
34 changes: 34 additions & 0 deletions lib/pact/provider_verifier/rspec_json_formatter_monkeypatch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'rspec'

begin
require 'rspec/core/formatters/json_formatter'

RSpec::Core::Formatters::JsonFormatter

# This looks dodgy, but it's actually safer than inheriting from
# RSpec::Core::Formatters::JsonFormatter and using a custom class,
# because if the JsonFormatter class gets refactored,
# the --format json option will still work, but the inheritance will break.

module RSpec
module Core
module Formatters
class JsonFormatter
alias_method :old_close, :close

def close(*args)
# Append a new line so that the output stream can be split at
# the new lines, and each JSON document parsed separately
old_close(*args)
output.write("\n")
end
end
end
end
end

rescue NameError
Pact.configuration.error_stream.puts "WARN: Could not find RSpec::Core::Formatters::JsonFormatter to modify it to put a new line between JSON result documents."
rescue LoadError
Pact.configuration.error_stream.puts "WARN: Could not load rspec/core/formatters/json_formatter to modify it to put a new line between JSON result documents."
end
8 changes: 4 additions & 4 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@
subject { `bundle exec bin/pact-provider-verifier ./test/me-they.json ./test/me-they.json --format json -a 1.0.100 --provider-base-url http://localhost:4567 --provider-states-setup-url http://localhost:4567/provider-state` }

it "sends the results of both pacts to stdout" do
expect(subject).to include "}{"
expect(subject).to include "}\n{"
expect(subject.scan(/\d examples, \d failure/).count).to eq 2
end

it "allows the results to be split and parsed to JSON" do
result_1, result_2 = subject.split("}{", 2)
JSON.parse(result_1 + "}")
JSON.parse("{" + result_2)
result_1, result_2 = subject.split("\n", 2)
JSON.parse(result_1)
JSON.parse(result_2)
end
end

Expand Down

0 comments on commit dc90d8e

Please sign in to comment.