Skip to content

Commit

Permalink
feat(cli): add --tag-with-git-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jan 15, 2020
1 parent 60b6f4a commit f652dd5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
12 changes: 9 additions & 3 deletions lib/pact/provider_verifier/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'pact/message'
require 'pact/cli/run_pact_verification'
require 'pact/provider_verifier/aggregate_pact_configs'
require 'pact/provider_verifier/git'
require 'rack/reverse_proxy'
require 'faraday_middleware'
require 'json'
Expand All @@ -17,14 +18,15 @@ class App
include Pact::WaitUntilServerAvailable

PROXY_PACT_HELPER = File.expand_path(File.join(File.dirname(__FILE__), "pact_helper.rb"))
EMPTY_ARRAY = [].freeze
attr_reader :pact_urls, :options, :consumer_version_tags, :provider_version_tags, :consumer_version_selectors, :publish_verification_results

def initialize pact_urls, options = {}
@pact_urls = pact_urls
@options = options
@consumer_version_tags = options[:consumer_version_tag] || []
@provider_version_tags = options[:provider_version_tag] || []
@consumer_version_selectors = parse_consumer_version_selectors(options[:consumer_version_selector] || [])
@consumer_version_tags = options.consumer_version_tag || EMPTY_ARRAY
@provider_version_tags = merge_provider_version_tags(options)
@consumer_version_selectors = parse_consumer_version_selectors(options.consumer_version_selector || EMPTY_ARRAY)
@publish_verification_results = options.publish_verification_results || ENV['PACT_BROKER_PUBLISH_VERIFICATION_RESULTS'] == 'true'
end

Expand Down Expand Up @@ -215,6 +217,10 @@ def parse_consumer_version_selectors consumer_version_selectors
consumer_version_selectors.collect{ | string | JSON.parse(string) }
end

def merge_provider_version_tags(options)
(options.provider_version_tag || EMPTY_ARRAY) + (options.tag_with_git_branch ? [Git.branch] : EMPTY_ARRAY)
end

def print_deprecation_note
if options.provider_states_url
$stderr.puts "WARN: The --provider-states-url option is deprecated and the URL endpoint can be removed from the application"
Expand Down
1 change: 1 addition & 0 deletions lib/pact/provider_verifier/cli/verify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class InvalidArgumentsError < ::Thor::Error; end
method_option :consumer_version_tag, type: :array, banner: "TAG", desc: "Retrieve the latest pacts with this consumer version tag. Used in conjunction with --provider. May be specified multiple times.", :required => false
method_option :consumer_version_selector, type: :array, banner: "SELECTOR", desc: "JSON string specifying a selector that identifies which pacts to verify. May be specified multiple times. See below for further documentation.", :required => false
method_option :provider_version_tag, type: :array, banner: "TAG", desc: "Tag to apply to the provider application version. May be specified multiple times.", :required => false
method_option :tag_with_git_branch, aliases: "-g", type: :boolean, default: false, required: false, desc: "Tag provider version with the name of the current git branch. Default: false"
method_option :provider_app_version, aliases: "-a", desc: "Provider application version, required when publishing verification results", :required => false
method_option :publish_verification_results, aliases: "-r", desc: "Publish verification results to the broker. This can also be enabled by setting the environment variable PACT_BROKER_PUBLISH_VERIFICATION_RESULTS=true", required: false, type: :boolean, default: false
method_option :enable_pending, desc: "Allow pacts which are in pending state to be verified without causing the overall task to fail. For more information, see https://pact.io/pending", required: false, type: :boolean, default: false
Expand Down
5 changes: 5 additions & 0 deletions lib/pact/provider_verifier/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Pact
module ProviderVerifier
class Error < StandardError; end
end
end
17 changes: 17 additions & 0 deletions lib/pact/provider_verifier/git.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'pact/provider_verifier/error'

# Keep in sync with pact_broker-client/lib/pact_broker/client/git.rb

module Pact
module ProviderVerifier
module Git
COMMAND = 'git rev-parse --abbrev-ref HEAD'

def self.branch
`#{COMMAND}`.strip
rescue StandardError => e
raise Pact::ProviderVerifier::Error, "Could not determine current git branch using command `#{COMMAND}`. #{e.class} #{e.message}"
end
end
end
end
2 changes: 1 addition & 1 deletion script/dev/broker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ bundle exec bin/pact-provider-verifier \
--broker-password O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1 \
--provider Bar \
--provider-version-tag pdev \
--tag-with-git-branch \
--consumer-version-selector '{"tag": "dev", "latest": true}' \
--pact-broker-base-url https://test.pact.dius.com.au \
-a 1.0.100 \
--provider-base-url http://localhost:4567 \
--provider-states-setup-url http://localhost:4567/provider-state \
--verbose \
--enable-pending

kill -2 $pid
Expand Down
43 changes: 43 additions & 0 deletions spec/lib/pact/provider_verifier/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,50 @@ module Pact
module ProviderVerifier
describe App do
describe "call" do
before do
allow(AggregatePactConfigs).to receive(:call).and_return([])
allow(Cli::RunPactVerification).to receive(:call)
allow(Pact).to receive(:clear_configuration)
allow(Pact).to receive(:clear_consumer_world)
allow(Pact).to receive(:clear_provider_world)
allow_any_instance_of(App).to receive(:wait_until_server_available).and_return(true)
allow($stderr).to receive(:puts)
allow(Git).to receive(:branch)
end

let(:options) do
double('options',
provider_base_url: "http://provider",
provider_version_tag: ["foo"],
wait: 1,
provider_states_url: nil
).as_null_object
end

let(:pact_urls) { ["http://pact"] }

subject { App.call(pact_urls, options) }

context "when tag_with_git_branch is true" do
before do
allow(Git).to receive(:branch).and_return("master")
end
it "merges the git branch with any specified provider tags" do
# This is a shitty way to test the provider tags, but it's easier than checking
# that the pact configuration DSL is called the right way!
expect(AggregatePactConfigs).to receive(:call).with(
anything,
anything,
anything,
anything,
["foo", "master"],
anything,
anything,
anything
)
subject
end
end
end
end
end
Expand Down

0 comments on commit f652dd5

Please sign in to comment.