Skip to content

Commit

Permalink
feat: renamed pact-publish command to pact-broker publish
Browse files Browse the repository at this point in the history
BREAKING CHANGE
Since the pact-publish command was written, more commands have been written that will belong
in the `pact-broker` CLI family, so it's better to move it now before anyone is using it.
Sorry for the inconvenience.
  • Loading branch information
bethesque committed Oct 19, 2017
1 parent 24a5473 commit ed7d23a
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 148 deletions.
4 changes: 0 additions & 4 deletions bin/pact-publish

This file was deleted.

60 changes: 58 additions & 2 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
require 'pact_broker/client/can_i_deploy'
require 'pact_broker/client/version'
require 'pact_broker/client/cli/version_selector_options_parser'
require 'thor'
require 'pact_broker/client/cli/custom_thor'
require 'pact_broker/client/publish_pacts'
require 'rake/file_list'

module PactBroker
module Client
module CLI
class Broker < Thor
# Thor::Error will have its backtrace hidden
class PactPublicationError < ::Thor::Error; end

class Broker < CustomThor
desc 'can-i-deploy', "Returns exit code 0 or 1, indicating whether or not the specified application versions are compatible."

method_option :name, required: true, aliases: "-n", desc: "The application name. Use once for each pacticipant being checked."
Expand All @@ -24,12 +29,63 @@ def can_i_deploy
exit(1) unless result.success
end

desc 'publish PACT_DIRS_OR_FILES ...', "Publish pacts to a Pact Broker."
method_option :consumer_app_version, required: true, aliases: "-a", desc: "The consumer application version"
method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
method_option :tag, aliases: "-t", type: :array, banner: "TAG", desc: "Tag name for consumer version. Can be specified multiple times."
method_option :verbose, aliases: "-v", desc: "Verbose output", :required => false

def publish(*pact_files)
validate_pact_files(pact_files)
success = publish_pacts(pact_files)
raise PactPublicationError, "One or more pacts failed to be published" unless success
rescue PactBroker::Client::Error => e
raise PactPublicationError, "#{e.class} - #{e.message}"
end

desc 'version', "Show the pact_broker-client gem version"
def version
$stdout.puts PactBroker::Client::VERSION
end

no_commands do

def self.exit_on_failure?
true
end

def validate_pact_files pact_files
unless pact_files && pact_files.any?
raise RequiredArgumentMissingError, "No value provided for required pact_files"
end
end

def publish_pacts pact_files
PactBroker::Client::PublishPacts.call(
options.broker_base_url,
file_list(pact_files),
options.consumer_app_version,
tags,
pact_broker_client_options
)
end

def file_list pact_files
Rake::FileList[pact_files].collect do | path |
if File.directory?(path)
Rake::FileList[File.join(path, "*.json")]
else
path
end
end.flatten
end

def tags
[*options.tag].compact
end

def pact_broker_client_options
if options.broker_username
{
Expand Down
20 changes: 1 addition & 19 deletions lib/pact_broker/client/cli/custom_thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ module CLI
##
# Custom Thor task allows the following:
#
# `script arg1 arg2` to be interpreted as `script <default_task> arg1 arg2`
# `--option 1 --option 2` to be interpreted as `--option 1 2` (the standard Thor format for multiple value options)
# `script --help` to display the help for the default task instead of the command list
#
class CustomThor < ::Thor

Expand All @@ -17,24 +15,8 @@ def self.start given_args = ARGV, config = {}
super(massage_args(given_args))
end

def help *args
if args.empty?
super(self.class.default_task)
else
super
end
end

def self.massage_args argv
prepend_default_task_name(turn_muliple_tag_options_into_array(argv))
end

def self.prepend_default_task_name argv
if known_first_arguments.include?(argv[0])
argv
else
[default_command] + argv
end
turn_muliple_tag_options_into_array(argv)
end

# other task names, help, and the help shortcuts
Expand Down
88 changes: 0 additions & 88 deletions lib/pact_broker/client/cli/publish.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'pact_broker/client/cli/publish'
require 'pact_broker/client/cli/broker'

module PactBroker::Client::CLI
describe Publish do
describe Broker do
describe ".broker" do
before do
allow(PactBroker::Client::PublishPacts).to receive(:call).and_return(true)
Expand All @@ -17,7 +17,7 @@ module PactBroker::Client::CLI
}
end

let(:invoke_broker) { subject.broker(*file_list) }
let(:invoke_broker) { subject.publish(*file_list) }

context "with minimum valid options" do
it "invokes the PublishPacts command" do
Expand All @@ -26,7 +26,7 @@ module PactBroker::Client::CLI
["spec/support/cli_test_pacts/foo.json"],
"1.2.3",
[],
{}
{verbose: nil}
)
invoke_broker
end
Expand Down
31 changes: 0 additions & 31 deletions spec/lib/pact_broker/client/cli/custom_thor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,11 @@ def test_multiple_options
describe CustomThor do
subject { TestThor.new }

it "invokes the default task when aguments are given without specifying a task" do
expect(Delegate).to receive(:call).with(argument: 'foo')
TestThor.start(%w{foo})
end

it "converts options that are specified multiple times into a single array" do
expect(Delegate).to receive(:call).with({'multi' => ['one', 'two']})
TestThor.start(%w{test_multiple_options --multi one --multi two})
end

describe ".prepend_default_task_name" do
let(:argv_with) { [TestThor.default_command, 'foo'] }

context "when the default task name is given" do
it "does not prepend the default task name" do
expect(TestThor.prepend_default_task_name(argv_with)).to eq(argv_with)
end
end

context "when the first argument is --help" do
let(:argv) { ['--help', 'foo'] }

it "does not prepend the default task name" do
expect(TestThor.prepend_default_task_name(argv)).to eq(argv)
end
end

context "when the default task name is not given" do
let(:argv) { ['foo'] }

it "prepends the default task name" do
expect(TestThor.prepend_default_task_name(argv)).to eq(argv_with)
end
end
end

describe ".turn_muliple_tag_options_into_array" do
it "turns '--tag foo --tag bar' into '--tag foo bar'" do
input = %w{--ignore this --tag foo --tag bar --wiffle --that}
Expand Down

0 comments on commit ed7d23a

Please sign in to comment.