Class: Coveralls::Lcov::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/coveralls/lcov/converter.rb

Instance Method Summary collapse

Constructor Details

#initialize(tracefile, source_encoding = Encoding::UTF_8) ⇒ Converter

Returns a new instance of Converter.



5
6
7
8
# File 'lib/coveralls/lcov/converter.rb', line 5

def initialize(tracefile, source_encoding = Encoding::UTF_8)
  @tracefile = tracefile
  @source_encoding = source_encoding
end

Instance Method Details

#convertObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/coveralls/lcov/converter.rb', line 10

def convert
  source_files = []
  lcov_info = parse_tracefile
  lcov_info.each do |filename, info|
    source_files << generate_source_file(filename, info)
  end
  payload = {
    :service_name   => "travis-ci",
    :service_job_id => ENV["TRAVIS_JOB_ID"],
    :git            => git_info,
    :source_files   => source_files,
  }
  payload
end

#generate_source_file(filename, info) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/coveralls/lcov/converter.rb', line 43

def generate_source_file(filename, info)
  source = File.open(filename, "r:#{@source_encoding}"){|file| file.read }.encode("UTF-8")
  lines = source.lines
  coverage = Array.new(lines.to_a.size)
  source.lines.each_with_index do |line, index|
    coverage[index] = info[index + 1]
  end
  top_src_dir = Dir.pwd
  {
    :name     => filename.sub(%r!#{top_src_dir}/!, ""),
    :source   => source,
    :coverage => coverage,
  }
end

#git_infoObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/coveralls/lcov/converter.rb', line 58

def git_info
  {
    :head => {
      :id              => `git log -1 --format=%H`,
      :committer_email => `git log -1 --format=%ce`,
      :committer_name  => `git log -1 --format=%cN`,
      :author_email    => `git log -1 --format=%ae`,
      :author_name     => `git log -1 --format=%aN`,
      :message         => `git log -1 --format=%s`,
    },
    :remotes => [], # FIXME need this?
    :branch  => `git rev-parse --abbrev-ref HEAD`,
  }
end

#parse_tracefileObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/coveralls/lcov/converter.rb', line 25

def parse_tracefile
  lcov_info = Hash.new{|h,k| h[k] = {} }
  source_file = nil
  File.readlines(@tracefile).each do |line|
    case line.chomp
    when /\ASF:(.+)/
      source_file = $1
    when /\ADA:(\d+),(\d+)/
      line_no = $1.to_i
      count = $2.to_i
      lcov_info[source_file][line_no] = count
    when /\Aend_of_record/
      source_file = nil
    end
  end
  lcov_info
end