diff --git a/bin/pact-publish b/bin/pact-publish deleted file mode 100755 index 6a8df444..00000000 --- a/bin/pact-publish +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env ruby -require 'pact_broker/client/cli/publish' - -PactBroker::Client::CLI::Publish.start diff --git a/lib/pact_broker/client/cli/broker.rb b/lib/pact_broker/client/cli/broker.rb index 43ffe816..f5074137 100644 --- a/lib/pact_broker/client/cli/broker.rb +++ b/lib/pact_broker/client/cli/broker.rb @@ -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." @@ -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 { diff --git a/lib/pact_broker/client/cli/custom_thor.rb b/lib/pact_broker/client/cli/custom_thor.rb index 597fd202..fcd119a1 100644 --- a/lib/pact_broker/client/cli/custom_thor.rb +++ b/lib/pact_broker/client/cli/custom_thor.rb @@ -6,9 +6,7 @@ module CLI ## # Custom Thor task allows the following: # - # `script arg1 arg2` to be interpreted as `script 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 @@ -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 diff --git a/lib/pact_broker/client/cli/publish.rb b/lib/pact_broker/client/cli/publish.rb deleted file mode 100644 index c2ba7c32..00000000 --- a/lib/pact_broker/client/cli/publish.rb +++ /dev/null @@ -1,88 +0,0 @@ -require 'pact_broker/client/cli/custom_thor' -require 'pact_broker/client/publish_pacts' -require 'pact_broker/client/version' -require 'rake/file_list' - -module PactBroker - module Client - module CLI - - # Thor::Error will have its backtrace hidden - class PactPublicationError < ::Thor::Error; end - - class Publish < CustomThor - desc '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 broker(*pact_files) - validate(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 - - default_task :broker - - no_commands do - def self.exit_on_failure? - true - end - - def validate 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_password - { - basic_auth: { - username: options.broker_username, - password: options.broker_password - } - } - else - {} - end - end - end - end - end - end -end diff --git a/spec/lib/pact_broker/client/cli/broker_spec.rb b/spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb similarity index 100% rename from spec/lib/pact_broker/client/cli/broker_spec.rb rename to spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb diff --git a/spec/lib/pact_broker/client/cli/publish_spec.rb b/spec/lib/pact_broker/client/cli/broker_publish_spec.rb similarity index 96% rename from spec/lib/pact_broker/client/cli/publish_spec.rb rename to spec/lib/pact_broker/client/cli/broker_publish_spec.rb index 7d049642..833d314d 100644 --- a/spec/lib/pact_broker/client/cli/publish_spec.rb +++ b/spec/lib/pact_broker/client/cli/broker_publish_spec.rb @@ -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) @@ -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 @@ -26,7 +26,7 @@ module PactBroker::Client::CLI ["spec/support/cli_test_pacts/foo.json"], "1.2.3", [], - {} + {verbose: nil} ) invoke_broker end diff --git a/spec/lib/pact_broker/client/cli/custom_thor_spec.rb b/spec/lib/pact_broker/client/cli/custom_thor_spec.rb index 0a184904..06377288 100644 --- a/spec/lib/pact_broker/client/cli/custom_thor_spec.rb +++ b/spec/lib/pact_broker/client/cli/custom_thor_spec.rb @@ -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}