From 9220703b17e9a88ca51b7b3bb491121521556b51 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 10 Oct 2017 13:49:01 +1100 Subject: [PATCH] feat: output table of verification results when present --- lib/pact_broker/client/can_i_deploy.rb | 8 ++++- .../client/matrix_text_formatter.rb | 31 +++++++++++++++++++ .../pact_broker/client/can_i_deploy_spec.rb | 9 +++++- .../client/matrix_text_formatter_spec.rb | 29 +++++++++++++++++ .../pacts/pact_broker_client-pact_broker.json | 29 ++++++++++++++--- .../pact_broker_client_matrix_spec.rb | 7 ++--- spec/support/matrix.json | 22 +++++++++++++ spec/support/matrix.txt | 3 ++ spec/support/matrix_error.txt | 3 ++ 9 files changed, 130 insertions(+), 11 deletions(-) create mode 100644 lib/pact_broker/client/matrix_text_formatter.rb create mode 100644 spec/lib/pact_broker/client/matrix_text_formatter_spec.rb create mode 100644 spec/support/matrix.json create mode 100644 spec/support/matrix.txt create mode 100644 spec/support/matrix_error.txt diff --git a/lib/pact_broker/client/can_i_deploy.rb b/lib/pact_broker/client/can_i_deploy.rb index a084c542..a7896075 100644 --- a/lib/pact_broker/client/can_i_deploy.rb +++ b/lib/pact_broker/client/can_i_deploy.rb @@ -1,6 +1,8 @@ require 'pact_broker/client/error' require 'pact_broker/client/pact_broker_client' require 'pact_broker/client/retry' +require 'pact_broker/client/matrix_text_formatter' + module PactBroker module Client @@ -27,7 +29,7 @@ def initialize(pact_broker_base_url, version_selectors, pact_broker_client_optio def call if matrix.any? - Result.new(true, 'Computer says yes \o/') + Result.new(true, success_message(matrix)) else Result.new(false, 'Computer says no ¯\_(ツ)_/¯') end @@ -41,6 +43,10 @@ def call attr_reader :pact_broker_base_url, :version_selectors, :pact_broker_client_options + def success_message matrix + 'Computer says yes \o/' + "\n\n" + MatrixTextFormatter.call(matrix) + end + def matrix @matrix ||= Retry.until_true { pact_broker_client.matrix.get(version_selectors) } end diff --git a/lib/pact_broker/client/matrix_text_formatter.rb b/lib/pact_broker/client/matrix_text_formatter.rb new file mode 100644 index 00000000..39b8e7bc --- /dev/null +++ b/lib/pact_broker/client/matrix_text_formatter.rb @@ -0,0 +1,31 @@ +require 'table_print' + +module PactBroker + module Client + class MatrixTextFormatter + Line = Struct.new(:consumer, :consumer_version, :pact_publication_date, :provider, :provider_version, :verification_date) + + def self.call(matrix_lines) + data = matrix_lines.collect do | line | + Line.new( + lookup(line, :consumer, :name), + lookup(line, :consumer, :version, :number), + lookup(line, :pact, :createdAt), + lookup(line, :consumer, :name), + lookup(line, :provider, :version, :number), + lookup(line, :verification, :executedAt), + ) + end + + printer = TablePrint::Printer.new(data) + printer.table_print + end + + def self.lookup line, *keys + keys.reduce(line) { | line, key | line[key] } + rescue NoMethodError + "???" + end + end + end +end diff --git a/spec/lib/pact_broker/client/can_i_deploy_spec.rb b/spec/lib/pact_broker/client/can_i_deploy_spec.rb index 8866e9f8..2f5e9921 100644 --- a/spec/lib/pact_broker/client/can_i_deploy_spec.rb +++ b/spec/lib/pact_broker/client/can_i_deploy_spec.rb @@ -12,6 +12,7 @@ module Client before do allow_any_instance_of(PactBroker::Client::PactBrokerClient).to receive(:matrix).and_return(matrix_client) allow(matrix_client).to receive(:get).and_return(matrix) + allow(MatrixTextFormatter).to receive(:call).and_return('text matrix') end subject { CanIDeploy.call(pact_broker_base_url, version_selectors, pact_broker_client_options) } @@ -21,13 +22,19 @@ module Client subject end + it "creates a text table out of the matrix" do + expect(MatrixTextFormatter).to receive(:call).with(matrix) + subject + end + context "when compatible versions are found" do it "returns a success response" do expect(subject.success).to be true end - it "returns a success message" do + it "returns a success message with the text table" do expect(subject.message).to include "Computer says yes" + expect(subject.message).to include "\n\ntext matrix" end end diff --git a/spec/lib/pact_broker/client/matrix_text_formatter_spec.rb b/spec/lib/pact_broker/client/matrix_text_formatter_spec.rb new file mode 100644 index 00000000..49241f9a --- /dev/null +++ b/spec/lib/pact_broker/client/matrix_text_formatter_spec.rb @@ -0,0 +1,29 @@ +require 'pact_broker/client/matrix_text_formatter' + +module PactBroker + module Client + describe MatrixTextFormatter do + let(:matrix_lines) { JSON.parse(File.read('spec/support/matrix.json'), symbolize_names: true) } + let(:expected_matrix_lines) { File.read('spec/support/matrix.txt') } + + # SublimeText removes whitespace from the end of files when you save them, + # so removing trailing whitespace before comparing + subject { MatrixTextFormatter.call(matrix_lines).split("\n").collect(&:strip).join("\n") } + + context "with valid data" do + it "it has the right text" do + expect(subject).to eq expected_matrix_lines + end + end + + context "with invalid data" do + let(:expected_matrix_lines) { File.read('spec/support/matrix_error.txt') } + let(:matrix_lines) { [{}] } + + it "doesn't blow up" do + expect(subject).to eq expected_matrix_lines + end + end + end + end +end diff --git a/spec/pacts/pact_broker_client-pact_broker.json b/spec/pacts/pact_broker_client-pact_broker.json index c049b82f..d53f2bfc 100644 --- a/spec/pacts/pact_broker_client-pact_broker.json +++ b/spec/pacts/pact_broker_client-pact_broker.json @@ -211,10 +211,31 @@ "headers": { }, "body": { - "matrix": [ - { - } - ] + "matrix": { + "json_class": "Pact::SomethingLike", + "contents": [ + { + "consumer": { + "name": "Foo", + "version": { + "number": "4" + } + }, + "provider": { + "name": "Bar", + "version": { + "number": "5" + } + }, + "verification": { + "executedAt": "2017-10-10T12:49:04+11:00" + }, + "pact": { + "createdAt": "2017-10-10T12:49:04+11:00" + } + } + ] + } } } }, diff --git a/spec/service_providers/pact_broker_client_matrix_spec.rb b/spec/service_providers/pact_broker_client_matrix_spec.rb index 95a64566..f98c6326 100644 --- a/spec/service_providers/pact_broker_client_matrix_spec.rb +++ b/spec/service_providers/pact_broker_client_matrix_spec.rb @@ -7,11 +7,8 @@ module PactBroker::Client include_context "pact broker" describe "retriving the compatibility matrix" do - let(:matrix_response_body) do - { - matrix: [{}] - } - end + let(:matrix_response_body) { { matrix: Pact.like(matrix) } } + let(:matrix) { JSON.parse(File.read('spec/support/matrix.json')) } context "when results are found" do before do diff --git a/spec/support/matrix.json b/spec/support/matrix.json new file mode 100644 index 00000000..d038e55c --- /dev/null +++ b/spec/support/matrix.json @@ -0,0 +1,22 @@ +[ + { + "consumer": { + "name": "Foo", + "version": { + "number": "4" + } + }, + "provider": { + "name": "Bar", + "version": { + "number": "5" + } + }, + "verification": { + "executedAt": "2017-10-10T12:49:04+11:00" + }, + "pact": { + "createdAt": "2017-10-10T12:49:04+11:00" + } + } +] diff --git a/spec/support/matrix.txt b/spec/support/matrix.txt new file mode 100644 index 00000000..30a52b20 --- /dev/null +++ b/spec/support/matrix.txt @@ -0,0 +1,3 @@ +CONSUMER | CONSUMER_VERSION | PACT_PUBLICATION_DATE | PROVIDER | PROVIDER_VERSION | VERIFICATION_DATE +---------|------------------|---------------------------|----------|------------------|-------------------------- +Foo | 4 | 2017-10-10T12:49:04+11:00 | Foo | 5 | 2017-10-10T12:49:04+11:00 \ No newline at end of file diff --git a/spec/support/matrix_error.txt b/spec/support/matrix_error.txt new file mode 100644 index 00000000..764c0869 --- /dev/null +++ b/spec/support/matrix_error.txt @@ -0,0 +1,3 @@ +CONSUMER | CONSUMER_VERSION | PACT_PUBLICATION_DATE | PROVIDER | PROVIDER_VERSION | VERIFICATION_DATE +---------|------------------|-----------------------|----------|------------------|------------------ +??? | ??? | ??? | ??? | ??? | ??? \ No newline at end of file