Class: Minitest::CoverageRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/coverage_reporter.rb

Constant Summary collapse

RESOURCE_BLACK_LIST =
['tasks','home','statistics','table_preferences','autosign']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ CoverageRunner

Returns a new instance of CoverageRunner.



8
9
10
11
12
13
14
# File 'lib/minitest/coverage_reporter.rb', line 8

def initialize(file_path)
  @raw_data = JSON.load(File.open(file_path))
  @api_endpoints = {}
  @covered_resources = []
  @uncovered_resources = []
  @partially_covered_resources = {}
end

Instance Attribute Details

#api_endpointsObject (readonly)

Returns the value of attribute api_endpoints.



7
8
9
# File 'lib/minitest/coverage_reporter.rb', line 7

def api_endpoints
  @api_endpoints
end

#covered_resourcesObject (readonly)

Returns the value of attribute covered_resources.



7
8
9
# File 'lib/minitest/coverage_reporter.rb', line 7

def covered_resources
  @covered_resources
end

#partially_covered_resourcesObject (readonly)

Returns the value of attribute partially_covered_resources.



7
8
9
# File 'lib/minitest/coverage_reporter.rb', line 7

def partially_covered_resources
  @partially_covered_resources
end

#raw_dataObject (readonly)

Returns the value of attribute raw_data.



7
8
9
# File 'lib/minitest/coverage_reporter.rb', line 7

def raw_data
  @raw_data
end

#uncovered_resourcesObject (readonly)

Returns the value of attribute uncovered_resources.



7
8
9
# File 'lib/minitest/coverage_reporter.rb', line 7

def uncovered_resources
  @uncovered_resources
end

Instance Method Details

#color(str, code) ⇒ Object



77
78
79
# File 'lib/minitest/coverage_reporter.rb', line 77

def color(str, code)
  puts "\e[#{code}m#{str}\e[0m"
end

#covered_endpoints_by_resourceObject



34
35
36
# File 'lib/minitest/coverage_reporter.rb', line 34

def covered_endpoints_by_resource
  get_endpoints_by_resource(filtered_endpoints(true))
end

#covered_endpoints_percentageObject



58
59
60
# File 'lib/minitest/coverage_reporter.rb', line 58

def covered_endpoints_percentage
  endpoints_percentage(filtered_endpoints(true))
end

#endpoints_percentage(endpoints) ⇒ Object



50
51
52
# File 'lib/minitest/coverage_reporter.rb', line 50

def endpoints_percentage(endpoints)
  "#{(endpoints.count / api_endpoints.count.to_f * 100 ).to_i}%"
end

#filtered_endpoints(covered) ⇒ Object



16
17
18
# File 'lib/minitest/coverage_reporter.rb', line 16

def filtered_endpoints(covered)
  api_endpoints.select { |_k, v| v == covered }.keys
end

#get_coverageObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/minitest/coverage_reporter.rb', line 38

def get_coverage
  get_endpoints_by_resource(api_endpoints.keys).each do |resource, actions|
    if covered_endpoints_by_resource[resource] == actions
      @covered_resources << resource
    elsif uncovered_endpoints_by_resource[resource] == actions
      @uncovered_resources << resource
    else
      @partially_covered_resources[resource] = uncovered_endpoints_by_resource[resource]
    end
  end
end

#get_endpoints_by_resource(endpoints) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/minitest/coverage_reporter.rb', line 20

def get_endpoints_by_resource(endpoints)
  endpoints_by_resource = {}
  endpoints.each do |url|
    resource,action = url.split("/")
    endpoints_by_resource[resource] ||= []
    endpoints_by_resource[resource] << action
  end
  endpoints_by_resource
end

#output_coverageObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/minitest/coverage_reporter.rb', line 81

def output_coverage
  covered_resources
  color("COVERED RESOURCES" , 32)
  puts covered_resources
  color("NOT COVERED AT ALL", 31)
  puts uncovered_resources
  color("PARTIALLY COVERED RESOURCES", 33)
  partially_covered_resources.each do |resource, endpoints|
    puts "#{resource}: #{endpoints.join(' ')}"
  end
  color("covered endpoints #{covered_endpoints_percentage} uncovered endpoints #{uncovered_endpoints_percentage}", 35)
end

#run_testsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/minitest/coverage_reporter.rb', line 62

def run_tests
  raw_data["docs"]["resources"].each do |resource|
    resource[1]["methods"].each do | method |
      @api_endpoints[method["doc_url"].delete_prefix!("../apidoc/v2/")] = false
    end unless RESOURCE_BLACK_LIST.include? resource[0]
  end
  HammerCLIForeman::Testing::APIExpectations.api_calls.each do |api_call|
    resource, action = api_call
      url = "#{resource}/#{action}"
      @api_endpoints[url] =  true
  end
  get_coverage
  output_coverage
end

#uncovered_endpoints_by_resourceObject



30
31
32
# File 'lib/minitest/coverage_reporter.rb', line 30

def uncovered_endpoints_by_resource
  get_endpoints_by_resource(filtered_endpoints(false))
end

#uncovered_endpoints_percentageObject



54
55
56
# File 'lib/minitest/coverage_reporter.rb', line 54

def uncovered_endpoints_percentage
  endpoints_percentage(filtered_endpoints(false))
end