Skip to content

Commit

Permalink
feat: wiring up fetch_pacts with honours_pacts_from_pact_broker DSL.
Browse files Browse the repository at this point in the history
  • Loading branch information
rashiagarwal committed Jun 23, 2018
1 parent 81fd0ed commit b609289
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 56 deletions.
19 changes: 13 additions & 6 deletions lib/pact/provider/configuration/pact_verification_with_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ class PactVerificationWithTags

extend Pact::DSL

attr_accessor :tags, :pact_uri

def initialize tags, options = {}
@tags = tags
attr_accessor :name, :pact_broker_base_url, :tags, :pact_uri

def initialize(name, options = {})
puts options
@tags = options.fetch(:consumer_version_tags) || []
@pact_broker_base_url = options.fetch(:pact_broker_base_url)
@provider_name = name
@options = options
@pact_uri = nil
end

Expand All @@ -33,8 +37,11 @@ def finalize
private

def create_pact_verification
verification = Pact::Provider::PactVerificationWithTags.new(tags, pact_uri)
Pact.provider_world.add_pact_verification verification
pacts = Pact::PactBroker::FetchPacts.call(@provider_name, tags, pact_broker_base_url, @options)
pacts.each do |pact_uri|
verification = Pact::Provider::PactVerificationWithTags.new(pact_uri)
Pact.provider_world.add_pact_verification verification
end
end

def validate
Expand Down
8 changes: 4 additions & 4 deletions lib/pact/provider/configuration/service_provider_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ def honours_pact_with consumer_name, options = {}, &block
create_pact_verification consumer_name, options, &block
end

def honours_pacts_from_pact_broker(tags, options = {}, &block)
create_pact_verification_with_tags tags, options, &block
def honours_pacts_from_pact_broker options = {}, &block
create_pact_verification_with_tags options, &block
end
end

def create_pact_verification consumer_name, options, &block
PactVerification.build(consumer_name, options, &block)
end

def create_pact_verification_with_tags(tags, options, &block)
PactVerificationWithTags.build(tags, options, &block)
def create_pact_verification_with_tags(options, &block)
PactVerificationWithTags.build(name, options, &block)
end

def finalize
Expand Down
6 changes: 2 additions & 4 deletions lib/pact/provider/pact_verification_with_tags.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
module Pact::Provider
class PactVerificationWithTags
attr_reader :tags, :uri
def initialize(tags, uri)
@tags = tags
attr_reader :uri
def initialize(uri)
@uri = uri
end

def ==(other)
other.is_a?(PactVerificationWithTags) &&
tags == other.tags &&
uri == other.uri
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'spec_helper'
require 'pact/provider/configuration/pact_verification_with_tags'
require 'pact/pact_broker/fetch_pacts'

module Pact
module Provider
Expand All @@ -8,6 +9,7 @@ module Configuration

describe 'create_verification' do
let(:url) { 'http://some/uri' }
let(:provider_name) {'provider-name'}
let(:pact_repository_uri_options) do
{
username: 'pact_broker_username',
Expand All @@ -22,19 +24,24 @@ module Configuration
}
end

let(:options) do
{
pact_broker_base_url: url,
consumer_version_tags: [tag]
}
end
context "with valid values" do
subject do
uri = url
tags = [tag]
PactVerificationWithTags.build(tags, options) do
pact_uri uri, pact_repository_uri_options
PactVerificationWithTags.build(provider_name, options) do
end
end

it "creates a Verification" do
pact_uri = Pact::Provider::PactURI.new(url, pact_repository_uri_options)
allow(Pact::PactBroker::FetchPacts).to receive(:call).and_return(['pact-urls'])

tags = [tag]
expect(Pact::Provider::PactVerificationWithTags).to receive(:new).with(tags, pact_uri)
expect(Pact::PactBroker::FetchPacts).to receive(:call).with(provider_name, tags, url, options)
expect(Pact::Provider::PactVerificationWithTags).to receive(:new).with('pact-urls')
subject
end
end
Expand Down
52 changes: 16 additions & 36 deletions spec/lib/pact/provider/configuration/service_provider_dsl_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'spec_helper'
require 'pact/provider/configuration/service_provider_dsl'
require 'pact/provider/pact_uri'
require 'pact/pact_broker/fetch_pacts'

module Pact

Expand Down Expand Up @@ -134,31 +135,7 @@ module Configuration
end
let(:pact_url) { 'blah' }

context 'with no optional params' do
subject do
ServiceProviderDSL.build 'some-provider' do
app {}
honours_pacts_from_pact_broker nil do
pact_uri pact_url
end
end
end

it 'adds a verification to the Pact.provider_world' do
subject
pact_uri = Pact::Provider::PactURI.new(pact_url)
expect(Pact.provider_world.pact_verifications.first)
.to eq(Pact::Provider::PactVerificationWithTags.new(nil, pact_uri))
end
end

context 'with all params specified' do
let(:pact_uri_options) do
{
username: 'pact_user',
password: 'pact_pw'
}
end
let(:tag_1) { 'master' }

let(:tag_2) do
Expand All @@ -169,44 +146,47 @@ module Configuration
}
end

let(:options) do
{
pact_broker_base_url: 'some-url',
consumer_version_tags: [tag_1, tag_2]
}
end

subject do
ServiceProviderDSL.build 'some-provider' do
app {}
honours_pacts_from_pact_broker [tag_1, tag_2] do
pact_uri pact_url, pact_uri_options
honours_pacts_from_pact_broker options do
end
end
end

it 'adds a verification to the Pact.provider_world' do
allow(Pact::PactBroker::FetchPacts).to receive(:call).and_return(['pact-urls'])

subject
pact_uri = Pact::Provider::PactURI.new(pact_url, pact_uri_options)
tags = [tag_1, tag_2]

expect(Pact.provider_world.pact_verifications.first)
.to eq(Pact::Provider::PactVerificationWithTags.new(tags, pact_uri))
.to eq(Pact::Provider::PactVerificationWithTags.new('pact-urls'))
end
end

end

describe "CONFIG_RU_APP" do
context "when a config.ru file does not exist" do

describe 'CONFIG_RU_APP' do
context 'when a config.ru file does not exist' do
let(:path_that_does_not_exist) { './tmp/this/path/does/not/exist/probably' }

before do
allow(Pact.configuration).to receive(:config_ru_path).and_return(path_that_does_not_exist)
end

it "raises an error with some helpful text" do
it 'raises an error with some helpful text' do
expect { ServiceProviderDSL::CONFIG_RU_APP.call }
.to raise_error /Could not find config\.ru file.*#{Regexp.escape(path_that_does_not_exist)}/
end

end
end
end

end
end
end

0 comments on commit b609289

Please sign in to comment.