Module: WhatToRun

Extended by:
WhatToRun
Included in:
WhatToRun
Defined in:
lib/what_to_run.rb

Instance Method Summary collapse

Instance Method Details

#cov_mapObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/what_to_run.rb', line 63

def cov_map
  cov_map = Hash.new { |h, file| h[file] = Hash.new { |i, line| i[line] = [] } }

  File.open('run_log.json') do |f|
    # Read in the coverage info
    JSON.parse(f.read).each do |args|
      if args.length == 4 # for Minitest
        desc = args.first(2).join('#')
      else                # for RSpec
        desc = args.first
      end

      before, after = args.last(2)

      # calculate the per test coverage
      delta = diff before, after

      delta.each_pair do |file, lines|
        file_map = cov_map[file]

        lines.each_with_index do |val, i|
          # skip lines that weren't executed
          next unless val && val > 0

          # add the test name to the map. Multiple tests can execute the same
          # line, so we need to use an array.
          file_map[i + 1] << desc
        end
      end
    end
  end

  cov_map
end

#diff(before, after) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/what_to_run.rb', line 42

def diff before, after
  after.each_with_object({}) do |(file_name,line_cov), res|
    before_line_cov = before[file_name]

    # skip arrays that are exactly the same
    next if before_line_cov == line_cov

    # subtract the old coverage from the new coverage
    cov = line_cov.zip(before_line_cov).map do |line_after, line_before|
      if line_after
        line_after - line_before
      else
        line_after
      end
    end

    # add the "diffed" coverage to the hash
    res[file_name] = cov
  end
end

#lines_to_runObject



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

def lines_to_run
  repo = Rugged::Repository.new '.'
  lines_to_run = Set.new

  repo.index.diff.each_patch { |patch|
    file = patch.delta.old_file[:path]

    patch.each_hunk { |hunk|
      hunk.each_line { |line|
        case line.line_origin
        when :addition
          lines_to_run << [file, line.new_lineno]
        when :deletion
          lines_to_run << [file, line.old_lineno]
        when :context
          # do nothing
        end
      }
    }
  }

  lines_to_run
end

#predictObject



9
10
11
12
13
14
15
16
# File 'lib/what_to_run.rb', line 9

def predict
  lines_to_run.inject([]) do |tests, (file, line)|
    cov_map[File.expand_path(file)][line].each do |desc|
      tests += Array(desc)
    end
    tests
  end
end