Skip to content

Commit

Permalink
Merge pull request #16 from ryancyq/simplecov
Browse files Browse the repository at this point in the history
refactor: coverage rake task
  • Loading branch information
ryancyq authored Sep 28, 2024
2 parents 64034d1 + 6407dc0 commit 678ac6b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
6 changes: 5 additions & 1 deletion .simplecov
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# frozen_string_literal: true

SimpleCov.start do
SimpleCov.configure do
enable_coverage :branch
command_name "ruby-#{RUBY_VERSION}"
coverage_dir File.join(ENV.fetch("COV_DIR", "coverage"), command_name)

at_fork do |pid|
command_name "#{command_name} (subprocess: #{pid})"
end

if ENV["CI"]
require "simplecov-cobertura"
formatter SimpleCov::Formatter::CoberturaFormatter
Expand Down
6 changes: 6 additions & 0 deletions .simplecov_spawn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

require "simplecov"

SimpleCov.at_fork.call(Process.pid)
SimpleCov.start
13 changes: 10 additions & 3 deletions tasks/coverage.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@
namespace :coverage do
desc "Run coverage with spec"
task :run do
require "simplecov"
require "rake/clean"
# clean up previously generated report
Rake::Cleaner.cleanup_files([SimpleCov.coverage_dir])

Rake::Task[:spec].invoke(coverage: true)
end

desc "Collates coverage results generated by different test runners"
task :report do
coverage_dir = File.join(ENV.fetch("COV_DIR", "coverage"), "ruby-*")
require "simplecov"
coverage_dir = File.join(File.dirname(SimpleCov.coverage_dir), "ruby-*")
coverage_reports = Dir[File.join(coverage_dir, ".resultset.json")]

raise <<~MSG if coverage_reports.empty?
Coverage reports not found, searched in:
#{Dir[coverage_dir].map { |dir| " - #{dir}\n" }.join}
#{Dir[coverage_dir].map { |dir| " - #{dir}" }.join("\n")}
MSG

require "simplecov"
SimpleCov.collate(coverage_reports) do
coverage_dir "coverage"
end
end
end

task coverage: ["coverage:run", "coverage:report"]
6 changes: 2 additions & 4 deletions tasks/rspec.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec, [:coverage]) do |config, args|
opts = []
opts << "--format documentation"
opts << "--require 'simplecov'" if args[:coverage]
config.rspec_opts = opts.join(" ")
config.ruby_opts = "-r./.simplecov_spawn" if args[:coverage]
config.rspec_opts = "--format documentation"
end

0 comments on commit 678ac6b

Please sign in to comment.