From 57fa24e44efc4d8aa42bb855a8217f145b5b1b5b Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 10 Oct 2017 15:56:25 +1100 Subject: [PATCH] feat(can-i-deploy): add --output option allowing table or json to be specified For @mefellows --- lib/pact_broker/client/can_i_deploy.rb | 21 ++++++++---- lib/pact_broker/client/cli/broker.rb | 3 +- lib/pact_broker/client/matrix/formatter.rb | 20 +++++++++++ .../client/matrix/json_formatter.rb | 13 ++++++++ .../client/matrix/text_formatter.rb | 33 +++++++++++++++++++ .../client/matrix_text_formatter.rb | 31 ----------------- .../pact_broker/client/can_i_deploy_spec.rb | 7 ++-- .../lib/pact_broker/client/cli/broker_spec.rb | 7 ++-- .../text_formatter_spec.rb} | 6 ++-- 9 files changed, 94 insertions(+), 47 deletions(-) create mode 100644 lib/pact_broker/client/matrix/formatter.rb create mode 100644 lib/pact_broker/client/matrix/json_formatter.rb create mode 100644 lib/pact_broker/client/matrix/text_formatter.rb delete mode 100644 lib/pact_broker/client/matrix_text_formatter.rb rename spec/lib/pact_broker/client/{matrix_text_formatter_spec.rb => matrix/text_formatter_spec.rb} (80%) diff --git a/lib/pact_broker/client/can_i_deploy.rb b/lib/pact_broker/client/can_i_deploy.rb index a7896075..2d4c96ea 100644 --- a/lib/pact_broker/client/can_i_deploy.rb +++ b/lib/pact_broker/client/can_i_deploy.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/pact_broker/client/cli/broker.rb b/lib/pact_broker/client/cli/broker.rb index 03c6804f..9b98228a 100644 --- a/lib/pact_broker/client/cli/broker.rb +++ b/lib/pact_broker/client/cli/broker.rb @@ -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 diff --git a/lib/pact_broker/client/matrix/formatter.rb b/lib/pact_broker/client/matrix/formatter.rb new file mode 100644 index 00000000..84a79a45 --- /dev/null +++ b/lib/pact_broker/client/matrix/formatter.rb @@ -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 diff --git a/lib/pact_broker/client/matrix/json_formatter.rb b/lib/pact_broker/client/matrix/json_formatter.rb new file mode 100644 index 00000000..54b78f5b --- /dev/null +++ b/lib/pact_broker/client/matrix/json_formatter.rb @@ -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 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..1540853e --- /dev/null +++ b/lib/pact_broker/client/matrix/text_formatter.rb @@ -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 diff --git a/lib/pact_broker/client/matrix_text_formatter.rb b/lib/pact_broker/client/matrix_text_formatter.rb deleted file mode 100644 index eb3b43b4..00000000 --- a/lib/pact_broker/client/matrix_text_formatter.rb +++ /dev/null @@ -1,31 +0,0 @@ -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, :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 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 2f5e9921..a1de4dec 100644 --- a/spec/lib/pact_broker/client/can_i_deploy_spec.rb +++ b/spec/lib/pact_broker/client/can_i_deploy_spec.rb @@ -8,14 +8,15 @@ 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) @@ -23,7 +24,7 @@ module Client 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 diff --git a/spec/lib/pact_broker/client/cli/broker_spec.rb b/spec/lib/pact_broker/client/cli/broker_spec.rb index cd5b53da..986bdb27 100644 --- a/spec/lib/pact_broker/client/cli/broker_spec.rb +++ b/spec/lib/pact_broker/client/cli/broker_spec.rb @@ -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 @@ -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 diff --git a/spec/lib/pact_broker/client/matrix_text_formatter_spec.rb b/spec/lib/pact_broker/client/matrix/text_formatter_spec.rb similarity index 80% rename from spec/lib/pact_broker/client/matrix_text_formatter_spec.rb rename to spec/lib/pact_broker/client/matrix/text_formatter_spec.rb index 49241f9a..d49e329f 100644 --- a/spec/lib/pact_broker/client/matrix_text_formatter_spec.rb +++ b/spec/lib/pact_broker/client/matrix/text_formatter_spec.rb @@ -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