Class: Covered::Capture

Inherits:
Wrapper show all
Defined in:
lib/covered/capture.rb

Overview

Captures Ruby coverage data and forwards it to another coverage output.

Constant Summary collapse

EVAL_PATHS =
{
	"(eval)" => true,
	"(irb)" => true,
	"eval" => true
}

Instance Attribute Summary

Attributes inherited from Wrapper

#The wrapped output., #output

Instance Method Summary collapse

Methods inherited from Wrapper

#accept?, #add, #each, #expand_path, #initialize, #mark, #relative_path, #to_h

Methods inherited from Base

#accept?, #add, #each, #expand_path, #mark, #relative_path

Constructor Details

This class inherits a constructor from Covered::Wrapper

Instance Method Details

#clearObject

Clear any collected coverage data without stopping coverage.



21
22
23
24
25
# File 'lib/covered/capture.rb', line 21

def clear
	super
	
	::Coverage.result(stop: false, clear: true)
end

#execute(source, binding: TOPLEVEL_BINDING) ⇒ Object

Execute the given source while capturing coverage for it.



59
60
61
62
63
64
65
# File 'lib/covered/capture.rb', line 59

def execute(source, binding: TOPLEVEL_BINDING)
	start
	
	eval(source.code!, binding, source.path, source.line_offset)
ensure
	finish
end

#finishObject

Stop coverage collection and add the collected results to the output. Ignores Ruby’s anonymous eval paths and files that no longer exist.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/covered/capture.rb', line 35

def finish
	results = ::Coverage.result
	
	results.each do |path, result|
		next if EVAL_PATHS.include?(path)
		
		path = self.expand_path(path)
		
		# Skip files which don't exist. This can happen if `eval` is used with an invalid/incorrect path:
		if File.exist?(path)
			@output.mark(path, 1, result[:lines])
		else
			# warn "Skipping coverage for #{path.inspect} because it doesn't exist!"
			# Ignore.
		end
	end
	
	super
end

#startObject

Start Ruby coverage collection.



14
15
16
17
18
# File 'lib/covered/capture.rb', line 14

def start
	super
	
	::Coverage.start(lines: true, eval: true)
end