class Pact::Provider::PactSpecRunner
Attributes
Public Class Methods
Source
# File lib/pact/provider/pact_spec_runner.rb, line 26 def initialize pact_urls, options = {} @pact_urls = pact_urls @options = options @results = nil end
Public Instance Methods
Source
# File lib/pact/provider/pact_spec_runner.rb, line 32 def run begin configure_rspec initialize_specs run_specs ensure ::RSpec.reset Pact.clear_provider_world Pact.clear_consumer_world end end
Private Instance Methods
Source
# File lib/pact/provider/pact_spec_runner.rb, line 171 def class_exists? name Kernel.const_get name rescue NameError false end
Source
# File lib/pact/provider/pact_spec_runner.rb, line 140 def configure_output Pact::RSpec.with_rspec_3 do ::RSpec.configuration.add_formatter Pact::Provider::RSpec::PactBrokerFormatter, StringIO.new end output = options[:out] || Pact.configuration.output_stream if options[:format] formatter = options[:format] == 'json' ? Pact::Provider::RSpec::JsonFormatter : options[:format] # Send formatted output to $stdout for parsing, unless a file is specified output = options[:out] || $stdout ::RSpec.configuration.add_formatter formatter, output # Don't want to mess up the JSON parsing with INFO and DEBUG messages to stdout, so send it to stderr Pact.configuration.output_stream = Pact.configuration.error_stream if !options[:out] else # Sometimes the formatter set in the cli.rb get set with an output of StringIO.. don't know why formatter_class = Pact::RSpec.formatter_class pact_formatter = ::RSpec.configuration.formatters.find {|f| f.class == formatter_class && f.output == ::RSpec.configuration.output_stream} ::RSpec.configuration.add_formatter(formatter_class, output) unless pact_formatter end ::RSpec.configuration.full_backtrace = @options[:full_backtrace] end
Source
# File lib/pact/provider/pact_spec_runner.rb, line 46 def configure_rspec monkey_patch_backtrace_formatter config = ::RSpec.configuration config.color = true config.pattern = "pattern which doesn't match any files" config.backtrace_inclusion_patterns = [Regexp.new(Dir.getwd), /pact.*main/] config.extend Pact::Provider::RSpec::ClassMethods config.include Pact::Provider::RSpec::InstanceMethods config.include Pact::Provider::TestMethods if options[:silent] config.output_stream = StringIO.new config.error_stream = StringIO.new else config.error_stream = Pact.configuration.error_stream config.output_stream = Pact.configuration.output_stream end configure_output config.before(:suite) do # Preload app before suite so the classes loaded in memory are consistent for # before :each and after :each hooks. # Otherwise the app and all its dependencies are loaded between the first before :each # and the first after :each, leading to inconsistent behaviour # (eg. with database_cleaner transactions) Pact.configuration.provider.app end # For the Pact::Provider::RSpec::PactBrokerFormatter Pact.provider_world.verbose = options[:verbose] Pact.provider_world.pact_sources = pact_sources jsons = pact_jsons executing_with_ruby = executing_with_ruby? config.after(:suite) do | suite | Pact.provider_world.failed_examples = suite.reporter.failed_examples Pact::Provider::Help::Write.call(Pact.provider_world.pact_sources) if executing_with_ruby end end
Source
# File lib/pact/provider/pact_spec_runner.rb, line 177 def executing_with_ruby? ENV['PACT_EXECUTING_LANGUAGE'] == 'ruby' end
Source
# File lib/pact/provider/pact_spec_runner.rb, line 127 def initialize_specs pact_sources.each do | pact_source | spec_options = { criteria: options[:criteria], ignore_failures: options[:ignore_failures], request_customizer: options[:request_customizer] } Pact::Utils::Metrics.report_metric("Pacts verified", "ProviderTest", "Completed") honour_pactfile pact_source, ordered_pact_json(pact_source.pact_json), spec_options end end
Source
# File lib/pact/provider/pact_spec_runner.rb, line 109 def monkey_patch_backtrace_formatter Pact::RSpec.with_rspec_3 do require 'pact/provider/rspec/backtrace_formatter' end end
Source
# File lib/pact/provider/pact_spec_runner.rb, line 163 def ordered_pact_json(pact_json) return pact_json if Pact.configuration.interactions_replay_order == :recorded consumer_contract = JSON.parse(pact_json) consumer_contract["interactions"] = consumer_contract["interactions"].shuffle consumer_contract.to_json end
Source
# File lib/pact/provider/pact_spec_runner.rb, line 123 def pact_jsons pact_sources.collect(&:pact_json) end
Source
# File lib/pact/provider/pact_spec_runner.rb, line 115 def pact_sources @pact_sources ||= begin pact_urls.collect do | pact_url | Pact::Provider::PactSource.new(pact_url) end end end
Source
# File lib/pact/provider/pact_spec_runner.rb, line 105 def rspec_runner_options ["--options", Pact.project_root.join("lib/pact/provider/rspec/custom_options_file").to_s] end
Source
# File lib/pact/provider/pact_spec_runner.rb, line 90 def run_specs exit_code = if Pact::RSpec.runner_defined? ::RSpec::Core::Runner.run(rspec_runner_options) else ::RSpec::Core::CommandLine.new(NoConfigurationOptions.new) .run(::RSpec.configuration.output_stream, ::RSpec.configuration.error_stream) end if options[:ignore_failures] 0 else Pact::Provider::RSpec::CalculateExitCode.call(pact_sources, Pact.provider_world.failed_examples) end end