Skip to content

Commit

Permalink
feat(can-i-deploy): display links to verification results in the outp…
Browse files Browse the repository at this point in the history
…ut of can-i-deploy when using text format
  • Loading branch information
bethesque committed Jan 21, 2021
1 parent aceeb5f commit 976950d
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 38 deletions.
56 changes: 48 additions & 8 deletions doc/pacts/markdown/Pact Broker Client - Pact Broker.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,12 @@ Pact Broker will respond with:
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand All @@ -185,7 +190,12 @@ Pact Broker will respond with:
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand Down Expand Up @@ -233,7 +243,12 @@ Pact Broker will respond with:
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand Down Expand Up @@ -281,7 +296,12 @@ Pact Broker will respond with:
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand Down Expand Up @@ -329,7 +349,12 @@ Pact Broker will respond with:
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand Down Expand Up @@ -413,7 +438,12 @@ Pact Broker will respond with:
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand Down Expand Up @@ -484,7 +514,12 @@ Pact Broker will respond with:
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand Down Expand Up @@ -735,7 +770,12 @@ Pact Broker will respond with:
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand Down
55 changes: 44 additions & 11 deletions lib/pact_broker/client/matrix/text_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,74 @@
require 'table_print'
require 'dig_rb'

module PactBroker
module Client
class Matrix
class TextFormatter

Line = Struct.new(:consumer, :consumer_version, :provider, :provider_version, :success)
Line = Struct.new(:consumer, :consumer_version, :provider, :provider_version, :success, :ref)

OPTIONS = [
{ consumer: {} },
{ consumer_version: {display_name: 'C.VERSION'} },
{ provider: {} },
{ provider_version: {display_name: 'P.VERSION'} },
{ success: {display_name: 'SUCCESS?'} }
{ success: {display_name: 'SUCCESS?'} },
{ ref: { display_name: 'RESULT#' }}
]

def self.call(matrix)
matrix_rows = matrix[:matrix]
return "" if matrix_rows.size == 0
data = matrix_rows.collect do | line |
verification_result_number = 0
data = matrix_rows.each_with_index.collect do | line |
has_verification_result_url = lookup(line, nil, :verificationResult, :_links, :self, :href)
if has_verification_result_url
verification_result_number += 1
end
Line.new(
lookup(line, :consumer, :name),
lookup(line, :consumer, :version, :number),
lookup(line, :provider, :name),
lookup(line, :provider, :version, :number),
lookup(line, :verificationResult, :success).to_s
lookup(line, "???", :consumer, :name),
lookup(line, "???", :consumer, :version, :number),
lookup(line, "???", :provider, :name) ,
lookup(line, "???", :provider, :version, :number),
(lookup(line, "???", :verificationResult, :success)).to_s,
has_verification_result_url ? verification_result_number : ""
)
end

printer = TablePrint::Printer.new(data, OPTIONS)
printer.table_print
printer.table_print + verification_result_urls_text(matrix)
end

def self.lookup line, *keys
def self.lookup line, default, *keys
keys.reduce(line) { | line, key | line[key] }
rescue NoMethodError
"???"
default
end

def self.verification_results_urls_and_successes(matrix)
(matrix[:matrix] || []).collect do | row |
url = row.dig(:verificationResult, :_links, :self, :href)
if url
success = row.dig(:verificationResult, :success)
[url, success]
else
nil
end
end.compact
end

def self.verification_result_urls_text(matrix)
text = self.verification_results_urls_and_successes(matrix).each_with_index.collect do |(url, success), i|
status = success ? 'success' : 'failure'
"#{i+1}. #{url} (#{status})"
end.join("\n")

if text.size > 0
"\n\nVERIFICATION RESULTS\n--------------------\n#{text}"
else
text
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions pact-broker-client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'table_print', '~> 1.5'
gem.add_runtime_dependency 'thor', '>= 0.20', '< 2.0'
gem.add_runtime_dependency 'rake', '~> 13.0' #For FileList
gem.add_runtime_dependency 'dig_rb', '~> 1.0'

gem.add_development_dependency 'fakefs', '~> 0.4'
gem.add_development_dependency 'webmock', '~> 3.0'
Expand Down
33 changes: 29 additions & 4 deletions spec/lib/pact_broker/client/matrix/text_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,54 @@
require 'pact_broker/client/matrix/resource'
require 'pact_broker/client/matrix/text_formatter'

module PactBroker
module Client
describe Matrix::TextFormatter do
let(:matrix_lines) { JSON.parse(File.read('spec/support/matrix.json'), symbolize_names: true)[:matrix] }
let(:matrix) { PactBroker::Client::Matrix::Resource.new(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 { Matrix::TextFormatter.call(matrix: matrix_lines).split("\n").collect(&:strip).join("\n") }
def strip_trailing_whitespace(text)
text.split("\n").collect(&:strip).join("\n")
end

subject { strip_trailing_whitespace(Matrix::TextFormatter.call(matrix)) }

context "with valid data" do
it "it has the right text" do
expect(subject).to eq expected_matrix_lines
expect(subject).to start_with expected_matrix_lines
end
end

context "with invalid data" do
let(:expected_matrix_lines) { File.read('spec/support/matrix_error.txt') }
let(:matrix_lines) { [{}] }
let(:matrix) { PactBroker::Client::Matrix::Resource.new(matrix: [{}]) }

it "doesn't blow up" do
expect(subject).to eq expected_matrix_lines
end
end

context "when some rows have a verification result URL and some don't" do
let(:matrix_lines) do
line_creator = -> { JSON.parse(File.read('spec/support/matrix.json'), symbolize_names: true)[:matrix].first }
line_1 = line_creator.call
line_2 = line_creator.call
line_3 = line_creator.call
line_2[:verificationResult] = nil
line_3[:verificationResult][:success] = false
[line_1, line_2, line_3]
end

let(:matrix) { PactBroker::Client::Matrix::Resource.new(matrix: matrix_lines) }

let(:expected_matrix_lines) { File.read('spec/support/matrix_with_results.txt') }

it "only provides a result number for the lines that have a result URL" do
expect(subject).to eq expected_matrix_lines
end
end
end
end
end
56 changes: 48 additions & 8 deletions spec/pacts/pact_broker_client-pact_broker.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,12 @@
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand Down Expand Up @@ -271,7 +276,12 @@
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand Down Expand Up @@ -321,7 +331,12 @@
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand Down Expand Up @@ -418,7 +433,12 @@
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand All @@ -439,7 +459,12 @@
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand Down Expand Up @@ -492,7 +517,12 @@
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand Down Expand Up @@ -542,7 +572,12 @@
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand Down Expand Up @@ -592,7 +627,12 @@
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand Down
7 changes: 6 additions & 1 deletion spec/support/matrix.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
Expand Down
6 changes: 3 additions & 3 deletions spec/support/matrix.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CONSUMER | C.VERSION | PROVIDER | P.VERSION | SUCCESS?
---------|-----------|----------|-----------|---------
Foo | 4 | Bar | 5 | true
CONSUMER | C.VERSION | PROVIDER | P.VERSION | SUCCESS? | RESULT#
---------|-----------|----------|-----------|----------|--------
Foo | 4 | Bar | 5 | true | 1
Loading

0 comments on commit 976950d

Please sign in to comment.