Class: RSpec::FlakeClassifier::Features

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/flake/classifier/features.rb,
sig/rspec/flake/classifier.rbs

Constant Summary collapse

RESOURCE_PATTERNS =
{
  "network" => /Net::HTTP|HTTP\.|Faraday|Socket|TCPSocket|UDPSocket|WebMock|VCR/,
  "filesystem" => /\bFile\b|\bDir\b|Tempfile|Pathname|tmpdir|fixture/,
  "time" => /\bTime\b|\bDate\b|Timecop|travel_to|freeze_time|timezone|TZ/,
  "randomness" => /Random|SecureRandom|srand|shuffle|sample|Faker/,
  "concurrency" => /Thread|Fiber|Mutex|Queue|Concurrent::|fork/,
  "async_wait" => /sleep|wait|eventually|Capybara|Selenium/
}.freeze

Instance Method Summary collapse

Instance Method Details

#extract(file: nil, source: nil, duration: nil, metadata: {}) ⇒ Hash[String, untyped]

Parameters:

  • file: (String, nil) (defaults to: nil)
  • source: (String, nil) (defaults to: nil)
  • duration: (Float, nil) (defaults to: nil)
  • metadata: (Hash[Symbol, untyped]) (defaults to: {})

Returns:

  • (Hash[String, untyped])


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rspec/flake/classifier/features.rb', line 15

def extract(file: nil, source: nil, duration: nil, metadata: {})
  source ||= file && File.file?(file) ? File.read(file) : ""
  features = {
    "file" => file,
    "duration" => duration&.to_f,
    "line_count" => source.lines.count,
    "expectation_count" => source.scan(/\b(expect|should|is_expected)\b/).length,
    "example_count" => source.scan(/\b(it|specify|example)\s+["'{(]/).length,
    "metadata" => stringify_keys()
  }

  RESOURCE_PATTERNS.each do |name, pattern|
    features["uses_#{name}"] = source.match?(pattern)
  end

  features
end