Skip to content

Instantly share code, notes, and snippets.

@kei-s
Last active January 18, 2022 04:23
Show Gist options
  • Save kei-s/e5d054942b2fd0070ed46363eeefd837 to your computer and use it in GitHub Desktop.
Save kei-s/e5d054942b2fd0070ed46363eeefd837 to your computer and use it in GitHub Desktop.
Show covered files from SimpleCov result html
# Show covered files from SimpleCov result html
#
# Usage:
#
# ruby covered-files-from-simplecov.rb index.html
#
require 'nokogiri'
THRESHOLD = 100
class SimpleCovHTMLDocument < Nokogiri::XML::SAX::Document
attr_reader :files
def initialize
@in_t_file = false
@in_t_file_coverage = false
@file = nil
@files = {}
super
end
def start_element(name, attributes = [])
attrs = attributes.to_h
if name == 'tr' && attrs['class'] == 't-file'
@in_t_file = true
end
if @in_t_file && name == 'a'
@file = attrs['title']
end
if @in_t_file && name == 'td' && attrs['class'].include?('t-file__coverage')
@in_t_file_coverage = true
end
end
def characters(string)
return unless @in_t_file_coverage
@files[@file] = string.to_f
end
def end_element(name)
@in_t_file = false if @in_t_file && name == 'tr'
@in_t_file_coverage = false if @in_t_file_coverage && name == 'td'
end
end
doc = SimpleCovHTMLDocument.new
parser = Nokogiri::XML::SAX::Parser.new(doc)
parser.parse(File.open(ARGV[0]))
doc.files.each do |file, coverage|
puts file if THRESHOLD <= coverage
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment