-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add command line tool to publish pacts
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env ruby | ||
require 'pact_broker/client/publish_cli' | ||
PactBroker::Client::PublishCLI.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require 'thor' | ||
require 'pact_broker/client/publish_pacts' | ||
require 'rake/file_list' | ||
|
||
module PactBroker | ||
module Client | ||
|
||
# Thor::Error will have its backtrace hidden | ||
class PactPublicationError < ::Thor::Error; end | ||
|
||
class PublishCLI < Thor | ||
|
||
desc 'publish', "Publish pacts to a Pact Broker." | ||
method_option :pact_dir, required: true, aliases: "-d", desc: "The directory containing the pacts to publish" | ||
method_option :consumer_version, required: true, aliases: "-v", desc: "The consumer application version" | ||
method_option :base_url, required: true, aliases: "-b" | ||
method_option :tag, aliases: "-t" | ||
method_option :username, aliases: "-u" | ||
method_option :password, aliases: "-p" | ||
|
||
def publish | ||
success = PactBroker::Client::PublishPacts.call( | ||
options[:base_url], | ||
file_list, | ||
options[:consumer_version], | ||
tags, | ||
pact_broker_client_options | ||
) | ||
raise PactPublicationError, "One or more pacts failed to be published" unless success | ||
end | ||
|
||
default_task :publish | ||
|
||
no_commands do | ||
def file_list | ||
Rake::FileList["#{options[:pact_dir]}/*.json"] | ||
end | ||
|
||
def tags | ||
[options[:tag]].compact | ||
end | ||
|
||
def pact_broker_client_options | ||
if options[:username] | ||
{ | ||
username: options[:username], | ||
password: options[:password] | ||
} | ||
else | ||
{} | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
require 'pact_broker/client/publish_cli' | ||
|
||
module PactBroker | ||
module Client | ||
describe PublishCLI do | ||
|
||
before do | ||
allow(PactBroker::Client::PublishPacts).to receive(:call).and_return(true) | ||
end | ||
|
||
describe "#publish" do | ||
let(:minimum_valid_options) do | ||
{ | ||
pact_dir: 'spec/pacts', | ||
consumer_version: '1.2.3', | ||
base_url: 'http://pact-broker' | ||
} | ||
end | ||
|
||
context "with minimum valid options" do | ||
before do | ||
subject.options = minimum_valid_options | ||
end | ||
|
||
it "invokes the PublishPacts command" do | ||
expect(PactBroker::Client::PublishPacts).to receive(:call).with( | ||
"http://pact-broker", | ||
Rake::FileList["spec/pacts/*.json"], | ||
"1.2.3", | ||
[], | ||
{} | ||
) | ||
subject.publish | ||
end | ||
end | ||
|
||
context "with a tag" do | ||
before do | ||
subject.options = minimum_valid_options.merge(tag: 'foo') | ||
end | ||
|
||
it "passes in the tag" do | ||
expect(PactBroker::Client::PublishPacts).to receive(:call).with( | ||
anything, | ||
anything, | ||
anything, | ||
['foo'], | ||
anything | ||
) | ||
subject.publish | ||
end | ||
end | ||
|
||
context "with basic auth options specified" do | ||
before do | ||
subject.options = minimum_valid_options.merge(username: 'foo', password: 'bar') | ||
end | ||
|
||
it "passes in the basic auth options" do | ||
expect(PactBroker::Client::PublishPacts).to receive(:call).with( | ||
anything, | ||
anything, | ||
anything, | ||
anything, | ||
{username: 'foo', password: 'bar'} | ||
) | ||
subject.publish | ||
end | ||
end | ||
|
||
context "when an error occurs publishing" do | ||
before do | ||
allow(PactBroker::Client::PublishPacts).to receive(:call).and_return(false) | ||
subject.options = minimum_valid_options | ||
end | ||
|
||
it "raises a PactBroker::Client::PactPublicationError" do | ||
expect { subject.publish }.to raise_error PactBroker::Client::PactPublicationError | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |