Class: Covered::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/covered/config.rb

Overview

Loads project coverage configuration and controls a configured policy.

Constant Summary collapse

PATH =
"config/covered.rb"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, reports, persist = true) ⇒ Config

Initialize the configuration for a project root and reports.



57
58
59
60
61
62
63
64
# File 'lib/covered/config.rb', line 57

def initialize(root, reports, persist = true)
	@root = root
	@reports = reports
	@policy = nil
	@persist = persist
	
	@environment = nil
end

Instance Attribute Details

#coverageObject (readonly)

Returns the value of attribute coverage.



75
76
77
# File 'lib/covered/config.rb', line 75

def coverage
  @coverage
end

#The active coverage policy, if assigned by an integration.(activecoveragepolicy) ⇒ Object (readonly)



75
# File 'lib/covered/config.rb', line 75

attr :coverage

Class Method Details

.load(root: self.root, reports: self.reports, persist: true) ⇒ Object

Load the project coverage configuration for the given root.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/covered/config.rb', line 41

def self.load(root: self.root, reports: self.reports, persist: true)
	derived = Class.new(self)
	
	if path = self.path(root)
		config = Module.new
		config.module_eval(::File.read(path), path)
		derived.prepend(config)
	end
	
	return derived.new(root, reports, persist)
end

.path(root) ⇒ Object

The coverage configuration path under the given root.



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

def self.path(root)
	path = ::File.expand_path(PATH, root)
	
	if ::File.exist?(path)
		return path
	end
end

.reportsObject

The report names requested by the environment.



32
33
34
# File 'lib/covered/config.rb', line 32

def self.reports
	ENV["COVERAGE"]
end

.rootObject

The root directory used for coverage configuration.



15
16
17
# File 'lib/covered/config.rb', line 15

def self.root
	ENV["COVERED_ROOT"] || Dir.pwd
end

Instance Method Details

#call(output) ⇒ Object

Generate coverage reports to the given output.



113
114
115
# File 'lib/covered/config.rb', line 113

def call(output)
	policy.call(output)
end

#each(&block) ⇒ Object

Enumerate the coverage data from the configured policy.



120
121
122
# File 'lib/covered/config.rb', line 120

def each(&block)
	policy.each(&block)
end

#finishObject

Finish coverage tracking. Stops the policy capture pipeline and restores the environment saved by #start.



102
103
104
105
106
107
108
109
# File 'lib/covered/config.rb', line 102

def finish
	# Finish coverage tracking:
	policy.finish
	
	# Restore the environment:
	ENV.replace(@environment)
	@environment = nil
end

#ignore_pathsObject

Which paths to ignore when computing coverage for a given project.



147
148
149
# File 'lib/covered/config.rb', line 147

def ignore_paths
	["test/", "fixtures/", "spec/", "vendor/", "config/"]
end

#include_patternsObject

Which paths to include when computing coverage for a given project.



153
154
155
# File 'lib/covered/config.rb', line 153

def include_patterns
	["lib/**/*.rb"]
end

#make_policy(policy) ⇒ Object

Override this method to implement your own policy.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/covered/config.rb', line 159

def make_policy(policy)
	# Only files in the root would be tracked:
	policy.root(@root)
	
	patterns = ignore_paths.map do |path|
		File.join(@root, path)
	end
	
	# We will ignore any files in the test or spec directory:
	policy.skip(Regexp.union(patterns))
	
	# We will include all files under lib, even if they aren't loaded:
	include_patterns.each do |pattern|
		policy.include(pattern)
	end
	
	policy.persist! if @persist
	
	policy.reports!(@reports)
end

#outputObject

The configured policy output wrapper.



85
86
87
# File 'lib/covered/config.rb', line 85

def output
	policy.output
end

#policyObject

The configured coverage policy.



79
80
81
# File 'lib/covered/config.rb', line 79

def policy
	@policy ||= Policy.new.tap{|policy| make_policy(policy)}.freeze
end

#policy_for(paths = nil, ignore_mtime: true) ⇒ Object

Build a configured policy using coverage data from persistent storage.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/covered/config.rb', line 129

def policy_for(paths = nil, ignore_mtime: true)
	paths ||= Dir.glob(Persist::DEFAULT_PATH, base: @root)
	paths = Array(paths)
	
	if paths.empty?
		raise ArgumentError, "No coverage paths specified!"
	end
	
	paths.each do |path|
		# It would be nice to have a better algorithm here than just ignoring mtime - perhaps using checksums:
		Persist.new(policy.output, path).load!(ignore_mtime: ignore_mtime)
	end
	
	return policy
end

#report?Boolean Also known as: record?

Whether reports should be generated.

Returns:

  • (Boolean)


68
69
70
# File 'lib/covered/config.rb', line 68

def report?
	!!@reports
end

#startObject

Start coverage tracking. Stores the current environment, configures child process autostart, and starts the policy capture pipeline.



91
92
93
94
95
96
97
98
# File 'lib/covered/config.rb', line 91

def start
	# Save and setup the environment:
	@environment = ENV.to_h
	autostart!
	
	# Start coverage tracking:
	policy.start
end