Skip to content

Commit

Permalink
feat(can-i-deploy): add --output option allowing table or json to be …
Browse files Browse the repository at this point in the history
…specified

For @mefellows
  • Loading branch information
bethesque committed Oct 10, 2017
1 parent 962b7d1 commit 57fa24e
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 47 deletions.
21 changes: 15 additions & 6 deletions lib/pact_broker/client/can_i_deploy.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'pact_broker/client/error'
require 'pact_broker/client/pact_broker_client'
require 'pact_broker/client/retry'
require 'pact_broker/client/matrix_text_formatter'
require 'pact_broker/client/matrix/formatter'


module PactBroker
Expand All @@ -17,13 +17,14 @@ def initialize success, message = nil
end
end

def self.call(pact_broker_base_url, version_selectors, pact_broker_client_options={})
new(pact_broker_base_url, version_selectors, pact_broker_client_options).call
def self.call(pact_broker_base_url, version_selectors, options, pact_broker_client_options={})
new(pact_broker_base_url, version_selectors, options, pact_broker_client_options).call
end

def initialize(pact_broker_base_url, version_selectors, pact_broker_client_options)
def initialize(pact_broker_base_url, version_selectors, options, pact_broker_client_options)
@pact_broker_base_url = pact_broker_base_url
@version_selectors = version_selectors
@options = options
@pact_broker_client_options = pact_broker_client_options
end

Expand All @@ -41,10 +42,18 @@ def call

private

attr_reader :pact_broker_base_url, :version_selectors, :pact_broker_client_options
attr_reader :pact_broker_base_url, :version_selectors, :options, :pact_broker_client_options

def success_message matrix
'Computer says yes \o/' + "\n\n" + MatrixTextFormatter.call(matrix)
message = Matrix::Formatter.call(matrix, format)
if format != 'json'
message = 'Computer says yes \o/' + "\n\n" + message
end
message
end

def format
options[:output]
end

def matrix
Expand Down
3 changes: 2 additions & 1 deletion lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ class Broker < Thor
method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
method_option :broker_username, aliases: "-n", desc: "Pact Broker basic auth username"
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
method_option :output, aliases: "-o", desc: "json or table", default: 'table'
method_option :verbose, aliases: "-v", desc: "Verbose output", :required => false

def can_i_deploy(*selectors)
result = CanIDeploy.call(options.broker_base_url, selectors, pact_broker_client_options)
result = CanIDeploy.call(options.broker_base_url, selectors, {output: options.output}, pact_broker_client_options)
if result.success
$stdout.puts result.message
else
Expand Down
20 changes: 20 additions & 0 deletions lib/pact_broker/client/matrix/formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'pact_broker/client/matrix/json_formatter'
require 'pact_broker/client/matrix/text_formatter'

module PactBroker
module Client
class Matrix
class Formatter
def self.call(matrix_lines, format)
formatter = case format
when 'json' then JsonFormatter
when 'table' then TextFormatter
else
raise PactBroker::Client::Error.new("Invalid output option '#{format}. Must be one of 'table' or 'json'.")
end
formatter.call(matrix_lines)
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/pact_broker/client/matrix/json_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'table_print'

module PactBroker
module Client
class Matrix
class JsonFormatter
def self.call(matrix_lines)
JSON.pretty_generate(matrix: matrix_lines)
end
end
end
end
end
33 changes: 33 additions & 0 deletions lib/pact_broker/client/matrix/text_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'table_print'

module PactBroker
module Client
class Matrix
class TextFormatter
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, :verificationResult, :verifiedAt),
)
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
end
31 changes: 0 additions & 31 deletions lib/pact_broker/client/matrix_text_formatter.rb

This file was deleted.

7 changes: 4 additions & 3 deletions spec/lib/pact_broker/client/can_i_deploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ module Client
let(:pact_broker_client_options) { { foo: 'bar' } }
let(:matrix_client) { instance_double('PactBroker::Client::Matrix') }
let(:matrix) { ['foo'] }
let(:options) { {output: 'text' } }

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')
allow(Matrix::Formatter).to receive(:call).and_return('text matrix')
end

subject { CanIDeploy.call(pact_broker_base_url, version_selectors, pact_broker_client_options) }
subject { CanIDeploy.call(pact_broker_base_url, version_selectors, options, pact_broker_client_options) }

it "retrieves the matrix from the pact broker" do
expect(matrix_client).to receive(:get).with(version_selectors)
subject
end

it "creates a text table out of the matrix" do
expect(MatrixTextFormatter).to receive(:call).with(matrix)
expect(Matrix::Formatter).to receive(:call).with(matrix, 'text')
subject
end

Expand Down
7 changes: 4 additions & 3 deletions spec/lib/pact_broker/client/cli/broker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ module CLI
let(:version_selectors) { ['Foo/version/1', 'Bar/version/1'] }
let(:minimum_valid_options) do
{
broker_base_url: 'http://pact-broker'
broker_base_url: 'http://pact-broker',
output: 'table'
}
end

let(:invoke_can_i_deploy) { subject.can_i_deploy(*version_selectors) }

it "invokes the CanIDeploy service" do
expect(CanIDeploy).to receive(:call).with('http://pact-broker', version_selectors, {})
expect(CanIDeploy).to receive(:call).with('http://pact-broker', version_selectors, {output: 'table'}, {})
invoke_can_i_deploy
end

Expand All @@ -34,7 +35,7 @@ module CLI
end

it "invokes the CanIDeploy service with the basic auth credentials" do
expect(CanIDeploy).to receive(:call).with(anything, anything, {username: "foo", password: "bar"})
expect(CanIDeploy).to receive(:call).with(anything, anything, anything, {username: "foo", password: "bar"})
invoke_can_i_deploy
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require 'pact_broker/client/matrix_text_formatter'
require 'pact_broker/client/matrix/text_formatter'

module PactBroker
module Client
describe MatrixTextFormatter do
describe Matrix::TextFormatter 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") }
subject { Matrix::TextFormatter.call(matrix_lines).split("\n").collect(&:strip).join("\n") }

context "with valid data" do
it "it has the right text" do
Expand Down

2 comments on commit 57fa24e

@mefellows
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm famous - my name is in a git commit message! :)

@bethesque
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're immortalised now.

Please sign in to comment.