Class: SimpleCov::Formatter::HTMLFormatter
- Defined in:
- lib/simplecov/formatter/html_formatter.rb,
sig/simplecov.rbs
Overview
Writes the HTML report: coverage_data.js plus pre-compiled static assets, and coverage.json as a side artifact (shared serialization via JSONFormatter.build_hash).
Constant Summary collapse
- DATA_FILENAME =
"coverage_data.js"
Instance Method Summary collapse
-
#atomic_write(dest, content) ⇒ void
Write via temp file + File.rename so parallel writers can't race and read-only existing assets are replaced without EACCES.
- #copy_static_assets(dest_dir = output_path) ⇒ void
- #entry_point_filename ⇒ String
- #format(result) ⇒ void
-
#format_from_json(json_path, output_dir) ⇒ void
Generate HTML from a pre-existing coverage.json file without a live SimpleCov::Result or a running test suite.
- #public_dir ⇒ String
Methods inherited from Base
#displayable_output_path, #initialize, #message_prefix, #output_message, #output_path, #relative_or_absolute_output_path, #stats_line
Constructor Details
This class inherits a constructor from SimpleCov::Formatter::Base
Instance Method Details
#atomic_write(dest, content) ⇒ void
This method returns an undefined value.
Write via temp file + File.rename so parallel writers can't race and read-only existing assets are replaced without EACCES.
68 69 70 71 72 73 74 75 76 |
# File 'lib/simplecov/formatter/html_formatter.rb', line 68 def atomic_write(dest, content) temp = "#{dest}.#{Process.pid}.#{rand(2**32).to_s(36)}" begin File.binwrite(temp, content) File.rename(temp, dest) ensure FileUtils.rm_f(temp) end end |
#copy_static_assets(dest_dir = output_path) ⇒ void
This method returns an undefined value.
55 56 57 58 59 |
# File 'lib/simplecov/formatter/html_formatter.rb', line 55 def copy_static_assets(dest_dir = output_path) Dir[File.join(public_dir, "*")].each do |src| atomic_write(File.join(dest_dir, File.basename(src)), File.binread(src)) end end |
#entry_point_filename ⇒ String
51 52 53 |
# File 'lib/simplecov/formatter/html_formatter.rb', line 51 def entry_point_filename "index.html" end |
#format(result) ⇒ void
This method returns an undefined value.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/simplecov/formatter/html_formatter.rb', line 17 def format(result) # `coverage_data.js` feeds the client-side viewer, which renders # source from the embedded array — it always needs `source`, # regardless of `SimpleCov.source_in_json`. The side-file # `coverage.json` honors the setting so downstream tools that # read source from disk can opt into a smaller payload. When # the setting is at its default (true), the two files share a # single serialization. FileUtils.mkdir_p(output_path) viewer_json = JSON.pretty_generate(JSONFormatter.build_hash(result, include_source: true)) coverage_json = SimpleCov.source_in_json ? viewer_json : JSON.pretty_generate(JSONFormatter.build_hash(result)) atomic_write(File.join(output_path, JSONFormatter::FILENAME), coverage_json) atomic_write(File.join(output_path, DATA_FILENAME), "window.SIMPLECOV_DATA = #{viewer_json};\n") copy_static_assets # stderr, not stdout: this is a status message, not the program's # output. Keeps the line out of pipelines like `rspec -f json`. And # $stderr.puts, not `warn`: a status line should not reach # `Warning.warn` hooks or vanish under `-W0` (see #1225). $stderr.puts (result) unless @silent # rubocop:disable Style/StderrPuts end |
#format_from_json(json_path, output_dir) ⇒ void
This method returns an undefined value.
Generate HTML from a pre-existing coverage.json file without a live SimpleCov::Result or a running test suite.
42 43 44 45 46 47 |
# File 'lib/simplecov/formatter/html_formatter.rb', line 42 def format_from_json(json_path, output_dir) FileUtils.mkdir_p(output_dir) json = File.read(json_path) atomic_write(File.join(output_dir, DATA_FILENAME), "window.SIMPLECOV_DATA = #{json};\n") copy_static_assets(output_dir) end |
#public_dir ⇒ String
78 79 80 81 82 |
# File 'lib/simplecov/formatter/html_formatter.rb', line 78 def public_dir # `.to_s` collapses `__dir__`'s nil arm (only possible under eval, # which can't happen for a file on disk) so the path is a String. File.join(__dir__.to_s, "html_formatter/public/") end |