Sha256: ded05f700c39f9bbd760d7709bcbb2a1d28854c4ff5e8c3208cc20230e8dd524

Contents?: true

Size: 1.46 KB

Versions: 7

Compression:

Stored size: 1.46 KB

Contents

module Slather
  class CoverageFile

    attr_accessor :project, :gcno_file_pathname

    def initialize(gcno_file_pathname)
      @gcno_file_pathname = Pathname(gcno_file_pathname)
    end

    def source_file_pathname
      @source_file_pathname ||= begin
        base_filename = gcno_file_pathname.basename.sub_ext("")
        # TODO: Handle Swift
        path = Dir["#{project.main_group.real_path}/*/#{base_filename}.m"].first
        path && Pathname(path)
      end
    end

    def source_file
      File.new(source_file_pathname)
    end

    def source_data
      source_file.read
    end

    def source_file_pathname_relative_to_project_root
      source_file_pathname.relative_path_from(project.main_group.real_path)
    end

    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

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

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

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
slather-0.0.232 lib/slather/coverage_file.rb
slather-0.0.231 lib/slather/coverage_file.rb
slather-0.0.23 lib/slather/coverage_file.rb
slather-0.0.22 lib/slather/coverage_file.rb
slather-0.0.21 lib/slather/coverage_file.rb
slather-0.0.2 lib/slather/coverage_file.rb
slather-0.0.1 lib/slather/coverage_file.rb