Skip to content

Commit

Permalink
Implement codecov format for unreachable tool (#15059)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blacksmoke16 authored Oct 10, 2024
1 parent f3d49d7 commit 0606cf0
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/compiler/crystal/tools/unreachable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require "csv"
module Crystal
class Command
private def unreachable
config, result = compile_no_codegen "tool unreachable", path_filter: true, unreachable_command: true, allowed_formats: %w[text json csv]
config, result = compile_no_codegen "tool unreachable", path_filter: true, unreachable_command: true, allowed_formats: %w[text json csv codecov]

unreachable = UnreachableVisitor.new

Expand Down Expand Up @@ -42,6 +42,8 @@ module Crystal
to_json(STDOUT)
when "csv"
to_csv(STDOUT)
when "codecov"
to_codecov(STDOUT)
else
to_text(STDOUT)
end
Expand Down Expand Up @@ -111,6 +113,31 @@ module Crystal
end
end
end

# https://docs.codecov.com/docs/codecov-custom-coverage-format
def to_codecov(io)
hits = Hash(String, Hash(Int32, Int32)).new { |hash, key| hash[key] = Hash(Int32, Int32).new(0) }

each do |a_def, location, count|
hits[location.filename][location.line_number] = count
end

JSON.build io do |builder|
builder.object do
builder.string "coverage"
builder.object do
hits.each do |filename, line_coverage|
builder.string filename
builder.object do
line_coverage.each do |line, count|
builder.field line, count
end
end
end
end
end
end
end
end

# This visitor walks the entire reachable code tree and collect locations
Expand Down

0 comments on commit 0606cf0

Please sign in to comment.