Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Add inline assets option #107

Merged
merged 4 commits into from
Oct 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions lib/simplecov-html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "fileutils"
require "digest/sha1"
require "time"
require "base64"

# Ensure we are using a compatible version of SimpleCov
major, minor, patch = SimpleCov::VERSION.scan(/\d+/).first(3).map(&:to_i)
Expand All @@ -19,11 +20,15 @@ class HTMLFormatter
def initialize
@branchable_result = SimpleCov.branch_coverage?
@templates = {}
@inline_assets = !ENV["SIMPLECOV_INLINE_ASSETS"].nil?
@public_assets_dir = File.join(File.dirname(__FILE__), "../public/")
Copy link
Contributor Author

@frankh frankh Oct 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved @public_assets_dir here to fix ABC rubocop complaints

end

def format(result)
Dir[File.join(File.dirname(__FILE__), "../public/*")].each do |path|
FileUtils.cp_r(path, asset_output_path)
unless @inline_assets
Dir[File.join(@public_assets_dir, "*")].each do |path|
FileUtils.cp_r(path, asset_output_path)
end
end

File.open(File.join(output_path, "index.html"), "wb") do |file|
Expand Down Expand Up @@ -71,9 +76,26 @@ def asset_output_path
end

def assets_path(name)
return asset_inline(name) if @inline_assets

File.join("./assets", SimpleCov::Formatter::HTMLFormatter::VERSION, name)
end

def asset_inline(name)
path = File.join(@public_assets_dir, name)

# Only have a few content types, just hardcode them
content_type = {
".js" => "text/javascript",
".png" => "image/png",
".gif" => "image/gif",
".css" => "text/css",
}[File.extname(name)]

base64_content = Base64.strict_encode64 File.open(path).read
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put a require "base64" on top of the file? I got a NameError when I tried to run.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey, sorry i missed this comment

better 3 years late than never I guess?

"data:#{content_type};base64,#{base64_content}"
end

# Returns the html for the given source_file
def formatted_source_file(source_file)
template("source_file").result(binding)
Expand Down