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, service_name = "travis-ci", service_job_id = nil) ⇒ Converter

Returns a new instance of Converter.



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

def initialize(tracefile, source_encoding = Encoding::UTF_8, service_name = "travis-ci", service_job_id = nil)
  @tracefile = tracefile
  @source_encoding = source_encoding
  @service_name = service_name
  @service_job_id = service_job_id
end

Instance Method Details

#convertObject



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

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: @service_name,
    service_job_id: service_job_id,
    git: git_info,
    source_files: source_files,
  }
  payload
end

#generate_source_file(filename, info) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/coveralls/lcov/converter.rb', line 62

def generate_source_file(filename, info)
  source = File.open(filename, "r:#{@source_encoding}", &: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["coverage"][index + 1]
  end
  top_src_dir = Dir.pwd
  source_file = {
    name: filename.sub(%r!#{top_src_dir}/!, ""),
    source: source,
    coverage: coverage,
  }
  unless info["branches"].empty?
    branches = []
    info["branches"].each do |line_no, blocks_no|
      blocks_no.each do |block_no, branches_no|
        branches_no.each do |branch_no, hits|
          branches.push(line_no, block_no, branch_no, hits)
        end
      end
    end
    source_file["branches"] = branches
  end
  source_file
end

#git_infoObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/coveralls/lcov/converter.rb', line 89

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: ENV["TRAVIS_BRANCH"] || `git rev-parse --abbrev-ref HEAD`,
  }
end

#parse_tracefileObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/coveralls/lcov/converter.rb', line 27

def parse_tracefile
  lcov_info = Hash.new {|h, k| h[k] = { "coverage" => {}, "branches" => {} } }
  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
      coverage = lcov_info[source_file]["coverage"]
      coverage[line_no] = (coverage[line_no] || 0) + count
    when /\ABRDA:(\d+),(\d+),(\d+),(\d+|-)/
      line_no = $1.to_i
      block_no = $2.to_i
      branch_no = $3.to_i
      hits = 0
      unless $4 == "-"
        hits = $4.to_i
      end
      branches = lcov_info[source_file]['branches']
      branches_line = branches[line_no] = branches[line_no] || {}
      branches_block = branches_line[block_no] = branches_line[block_no] || {}
      branches_block[branch_no] = (branches_block[branch_no] || 0) + hits
    when /\Aend_of_record/
      source_file = nil
    end
  end
  lcov_info
rescue => ex
  warn "Could not read tracefile: #{@tracefile}"
  warn "#{ex.class}: #{ex.message}"
  exit(false)
end

#service_job_idObject



104
105
106
# File 'lib/coveralls/lcov/converter.rb', line 104

def service_job_id
  ENV["TRAVIS_JOB_ID"] || @service_job_id
end