Class: Slather::CoverageFile

Inherits:
Object
  • Object
show all
Includes:
CoverageInfo, CoverallsCoverage
Defined in:
lib/slather/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, #ignored?, #include_file?, #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, gcno_file_pathname) ⇒ CoverageFile

Returns a new instance of CoverageFile.



12
13
14
15
# File 'lib/slather/coverage_file.rb', line 12

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.



10
11
12
# File 'lib/slather/coverage_file.rb', line 10

def gcno_file_pathname
  @gcno_file_pathname
end

#projectObject

Returns the value of attribute project.



10
11
12
# File 'lib/slather/coverage_file.rb', line 10

def project
  @project
end

Instance Method Details

#all_linesObject



67
68
69
70
71
72
73
74
# File 'lib/slather/coverage_file.rb', line 67

def all_lines
  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
  else
    []
  end
end

#branch_coverage_dataObject



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

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

#cleaned_gcov_dataObject



76
77
78
79
# File 'lib/slather/coverage_file.rb', line 76

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

#coverage_for_line(line) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/slather/coverage_file.rb', line 101

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/slather/coverage_file.rb', line 44

def gcov_data
  @gcov_data ||= begin
    gcov_data = ""

    Dir.chdir(project.project_dir) do
      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.exist?(gcov_file_name)
        gcov_data = File.new(gcov_file_name).read
      else
        gcov_data = ""
      end

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

    gcov_data
  end
end

#line_coverage_dataObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/slather/coverage_file.rb', line 85

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

#line_number_in_line(line) ⇒ Object



97
98
99
# File 'lib/slather/coverage_file.rb', line 97

def line_number_in_line(line)
  line.split(':')[1].strip.to_i
end

#line_number_separatorObject



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

def line_number_separator
  ":"
end

#raw_dataObject



81
82
83
# File 'lib/slather/coverage_file.rb', line 81

def raw_data
  self.gcov_data
end

#source_dataObject



40
41
42
# File 'lib/slather/coverage_file.rb', line 40

def source_data
  source_file.read
end

#source_fileObject



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

def source_file
  File.new(source_file_pathname)
end

#source_file_basenameObject



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

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

#source_file_pathnameObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/slather/coverage_file.rb', line 17

def source_file_pathname
  @source_file_pathname ||= begin
    base_filename = gcno_file_pathname.basename.sub_ext("")
    path = nil
    if project.source_directory
      path = Dir["#{project.source_directory}/**/#{base_filename}.{#{supported_file_extensions.join(",")}}"].first
      path &&= Pathname(path)
    else
      pbx_file = project.files.detect { |pbx_file|
        current_base_filename = pbx_file.real_path.basename
        ext_name = File.extname(current_base_filename.to_s)[1..-1]
        current_base_filename.sub_ext("") == base_filename && supported_file_extensions.include?(ext_name)
      }
      path = pbx_file && pbx_file.real_path
    end
    path
  end
end