55require "fileutils"
66require "digest/sha1"
77require "time"
8+ require "base64"
89
910# Ensure we are using a compatible version of SimpleCov
1011major , minor , patch = SimpleCov ::VERSION . scan ( /\d +/ ) . first ( 3 ) . map ( &:to_i )
@@ -19,11 +20,16 @@ class HTMLFormatter # rubocop:disable Metrics/ClassLength
1920 def initialize
2021 @branch_coverage = SimpleCov . branch_coverage?
2122 @method_coverage = SimpleCov . method_coverage?
23+ @templates = { }
24+ @inline_assets = !ENV [ "SIMPLECOV_INLINE_ASSETS" ] . nil?
25+ @public_assets_dir = File . join ( File . dirname ( __FILE__ ) , "../public/" )
2226 end
2327
2428 def format ( result )
25- Dir [ File . join ( File . dirname ( __FILE__ ) , "../public/*" ) ] . each do |path |
26- FileUtils . cp_r ( path , asset_output_path )
29+ unless @inline_assets
30+ Dir [ File . join ( @public_assets_dir , "*" ) ] . each do |path |
31+ FileUtils . cp_r ( path , asset_output_path )
32+ end
2733 end
2834
2935 File . open ( File . join ( output_path , "index.html" ) , "wb" ) do |file |
@@ -68,7 +74,7 @@ def line_status?(source_file, line)
6874
6975 # Returns the an erb instance for the template of given name
7076 def template ( name )
71- ERB . new ( File . read ( File . join ( File . dirname ( __FILE__ ) , "../views/" , "#{ name } .erb" ) ) )
77+ @templates [ name ] ||= ERB . new ( File . read ( File . join ( File . dirname ( __FILE__ ) , "../views/" , "#{ name } .erb" ) ) )
7278 end
7379
7480 def output_path
@@ -84,9 +90,26 @@ def asset_output_path
8490 end
8591
8692 def assets_path ( name )
93+ return asset_inline ( name ) if @inline_assets
94+
8795 File . join ( "./assets" , SimpleCov ::Formatter ::HTMLFormatter ::VERSION , name )
8896 end
8997
98+ def asset_inline ( name )
99+ path = File . join ( @public_assets_dir , name )
100+
101+ # Only have a few content types, just hardcode them
102+ content_type = {
103+ ".js" => "text/javascript" ,
104+ ".png" => "image/png" ,
105+ ".gif" => "image/gif" ,
106+ ".css" => "text/css" ,
107+ } [ File . extname ( name ) ]
108+
109+ base64_content = Base64 . strict_encode64 File . open ( path ) . read
110+ "data:#{ content_type } ;base64,#{ base64_content } "
111+ end
112+
90113 # Returns the html for the given source_file
91114 def formatted_source_file ( source_file )
92115 template ( "source_file" ) . result ( binding )
0 commit comments