From 1cf71bb625d1187b121df5f2aea93259133b8d0d Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 23 Jun 2020 17:06:32 +1000 Subject: [PATCH] feat: add --log-dir and --log-level to CLI --- README.md | 50 ++++++++++++------------ lib/pact/provider_verifier/app.rb | 2 + lib/pact/provider_verifier/cli/verify.rb | 2 + spec/integration_spec.rb | 16 ++++++++ 4 files changed, 44 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 29a9545..a4de253 100644 --- a/README.md +++ b/README.md @@ -41,55 +41,53 @@ Usage: Options: -h, --provider-base-url=PROVIDER_BASE_URL - # Provider host URL + # Provider host URL -c, [--provider-states-setup-url=PROVIDER_STATES_SETUP_URL] - # Base URL to setup the provider states at + # Base URL to setup the provider states at [--pact-broker-base-url=PACT_BROKER_BASE_URL] - # Base URL of the Pact Broker from which to retrieve the pacts. Can also be set using the environment variable PACT_BROKER_BASE_URL. + # Base URL of the Pact Broker from which to retrieve the pacts. Can also be set using the environment variable PACT_BROKER_BASE_URL. -n, [--broker-username=BROKER_USERNAME] - # Pact Broker basic auth username. Can also be set using the environment variable PACT_BROKER_USERNAME. + # Pact Broker basic auth username. Can also be set using the environment variable PACT_BROKER_USERNAME. -p, [--broker-password=BROKER_PASSWORD] - # Pact Broker basic auth password. Can also be set using the environment variable PACT_BROKER_PASSWORD. + # Pact Broker basic auth password. Can also be set using the environment variable PACT_BROKER_PASSWORD. -k, [--broker-token=BROKER_TOKEN] - # Pact Broker bearer token. Can also be set using the environment variable PACT_BROKER_TOKEN. + # Pact Broker bearer token. Can also be set using the environment variable PACT_BROKER_TOKEN. [--provider=PROVIDER] [--consumer-version-tag=TAG] - # Retrieve the latest pacts with this consumer version tag. Used in conjunction with --provider. May be specified multiple times. - [--consumer-version-selector=SELECTOR] - # JSON string specifying a selector that identifies which pacts to verify. May be specified multiple times. See below for further documentation. + # Retrieve the latest pacts with this consumer version tag. Used in conjunction with --provider. May be specified multiple times. [--provider-version-tag=TAG] - # Tag to apply to the provider application version. May be specified multiple times. + # Tag to apply to the provider application version. May be specified multiple times. + -g, [--tag-with-git-branch], [--no-tag-with-git-branch] + # Tag provider version with the name of the current git branch. Default: false -a, [--provider-app-version=PROVIDER_APP_VERSION] - # Provider application version, required when publishing verification results + # Provider application version, required when publishing verification results -r, [--publish-verification-results], [--no-publish-verification-results] - # Publish verification results to the broker. This can also be enabled by setting the environment variable PACT_BROKER_PUBLISH_VERIFICATION_RESULTS=true - [--enable-pending], [--no-enable-pending] - # Allow pacts which are in pending state to be verified without causing the overall task to fail. For more information, see https://pact.io/pending + # Publish verification results to the broker. This can also be enabled by setting the environment variable PACT_BROKER_PUBLISH_VERIFICATION_RESULTS=true [--custom-provider-header=CUSTOM_PROVIDER_HEADER] - # Header to add to provider state set up and pact verification requests. eg 'Authorization: Basic cGFjdDpwYWN0'. May be specified multiple times. + # Header to add to provider state set up and pact verification requests. eg 'Authorization: Basic cGFjdDpwYWN0'. May be specified multiple times. [--custom-middleware=FILE] - # Ruby file containing a class implementing Pact::ProviderVerifier::CustomMiddleware. This allows the response to be modified before replaying. Use with caution! + # Ruby file containing a class implementing Pact::ProviderVerifier::CustomMiddleware. This allows the response to be modified before replaying. Use with caution! -v, [--verbose=VERBOSE] - # Verbose output + # Verbose output -f, [--format=FORMATTER] - # RSpec formatter. Defaults to custom Pact formatter. Other options are json and RspecJunitFormatter (which outputs xml). + # RSpec formatter. Defaults to custom Pact formatter. Other options are json and RspecJunitFormatter (which outputs xml). -o, [--out=FILE] - # Write output to a file instead of $stdout. + # Write output to a file instead of $stdout. [--wait=SECONDS] - # The number of seconds to poll for the provider to become available before running the verification + # The number of seconds to poll for the provider to become available before running the verification - # Default: 0 + # Default: 0 Description: - To verify a pact from a known URL, specify one or more PACT_URL arguments. If the pact - is hosted in a Pact Broker that uses authentication, specify the relevant + To verify a pact from a known URL, specify one or more PACT_URL arguments. If the + pact is hosted in a Pact Broker that uses authentication, specify the relevant --broker-username/--broker-password or --broker-token fields. To dynamically fetch pacts from a Pact Broker based on the provider name, specify the --pact-broker-base-url, --provider and relevant authentication fields. - Selectors: These are specified using JSON strings. The keys are 'tag' (the name of the consumer - version tag) and 'latest' (true|false). For example '{"tag": "master", "latest": - true}'. For a detailed explanation of selectors, see https://pact.io/selectors + Selectors: These are specified using JSON strings. The keys are 'tag' (the name of the + consumer version tag) and 'latest' (true|false). For example '{"tag": "master", + "latest": true}'. For a detailed explanation of selectors, see ``` ## Examples diff --git a/lib/pact/provider_verifier/app.rb b/lib/pact/provider_verifier/app.rb index 822f346..f0ca27e 100644 --- a/lib/pact/provider_verifier/app.rb +++ b/lib/pact/provider_verifier/app.rb @@ -97,6 +97,8 @@ def configure_output require 'pact/configuration' Pact.configuration.output_stream = Pact.configuration.error_stream end + Pact.configuration.log_dir = options.log_dir if options.log_dir + Pact.configuration.logger.level = options.log_level.downcase.to_sym if options.log_level end def configure_reverse_proxy diff --git a/lib/pact/provider_verifier/cli/verify.rb b/lib/pact/provider_verifier/cli/verify.rb index 98e6b2a..db65fc3 100644 --- a/lib/pact/provider_verifier/cli/verify.rb +++ b/lib/pact/provider_verifier/cli/verify.rb @@ -49,6 +49,8 @@ class AuthError < ::Thor::Error; end method_option :ignore_failures, type: :boolean, default: false, desc: "If specified, process will always exit with exit code 0", hide: true method_option :pact_urls, aliases: "-u", hide: true, :required => false method_option :wait, banner: "SECONDS", required: false, type: :numeric, desc: "The number of seconds to poll for the provider to become available before running the verification", default: 0 + method_option :log_dir, desc: "The directory for the pact.log file" + method_option :log_level, desc: "The logging level" def verify(*pact_urls) validate_verify diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index eff221f..2e8a21e 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -131,6 +131,22 @@ end end + context "running verification a log dir" do + before do + FileUtils.rm_rf 'tmp/logs' + end + + subject { `bundle exec bin/pact-provider-verifier ./test/me-they.json -a 1.0.100 --provider-base-url http://localhost:4567 --provider-states-setup-url http://localhost:4567/provider-state --log-dir tmp/logs --log-level info` } + + it "the logs are written at the right level" do + subject + expect(File.exist?('tmp/logs/pact.log')) + logs = File.read('tmp/logs/pact.log') + expect(logs).to include ('INFO') + expect(logs).to_not include ('DEBUG') + end + end + after(:all) do Process.kill 'KILL', @pipe.pid end