From dc90d8e4aedcbc8ad595a528f7dd7260995e9335 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 13 Jul 2018 09:01:06 +1000 Subject: [PATCH] feat: print a new line between JSON documents when using --format json This allows the stream to be parsed as jsonl Fixes: https://github.com/pact-foundation/pact-go/issues/88 --- lib/pact/provider_verifier/app.rb | 7 ++++ .../rspec_json_formatter_monkeypatch.rb | 34 +++++++++++++++++++ spec/integration_spec.rb | 8 ++--- 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 lib/pact/provider_verifier/rspec_json_formatter_monkeypatch.rb diff --git a/lib/pact/provider_verifier/app.rb b/lib/pact/provider_verifier/app.rb index 6c3514d..9d1ef0c 100644 --- a/lib/pact/provider_verifier/app.rb +++ b/lib/pact/provider_verifier/app.rb @@ -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 @@ -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") diff --git a/lib/pact/provider_verifier/rspec_json_formatter_monkeypatch.rb b/lib/pact/provider_verifier/rspec_json_formatter_monkeypatch.rb new file mode 100644 index 0000000..f26d5ed --- /dev/null +++ b/lib/pact/provider_verifier/rspec_json_formatter_monkeypatch.rb @@ -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 diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index 55edcd5..3584780 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -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