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, #mark, #relative_path, #to_h

Methods inherited from Base

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

Constructor Details

#initializeCapture

Initialize capture with independent tracer state.



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

def initialize(...)
	super
	
	@tracer = nil
	@files = {}
end

Instance Method Details

#clearObject

Clear any collected coverage data without stopping coverage.



31
32
33
34
35
36
37
38
# File 'lib/covered/capture.rb', line 31

def clear
	super
	
	@tracer&.stop
	@files = {}
	@tracer = build_tracer
	@tracer.start
end

#execute(source, binding: TOPLEVEL_BINDING) ⇒ Object

Execute the given source while capturing coverage for it.



75
76
77
78
79
80
81
# File 'lib/covered/capture.rb', line 75

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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/covered/capture.rb', line 48

def finish
	@tracer&.stop
	
	@files.each do |path, lines|
		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, 0, lines)
		else
			# warn "Skipping coverage for #{path.inspect} because it doesn't exist!"
			# Ignore.
		end
	end
	
	@tracer = nil
	@files = {}
	
	super
end

#startObject

Start Ruby coverage collection.



22
23
24
25
26
27
28
# File 'lib/covered/capture.rb', line 22

def start
	super
	
	@files = {}
	@tracer = build_tracer
	@tracer.start
end