Skip to content

Commit

Permalink
Add SimpleCov reporting with GitHub Job Summary
Browse files Browse the repository at this point in the history
This GitHub Job Summary is very simple for now.
  • Loading branch information
lloeki committed Jul 18, 2024
1 parent e657d65 commit 5bd9416
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ trim_trailing_whitespace = true
[*.yml]
indent_style = space
indent_size = 2

[*.md.erb]
insert_final_newline = false
trim_trailing_whitespace = false
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ jobs:
- name: Install dependencies
run: bundler install
- name: Run the test
run: bundle exec rake test spec
run: bundle exec rake test spec && cat coverage/coverage.md >> $GITHUB_STEP_SUMMARY
env:
COVERAGE: true

notify:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/.gem_rbs_collection
/tmp
/gemfiles/*.lock
/coverage
40 changes: 40 additions & 0 deletions .projections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"lib/*.rb": {
"type": "lib",
"alternate": "spec/{}_spec.rb",
"related": [
"sig/{}.rbs"
]
},
"test/*_test.rb": {
"type": "test",
"dispatch": "bundle exec ruby -Itest {file}",
"alternate": "lib/{}.rb",
"related": [
"sig/{}.rbs"
]
},
"spec/*_spec.rb": {
"type": "spec",
"dispatch": "bundle exec ruby -Ispec {file}",
"alternate": "lib/{}.rb",
"related": [
"sig/{}.rbs"
]
},
"sig/*.rbs": {
"type": "sig",
"alternate": "lib/{}.rb",
"related": [
"spec/{}_spec.rb",
"test/{}_test.rb"
]
},
"README.md": {
"type": "doc"
},
"*": {
"make": "bundle exec rake",
"console": "bundle exec irb -rgraft"
}
}
1 change: 1 addition & 0 deletions gemfiles/ruby-3.3.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gem "rake", "~> 13.1.0"
group :test do
gem "minitest", "~> 5.22.3"
gem "minitest-reporters", "~> 1.7.1"
gem "simplecov", require: false
end

group :debug do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true

require_relative "spec_helper/simplecov"
require_relative "spec_helper/minitest/reporter"
24 changes: 24 additions & 0 deletions spec/spec_helper/simplecov.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
if %w[y yes true 1].include? ENV["COVERAGE"]
require "simplecov"
require "simplecov_json_formatter"
require_relative "simplecov/formatters/markdown_formatter"

SimpleCov.configure do
add_filter %r{(?:spec|test|bin)}

SimpleCov.formatters = [
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::JSONFormatter,
SimpleCov::Formatter::MarkdownFormatter
]

# Ruby 2.5+
enable_coverage :branch
primary_coverage :branch

# Ruby 3.2+
enable_coverage_for_eval
end

SimpleCov.start
end
57 changes: 57 additions & 0 deletions spec/spec_helper/simplecov/formatters/markdown_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

# Ensure we are using a compatible version of SimpleCov
major, minor, patch = SimpleCov::VERSION.scan(/\d+/).first(3).map(&:to_i)
if major < 0 || minor < 9 || patch < 0
raise "The version of SimpleCov you are using is too old. " \
"Please update with `gem install simplecov` or `bundle update simplecov`"
end

require "simplecov-html"

module SimpleCov
module Formatter
class MarkdownFormatter < HTMLFormatter
def initialize
@branchable_result = SimpleCov.branch_coverage?
@templates = {}
end

def format(result)
File.open(File.join(output_path, "coverage.md"), "wb") do |file|
file.puts template("layout").result(binding)
end

# TODO: disabled til `views/source_file.md.erb` is implemented
# result.source_files.each do |source_file|
# path = File.dirname(File.join(output_path, shortened_filename(source_file)))
#
# FileUtils.mkdir_p(path)
#
# File.open(File.join(output_path, shortened_filename(source_file)) + ".md", "wb") do |file|
# file.puts formatted_source_file(source_file)
# end
# end

puts output_message(result)
end

def output_message(result)
str = "Markdown coverage report generated for #{result.command_name} to #{output_path}. #{result.covered_lines} / #{result.total_lines} LOC (#{result.covered_percent.round(2)}%) covered."
str += " #{result.covered_branches} / #{result.total_branches} branches (#{result.coverage_statistics[:branch].percent.round(2)}%) covered." if branchable_result?
str
end

private

# Returns the an erb instance for the template of given name
def template(name)
@templates[name] ||= ERB.new(File.read(File.join(File.dirname(__FILE__), "./views/", "#{name}.md.erb")))
end

def link_to_source_file(source_file)
shortened_filename source_file
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= percent.round(2) %>%
10 changes: 10 additions & 0 deletions spec/spec_helper/simplecov/formatters/views/file_list.md.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# All Files ( <%= covered_percent(source_files.covered_percent) %> covered at <%= source_files.covered_strength.round(2) %> ) hits / line

**<%= source_files.length %>** files in total.
**<%= source_files.lines_of_code %>** relevant lines, **<%= source_files.covered_lines %>** lines covered and **<%= source_files.missed_lines %>** lines missed. (<%= covered_percent(source_files.covered_percent) %>)<% if branchable_result? %>
**<%= source_files.total_branches %>** total branches, **<%= source_files.covered_branches %>** branches covered and **<%= source_files.missed_branches %>** branches missed. (<%= covered_percent(source_files.branch_covered_percent) %>)<% end %>

| File | % covered | Lines | Relevant Lines | Lines covered | Lines missed | Avg. Hits / Line | <% if branchable_result? %> Branch Coverage | Branches | Covered branches | Missed branches | <% end %>
| ---- | --------: | ----: | -------------: | ------------: | -----------: | ---------------: | <% if branchable_result? %> --------------: | -------: | ---------------: | --------------: | <% end %>
<% source_files.each do |source_file| %>| <%= link_to_source_file(source_file) %> | <%= sprintf("%.2f", source_file.covered_percent.round(2)) %> % | <%= source_file.lines.count %> | <%= source_file.covered_lines.count + source_file.missed_lines.count %> | <%= source_file.covered_lines.count %> | <%= source_file.missed_lines.count %> | <%= sprintf("%.2f", source_file.covered_strength.round(2)) %> |<% if branchable_result? %> <%= sprintf("%.2f", source_file.branches_coverage_percent.round(2)) %> % | <%= source_file.total_branches.count %> | <%= source_file.covered_branches.count %> | <%= source_file.missed_branches.count %> |<% end %>
<% end %>
6 changes: 6 additions & 0 deletions spec/spec_helper/simplecov/formatters/views/layout.md.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!-- Generated <%= Time.now %> -->
<%= formatted_file_list("All Files", result.source_files) %>

<% result.groups.each do |name, files| %>
<%= formatted_file_list(name, files) %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= shortened_filename source_file %>

0 comments on commit 5bd9416

Please sign in to comment.