Module: Codacy::Parser

Defined in:
lib/codacy/parser.rb

Class Method Summary collapse

Class Method Details

.parse_file(simplecov_result) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/codacy/parser.rb', line 4

def self.parse_file(simplecov_result)
  project_dir = Codacy::Git.work_dir

  logger.info("Parsing simplecov result to Codacy format...")
  logger.debug(simplecov_result.original_result)

  file_reports = simplecov_result.original_result.map do |k, v|
    file_dir = k.sub(project_dir, "").sub("/", "")
    coverage_lines = v.each_with_index.map do |covered, lineNr|
      if !covered.nil?
        [(lineNr + 1).to_s, covered]
      else
        nil
      end
    end.compact
    lines_covered = v.compact.length == 0 ? 0 : ((coverage_lines.count { |x| x[1] > 0 }.to_f / v.compact.length) * 100).round
    Hash[
        [['filename', file_dir],
         ['total', lines_covered],
         ['coverage', Hash[coverage_lines]]]
    ]
  end

  total = simplecov_result.source_files.covered_percent.round

  Hash[[['total', total], ['fileReports', file_reports]]]
end