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

#coverage_dataObject



54
55
56
57
58
59
60
# File 'lib/slather/coverage_file.rb', line 54

def coverage_data
  first_line_start = gcov_data =~ /^\s+(-|#+|[0-9+]):\s+1:/

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

#coverage_for_line(line) ⇒ Object



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

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
# 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}"`
    # 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 = File.new("./#{source_file_pathname.basename}.gcov")
    gcov_data = gcov_file.read

    gcov_files_created.each { |file| FileUtils.rm(file) }

    gcov_data
  end
end

#ignored?Boolean

Returns:

  • (Boolean)


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

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

#num_lines_testableObject



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

def num_lines_testable
  coverage_data.compact.count
end

#num_lines_testedObject



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

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

#percentage_lines_testedObject



84
85
86
# File 'lib/slather/coverage_file.rb', line 84

def percentage_lines_tested
  (num_lines_tested / num_lines_testable.to_f) * 100.0
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_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