Class: GitTest

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

Constant Summary collapse

LHS =
'changed_items'
RHS =
'impacted_items'

Instance Method Summary collapse

Instance Method Details

#joinObject



60
61
62
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
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/git_test.rb', line 60

def join
  begin
    mapping = CSV.read(options[:mapping], headers: true, skip_lines: /#/)
    mapping.each do |row|
      row["pattern"] = Regexp.new(row["pattern"], Regexp::IGNORECASE)
    end
  rescue CSV::MalformedCSVError, ArgumentError => e
    $stderr.puts "An error occured when parsing the mapping file '#{options[:mapping]}:\n #{e}"
    exit 1
  end

  begin
    change_rec = CSV.read(options[:change_recommendation], headers: true)
  rescue CSV::MalformedCSVError, ArgumentError => e
    $stderr.puts "An error occured when parsing the change recommendation '#{options[:change_recommendation]}:\n #{e}"
    exit 1
  end

  pattern_matched = Hash.new

  # print header
  CSV {|r| r << %W(#{LHS} #{RHS} support #{LHS}_tested_by #{RHS}_tested_by)}
  change_rec.each do |row|
    support = row["support"].to_r.to_f

    tests = {'lhs': Set.new, 'rhs': Set.new}

    ['lhs','rhs'].each do |side|
      file = row[side]
      # check if the files has already been mapped
      if pattern_matched[file].nil?
        pattern_matched[file] = []
        # iterate through mapping file to find matching tests
        mapping.each do |mapping_row|
          pattern = mapping_row["pattern"]
          test = mapping_row["test"]
          if file =~ pattern
            pattern_matched[file] << test
          end
        end
      end
      tests[side] = pattern_matched[file]
    end

    # print new row
    CSV {|r| r << [row['lhs'], row['rhs'], support, (tests['lhs'].empty? ? nil : tests['lhs'].join(",")), (tests['rhs'].empty? ? nil : tests['rhs'].join(","))]}
  end                          
end

#planObject



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 plan
  begin
    change_rec = CSV.read(options[:change_recommendation], headers: true)
  rescue CSV::MalformedCSVError, ArgumentError => e
    $stderr.puts "An error occured when parsing the change recommendation '#{options[:change_recommendation]}':\n #{e}"
    exit 1
  end


  test_plan = Hash.new
  coverage = {LHS => Set.new, RHS => Set.new}

  change_rec.each do |row|
    support = row["support"].to_r.to_f
    # find relevant tests
    [LHS,RHS].each do |side|
      tests = (row["#{side}_tested_by"].nil? ? [] : row["#{side}_tested_by"].split(","))
      file = row[side]
      tests.each do |test|
        coverage[side] << file
        if test_plan[test].nil?
          test_plan[test] = {'cg' => 0, LHS => Set.new, RHS => Set.new}
        end
        test_plan[test]['cg'] += support
        test_plan[test][side] << file 
      end
    end
  end
  # normalize the cumulative gain
  max_cg = test_plan.map {|_,v| v['cg']}.max
  test_plan.each {|test,v| test_plan[test]['cg'] = v['cg']/max_cg}

  # print warnings to stderr if any
  [LHS,RHS].each do |side|
    items_in_plan = change_rec.map {|row| row[side]}.to_set
    if coverage[side].size < items_in_plan.size
      uncovered = items_in_plan - coverage[side]
      $stderr.puts "#{uncovered.size} #{side.gsub('_',' ')} 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,#{LHS}_covered,#{RHS}_covered"
  test_plan.sort_by {|k,v| [v[LHS].size,v[RHS].size].max}.each do |test,v|
    $stdout.puts "#{test},#{v['cg']},#{v[LHS].size},#{v[RHS].size}"
  end
end