Class: Pact::Provider::RSpec::JsonFormatter

Inherits:
RSpec::Core::Formatters::JsonFormatter
  • Object
show all
Defined in:
lib/pact/provider/rspec/json_formatter.rb

Instance Method Summary collapse

Instance Method Details

#calculate_status(example) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/pact/provider/rspec/json_formatter.rb', line 47

def calculate_status(example)
  if example.execution_result.status == :failed && example.[:pact_ignore_failures]
    'pending'
  else
    example.execution_result.status.to_s
  end
end

#create_custom_summary(summary) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/pact/provider/rspec/json_formatter.rb', line 79

def create_custom_summary(summary)
  ::RSpec::Core::Notifications::SummaryNotification.new(
    summary.duration,
    summary.examples,
    summary.examples.select{ | example | example.execution_result.status == :failed && !example.[:pact_ignore_failures] },
    summary.examples.select{ | example | example.execution_result.status == :failed && example.[:pact_ignore_failures] },
    summary.load_time,
    summary.errors_outside_of_examples_count
  )
end

#dump_summary(summary) ⇒ Object



9
10
11
12
# File 'lib/pact/provider/rspec/json_formatter.rb', line 9

def dump_summary(summary)
  super(create_custom_summary(summary))
  output_hash[:summary][:pacts] = pacts(summary)
end

#extract_differences(example) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/pact/provider/rspec/json_formatter.rb', line 90

def extract_differences(example)
  if example.[:pact_diff]
    Pact::Matchers::ExtractDiffMessages.call(example.[:pact_diff]).to_a
  else
    []
  end
end

#format_example(example) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pact/provider/rspec/json_formatter.rb', line 14

def format_example(example)
  {
    :id => example.id,
    :interaction_index => example.[:pact_interaction].index,
    :description => example.description,
    :full_description => example.full_description,
    :status => calculate_status(example),
    :file_path => example.[:file_path],
    :line_number  => example.[:line_number],
    :run_time => example.execution_result.run_time,
    :mismatches => extract_differences(example),
    :pact_url => example.[:pact_uri].uri
  }
end

#pacts(summary) ⇒ Object

There will most likely be only one pact associated with this RSpec execution, because the most likely user of this formatter is the Go implementation that parses the JSON and builds Go tests from them. If the JSON formatter is used by someone else and they have multiple pacts, all the notices for the pacts will be mushed together in one collection, so it will be hard to know which notice belongs to which pact.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pact/provider/rspec/json_formatter.rb', line 61

def pacts(summary)
  unique_pact_metadatas(summary).collect do |  |
    pact_uri = [:pact_uri]
    notices = (pact_uri.[:notices] && pact_uri.[:notices].before_verification_notices) || []
    {
      notices: notices,
      url: pact_uri.uri,
      consumer_name: [:pact_consumer_contract].consumer.name,
      provider_name: [:pact_consumer_contract].provider.name,
      short_description: pact_uri.[:short_description]
    }
  end
end

#stop(notification) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pact/provider/rspec/json_formatter.rb', line 29

def stop(notification)
  output_hash[:examples] = notification.examples.map do |example|
    format_example(example).tap do |hash|
      e = example.exception
      if e
        hash[:exception] =  {
          class: e.class.name,
          message: e.message,
        }
        # No point providing a backtrace for a mismatch, too much noise
        if !e.is_a?(::RSpec::Expectations::ExpectationNotMetError)
          hash[:exception][:backtrace]
        end
      end
    end
  end
end

#unique_pact_metadatas(summary) ⇒ Object



75
76
77
# File 'lib/pact/provider/rspec/json_formatter.rb', line 75

def unique_pact_metadatas(summary)
  summary.examples.collect(&:metadata).group_by{ |  | [:pact_uri].uri }.values.collect(&:first)
end