-
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.
Merge pull request #147 from pact-foundation/feat/error-on-unknown-op…
…tion feat: print warnings and allow error to be raised when unknown option is used
- Loading branch information
Showing
4 changed files
with
89 additions
and
1 deletion.
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
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
38 changes: 38 additions & 0 deletions
38
lib/pact_broker/client/cli/thor_unknown_options_monkey_patch.rb
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,38 @@ | ||
require "thor" | ||
require "term/ansicolor" | ||
|
||
# Monkey patch Thor so we can print out a warning when there are unknown options, rather than raising an error. | ||
# If PACT_BROKER_ERROR_ON_UNKNOWN_OPTION=true, raise the error, as the user will have opted in to this behaviour. | ||
# This is for backwards compatibility reasons, and in the next major version, the flag will be removed. | ||
|
||
class Thor | ||
class Options < Arguments | ||
|
||
alias_method :original_check_unknown!, :check_unknown! | ||
|
||
# Replace the original check_unknown! method with an implementation | ||
# that will print a warning rather than raising an error | ||
# unless PACT_BROKER_ERROR_ON_UNKNOWN_OPTION=true is set. | ||
def check_unknown! | ||
if raise_error_on_unknown_options? | ||
original_check_unknown! | ||
else | ||
check_unknown_and_warn | ||
end | ||
end | ||
|
||
def raise_error_on_unknown_options? | ||
ENV["PACT_BROKER_ERROR_ON_UNKNOWN_OPTION"] == "true" | ||
end | ||
|
||
def check_unknown_and_warn | ||
begin | ||
original_check_unknown! | ||
rescue UnknownArgumentError => e | ||
$stderr.puts(::Term::ANSIColor.yellow(e.message)) | ||
$stderr.puts(::Term::ANSIColor.yellow("This is a warning rather than an error so as not to break backwards compatibility. To raise an error for unknown options set PACT_BROKER_ERROR_ON_UNKNOWN_OPTION=true")) | ||
$stderr.puts("\n") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require "open3" | ||
require "pact_broker/client/cli/broker" | ||
|
||
# This is not the ideal way to write a test, but I tried to write it with an in memory invocation, | ||
# and I couldn't get the capture to work, and it became super complicated. | ||
|
||
RSpec.describe "using unknown options", skip_windows: true do | ||
let(:unknown_switches_text) { "Unknown switches" } | ||
let(:warning_text) { "This is a warning"} | ||
let(:command) { "bundle exec bin/pact-broker can-i-deploy --pacticipant Foo --foo --broker-base-url http://example.org" } | ||
|
||
it "prints an 'unknown switches' warning to stderr and also includes the normal output of the command" do | ||
stderr_lines = nil | ||
|
||
Open3.popen3(command) do |stdin, stdout, stderr, thread| | ||
stderr_lines = stderr.readlines | ||
end | ||
|
||
expect(stderr_lines.join("\n")).to include(unknown_switches_text) | ||
expect(stderr_lines.join("\n")).to include(warning_text) | ||
|
||
expect(stderr_lines.size).to be > 2 | ||
end | ||
|
||
|
||
context "with PACT_BROKER_ERROR_ON_UNKNOWN_OPTION=true" do | ||
it "prints an 'unknown switches' message to stderr and does NOT include the normal output of the command as it exits straight after" do | ||
stderr_lines = nil | ||
|
||
Open3.popen3({ "PACT_BROKER_ERROR_ON_UNKNOWN_OPTION" => "true" }, command) do |stdin, stdout, stderr, thread| | ||
stderr_lines = stderr.readlines | ||
end | ||
|
||
expect(stderr_lines.first).to include(unknown_switches_text) | ||
expect(stderr_lines.join("\n")).to_not include(warning_text) | ||
expect(stderr_lines.size).to eq 1 | ||
end | ||
end | ||
end |