Class: Slather::ProfdataCoverageFile

Inherits:
Object
  • Object
show all
Includes:
CoverageInfo, CoverallsCoverage
Defined in:
lib/slather/profdata_coverage_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CoverallsCoverage

#as_json

Methods included from CoverageInfo

#branch_coverage_data_for_statement_on_line, #num_branch_hits_for_statement_on_line, #num_branches_for_statement_on_line, #num_branches_testable, #num_branches_tested, #num_lines_testable, #num_lines_tested, #percentage_branch_coverage_for_statement_on_line, #percentage_lines_tested, #rate_branch_coverage_for_statement_on_line, #rate_branches_tested, #rate_lines_tested, #source_file_pathname_relative_to_repo_root

Constructor Details

#initialize(project, source, line_numbers_first) ⇒ ProfdataCoverageFile

Returns a new instance of ProfdataCoverageFile.



13
14
15
16
17
18
# File 'lib/slather/profdata_coverage_file.rb', line 13

def initialize(project, source, line_numbers_first)
  self.project = project
  self.source = source
  self.line_numbers_first = line_numbers_first
  create_line_data
end

Instance Attribute Details

#line_dataObject

Returns the value of attribute line_data.



11
12
13
# File 'lib/slather/profdata_coverage_file.rb', line 11

def line_data
  @line_data
end

#line_numbers_firstObject

Returns the value of attribute line_numbers_first.



11
12
13
# File 'lib/slather/profdata_coverage_file.rb', line 11

def line_numbers_first
  @line_numbers_first
end

#projectObject

Returns the value of attribute project.



11
12
13
# File 'lib/slather/profdata_coverage_file.rb', line 11

def project
  @project
end

#sourceObject

Returns the value of attribute source.



11
12
13
# File 'lib/slather/profdata_coverage_file.rb', line 11

def source
  @source
end

Instance Method Details

#all_linesObject



74
75
76
77
78
79
# File 'lib/slather/profdata_coverage_file.rb', line 74

def all_lines
  if @all_lines == nil
    @all_lines = source_code_lines
  end
  @all_lines
end

#branch_coverage_dataObject



175
176
177
178
179
# File 'lib/slather/profdata_coverage_file.rb', line 175

def branch_coverage_data
  @branch_coverage_data ||= begin
    Hash.new
  end
end

#cleaned_gcov_dataObject



87
88
89
# File 'lib/slather/profdata_coverage_file.rb', line 87

def cleaned_gcov_data
  source_data
end

#coverage_for_line(line, line_numbers_first = self.line_numbers_first) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/slather/profdata_coverage_file.rb', line 131

def coverage_for_line(line, line_numbers_first = self.line_numbers_first)
  line = line.gsub(":", "|")

  if line_numbers_first
    line =~ /^(\s*)(\d*)\|(\s*)(\d+)\|/
    group = $4
  else
    line =~ /^(\s*)(\d*)\|/
    group = $2
  end

  if group == nil
    # Check for thousands or millions (llvm-cov outputs hit counts as 25.3k or 3.8M)
    if line_numbers_first
      did_match = line =~ /^(\s*)(\d+)\|(\s*)(\d+\.\d+)(k|M)\|/
      group = $4
      units_group = $5
    else
      did_match = line =~ /^(\s*)(\d+\.\d+)(k|M)\|/
      group = $2
      units_group = $3
    end

    if did_match
      count = group.strip
      units = units_group == 'k' ? 1000 : 1000000

      (count.to_f * units).to_i
    else
      return nil
    end
  else
    match = group.strip
    case match
    when /[0-9]+/
      match.to_i
    when /#+/
      0
    when "-"
      nil
    end
  end
end

#ignored?Boolean

Returns:

  • (Boolean)


194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/slather/profdata_coverage_file.rb', line 194

def ignored?
  # This indicates a llvm-cov coverage warning (occurs if a passed in source file 
  # is not covered or with ccache in some cases).
  ignore = source_file_pathname.to_s.end_with? "isn't covered."

  if !ignore
    # Ignore source files inside of platform SDKs
    ignore = (/Xcode.*\.app\/Contents\/Developer\/Platforms/ =~ source_file_pathname.to_s) != nil
  end

  ignore ? ignore : super
end

#line_coverage_dataObject



125
126
127
128
129
# File 'lib/slather/profdata_coverage_file.rb', line 125

def line_coverage_data
  source_code_lines.map do |line|
    coverage_for_line(line, self.line_numbers_first)
  end
end

#line_number_in_line(line, line_numbers_first = self.line_numbers_first) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/slather/profdata_coverage_file.rb', line 95

def line_number_in_line(line, line_numbers_first = self.line_numbers_first)
  if line_numbers_first
    line =~ /^(\s*)(\d*)/
    group = $2
  else
    line =~ /^(\s*)(\d*)\|(\s*)(\d+)\|/
    group = $4
  end

  if group != nil
    match = group.strip
    case match
      when /[0-9]+/
        return match.to_i
    end
  else
    # llvm-cov outputs hit counts as 25.3k or 3.8M, so check this pattern as well
    did_match = line =~ /^(\s*)(\d+\.\d+)(k|M)\|(\s*)(\d+)\|/

    if did_match
      match = $5.strip
      case match
        when /[0-9]+/
          return match.to_i
      end
    end
  end
  0
end

#line_number_separatorObject



185
186
187
# File 'lib/slather/profdata_coverage_file.rb', line 185

def line_number_separator
  "|"
end

#path_on_first_line?Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/slather/profdata_coverage_file.rb', line 28

def path_on_first_line?
  path = self.source.split("\n")[0].sub ":", ""
  !path.include?("|//")
end

#raw_dataObject



91
92
93
# File 'lib/slather/profdata_coverage_file.rb', line 91

def raw_data
  self.source
end

#raw_sourceObject



81
82
83
84
85
# File 'lib/slather/profdata_coverage_file.rb', line 81

def raw_source
  self.source.lines.map do |line|
    line.split('|').last
  end.join
end

#source_code_linesObject



66
67
68
# File 'lib/slather/profdata_coverage_file.rb', line 66

def source_code_lines
  self.source.split("\n")[(path_on_first_line? ? 1 : 0)..-1]
end

#source_dataObject



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

def source_data
  all_lines.join("\n")
end

#source_fileObject



62
63
64
# File 'lib/slather/profdata_coverage_file.rb', line 62

def source_file
  File.new(source_file_pathname)
end

#source_file_basenameObject



181
182
183
# File 'lib/slather/profdata_coverage_file.rb', line 181

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

#source_file_pathnameObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/slather/profdata_coverage_file.rb', line 33

def source_file_pathname
  @source_file_pathname ||= begin
    if path_on_first_line?
      path = self.source.split("\n")[0].sub ":", ""
      path &&= Pathname(path)
    else
      # llvm-cov was run with just one matching source file
      # It doesn't print the source path in this case, so we have to find it ourselves
      # This is slow because we read every source file and compare it, but this should only happen if there aren't many source files
      digest = Digest::MD5.digest(self.raw_source)
      path = nil

      project.find_source_files.each do |file|
        file_digest = Digest::MD5.digest(File.read(file).strip)

        if digest == file_digest
          path = file
        end
      end

      path
    end
  end
end

#source_file_pathname=(source_file_pathname) ⇒ Object



58
59
60
# File 'lib/slather/profdata_coverage_file.rb', line 58

def source_file_pathname= (source_file_pathname)
    @source_file_pathname = source_file_pathname
end