Class: QuickRspec

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/quick-rspec.rb

Constant Summary collapse

@@coverage =
{}
@@last_cover =
{}

Class Method Summary collapse

Class Method Details

.collect_stats(example) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/quick-rspec.rb', line 18

def collect_stats(example)
  spec = "#{example.metadata[:rerun_file_path]}[#{example.metadata[:scoped_id]}]"
  root = Rails.root.to_s
  result = {}
  cover = Coverage.peek_result
  cover.each do |path, coverage|
    next unless path =~ %r{^#{Regexp.escape(root)}}i
    next if path =~ %r{^#{Regexp.escape(root)}\/spec\/}i
    last_cover = @@last_cover[path]
    path = QuickRspec.relevant_path(path)
    coverage = coverage.each_with_index.map.map do |count, index|
      count -= last_cover[index] if count.present? && last_cover.present? && last_cover[index].present?
      index if count&.positive?
    end
    coverage.compact!
    next unless coverage.any?
    result[path] = coverage
  end
  @@coverage[spec] = result
  @@last_cover = cover
end

.load_statsObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/quick-rspec.rb', line 45

def load_stats
  @@coverage = begin
                 JSON.parse(File.read(Rails.root.join('tmp/quick_rspec.new.json')))
               rescue Errno::ENOENT
                 begin
                   JSON.parse(File.read(Rails.root.join('tmp/quick_rspec.json')))
                 rescue Errno::ENOENT
                   {}
                 end
               end
end

.relevant_path(path) ⇒ Object



40
41
42
43
# File 'lib/quick-rspec.rb', line 40

def relevant_path(path)
  path = Rails.root.join(path).to_s
  path[Rails.root.to_s.length + 1..-1]
end

.save_statsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/quick-rspec.rb', line 57

def save_stats
  File.open(Rails.root.join('tmp/quick_rspec.new.json'), 'w+') do |f|
    f.write @@coverage.to_json
  end
  File.open(Rails.root.join('tmp/quick_rspec.stats'), 'w+') do |f|
    @@coverage.each do |spec_name, coverage|
      f.write "#{spec_name}:\n"
      coverage.each do |path, lines|
        f.write "  #{path}:\n"
        f.write "    #{lines}\n"
      end
    end
  end
end

.startObject



14
15
16
# File 'lib/quick-rspec.rb', line 14

def start
  Coverage.start
end

.where_tested(file) ⇒ Object



72
73
74
75
76
77
# File 'lib/quick-rspec.rb', line 72

def where_tested(file)
  @@coverage.map do |spec, coverage|
    next unless file.in? coverage.keys
    spec
  end.compact
end