Module: RSpec::FlakeClassifier::RSpecIntegration

Defined in:
lib/rspec/flake/classifier.rb

Class Method Summary collapse

Class Method Details

.changed_lines(configuration) ⇒ Object



205
206
207
# File 'lib/rspec/flake/classifier.rb', line 205

def changed_lines(configuration)
  provider_value(configuration.changed_lines_provider)
end

.classify_failure(example, investigation, probes:, deflaker:, sensitivity:) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rspec/flake/classifier.rb', line 153

def classify_failure(example, investigation, probes:, deflaker:, sensitivity:)
  Classify::Classifier.new.classify(
    message: example.exception.message,
    backtrace: example.exception.backtrace,
    order_dependent: investigation&.order_type == "od",
    duration_samples: investigation&.runs&.map(&:duration),
    resources: probes.fetch(:resources, []),
    files: probes.fetch(:files, []),
    sockets: probes.fetch(:sockets, []),
    source: probes[:source],
    metadata: {
      deflaker: deflaker&.to_h,
      sensitivity: sensitivity&.to_h
    }.compact
  )
end

.clear_exception(example) ⇒ Object



252
253
254
# File 'lib/rspec/flake/classifier.rb', line 252

def clear_exception(example)
  example.instance_variable_set(:@exception, nil)
end

.coverage_for(example, configuration, coverage_before) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rspec/flake/classifier.rb', line 170

def coverage_for(example, configuration, coverage_before)
  provided = provider_value(configuration.coverage_provider, example)
  return provided if provided

  integration_coverage = Integrations.coverage_for(example)
  return integration_coverage if integration_coverage

   = example.[:flake_classifier_coverage] || example.[:covered_lines]
  return  if 

  coverage_before.delta
end

.current_seedObject



272
273
274
275
276
# File 'lib/rspec/flake/classifier.rb', line 272

def current_seed
  return unless defined?(::RSpec) && ::RSpec.respond_to?(:configuration)

  ::RSpec.configuration.seed
end

.deflaker_base(configuration) ⇒ Object



209
210
211
212
213
# File 'lib/rspec/flake/classifier.rb', line 209

def deflaker_base(configuration)
  return "HEAD" unless configuration.deflaker.is_a?(Hash)

  configuration.deflaker.fetch(:base, configuration.deflaker.fetch("base", "HEAD"))
end

.deflaker_result(configuration, coverage) ⇒ Object



183
184
185
186
187
188
189
190
191
192
# File 'lib/rspec/flake/classifier.rb', line 183

def deflaker_result(configuration, coverage)
  return nil unless configuration.deflaker
  return nil if coverage.nil? || coverage.empty?

  Deflaker.new.suspect?(
    coverage: coverage,
    changed_lines: changed_lines(configuration),
    base: deflaker_base(configuration)
  )
end

.failure_metadata(example, prior_examples, coverage, probes, deflaker, sensitivity) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/rspec/flake/classifier.rb', line 225

def (example, prior_examples, coverage, probes, deflaker, sensitivity)
  {
    example_id: example.id,
    file_path: example.[:file_path],
    line_number: example.[:line_number],
    prior_examples: prior_examples,
    coverage: coverage,
    probes: probes,
    deflaker: deflaker&.to_h,
    sensitivity: sensitivity&.to_h
  }.compact
end

.handle_failure(example, configuration, prior_examples: [], coverage: {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
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
141
142
# File 'lib/rspec/flake/classifier.rb', line 99

def handle_failure(example, configuration, prior_examples: [], coverage: {})
  signature = Signature.from_exception(example.exception)
  store = Store::JSONLStore.new(configuration.store_path)
  known_flake = store.known_flake?(signature.digest)
  probes = ProbeEvidence.new(provider: configuration.probe_provider).collect(example)
  deflaker = deflaker_result(configuration, coverage)
  sensitivity = nil
   = (example, prior_examples, coverage, probes, deflaker, sensitivity)

  if known_flake && configuration.skip_known_flakes
    store.record(signature, classification: known_classification, metadata: )
    clear_exception(example)
    return
  end

  sensitivity = sensitivity_result(example, configuration)
   = (example, prior_examples, coverage, probes, deflaker, sensitivity)

  investigation = nil
  if configuration.auto_rerun
    investigation = rerun_example(example, configuration, prior_examples)
    clear_exception(example) if investigation.flaky?
  end

  classification = classify_failure(
    example,
    investigation,
    probes: probes,
    deflaker: deflaker,
    sensitivity: sensitivity
  )
  Integrations.tag_ci_execution(example: example, signature: signature, classification: classification)
  store.record(signature, classification: classification, metadata: )
  example.[:flake_classifier] = {
    signature: signature.digest,
    known_flake: known_flake,
    coverage: coverage,
    deflaker: deflaker&.to_h,
    sensitivity: sensitivity&.to_h,
    probes: probes,
    investigation: investigation&.to_h,
    classification: classification.to_h
  }
end

.historyObject



268
269
270
# File 'lib/rspec/flake/classifier.rb', line 268

def history
  @history ||= ExampleHistory.new
end

.install(rspec_config, configuration) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rspec/flake/classifier.rb', line 67

def install(rspec_config, configuration)
  return if ENV[Rerun::IsolatedRunner::DISABLE_ENV] == "1"
  return if rspec_config.instance_variable_defined?(:@flake_classifier_installed)

  rspec_config.instance_variable_set(:@flake_classifier_installed, true)
  reset!
  install_around_hook(rspec_config) do |example|
    prior_examples = RSpecIntegration.prior_examples
    coverage_before = CoverageSnapshot.capture
    example.run
    coverage = RSpecIntegration.coverage_for(example, configuration, coverage_before)
    if example.exception
      RSpecIntegration.handle_failure(
        example,
        configuration,
        prior_examples: prior_examples,
        coverage: coverage
      )
    end
  ensure
    RSpecIntegration.record_example(example)
  end
end

.install_around_hook(rspec_config, &hook) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/rspec/flake/classifier.rb', line 91

def install_around_hook(rspec_config, &hook)
  if rspec_config.respond_to?(:hooks) && rspec_config.hooks.respond_to?(:register)
    rspec_config.hooks.register(:append, :around, :each, &hook)
  else
    rspec_config.around(:each, &hook)
  end
end

.known_classificationObject



238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/rspec/flake/classifier.rb', line 238

def known_classification
  Classify::Result.new(
    status: "flaky",
    labels: [
      Classify::Label.new(
        category: "known_flaky",
        confidence: 1.0,
        evidence: ["signature matched the flake store"],
        action: "Inspect the stored classification and linked commits."
      )
    ]
  )
end

.prior_examplesObject



256
257
258
# File 'lib/rspec/flake/classifier.rb', line 256

def prior_examples
  history.prior_examples
end

.provider_value(provider, *args) ⇒ Object



215
216
217
218
219
220
221
222
223
# File 'lib/rspec/flake/classifier.rb', line 215

def provider_value(provider, *args)
  return nil unless provider

  return provider.call if provider.respond_to?(:arity) && provider.arity.zero?

  provider.call(*args)
rescue StandardError
  nil
end

.record_example(example) ⇒ Object



260
261
262
# File 'lib/rspec/flake/classifier.rb', line 260

def record_example(example)
  history.record(example)
end

.rerun_example(example, configuration, prior_examples) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/rspec/flake/classifier.rb', line 144

def rerun_example(example, configuration, prior_examples)
  RSpec::FlakeClassifier.investigate(
    example.id,
    seed: current_seed,
    prior_examples: prior_examples,
    runner: Rerun::IsolatedRunner.new(command: configuration.rspec_command)
  )
end

.reset!Object



264
265
266
# File 'lib/rspec/flake/classifier.rb', line 264

def reset!
  @history = ExampleHistory.new
end

.sensitivity_result(example, configuration) ⇒ Object



194
195
196
197
198
199
200
201
202
203
# File 'lib/rspec/flake/classifier.rb', line 194

def sensitivity_result(example, configuration)
  return nil unless configuration.run_sensitivity

  RSpec::FlakeClassifier.sensitivity(
    example.id,
    factors: configuration.sensitivity_factors,
    seed: current_seed,
    runner: Rerun::IsolatedRunner.new(command: configuration.rspec_command)
  )
end