Class: GitTest

Inherits:
Thor
  • Object
show all
Defined in:
lib/git_test.rb

Instance Method Summary collapse

Instance Method Details

#testObject



10
11
12
13
14
15
16
17
18
19
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
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/git_test.rb', line 10

def test
  begin
    mapping = CSV.read(options[:mapping], headers: true)
    change_rec = CSV.read(options[:change_recommendation], headers: true)
  rescue CSV::MalformedCSVError => e
    $stderr.puts "An error occured when parsing the mapping file:\n #{e}"
  end

  test_plan = Hash.new
  covered = Set.new
  uniq_impacted_items = change_rec.map {|row| row["rhs"]}.to_set

  change_rec.each do |row|
    lhs = row["lhs"]
    rhs = row["rhs"]
    support = row["support"].to_r.to_f
    mapping.each do |mapping_row|
      pattern = Regexp.new(mapping_row["pattern"], Regexp::IGNORECASE)
      test = mapping_row["test"]
      if rhs =~ pattern
        covered << rhs
        if test_plan[test].nil?
          test_plan[test] = {impact: support, covers: 1}
        else
          test_plan[test][:impact] += support
          test_plan[test][:covers] += 1
        end
      end
    end
  end                          
  # print warnings to stderr if any
  if covered.size < uniq_impacted_items.size
    uncovered = uniq_impacted_items - covered
    if uncovered.size < 11
      $stderr.puts "The following potentially impacted items were not covered by at least 1 test:\n #{uncovered.to_a.join("\n")}"
    else
      $stderr.puts "#{uncovered.size} potentially impacted items were not covered by at least 1 test. The first 10 were: \n #{uncovered.take(10).join("\n")}"
    end
  end

  # print testplan to stdout
  $stdout.puts "test,impact,covers"
  test_plan.sort_by {|k,v| -v[:impact]}.each do |test,relevance|
    $stdout.puts "#{test},#{relevance[:impact]},#{relevance[:covers]}"
  end
end