diff --git a/example/scripts/publish-pact.sh b/example/scripts/publish-pact.sh index 3bfbdbd3..7811e414 100755 --- a/example/scripts/publish-pact.sh +++ b/example/scripts/publish-pact.sh @@ -1,3 +1,3 @@ # assumes you've set PACT_BROKER_BASE_URL, PACT_BROKER_USERNAME and PACT_BROKER_PASSWORD already -bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --consumer-app-version=1.0.0 --tag master +bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --consumer-app-version=1.0.0 --tag master --verbose diff --git a/lib/pact_broker/client/cli/broker.rb b/lib/pact_broker/client/cli/broker.rb index f16ac6ef..b8412c07 100644 --- a/lib/pact_broker/client/cli/broker.rb +++ b/lib/pact_broker/client/cli/broker.rb @@ -57,8 +57,9 @@ def publish(*pact_files) require 'pact_broker/client/error' validate_credentials validate_pact_files(pact_files) - success = publish_pacts(pact_files) - raise PactPublicationError, "One or more pacts failed to be published" unless success + result = publish_pacts(pact_files) + $stdout.puts result.message + exit(1) unless result.success rescue PactBroker::Client::Error => e raise PactPublicationError, "#{e.class} - #{e.message}" end diff --git a/lib/pact_broker/client/publish_pacts.rb b/lib/pact_broker/client/publish_pacts.rb index 439057de..cbd4b5d1 100644 --- a/lib/pact_broker/client/publish_pacts.rb +++ b/lib/pact_broker/client/publish_pacts.rb @@ -1,11 +1,7 @@ require 'term/ansicolor' -require 'pact_broker/client' -require 'pact_broker/client/retry' -require 'pact_broker/client/pact_file' -require 'pact_broker/client/pact_hash' -require 'pact_broker/client/merge_pacts' require 'pact_broker/client/hal_client_methods' -require 'pact_broker/client/hash_refinements' +require 'base64' +require 'pact_broker/client/publish_pacts_the_old_way' module PactBroker module Client @@ -30,153 +26,100 @@ def initialize pact_broker_base_url, pact_file_paths, consumer_version_params, p def call validate - $stdout.puts("") - result = create_consumer_versions && apply_tags && publish_pacts - $stdout.puts("") - result + if index_resource.can?("pb:publish-contracts") + publish_pacts + PactBroker::Client::CommandResult.new(success?, message) + else + PublishPactsTheOldWay.call(pact_broker_base_url, pact_file_paths, consumer_version_params, pact_broker_client_options) + end end private - attr_reader :pact_broker_base_url, :pact_file_paths, :consumer_version_number, :branch, :tags, :build_url, :pact_broker_client_options, :version_required - - def pact_broker_client - @pact_broker_client ||= PactBroker::Client::PactBrokerClient.new(base_url: pact_broker_base_url, client_options: pact_broker_client_options) - end - - def index_entry_point - @index_entry_point ||= create_index_entry_point(pact_broker_base_url, pact_broker_client_options) - end - - def index_resource - @index_resource ||= Retry.while_error do - index_entry_point.get! - end - end - - def can_create_version_with_branch? - @can_create_version_with_branch ||= index_resource.can?('pb:pacticipant-version') - end + attr_reader :pact_broker_base_url, :pact_file_paths, :consumer_version_number, :branch, :tags, :build_url, :pact_broker_client_options, :response_entities - def merge_on_server? - pact_broker_client_options[:write] == :merge + def request_body_for(consumer_name) + { + pacticipantName: consumer_name, + versionNumber: consumer_version_number, + tags: tags, + branch: branch, + buildUrl: build_url, + contracts: contracts_for(consumer_name) + }.compact end def publish_pacts - pact_files.group_by(&:pact_name).collect do | pact_name, pact_files | - $stdout.puts "Merging #{pact_files.collect(&:path).join(", ")}" if pact_files.size > 1 - publish_pact(PactHash[merge_contents(pact_files)]) - end.all? - end - - def merge_contents(pact_files) - MergePacts.call(pact_files.collect(&:pact_hash)) - end - - def pact_files - @pact_files ||= pact_file_paths.collect{ |pact_file_path| PactFile.new(pact_file_path) } + @response_entities = consumer_names.collect do | consumer_name | + index_resource._link("pb:publish-contracts").post(request_body_for(consumer_name)) + end end - def consumer_names - pact_files.collect(&:consumer_name).uniq + def success? + response_entities.all?(&:success?) end - def publish_pact pact - begin - $stdout.puts "Publishing #{pact.pact_name} to pact broker at #{pact_broker_base_url}" - publish_pact_contents pact - rescue => e - $stderr.puts "Failed to publish #{pact.pact_name} due to error: #{e.class} - #{e}" - false - end + def message + response_entities.flat_map do | response_entity | + if response_entity.success? + response_entity.logs.collect do | log | + colorized_message(log) + end + else + ::Term::ANSIColor.red(response_entity.response.body.to_s) + end + end.join("\n") end - def create_consumer_versions - if create_versions? - consumer_names.collect do | consumer_name | - create_version(index_resource, consumer_name) - end - true + def colorized_message(log) + color = color_for_level(log["level"]) + if color + ::Term::ANSIColor.send(color, log["message"]) else - true + log["message"] end end - def create_versions? - if version_required - if can_create_version_with_branch? - true - else - raise PactBroker::Client::Error.new("This version of the Pact Broker does not support versions with branches or build URLs. Please upgrade your broker to 2.76.2 or later.") - end - elsif (branch || build_url) && can_create_version_with_branch? - true - else - false + def color_for_level(level) + case level + when "warn" then :yellow + when "error" then :red + when "info" then :green + else nil end end - def create_version(index_resource, consumer_name) - Retry.while_error do - version_resource = index_resource._link('pb:pacticipant-version').expand(version: consumer_version_number, pacticipant: consumer_name).put(version_body).assert_success! - message = if version_resource.response.status == 200 - "Replaced version #{consumer_version_number} of #{consumer_name}" - else - "Created version #{consumer_version_number} of #{consumer_name}" - end + def contracts_for(consumer_name) + pact_files_for(consumer_name).collect do | pact_file | + end - message = message + " (branch #{branch})" if branch - $stdout.puts message - if version_resource.response.status == 200 - $stdout.puts ::Term::ANSIColor.yellow("Replacing the version resource is not recommended under normal circumstances and may indicate that you have not configured your Pact pipeline correctly (unless you are just re-running a build for a particular commit). For more information see https://docs.pact.io/versioning") - end - true + pact_files_for(consumer_name).group_by(&:pact_name).values.collect do | pact_files | + $stderr.puts "Merging #{pact_files.collect(&:path).join(", ")}" if pact_files.size > 1 + pact_hash = PactHash[merge_contents(pact_files)] + { + role: "consumer", + providerName: pact_hash.provider_name, + specification: "pact", + contentType: "application/json", + content: Base64.strict_encode64(pact_hash.to_json) + } end end - def version_body - { - branch: branch, - buildUrl: build_url - }.compact + def merge_contents(pact_files) + MergePacts.call(pact_files.collect(&:pact_hash)) end - def apply_tags - return true if tags.empty? - tags.all? do | tag | - tag_consumer_version tag - end + def pact_files + @pact_files ||= pact_file_paths.collect{ |pact_file_path| PactFile.new(pact_file_path) } end - def tag_consumer_version tag - versions = pact_broker_client.pacticipants.versions - Retry.while_error do - consumer_names.collect do | consumer_name | - versions.tag(pacticipant: consumer_name, version: consumer_version_number, tag: tag) - $stdout.puts "Tagged version #{consumer_version_number} of #{consumer_name} as #{tag.inspect}" - true - end - end - rescue => e - $stderr.puts "Failed to tag version due to error: #{e.class} - #{e}" - false + def pact_files_for(consumer_name) + pact_files.select{ | pact_file | pact_file.consumer_name == consumer_name } end - def publish_pact_contents(pact) - Retry.while_error do - pacts = pact_broker_client.pacticipants.versions.pacts - if pacts.version_published?(consumer: pact.consumer_name, provider: pact.provider_name, consumer_version: consumer_version_number) - if merge_on_server? - $stdout.puts "A pact for this consumer version is already published. Merging contents." - else - $stdout.puts ::Term::ANSIColor.yellow("A pact for this consumer version is already published. Overwriting. (Note: Overwriting pacts is not recommended as it can lead to race conditions. Best practice is to provide a unique consumer version number for each publication. For more information, see https://docs.pact.io/versioning)") - end - end - - latest_pact_url = pacts.publish(pact_hash: pact, consumer_version: consumer_version_number) - $stdout.puts "The latest version of this pact can be accessed at the following URL:\n#{latest_pact_url}" - true - end + def consumer_names + pact_files.collect(&:consumer_name).uniq end def validate diff --git a/lib/pact_broker/client/publish_pacts_the_old_way.rb b/lib/pact_broker/client/publish_pacts_the_old_way.rb new file mode 100644 index 00000000..306610f5 --- /dev/null +++ b/lib/pact_broker/client/publish_pacts_the_old_way.rb @@ -0,0 +1,194 @@ +require 'term/ansicolor' +require 'pact_broker/client' +require 'pact_broker/client/retry' +require 'pact_broker/client/pact_file' +require 'pact_broker/client/pact_hash' +require 'pact_broker/client/merge_pacts' +require 'pact_broker/client/hal_client_methods' +require 'pact_broker/client/hash_refinements' +require 'pact_broker/client/command_result' + +module PactBroker + module Client + class PublishPactsTheOldWay + using PactBroker::Client::HashRefinements + include HalClientMethods + + def self.call(pact_broker_base_url, pact_file_paths, consumer_version_params, pact_broker_client_options={}) + new(pact_broker_base_url, pact_file_paths, consumer_version_params, pact_broker_client_options).call + end + + def initialize pact_broker_base_url, pact_file_paths, consumer_version_params, pact_broker_client_options={} + @pact_broker_base_url = pact_broker_base_url + @pact_file_paths = pact_file_paths + @consumer_version_number = consumer_version_params[:number].respond_to?(:strip) ? consumer_version_params[:number].strip : consumer_version_params[:number] + @branch = consumer_version_params[:branch] + @build_url = consumer_version_params[:build_url] + @tags = consumer_version_params[:tags] ? consumer_version_params[:tags].collect{ |tag| tag.respond_to?(:strip) ? tag.strip : tag } : [] + @version_required = consumer_version_params[:version_required] + @pact_broker_client_options = pact_broker_client_options + end + + def call + validate + $stdout.puts("") + result = create_consumer_versions && apply_tags && publish_pacts + $stdout.puts("") + if result + PactBroker::Client::CommandResult.new(true) + else + PactBroker::Client::CommandResult.new(false, "One or more pacts failed to be published") + end + end + + private + + attr_reader :pact_broker_base_url, :pact_file_paths, :consumer_version_number, :branch, :tags, :build_url, :pact_broker_client_options, :version_required + + def pact_broker_client + @pact_broker_client ||= PactBroker::Client::PactBrokerClient.new(base_url: pact_broker_base_url, client_options: pact_broker_client_options) + end + + def index_entry_point + @index_entry_point ||= create_index_entry_point(pact_broker_base_url, pact_broker_client_options) + end + + def index_resource + @index_resource ||= Retry.while_error do + index_entry_point.get! + end + end + + def can_create_version_with_branch? + @can_create_version_with_branch ||= index_resource.can?('pb:pacticipant-version') + end + + def merge_on_server? + pact_broker_client_options[:write] == :merge + end + + def publish_pacts + pact_files.group_by(&:pact_name).collect do | pact_name, pact_files | + $stdout.puts "Merging #{pact_files.collect(&:path).join(", ")}" if pact_files.size > 1 + publish_pact(PactHash[merge_contents(pact_files)]) + end.all? + end + + def merge_contents(pact_files) + MergePacts.call(pact_files.collect(&:pact_hash)) + end + + def pact_files + @pact_files ||= pact_file_paths.collect{ |pact_file_path| PactFile.new(pact_file_path) } + end + + def consumer_names + pact_files.collect(&:consumer_name).uniq + end + + def publish_pact pact + begin + $stdout.puts "Publishing #{pact.pact_name} to pact broker at #{pact_broker_base_url}" + publish_pact_contents pact + rescue => e + $stderr.puts "Failed to publish #{pact.pact_name} due to error: #{e.class} - #{e}" + false + end + end + + def create_consumer_versions + if create_versions? + consumer_names.collect do | consumer_name | + create_version(index_resource, consumer_name) + end + true + else + true + end + end + + def create_versions? + if version_required + if can_create_version_with_branch? + true + else + raise PactBroker::Client::Error.new("This version of the Pact Broker does not support versions with branches or build URLs. Please upgrade your broker to 2.76.2 or later.") + end + elsif (branch || build_url) && can_create_version_with_branch? + true + else + false + end + end + + def create_version(index_resource, consumer_name) + Retry.while_error do + version_resource = index_resource._link('pb:pacticipant-version').expand(version: consumer_version_number, pacticipant: consumer_name).put(version_body).assert_success! + message = if version_resource.response.status == 200 + "Replaced version #{consumer_version_number} of #{consumer_name}" + else + "Created version #{consumer_version_number} of #{consumer_name}" + end + + message = message + " (branch #{branch})" if branch + $stdout.puts message + if version_resource.response.status == 200 + $stdout.puts ::Term::ANSIColor.yellow("Replacing the version resource is not recommended under normal circumstances and may indicate that you have not configured your Pact pipeline correctly (unless you are just re-running a build for a particular commit). For more information see https://docs.pact.io/versioning") + end + true + end + end + + def version_body + { + branch: branch, + buildUrl: build_url + }.compact + end + + def apply_tags + return true if tags.empty? + tags.all? do | tag | + tag_consumer_version tag + end + end + + def tag_consumer_version tag + versions = pact_broker_client.pacticipants.versions + Retry.while_error do + consumer_names.collect do | consumer_name | + versions.tag(pacticipant: consumer_name, version: consumer_version_number, tag: tag) + $stdout.puts "Tagged version #{consumer_version_number} of #{consumer_name} as #{tag.inspect}" + true + end + end + rescue => e + $stderr.puts "Failed to tag version due to error: #{e.class} - #{e}" + false + end + + def publish_pact_contents(pact) + Retry.while_error do + pacts = pact_broker_client.pacticipants.versions.pacts + if pacts.version_published?(consumer: pact.consumer_name, provider: pact.provider_name, consumer_version: consumer_version_number) + if merge_on_server? + $stdout.puts "A pact for this consumer version is already published. Merging contents." + else + $stdout.puts ::Term::ANSIColor.yellow("A pact for this consumer version is already published. Overwriting. (Note: Overwriting pacts is not recommended as it can lead to race conditions. Best practice is to provide a unique consumer version number for each publication. For more information, see https://docs.pact.io/versioning)") + end + end + + latest_pact_url = pacts.publish(pact_hash: pact, consumer_version: consumer_version_number) + $stdout.puts "The latest version of this pact can be accessed at the following URL:\n#{latest_pact_url}" + true + end + end + + def validate + raise PactBroker::Client::Error.new("Please specify the consumer_version_number") unless (consumer_version_number && consumer_version_number.to_s.strip.size > 0) + raise PactBroker::Client::Error.new("Please specify the pact_broker_base_url") unless (pact_broker_base_url && pact_broker_base_url.to_s.strip.size > 0) + raise PactBroker::Client::Error.new("No pact files found") unless (pact_file_paths && pact_file_paths.any?) + end + end + end +end diff --git a/lib/pact_broker/client/tasks/publication_task.rb b/lib/pact_broker/client/tasks/publication_task.rb index 1e612f8e..e7d7f2c0 100644 --- a/lib/pact_broker/client/tasks/publication_task.rb +++ b/lib/pact_broker/client/tasks/publication_task.rb @@ -58,12 +58,12 @@ def rake_task &block desc "Publish pacts to pact broker" task task_name do block.call(self) - require 'pact_broker/client/publish_pacts' + require 'pact_broker/client/publish_pacts_the_old_way' pact_broker_client_options = { write: write_method, token: pact_broker_token } pact_broker_client_options[:basic_auth] = pact_broker_basic_auth if pact_broker_basic_auth && pact_broker_basic_auth.any? pact_broker_client_options.compact! consumer_version_params = { number: consumer_version, branch: the_branch, build_url: build_url, tags: all_tags, version_required: version_required }.compact - success = PactBroker::Client::PublishPacts.new(pact_broker_base_url, FileList[pattern], consumer_version_params, pact_broker_client_options).call + success = PactBroker::Client::PublishPactsTheOldWay.new(pact_broker_base_url, FileList[pattern], consumer_version_params, pact_broker_client_options).call raise "One or more pacts failed to be published" unless success end end diff --git a/script/publish-pact.sh b/script/publish-pact.sh index 7560e17f..2eda2aab 100755 --- a/script/publish-pact.sh +++ b/script/publish-pact.sh @@ -2,6 +2,7 @@ bundle exec bin/pact-broker publish spec/pacts/pact_broker_client-pact_broker.js --consumer-app-version 1.2.7 \ --broker-base-url http://localhost:9292 \ --broker-username localhost --broker-password localhost \ - --auto-detect-branch \ - --build-url http://mybuild + --auto-detect-version-properties \ + --build-url http://mybuild \ + --branch master diff --git a/spec/lib/pact_broker/client/cli/broker_publish_spec.rb b/spec/lib/pact_broker/client/cli/broker_publish_spec.rb index bee53730..43617a78 100644 --- a/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +++ b/spec/lib/pact_broker/client/cli/broker_publish_spec.rb @@ -1,12 +1,12 @@ require 'pact_broker/client/cli/broker' -require 'pact_broker/client/publish_pacts' +require 'pact_broker/client/publish_pacts_the_old_way' require 'pact_broker/client/git' module PactBroker::Client::CLI describe Broker do describe ".broker" do before do - allow(PactBroker::Client::PublishPacts).to receive(:call).and_return(true) + allow(PactBroker::Client::PublishPactsTheOldWay).to receive(:call).and_return(true) allow(PactBroker::Client::Git).to receive(:branch).and_return("bar") subject.options = OpenStruct.new(minimum_valid_options) end @@ -23,8 +23,8 @@ module PactBroker::Client::CLI let(:invoke_broker) { subject.publish(*file_list) } context "with minimum valid options" do - it "invokes the PublishPacts command" do - expect(PactBroker::Client::PublishPacts).to receive(:call).with( + it "invokes the PublishPactsTheOldWay command" do + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:call).with( "http://pact-broker", ["spec/support/cli_test_pacts/foo.json"], { number: "1.2.3", tags: [], version_required: false }, @@ -37,8 +37,8 @@ module PactBroker::Client::CLI context "with a file pattern specified" do let(:file_list) { ["spec/support/cli_test_pacts/*.json"] } - it "invokes the PublishPacts command with the expanded list of files" do - expect(PactBroker::Client::PublishPacts).to receive(:call).with( + it "invokes the PublishPactsTheOldWay command with the expanded list of files" do + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:call).with( anything, ["spec/support/cli_test_pacts/bar.json", "spec/support/cli_test_pacts/foo.json"], anything, @@ -51,8 +51,8 @@ module PactBroker::Client::CLI context "with a pact directory specified" do let(:file_list) { ["spec/support/cli_test_pacts"] } - it "invokes the PublishPacts command with the list of files in the directory" do - expect(PactBroker::Client::PublishPacts).to receive(:call).with( + it "invokes the PublishPactsTheOldWay command with the list of files in the directory" do + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:call).with( anything, ["spec/support/cli_test_pacts/bar.json", "spec/support/cli_test_pacts/foo.json"], anything, @@ -65,8 +65,8 @@ module PactBroker::Client::CLI context "with a windows directory specified" do let(:file_list) { ['spec\\support\cli_test_pacts'] } - it "invokes the PublishPacts command with the list of files in the directory" do - expect(PactBroker::Client::PublishPacts).to receive(:call).with( + it "invokes the PublishPactsTheOldWay command with the list of files in the directory" do + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:call).with( anything, ["spec/support/cli_test_pacts/bar.json", "spec/support/cli_test_pacts/foo.json"], anything, @@ -82,7 +82,7 @@ module PactBroker::Client::CLI end it "passes in the tag" do - expect(PactBroker::Client::PublishPacts).to receive(:call).with( + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:call).with( anything, anything, hash_including(tags: ['foo']), @@ -105,7 +105,7 @@ module PactBroker::Client::CLI end it "adds it to the list of tags when publishing" do - expect(PactBroker::Client::PublishPacts).to receive(:call).with( + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:call).with( anything, anything, hash_including(tags: ['foo', 'bar']), @@ -123,7 +123,7 @@ module PactBroker::Client::CLI end it "passes in the branch option" do - expect(PactBroker::Client::PublishPacts).to receive(:call).with( + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:call).with( anything, anything, hash_including(branch: "main", version_required: true), @@ -147,7 +147,7 @@ module PactBroker::Client::CLI end it "passes in the auto detected branch option with version_required: false" do - expect(PactBroker::Client::PublishPacts).to receive(:call).with( + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:call).with( anything, anything, hash_including(branch: "bar", version_required: false), @@ -172,7 +172,7 @@ module PactBroker::Client::CLI end it "passes in the auto detected branch option with version_required: true" do - expect(PactBroker::Client::PublishPacts).to receive(:call).with( + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:call).with( anything, anything, hash_including(branch: "bar", version_required: true), @@ -189,7 +189,7 @@ module PactBroker::Client::CLI end it "uses the specified branch" do - expect(PactBroker::Client::PublishPacts).to receive(:call).with( + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:call).with( anything, anything, hash_including(branch: "specified-branch", version_required: true), @@ -208,7 +208,7 @@ module PactBroker::Client::CLI end it "passes in the branch option" do - expect(PactBroker::Client::PublishPacts).to receive(:call).with( + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:call).with( anything, anything, hash_including(build_url: "http://ci"), @@ -226,7 +226,7 @@ module PactBroker::Client::CLI end it "passes in the basic auth options" do - expect(PactBroker::Client::PublishPacts).to receive(:call).with( + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:call).with( anything, anything, anything, @@ -258,7 +258,7 @@ module PactBroker::Client::CLI context "when an error is raised publishing" do before do - allow(PactBroker::Client::PublishPacts).to receive(:call).and_raise(PactBroker::Client::Error.new('foo')) + allow(PactBroker::Client::PublishPactsTheOldWay).to receive(:call).and_raise(PactBroker::Client::Error.new('foo')) end it "raises a PactPublicationError" do @@ -268,7 +268,7 @@ module PactBroker::Client::CLI context "when the publish command is not successful" do before do - allow(PactBroker::Client::PublishPacts).to receive(:call).and_return(false) + allow(PactBroker::Client::PublishPactsTheOldWay).to receive(:call).and_return(false) end it "raises a PactPublicationError" do diff --git a/spec/lib/pact_broker/client/publish_pacts_spec.rb b/spec/lib/pact_broker/client/publish_pacts_the_old_way_spec.rb similarity index 95% rename from spec/lib/pact_broker/client/publish_pacts_spec.rb rename to spec/lib/pact_broker/client/publish_pacts_the_old_way_spec.rb index 37633ece..9b6ccd9a 100644 --- a/spec/lib/pact_broker/client/publish_pacts_spec.rb +++ b/spec/lib/pact_broker/client/publish_pacts_the_old_way_spec.rb @@ -1,11 +1,11 @@ require 'spec_helper' require 'fakefs/safe' -require 'pact_broker/client/publish_pacts' +require 'pact_broker/client/publish_pacts_the_old_way' require 'json' module PactBroker module Client - describe PublishPacts do + describe PublishPactsTheOldWay do # The amount of stubbing that we have to do here indicates this class is doing # TOO MUCH and needs to be split up! @@ -23,7 +23,7 @@ module Client File.open("spec/pacts/consumer-provider.json", "w") { |file| file << pact_hash.to_json } File.open("spec/pacts/consumer-provider-2.json", "w") { |file| file << pact_hash.to_json } File.open("spec/pacts/foo-bar.json", "w") { |file| file << pact_hash_2.to_json } - allow_any_instance_of(PublishPacts).to receive(:create_index_entry_point).and_return(index_entry_point) + allow_any_instance_of(PublishPactsTheOldWay).to receive(:create_index_entry_point).and_return(index_entry_point) end after do @@ -64,7 +64,7 @@ module Client let(:index_resource) { instance_double("PactBroker::Client::Hal::Entity", can?: can_create_version ) } let(:can_create_version) { false } - subject { PublishPacts.new(pact_broker_base_url, pact_file_paths, consumer_version_params, pact_broker_client_options) } + subject { PublishPactsTheOldWay.new(pact_broker_base_url, pact_file_paths, consumer_version_params, pact_broker_client_options) } describe "call" do it "creates a PactBroker Client" do @@ -85,7 +85,7 @@ module Client end it "returns true" do - expect(subject.call).to be true + expect(subject.call.success).to eq true end end @@ -140,7 +140,7 @@ module Client end it "returns false" do - expect(subject.call).to be false + expect(subject.call.success).to be false end end @@ -212,7 +212,7 @@ module Client end it "returns false" do - expect(subject.call).to eq false + expect(subject.call.success).to eq false end end end @@ -230,7 +230,7 @@ module Client end it "returns false" do - expect(subject.call).to eq false + expect(subject.call.success).to eq false end end @@ -255,7 +255,7 @@ module Client end it "returns true" do - expect(subject.call).to eq true + expect(subject.call.success).to eq true end end diff --git a/spec/lib/pact_broker/client/tasks/publication_task_spec.rb b/spec/lib/pact_broker/client/tasks/publication_task_spec.rb index b8110cda..2a2432df 100644 --- a/spec/lib/pact_broker/client/tasks/publication_task_spec.rb +++ b/spec/lib/pact_broker/client/tasks/publication_task_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' require 'pact_broker/client/tasks/publication_task' -require 'pact_broker/client/publish_pacts' +require 'pact_broker/client/publish_pacts_the_old_way' module PactBroker::Client describe PublicationTask do @@ -9,11 +9,11 @@ module PactBroker::Client @consumer_version = "1.2.3" end - let(:publish_pacts) { instance_double("PactBroker::ClientSupport::PublishPacts", call: true)} + let(:publish_pacts_the_old_way) { instance_double("PactBroker::ClientSupport::PublishPactsTheOldWay", call: true)} let(:pact_file_list) { ['spec/pact/consumer-provider.json'] } before do - allow(PactBroker::Client::PublishPacts).to receive(:new).and_return(publish_pacts) + allow(PactBroker::Client::PublishPactsTheOldWay).to receive(:new).and_return(publish_pacts_the_old_way) allow(FileList).to receive(:[]).with(pattern).and_return(pact_file_list) allow(PactBroker::Client::Git).to receive(:branch).and_return('foo') end @@ -28,16 +28,16 @@ module PactBroker::Client end context "when pacts are succesfully published" do - it "invokes PublishPacts with the default values" do - expect(PactBroker::Client::PublishPacts).to receive(:new).with('http://pact-broker', pact_file_list, { number: '1.2.3', branch: "foo", tags: [], version_required: false}, {}).and_return(publish_pacts) - expect(publish_pacts).to receive(:call).and_return(true) + it "invokes PublishPactsTheOldWay with the default values" do + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:new).with('http://pact-broker', pact_file_list, { number: '1.2.3', branch: "foo", tags: [], version_required: false}, {}).and_return(publish_pacts_the_old_way) + expect(publish_pacts_the_old_way).to receive(:call).and_return(true) Rake::Task['pact:publish'].execute end end context "when a pact fails to be published" do it "raises an error" do - expect(publish_pacts).to receive(:call).and_return(false) + expect(publish_pacts_the_old_way).to receive(:call).and_return(false) expect { Rake::Task['pact:publish'].execute }.to raise_error("One or more pacts failed to be published") end end @@ -51,9 +51,9 @@ module PactBroker::Client end end - it "invokes PublishPacts with the write method set" do - expect(PactBroker::Client::PublishPacts).to receive(:new).with('http://pact-broker', pact_file_list, { number: "1.2.3", branch: "foo", tags: [], version_required: false }, {write: :merge}).and_return(publish_pacts) - expect(publish_pacts).to receive(:call).and_return(true) + it "invokes PublishPactsTheOldWay with the write method set" do + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:new).with('http://pact-broker', pact_file_list, { number: "1.2.3", branch: "foo", tags: [], version_required: false }, {write: :merge}).and_return(publish_pacts_the_old_way) + expect(publish_pacts_the_old_way).to receive(:call).and_return(true) Rake::Task['pact:publish:merge'].execute end end @@ -73,8 +73,8 @@ module PactBroker::Client Rake::Task['pact:publish:git_branch'].execute end - it "invokes PublishPacts with the git branch name as a tag" do - expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(tags: ['bar', 'foo']), anything).and_return(publish_pacts) + it "invokes PublishPactsTheOldWay with the git branch name as a tag" do + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:new).with(anything, anything, hash_including(tags: ['bar', 'foo']), anything).and_return(publish_pacts_the_old_way) Rake::Task['pact:publish:git_branch'].execute end end @@ -92,8 +92,8 @@ module PactBroker::Client Rake::Task['pact:publish:git_branch_auto_detect_true'].execute end - it "invokes PublishPacts with the branch name" do - expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(branch: "foo"), anything).and_return(publish_pacts) + it "invokes PublishPactsTheOldWay with the branch name" do + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:new).with(anything, anything, hash_including(branch: "foo"), anything).and_return(publish_pacts_the_old_way) Rake::Task['pact:publish:git_branch_auto_detect_true'].execute end end @@ -112,8 +112,8 @@ module PactBroker::Client Rake::Task['pact:publish:git_branch_auto_detect_true_with_branch'].execute end - it "invokes PublishPacts with the specified branch name" do - expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(branch: "main"), anything).and_return(publish_pacts) + it "invokes PublishPactsTheOldWay with the specified branch name" do + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:new).with(anything, anything, hash_including(branch: "main"), anything).and_return(publish_pacts_the_old_way) Rake::Task['pact:publish:git_branch_auto_detect_true_with_branch'].execute end end @@ -131,8 +131,8 @@ module PactBroker::Client Rake::Task['pact:publish:git_branch_auto_detect_false'].execute end - it "invokes PublishPacts without the branch name" do - expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_not_including(branch: "foo"), anything).and_return(publish_pacts) + it "invokes PublishPactsTheOldWay without the branch name" do + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:new).with(anything, anything, hash_not_including(branch: "foo"), anything).and_return(publish_pacts_the_old_way) Rake::Task['pact:publish:git_branch_auto_detect_false'].execute end end @@ -149,8 +149,8 @@ module PactBroker::Client Rake::Task['pact:publish:git_branch_auto_detect_default'].execute end - it "invokes PublishPacts with the branch name" do - expect(PactBroker::Client::PublishPacts).to receive(:new).with(anything, anything, hash_including(branch: "foo"), anything).and_return(publish_pacts) + it "invokes PublishPactsTheOldWay with the branch name" do + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:new).with(anything, anything, hash_including(branch: "foo"), anything).and_return(publish_pacts_the_old_way) Rake::Task['pact:publish:git_branch_auto_detect_default'].execute end end @@ -175,14 +175,14 @@ module PactBroker::Client let(:pattern) { @pattern } - it "invokes PublishPacts with the customised values" do - expect(PactBroker::Client::PublishPacts).to receive(:new).with( + it "invokes PublishPactsTheOldWay with the customised values" do + expect(PactBroker::Client::PublishPactsTheOldWay).to receive(:new).with( @pact_broker_base_url, pact_file_list, { number: "1.2.3", tags: [@tag], branch: "foo", version_required: false}, { basic_auth: @pact_broker_basic_auth, token: @pact_broker_token } ) - expect(publish_pacts).to receive(:call).and_return(true) + expect(publish_pacts_the_old_way).to receive(:call).and_return(true) Rake::Task['pact:publish:custom'].execute end end diff --git a/spec/service_providers/pact_broker_client_create_version_spec.rb b/spec/service_providers/pact_broker_client_create_version_spec.rb index 02098581..28090c53 100644 --- a/spec/service_providers/pact_broker_client_create_version_spec.rb +++ b/spec/service_providers/pact_broker_client_create_version_spec.rb @@ -1,6 +1,6 @@ require_relative 'pact_helper' require 'pact_broker/client' -require 'pact_broker/client/publish_pacts' +require 'pact_broker/client/publish_pacts_the_old_way' describe PactBroker::Client::Versions, pact: true do @@ -9,7 +9,7 @@ describe "creating a pacticipant version" do before do - allow(publish_pacts).to receive(:consumer_names).and_return(["Foo"]) + allow(publish_pacts_the_old_way).to receive(:consumer_names).and_return(["Foo"]) allow($stdout).to receive(:puts) end let(:version_path) { "/HAL-REL-PLACEHOLDER-INDEX-PB-PACTICIPANT-VERSION-{pacticipant}-{version}" } @@ -18,11 +18,11 @@ let(:branch) { "main" } let(:build_url) { "http://my-ci/builds/1" } let(:consumer_version_params) { { number: number, branch: branch, build_url: build_url } } - let(:publish_pacts) { PactBroker::Client::PublishPacts.new(pact_broker.mock_service_base_url, ["some-pact.json"], consumer_version_params, {}) } + let(:publish_pacts_the_old_way) { PactBroker::Client::PublishPactsTheOldWay.new(pact_broker.mock_service_base_url, ["some-pact.json"], consumer_version_params, {}) } let(:provider_state) { "version #{number} of pacticipant Foo does not exist" } let(:expected_response_status) { 201 } - subject { publish_pacts.send(:create_consumer_versions) } + subject { publish_pacts_the_old_way.send(:create_consumer_versions) } before do pact_broker