Sha256: 5f517465aecc8d959ffb7bed13c7c3a6a8565e50aa2e26b9419a6df3dfe72667

Contents?: true

Size: 1.05 KB

Versions: 12

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2018-2023, by Samuel Williams.

require_relative 'wrapper'

require 'coverage'

module Covered
	class Capture < Wrapper
		def start
			super
			
			::Coverage.start(lines: true, eval: true)
		end
		
		def clear
			super
			
			::Coverage.result(stop: false, clear: true)
		end
		
		EVAL_PATHS = {
			"(eval)" => true,
			"(irb)" => true,
			"eval" => true
		}
		
		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
		
		def execute(source, binding: TOPLEVEL_BINDING)
			start
			
			eval(source.code!, binding, source.path, source.line_offset)
		ensure
			finish
		end
	end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
covered-0.26.0 lib/covered/capture.rb
covered-0.25.1 lib/covered/capture.rb
covered-0.25.0 lib/covered/capture.rb
covered-0.24.3 lib/covered/capture.rb
covered-0.24.2 lib/covered/capture.rb
covered-0.24.1 lib/covered/capture.rb
covered-0.24.0 lib/covered/capture.rb
covered-0.23.0 lib/covered/capture.rb
covered-0.22.1 lib/covered/capture.rb
covered-0.22.0 lib/covered/capture.rb
covered-0.21.0 lib/covered/capture.rb
covered-0.20.2 lib/covered/capture.rb