Skip to content

Commit d031b98

Browse files
committed
feat(cli): add command line tool to publish pacts
1 parent da18c5f commit d031b98

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

bin/pact-publish

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env ruby
2+
require 'pact_broker/client/publish_cli'
3+
PactBroker::Client::PublishCLI.start

lib/pact_broker/client/publish_cli.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require 'thor'
2+
require 'pact_broker/client/publish_pacts'
3+
require 'rake/file_list'
4+
5+
module PactBroker
6+
module Client
7+
8+
# Thor::Error will have its backtrace hidden
9+
class PactPublicationError < ::Thor::Error; end
10+
11+
class PublishCLI < Thor
12+
13+
desc 'publish', "Publish pacts to a Pact Broker."
14+
method_option :pact_dir, required: true, aliases: "-d", desc: "The directory containing the pacts to publish"
15+
method_option :consumer_version, required: true, aliases: "-v", desc: "The consumer application version"
16+
method_option :base_url, required: true, aliases: "-b"
17+
method_option :tag, aliases: "-t"
18+
method_option :username, aliases: "-u"
19+
method_option :password, aliases: "-p"
20+
21+
def publish
22+
success = PactBroker::Client::PublishPacts.call(
23+
options[:base_url],
24+
file_list,
25+
options[:consumer_version],
26+
tags,
27+
pact_broker_client_options
28+
)
29+
raise PactPublicationError, "One or more pacts failed to be published" unless success
30+
end
31+
32+
default_task :publish
33+
34+
no_commands do
35+
def file_list
36+
Rake::FileList["#{options[:pact_dir]}/*.json"]
37+
end
38+
39+
def tags
40+
[options[:tag]].compact
41+
end
42+
43+
def pact_broker_client_options
44+
if options[:username]
45+
{
46+
username: options[:username],
47+
password: options[:password]
48+
}
49+
else
50+
{}
51+
end
52+
end
53+
end
54+
end
55+
end
56+
end

lib/pact_broker/client/publish_pacts.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module PactBroker
66
module Client
77
class PublishPacts
88

9+
def self.call(pact_broker_base_url, pact_files, consumer_version, tags, pact_broker_client_options={})
10+
new(pact_broker_base_url, pact_files, consumer_version, tags, pact_broker_client_options).call
11+
end
12+
913
def initialize pact_broker_base_url, pact_files, consumer_version, tags, pact_broker_client_options={}
1014
@pact_broker_base_url = pact_broker_base_url
1115
@pact_files = pact_files
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
require 'pact_broker/client/publish_cli'
2+
3+
module PactBroker
4+
module Client
5+
describe PublishCLI do
6+
7+
before do
8+
allow(PactBroker::Client::PublishPacts).to receive(:call).and_return(true)
9+
end
10+
11+
describe "#publish" do
12+
let(:minimum_valid_options) do
13+
{
14+
pact_dir: 'spec/pacts',
15+
consumer_version: '1.2.3',
16+
base_url: 'http://pact-broker'
17+
}
18+
end
19+
20+
context "with minimum valid options" do
21+
before do
22+
subject.options = minimum_valid_options
23+
end
24+
25+
it "invokes the PublishPacts command" do
26+
expect(PactBroker::Client::PublishPacts).to receive(:call).with(
27+
"http://pact-broker",
28+
Rake::FileList["spec/pacts/*.json"],
29+
"1.2.3",
30+
[],
31+
{}
32+
)
33+
subject.publish
34+
end
35+
end
36+
37+
context "with a tag" do
38+
before do
39+
subject.options = minimum_valid_options.merge(tag: 'foo')
40+
end
41+
42+
it "passes in the tag" do
43+
expect(PactBroker::Client::PublishPacts).to receive(:call).with(
44+
anything,
45+
anything,
46+
anything,
47+
['foo'],
48+
anything
49+
)
50+
subject.publish
51+
end
52+
end
53+
54+
context "with basic auth options specified" do
55+
before do
56+
subject.options = minimum_valid_options.merge(username: 'foo', password: 'bar')
57+
end
58+
59+
it "passes in the basic auth options" do
60+
expect(PactBroker::Client::PublishPacts).to receive(:call).with(
61+
anything,
62+
anything,
63+
anything,
64+
anything,
65+
{username: 'foo', password: 'bar'}
66+
)
67+
subject.publish
68+
end
69+
end
70+
71+
context "when an error occurs publishing" do
72+
before do
73+
allow(PactBroker::Client::PublishPacts).to receive(:call).and_return(false)
74+
subject.options = minimum_valid_options
75+
end
76+
77+
it "raises a PactBroker::Client::PactPublicationError" do
78+
expect { subject.publish }.to raise_error PactBroker::Client::PactPublicationError
79+
end
80+
end
81+
end
82+
end
83+
end
84+
end

0 commit comments

Comments
 (0)