Class: Slather::CoverageFile

Inherits:
Object
  • Object
show all
Defined in:
lib/slather/coverage_file.rb

Direct Known Subclasses

CoverallsCoverageFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, gcno_file_pathname) ⇒ CoverageFile

Returns a new instance of CoverageFile.



6
7
8
9
# File 'lib/slather/coverage_file.rb', line 6

def initialize(project, gcno_file_pathname)
  self.project = project
  self.gcno_file_pathname = Pathname(gcno_file_pathname)
end

Instance Attribute Details

#gcno_file_pathnameObject

Returns the value of attribute gcno_file_pathname.



4
5
6
# File 'lib/slather/coverage_file.rb', line 4

def gcno_file_pathname
  @gcno_file_pathname
end

#projectObject

Returns the value of attribute project.



4
5
6
# File 'lib/slather/coverage_file.rb', line 4

def project
  @project
end

Instance Method Details

#branch_coverage_dataObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/slather/coverage_file.rb', line 112

def branch_coverage_data
  @branch_coverage_data ||= begin
    branch_coverage_data = Hash.new
    
      gcov_data.scan(/(^(\s+(-|#+|[0-9]+):\s+[1-9]+:(.*)$\r?\n)(^branch\s+[0-9]+\s+[a-zA-Z0-9]+\s+[a-zA-Z0-9]+$\r?\n)+)+/) do |data|
        lines = data[0].split("\n")
        line_number = lines[0].split(':')[1].strip.to_i
        branch_coverage_data[line_number] = lines[1..-1].map do |line|
          if line.split(' ')[2].strip == "never"
            0
          else
            line.split(' ')[3].strip.to_i
          end
        end
      end
    branch_coverage_data
  end
end

#branch_coverage_data_for_statement_on_line(line_number) ⇒ Object



131
132
133
# File 'lib/slather/coverage_file.rb', line 131

def branch_coverage_data_for_statement_on_line(line_number)
  branch_coverage_data[line_number] || []
end

#cleaned_gcov_dataObject



69
70
71
72
# File 'lib/slather/coverage_file.rb', line 69

def cleaned_gcov_data
  data = gcov_data.gsub(/^function(.*) called [0-9]+ returned [0-9]+% blocks executed(.*)$\r?\n/, '')
  data.gsub(/^branch(.*)$\r?\n/, '')
end

#coverage_for_line(line) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/slather/coverage_file.rb', line 74

def coverage_for_line(line)
  line =~ /^(.+?):/

  match = $1.strip
  case match
  when /[0-9]+/
    match.to_i
  when /#+/
    0
  when "-"
    nil
  end
end

#gcov_dataObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/slather/coverage_file.rb', line 39

def gcov_data
  @gcov_data ||= begin
    gcov_output = `gcov "#{source_file_pathname}" --object-directory "#{gcno_file_pathname.parent}" --branch-probabilities --branch-counts`
    # Sometimes gcov makes gcov files for Cocoa Touch classes, like NSRange. Ignore and delete later.
    gcov_files_created = gcov_output.scan(/creating '(.+\..+\.gcov)'/)

    gcov_file_name = "./#{source_file_pathname.basename}.gcov"
    if File.exists?(gcov_file_name)
      gcov_data = File.new(gcov_file_name).read
    else
      gcov_data = ""
    end

    gcov_files_created.each { |file| FileUtils.rm_f(file) }
    gcov_data
  end
end

#ignored?Boolean

Returns:

  • (Boolean)


180
181
182
183
184
# File 'lib/slather/coverage_file.rb', line 180

def ignored?
  project.ignore_list.any? do |ignore|
    File.fnmatch(ignore, source_file_pathname_relative_to_repo_root)
  end
end

#line_coverage_dataObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/slather/coverage_file.rb', line 57

def line_coverage_data
  unless cleaned_gcov_data.empty?
    first_line_start = cleaned_gcov_data =~ /^\s+(-|#+|[0-9+]):\s+1:/

    cleaned_gcov_data[first_line_start..-1].split("\n").map do |line|
      coverage_for_line(line)
    end
  else
    []
  end
end

#num_branch_hits_for_statement_on_line(line_number) ⇒ Object



139
140
141
# File 'lib/slather/coverage_file.rb', line 139

def num_branch_hits_for_statement_on_line(line_number)
  branch_coverage_data_for_statement_on_line(line_number).count { |hit_count| hit_count > 0 }
end

#num_branches_for_statement_on_line(line_number) ⇒ Object



135
136
137
# File 'lib/slather/coverage_file.rb', line 135

def num_branches_for_statement_on_line(line_number)
  branch_coverage_data_for_statement_on_line(line_number).length
end

#num_branches_testableObject



156
157
158
159
160
# File 'lib/slather/coverage_file.rb', line 156

def num_branches_testable
  branch_coverage_data.keys.reduce(0) do |sum, line_number|
    sum += num_branches_for_statement_on_line(line_number)
  end
end

#num_branches_testedObject



162
163
164
165
166
# File 'lib/slather/coverage_file.rb', line 162

def num_branches_tested
  branch_coverage_data.keys.reduce(0) do |sum, line_number|
    sum += num_branch_hits_for_statement_on_line(line_number)
  end
end

#num_lines_testableObject



92
93
94
# File 'lib/slather/coverage_file.rb', line 92

def num_lines_testable
  line_coverage_data.compact.count
end

#num_lines_testedObject



88
89
90
# File 'lib/slather/coverage_file.rb', line 88

def num_lines_tested
  line_coverage_data.compact.select { |cd| cd > 0 }.count
end

#percentage_branch_coverage_for_statement_on_line(line_number) ⇒ Object



152
153
154
# File 'lib/slather/coverage_file.rb', line 152

def percentage_branch_coverage_for_statement_on_line(line_number)
  rate_branch_coverage_for_statement_on_line(line_number) * 100
end

#percentage_lines_testedObject



104
105
106
107
108
109
110
# File 'lib/slather/coverage_file.rb', line 104

def percentage_lines_tested
  if num_lines_testable == 0
    100
  else
    rate_lines_tested * 100
  end
end

#rate_branch_coverage_for_statement_on_line(line_number) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/slather/coverage_file.rb', line 143

def rate_branch_coverage_for_statement_on_line(line_number)
  branch_data = branch_coverage_data_for_statement_on_line(line_number)
  if branch_data.empty?
    0.0
  else
    (num_branch_hits_for_statement_on_line(line_number) / branch_data.length.to_f)
  end
end

#rate_branches_testedObject



168
169
170
171
172
173
174
# File 'lib/slather/coverage_file.rb', line 168

def rate_branches_tested
  if (num_branches_testable > 0)
    (num_branches_tested / num_branches_testable.to_f)
  else
    0.0
  end
end

#rate_lines_testedObject



96
97
98
99
100
101
102
# File 'lib/slather/coverage_file.rb', line 96

def rate_lines_tested
  if num_lines_testable > 0
    (num_lines_tested / num_lines_testable.to_f)
  else
    0
  end
end

#source_dataObject



31
32
33
# File 'lib/slather/coverage_file.rb', line 31

def source_data
  source_file.read
end

#source_fileObject



27
28
29
# File 'lib/slather/coverage_file.rb', line 27

def source_file
  File.new(source_file_pathname)
end

#source_file_basenameObject



176
177
178
# File 'lib/slather/coverage_file.rb', line 176

def source_file_basename
  File.basename(source_file_pathname, '.m')
end

#source_file_pathnameObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/slather/coverage_file.rb', line 11

def source_file_pathname
  @source_file_pathname ||= begin
    base_filename = gcno_file_pathname.basename.sub_ext("")
    # TODO: Handle Swift
    path = nil
    if project.source_directory
      path = Dir["#{project.source_directory}/**/#{base_filename}.m"].first
      path &&= Pathname(path)
    else
      pbx_file = project.files.detect { |pbx_file| pbx_file.real_path.basename.to_s == "#{base_filename}.m" }
      path = pbx_file && pbx_file.real_path
    end
    path
  end
end

#source_file_pathname_relative_to_repo_rootObject



35
36
37
# File 'lib/slather/coverage_file.rb', line 35

def source_file_pathname_relative_to_repo_root
  source_file_pathname.realpath.relative_path_from(Pathname("./").realpath)
end