Class: QuickRspec

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

Constant Summary collapse

@@threads =
[]
@@coverage =
{}
@@last_cover =
{}
@@mutex =
Mutex.new

Class Method Summary collapse

Class Method Details

.collect_stats(example) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/quick-rspec.rb', line 20

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

.load_statsObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/quick-rspec.rb', line 51

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

.on_exitObject



90
91
92
93
# File 'lib/quick-rspec.rb', line 90

def on_exit
  @@threads.map(&:join)
  QuickRspec.save_stats
end

.relevant_path(path) ⇒ Object



46
47
48
49
# File 'lib/quick-rspec.rb', line 46

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

.save_statsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/quick-rspec.rb', line 63

def save_stats
  new_coverage = @@coverage
  load_stats()
  new_coverage.each do |spec, coverage|
    @@coverage[spec] = coverage
  end
  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



16
17
18
# File 'lib/quick-rspec.rb', line 16

def start
  Coverage.start
end

.where_tested(file) ⇒ Object



83
84
85
86
87
88
# File 'lib/quick-rspec.rb', line 83

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