Skip to content

Commit

Permalink
feat: output table of verification results when present
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Oct 10, 2017
1 parent b3fd034 commit 9220703
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 11 deletions.
8 changes: 7 additions & 1 deletion lib/pact_broker/client/can_i_deploy.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
31 changes: 31 additions & 0 deletions lib/pact_broker/client/matrix_text_formatter.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion spec/lib/pact_broker/client/can_i_deploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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

Expand Down
29 changes: 29 additions & 0 deletions spec/lib/pact_broker/client/matrix_text_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -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
29 changes: 25 additions & 4 deletions spec/pacts/pact_broker_client-pact_broker.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
}
}
}
},
Expand Down
7 changes: 2 additions & 5 deletions spec/service_providers/pact_broker_client_matrix_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions spec/support/matrix.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
3 changes: 3 additions & 0 deletions spec/support/matrix.txt
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions spec/support/matrix_error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CONSUMER | CONSUMER_VERSION | PACT_PUBLICATION_DATE | PROVIDER | PROVIDER_VERSION | VERIFICATION_DATE
---------|------------------|-----------------------|----------|------------------|------------------
??? | ??? | ??? | ??? | ??? | ???

0 comments on commit 9220703

Please sign in to comment.