class Pact::Provider::PactSpecRunner

Attributes

options[R]
pact_urls[R]

Public Class Methods

new(pact_urls, options = {}) click to toggle source
# File lib/pact/provider/pact_spec_runner.rb, line 25
def initialize pact_urls, options = {}
  @pact_urls = pact_urls
  @options = options
  @results = nil
end

Public Instance Methods

run() click to toggle source
# File lib/pact/provider/pact_spec_runner.rb, line 31
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

class_exists?(name) click to toggle source
# File lib/pact/provider/pact_spec_runner.rb, line 168
def class_exists? name
  Kernel.const_get name
rescue NameError
  false
end
configure_output() click to toggle source
# File lib/pact/provider/pact_spec_runner.rb, line 137
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
configure_rspec() click to toggle source
# File lib/pact/provider/pact_spec_runner.rb, line 45
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
executing_with_ruby?() click to toggle source
# File lib/pact/provider/pact_spec_runner.rb, line 174
def executing_with_ruby?
  ENV['PACT_EXECUTING_LANGUAGE'] == 'ruby'
end
initialize_specs() click to toggle source
# File lib/pact/provider/pact_spec_runner.rb, line 126
def initialize_specs
  pact_sources.each do | pact_source |
    spec_options = {
      criteria: options[:criteria],
      ignore_failures: options[:ignore_failures],
      request_customizer: options[:request_customizer]
    }
    honour_pactfile pact_source, ordered_pact_json(pact_source.pact_json), spec_options
  end
end
monkey_patch_backtrace_formatter() click to toggle source
# File lib/pact/provider/pact_spec_runner.rb, line 108
def monkey_patch_backtrace_formatter
  Pact::RSpec.with_rspec_3 do
    require 'pact/provider/rspec/backtrace_formatter'
  end
end
ordered_pact_json(pact_json) click to toggle source
# File lib/pact/provider/pact_spec_runner.rb, line 160
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
pact_jsons() click to toggle source
# File lib/pact/provider/pact_spec_runner.rb, line 122
def pact_jsons
  pact_sources.collect(&:pact_json)
end
pact_sources() click to toggle source
# File lib/pact/provider/pact_spec_runner.rb, line 114
def pact_sources
  @pact_sources ||= begin
    pact_urls.collect do | pact_url |
      Pact::Provider::PactSource.new(pact_url)
    end
  end
end
rspec_runner_options() click to toggle source
# File lib/pact/provider/pact_spec_runner.rb, line 104
def rspec_runner_options
  ["--options", Pact.project_root.join("lib/pact/provider/rspec/custom_options_file").to_s]
end
run_specs() click to toggle source
# File lib/pact/provider/pact_spec_runner.rb, line 89
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