class ChefSpec::Coverage
def add(resource)
-
resource
(Chef::Resource
) --
def add(resource) if !exists?(resource) && !filtered?(resource) @collection[resource.to_s] = ResourceWrapper.new(resource) end end
def add_filter(filter = nil, &block)
-
(true)
-
Parameters:
-
block
(Proc
) -- -
filter
(Filter, String, Regexp
) --
def add_filter(filter = nil, &block) id = "#{filter.inspect}/#{block.inspect}".hash @filters[id] = if filter.is_a?(Filter) filter elsif filter.is_a?(String) StringFilter.new(filter) elsif filter.is_a?(Regexp) RegexpFilter.new(filter) elsif block BlockFilter.new(block) else raise ArgumentError, "Please specify either a string, " \ "filter, or block to filter source files with!" end true end
def add_output(&block)
-
(true)
-
Parameters:
-
block
(Proc
) --
def add_output(&block) @outputs << block end
def cover!(resource)
-
resource
(Chef::Resource
) --
def cover!(resource) wrapper = find(resource) wrapper.touch! if wrapper end
def exists?(resource)
def exists?(resource) !find(resource).nil? end
def filtered?(resource)
-
resource
(Chef::Resource
) --
def filtered?(resource) filters.any? { |_, filter| filter.matches?(resource) } end
def find(resource)
def find(resource) @collection[resource.to_s] end
def initialize
Create a new coverage object singleton.
def initialize @collection = {} @filters = {} @outputs = [] add_output do |report| erb = Erubis::Eruby.new(File.read(@template)) puts erb.evaluate(report) rescue NameError => e raise Error::ErbTemplateParseError.new(original_error: e.message) end @template = ChefSpec.root.join("templates", "coverage", "human.erb") end
def method_added(name)
def method_added(name) # Only delegate public methods if method_defined?(name) instance_eval <<-EOH, __FILE__, __LINE__ + 1 def #{name}(*args, &block) instance.public_send(:#{name}, *args, &block) end EOH end end
def report!
- Example: Generating a report -
def report! # Borrowed from simplecov#41 # # If an exception is thrown that isn't a "SystemExit", we need to capture # that error and re-raise. if $! exit_status = $!.is_a?(SystemExit) ? $!.status : EXIT_FAILURE else exit_status = EXIT_SUCCESS end report = {}.tap do |h| h[:total] = @collection.size h[:touched] = @collection.count { |_, resource| resource.touched? } h[:coverage] = ((h[:touched] / h[:total].to_f) * 100).round(2) end report[:untouched_resources] = @collection.collect do |_, resource| resource unless resource.touched? end.compact report[:all_resources] = @collection.values @outputs.each do |block| instance_exec(report, &block) end # Ensure we exit correctly (#351) Kernel.exit(exit_status) if exit_status && exit_status > 0 end
def set_template(file = "human.erb")
-
(true)
-
Parameters:
-
path
(string
) --
def set_template(file = "human.erb") @template = [ ChefSpec.root.join("templates", "coverage", file), File.expand_path(file, Dir.pwd), ].find { |f| File.exist?(f) } raise Error::TemplateNotFound.new(path: file) unless @template end
def start!(&block)
+at_exit+ handler for printing the coverage report.
Start the coverage reporting analysis. This method also adds the the
def start!(&block) warn("ChefSpec's coverage reporting is deprecated and will be removed in a future version") instance_eval(&block) if block at_exit { ChefSpec::Coverage.report! } end