Class: TrunkAnalyticsListener

Inherits:
Object
  • Object
show all
Defined in:
lib/trunk_spec_helper.rb

Overview

TrunkAnalyticsListener is a class that is used to listen to the execution of the Example class it generates and submits the final test reports

Instance Method Summary collapse

Constructor Details

#initializeTrunkAnalyticsListener

Returns a new instance of TrunkAnalyticsListener.



80
81
82
# File 'lib/trunk_spec_helper.rb', line 80

def initialize
  @testreport = TestReport.new('rspec', "#{$PROGRAM_NAME} #{ARGV.join(' ')}")
end

Instance Method Details

#add_test_case(example) ⇒ Object

trunk-ignore(rubocop/Metrics/AbcSize,rubocop/Metrics/MethodLength,rubocop/Metrics/CyclomaticComplexity)



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/trunk_spec_helper.rb', line 109

def add_test_case(example)
  if example.exception
    failure_message = example.exception.message
    # failure details is far more robust than the message, but noiser
    # if example.exception.backtrace
    # failure_details = example.exception.backtrace.join('\n')
    # end
  end
  # TODO: should we use concatenated string or alias when auto-generated description?
  name = example.full_description
  file = escape(example.[:file_path])
  classname = file.sub(%r{\.[^/.]+\Z}, '').gsub('/', '.').gsub(/\A\.+|\.+\Z/, '')
  line = example.[:line_number]
  started_at = example.execution_result.started_at.to_i
  finished_at = example.execution_result.finished_at.to_i
  id = generate_id(example)

  attempt_number = example.[:attempt_number] || 0
  status = example.execution_result.status.to_s
  case example.execution_result.status
  when :passed
    status = Status.new('success')
  when :failed
    status = Status.new('failure')
  when :pending
    status = Status.new('skipped')
  end
  parent_name = example.example_group.[:description]
  parent_name = parent_name.empty? ? 'rspec' : parent_name
  @testreport.add_test(id, name, classname, file, parent_name, line, status, attempt_number,
                       started_at, finished_at, failure_message || '')
end

#close(_notification) ⇒ Object



88
89
90
# File 'lib/trunk_spec_helper.rb', line 88

def close(_notification)
  @testreport.publish
end

#description_generated?(example) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
# File 'lib/trunk_spec_helper.rb', line 92

def description_generated?(example)
  auto_generated_exp = /^\s?is expected to eq .*$/
  full_description = example.full_description
  parent_description = example.example_group.description
  checked_description = full_description.sub(parent_description, '')
  auto_generated_exp.match(checked_description) != nil
end

#example_finished(notification) ⇒ Object



84
85
86
# File 'lib/trunk_spec_helper.rb', line 84

def example_finished(notification)
  add_test_case(notification.example)
end

#generate_id(example) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/trunk_spec_helper.rb', line 100

def generate_id(example)
  if description_generated?(example)
    # trunk-ignore(rubocop/Style/SoleNestedConditional)
    return "trunk:#{example.id}-#{example.location}" if description_generated?(example)
  end
  nil
end