Module: SimpleCov::ViewCoverage

Extended by:
ViewCoverage
Included in:
ViewCoverage
Defined in:
lib/simplecov/view_coverage.rb,
lib/simplecov/view_coverage/template_compiler.rb

Overview

Brings ActionView templates into the report, for projects that opt in with cover_views.

Rendering a template is already enough to measure it: ActionView compiles each one with module_eval(source, identifier, offset) where identifier is the template's own path, and the offset cancels the def line its wrapper adds, so eval coverage attributes the generated code back to the .erb file at its own line numbers.

A template no test renders is never compiled, so Coverage never hears about it and the report omits it entirely. That is the flattering direction to be wrong in, so every template the run didn't reach is compiled here instead, which lands it in the report at 0%.

Defined Under Namespace

Modules: TemplateCompiler

Instance Method Summary collapse

Instance Method Details

#compile_unrenderedObject

Called once per process, immediately before Coverage.result. It has to happen here rather than at the merge point, where unloaded .rb files are injected, because compiling a template needs ActionView and the merging process need not have it loaded. A standalone simplecov merge never does.



27
28
29
30
31
32
33
# File 'lib/simplecov/view_coverage.rb', line 27

def compile_unrendered
  return [] unless enabled?

  rendered = measured_paths
  discover.reject { |path| rendered.include?(path) }
    .select { |path| TemplateCompiler.call(path) }
end

#discoverObject

Expanded against SimpleCov.root rather than the working directory, and filtered through the same path-only filters the rest of the report honors, so skip "app/views/admin" keeps those templates out instead of compiling them only to drop them later.



43
44
45
46
# File 'lib/simplecov/view_coverage.rb', line 43

def discover
  globs = SimpleCov.view_globs || [] #: Array[String?]
  UnloadedFileInjector.discover(globs, root: SimpleCov.root, reject: SimpleCov.filters.select(&:path_only?))
end

#enabled?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/simplecov/view_coverage.rb', line 35

def enabled?
  SimpleCov.view_coverage? && Coverage.running? && TemplateCompiler.available?
end

#measured_pathsObject

The files Coverage is already holding data for, which for a template means the run rendered it. peek_result copies the whole coverage state to answer, so this stays a once-per-process call.



51
52
53
# File 'lib/simplecov/view_coverage.rb', line 51

def measured_paths
  Coverage.peek_result.keys.to_set
end