Class: FixtureKit::Analyzer::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/fixture_kit/analyzer/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format: ENV.fetch("ANALYZER_FORMAT", "text"), limit: ENV.fetch("ANALYZER_LIMIT", "50").to_i, lets_per_file: ENV.fetch("ANALYZER_LETS_PER_FILE", "8").to_i, min_reuse: ENV.fetch("ANALYZER_MIN_REUSE", "2").to_i, output_path: ENV["ANALYZER_OUTPUT"]) ⇒ Runner

Returns a new instance of Runner.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fixture_kit/analyzer/runner.rb', line 8

def initialize(
  format: ENV.fetch("ANALYZER_FORMAT", "text"),
  limit: ENV.fetch("ANALYZER_LIMIT", "50").to_i,
  lets_per_file: ENV.fetch("ANALYZER_LETS_PER_FILE", "8").to_i,
  min_reuse: ENV.fetch("ANALYZER_MIN_REUSE", "2").to_i,
  output_path: ENV["ANALYZER_OUTPUT"]
)
  @format = format
  @limit = limit
  @lets_per_file = lets_per_file
  @min_reuse = min_reuse
  @output_path = output_path
end

Class Method Details

.install_rspec_hook!Object

Install the at_exit hook that runs the analyzer after rspec –dry-run



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fixture_kit/analyzer/runner.rb', line 50

def self.install_rspec_hook!
  load_start = Time.now

  progress_thread = Thread.new do
    last_count = 0
    loop do
      sleep 5
      current = ::RSpec.world.example_groups.length rescue 0
      if current != last_count
        $stderr.puts "[fixture_kit:analyzer] Loading... #{current} top-level groups found (#{(Time.now - load_start).round(1)}s)"
        last_count = current
      end
    end
  end
  progress_thread.abort_on_exception = false

  at_exit do
    progress_thread.kill
    next unless defined?(::RSpec) && ::RSpec.world.example_groups.any?

    $stderr.puts "[fixture_kit:analyzer] Spec loading complete: #{::RSpec.world.example_groups.length} groups in #{(Time.now - load_start).round(1)}s"
    new.run(::RSpec.world.example_groups)
  end
end

Instance Method Details

#run(example_groups) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fixture_kit/analyzer/runner.rb', line 22

def run(example_groups)
  return if example_groups.empty?

  total = example_groups.length
  $stderr.puts "[fixture_kit:analyzer] Analyzing #{total} top-level example groups..."

  analyzer = GroupAnalyzer.new

  by_file = Hash.new { |h, k| h[k] = {lets: [], total_examples: 0} }

  example_groups.each_with_index do |group, idx|
    file = group.[:file_path] || group.[:absolute_file_path] || "unknown"
    if (idx + 1) % 500 == 0 || idx + 1 == total
      $stderr.puts "[fixture_kit:analyzer] Processing group #{idx + 1}/#{total} (#{file})"
    end
    by_file[file][:total_examples] += analyzer.total_examples(group)
    analyzer.analyze(group, by_file[file][:lets])
  end

  results = by_file.map do |file, data|
    FileResult.new(file: file, total_examples: data[:total_examples], lets: data[:lets])
  end.sort_by { |r| -r.max_reuse }

  output(results)
  results
end