From 29a7962dcd6598e68a299d9a379a3a3be189337b Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 27 Apr 2021 06:57:09 +1000 Subject: [PATCH] feat: provide a more helpful error message when the specified pact file does not exist --- lib/pact_broker/client/cli/broker.rb | 15 ++++++++++++++- .../client/cli/broker_publish_spec.rb | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/pact_broker/client/cli/broker.rb b/lib/pact_broker/client/cli/broker.rb index b0e4e095..493136bd 100644 --- a/lib/pact_broker/client/cli/broker.rb +++ b/lib/pact_broker/client/cli/broker.rb @@ -291,13 +291,26 @@ def file_list pact_files require 'rake/file_list' correctly_separated_pact_files = pact_files.collect{ |path| path.gsub(/\\+/, '/') } - Rake::FileList[correctly_separated_pact_files].collect do | path | + paths = Rake::FileList[correctly_separated_pact_files].collect do | path | if File.directory?(path) Rake::FileList[File.join(path, "*.json")] else path end end.flatten + validate_pact_path_list(paths) + end + + def validate_pact_path_list(paths) + paths.collect do | path | + if File.exist?(path) + path + elsif path.start_with?("-") + raise Thor::Error.new("ERROR: pact-broker publish was called with invalid arguments #{[path]}") + else + raise Thor::Error.new("Specified pact file '#{path}' does not exist. This sometimes indicates one of the arguments has been specified with the wrong name and has been incorrectly identified as a file path.") + end + end end def tags 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..6088ff0b 100644 --- a/spec/lib/pact_broker/client/cli/broker_publish_spec.rb +++ b/spec/lib/pact_broker/client/cli/broker_publish_spec.rb @@ -76,6 +76,22 @@ module PactBroker::Client::CLI end end + context "with an invalid argument specified that gets interpreted as a path" do + let(:file_list) { ['--wrong'] } + + it "raises a more helpful error" do + expect{ invoke_broker }.to raise_error(Thor::Error, 'ERROR: pact-broker publish was called with invalid arguments ["--wrong"]') + end + end + + context "when a specified file does not exist" do + let(:file_list) { ['no-existy'] } + + it "raises a more helpful error" do + expect{ invoke_broker }.to raise_error(Thor::Error, /Specified pact file 'no-existy' does not exist/) + end + end + context "with a tag" do before do subject.options = OpenStruct.new(minimum_valid_options.merge(tag: ['foo']))