From 63809e9afa1abd822ef0b29e52eecb838b329c03 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Thu, 8 Aug 2024 13:48:30 +0100 Subject: [PATCH] Reapply "refactor: replace ansicolor gem with rainbow to avoid naming collisions" This reverts commit ab79e512ce7f5135d0cd9a7094326de5f5c4d67c. --- lib/pact/provider/help/console_text.rb | 12 ++++-------- lib/pact/provider/help/prompt_text.rb | 6 ++---- lib/pact/provider/matchers/messages.rb | 12 +++++------- lib/pact/provider/print_missing_provider_states.rb | 8 +++----- lib/pact/provider/rspec/formatter_rspec_2.rb | 4 +--- lib/pact/provider/rspec/formatter_rspec_3.rb | 4 +--- lib/pact/provider/rspec/pact_broker_formatter.rb | 4 ++-- lib/tasks/pact.rake | 4 ++-- pact.gemspec | 2 +- spec/lib/pact/provider/matchers/messages_spec.rb | 4 +--- spec/support/cli.rb | 4 ++-- tasks/pact-test.rake | 4 ++-- 12 files changed, 26 insertions(+), 42 deletions(-) diff --git a/lib/pact/provider/help/console_text.rb b/lib/pact/provider/help/console_text.rb index 15c066fb..c2e7c76d 100644 --- a/lib/pact/provider/help/console_text.rb +++ b/lib/pact/provider/help/console_text.rb @@ -2,15 +2,13 @@ require 'fileutils' require 'pact/consumer/configuration' require 'pact/provider/help/write' -require 'term/ansicolor' +require 'rainbow' module Pact module Provider module Help class ConsoleText - C = ::Term::ANSIColor - def self.call reports_dir = Pact.configuration.reports_dir, options = {color: true} new(reports_dir || Pact.configuration.reports_dir, options).call end @@ -46,13 +44,11 @@ def error_text_plain end def error_text_coloured - C.red(error_text_plain) + Rainbow(error_text_plain).red end class ColorizeMarkdown - C = ::Term::ANSIColor - def self.call markdown markdown.split("\n").collect do | line | if line.start_with?("# ") @@ -66,11 +62,11 @@ def self.call markdown end def self.yellow_underling string - C.underline(C.yellow(string)) + Rainbow(string).yellow.underline end def self.green string - C.green(string) + Rainbow(string).green end end diff --git a/lib/pact/provider/help/prompt_text.rb b/lib/pact/provider/help/prompt_text.rb index 93f11b16..5d16b347 100644 --- a/lib/pact/provider/help/prompt_text.rb +++ b/lib/pact/provider/help/prompt_text.rb @@ -1,5 +1,5 @@ require 'pact/consumer/configuration' -require 'term/ansicolor' +require 'rainbow' require 'pathname' module Pact @@ -7,8 +7,6 @@ module Provider module Help class PromptText - C = ::Term::ANSIColor - def self.call reports_dir = Pact.configuration.reports_dir, options = {color: Pact.configuration.color_enabled} new(reports_dir, options).call end @@ -31,7 +29,7 @@ def prompt_text_plain end def prompt_text_colored - C.yellow(prompt_text_plain) + Rainbow(prompt_text_plain).yellow end def rake_args diff --git a/lib/pact/provider/matchers/messages.rb b/lib/pact/provider/matchers/messages.rb index c5ad9fb4..6e109826 100644 --- a/lib/pact/provider/matchers/messages.rb +++ b/lib/pact/provider/matchers/messages.rb @@ -1,15 +1,13 @@ -require 'term/ansicolor' +require 'rainbow' require 'pact/term' module Pact module Matchers module Messages - C = ::Term::ANSIColor - def match_term_failure_message diff, actual, diff_formatter, color_enabled actual_string = String === actual ? actual : actual.to_json - maybe_coloured_string = color_enabled ? C.white(actual_string) : actual_string + maybe_coloured_string = color_enabled ? Rainbow(actual_string).white : actual_string message = "Actual: #{maybe_coloured_string}\n\n" formatted_diff = diff_formatter.call(diff) message + colorize_if_enabled(formatted_diff, color_enabled) @@ -38,9 +36,9 @@ def expected_desc_for_it expected def colorize_if_enabled formatted_diff, color_enabled if color_enabled # RSpec wraps each line in the failure message with failure_color, turning it red. - # To ensure the lines in the diff that should be white, stay white, put an - # ANSI reset at the start of each line. - formatted_diff.split("\n").collect{ |line| ::Term::ANSIColor.reset + line }.join("\n") + # To ensure the lines in the diff that should be white, stay white, wrap each line + # in ANSI white. + formatted_diff.split("\n").collect{ |line| Rainbow(line).white }.join("\n") else formatted_diff end diff --git a/lib/pact/provider/print_missing_provider_states.rb b/lib/pact/provider/print_missing_provider_states.rb index 6ccb83a0..da60e86d 100644 --- a/lib/pact/provider/print_missing_provider_states.rb +++ b/lib/pact/provider/print_missing_provider_states.rb @@ -1,11 +1,9 @@ -require 'term/ansicolor' +require 'rainbow' module Pact module Provider class PrintMissingProviderStates - C = ::Term::ANSIColor - # Hash of consumer names to array of names of missing provider states def self.call missing_provider_states, output if missing_provider_states.any? @@ -15,8 +13,8 @@ def self.call missing_provider_states, output def self.colorize string lines = string.split("\n") - first_line = C.cyan(C.underline(lines[0])) - other_lines = C.cyan(lines[1..-1].join("\n")) + first_line = Rainbow(lines[0]).cyan.underline + other_lines = Rainbow(lines[1..-1].join("\n")).cyan first_line + "\n" + other_lines end diff --git a/lib/pact/provider/rspec/formatter_rspec_2.rb b/lib/pact/provider/rspec/formatter_rspec_2.rb index 9cf0f8b7..d0b824c4 100644 --- a/lib/pact/provider/rspec/formatter_rspec_2.rb +++ b/lib/pact/provider/rspec/formatter_rspec_2.rb @@ -1,6 +1,6 @@ require 'pact/provider/print_missing_provider_states' require 'rspec/core/formatters/documentation_formatter' -require 'term/ansicolor' +require 'rainbow' require 'pact/provider/help/prompt_text' module Pact @@ -13,8 +13,6 @@ def dump_commands_to_rerun_failed_examples end end - C = ::Term::ANSIColor - def dump_commands_to_rerun_failed_examples return if failed_examples.empty? diff --git a/lib/pact/provider/rspec/formatter_rspec_3.rb b/lib/pact/provider/rspec/formatter_rspec_3.rb index 8c3560c8..5eeaf481 100644 --- a/lib/pact/provider/rspec/formatter_rspec_3.rb +++ b/lib/pact/provider/rspec/formatter_rspec_3.rb @@ -1,6 +1,6 @@ require 'pact/provider/print_missing_provider_states' require 'rspec/core/formatters' -require 'term/ansicolor' +require 'rainbow' require 'pact/provider/help/prompt_text' module Pact @@ -22,8 +22,6 @@ def dump_summary(summary) :example_passed, :example_pending, :example_failed end - C = ::Term::ANSIColor - def example_group_started(notification) # This is the metadata on the top level "Verifying a pact between X and Y" describe block if @group_level == 0 diff --git a/lib/pact/provider/rspec/pact_broker_formatter.rb b/lib/pact/provider/rspec/pact_broker_formatter.rb index e655b8fd..2808cf1c 100644 --- a/lib/pact/provider/rspec/pact_broker_formatter.rb +++ b/lib/pact/provider/rspec/pact_broker_formatter.rb @@ -1,6 +1,6 @@ require 'rspec/core/formatters' require 'pact/provider/verification_results/publish_all' -require 'term/ansicolor' +require 'rainbow' require 'pact/matchers/extract_diff_messages' module Pact @@ -43,7 +43,7 @@ def format_example(example) if example.exception hash[:exception] = { class: example.exception.class.name, - message: ::Term::ANSIColor.uncolor(example.exception.message) + message: Rainbow(example.exception.message).white } end diff --git a/lib/tasks/pact.rake b/lib/tasks/pact.rake index 743e9731..2d2cf117 100644 --- a/lib/tasks/pact.rake +++ b/lib/tasks/pact.rake @@ -15,12 +15,12 @@ namespace :pact do desc "Verifies the pact at the given URI against this service provider." task 'verify:at', :pact_uri do | t, args | - require 'term/ansicolor' + require 'rainbow' require 'pact/tasks/task_helper' include Pact::TaskHelper - abort(::Term::ANSIColor.red("Please provide a pact URI. eg. rake pact:verify:at[../my-consumer/spec/pacts/my_consumer-my_provider.json]")) unless args[:pact_uri] + abort(Rainbow("Please provide a pact URI. eg. rake pact:verify:at[../my-consumer/spec/pacts/my_consumer-my_provider.json]").red) unless args[:pact_uri] handle_verification_failure do execute_pact_verify args[:pact_uri] end diff --git a/pact.gemspec b/pact.gemspec index 90338cea..3e293377 100644 --- a/pact.gemspec +++ b/pact.gemspec @@ -30,7 +30,7 @@ Gem::Specification.new do |gem| gem.add_runtime_dependency 'rack-test', '>= 0.6.3', '< 3.0.0' gem.add_runtime_dependency 'thor', '>= 0.20', '< 2.0' gem.add_runtime_dependency 'webrick', '~> 1.8' - gem.add_runtime_dependency 'term-ansicolor', '~> 1.7' + gem.add_runtime_dependency "rainbow", '~> 3.1' gem.add_runtime_dependency 'pact-support', '~> 1.16', '>= 1.16.9' gem.add_runtime_dependency 'pact-mock_service', '~> 3.0', '>= 3.3.1' diff --git a/spec/lib/pact/provider/matchers/messages_spec.rb b/spec/lib/pact/provider/matchers/messages_spec.rb index e4ae2b91..7bec993b 100644 --- a/spec/lib/pact/provider/matchers/messages_spec.rb +++ b/spec/lib/pact/provider/matchers/messages_spec.rb @@ -12,12 +12,10 @@ module Matchers let(:diff_formatter) { Pact::Matchers::UnixDiffFormatter } let(:message) { "line1\nline2"} let(:output_message) { "Actual: actual\n\n#{message}"} - let(:output_message_with_resets) { "Actual: \e[37mactual\e[0m\n\n#{r}line1\n#{r}line2"} - let(:r) { ::Term::ANSIColor.reset } + let(:output_message_with_resets) { "Actual: \e[37mactual\e[0m\n\n#{Rainbow('line1').white}\n#{Rainbow('line2').white}"} let(:diff) { double("diff") } let(:actual) { "actual" } let(:color_enabled) { true } - let(:ansi_reset_at_start_of_line) { /^#{Regexp.escape ::Term::ANSIColor.reset}/ } let(:message_line_count) { message.split("\n").size } before do diff --git a/spec/support/cli.rb b/spec/support/cli.rb index 8a3921f6..62f7b702 100644 --- a/spec/support/cli.rb +++ b/spec/support/cli.rb @@ -9,14 +9,14 @@ def execute_command command, options = {} end def ensure_patterns_present command, options, output - require 'term/ansicolor' + require 'rainbow' options[:with].each do | pattern | raise ("Could not find #{pattern.inspect} in output of #{command}" + "\n\n#{output}") unless output =~ pattern end end def ensure_patterns_not_present command, options, output - require 'term/ansicolor' + require 'rainbow' options[:without].each do | pattern | raise ("Expected not to find #{pattern.inspect} in output of #{command}" + "\n\n#{output}") if output =~ pattern end diff --git a/tasks/pact-test.rake b/tasks/pact-test.rake index 7a5e44e2..1dae68b3 100644 --- a/tasks/pact-test.rake +++ b/tasks/pact-test.rake @@ -126,10 +126,10 @@ namespace :pact do end def ensure_patterns_present command, options, stdout, stderr - require 'term/ansicolor' + require 'rainbow' output = stdout.read + stderr.read options[:with].each do | pattern | - raise (::Term::ANSIColor.red("Could not find #{pattern.inspect} in output of #{command}") + "\n\n#{output}") unless output =~ pattern + raise (Rainbow("Could not find #{pattern.inspect} in output of #{command}").red + "\n\n#{output}") unless output =~ pattern end end end