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) ⇒ Config

Initialize the configuration for a project root and reports.



55
56
57
58
59
60
61
# File 'lib/covered/config.rb', line 55

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

Instance Attribute Details

#coverageObject (readonly)

Returns the value of attribute coverage.



72
73
74
# File 'lib/covered/config.rb', line 72

def coverage
  @coverage
end

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



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

attr :coverage

Class Method Details

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

Load the project coverage configuration for the given root.



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

def self.load(root: self.root, reports: self.reports)
	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)
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.



110
111
112
# File 'lib/covered/config.rb', line 110

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

#each(&block) ⇒ Object

Enumerate the coverage data from the configured policy.



117
118
119
# File 'lib/covered/config.rb', line 117

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

#finishObject

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



99
100
101
102
103
104
105
106
# File 'lib/covered/config.rb', line 99

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.



144
145
146
# File 'lib/covered/config.rb', line 144

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

#include_patternsObject

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



150
151
152
# File 'lib/covered/config.rb', line 150

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

#make_policy(policy) ⇒ Object

Override this method to implement your own policy.



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

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!
	
	policy.reports!(@reports)
end

#outputObject

The configured policy output wrapper.



82
83
84
# File 'lib/covered/config.rb', line 82

def output
	policy.output
end

#policyObject

The configured coverage policy.



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

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.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/covered/config.rb', line 126

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)


65
66
67
# File 'lib/covered/config.rb', line 65

def report?
	!!@reports
end

#startObject

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



88
89
90
91
92
93
94
95
# File 'lib/covered/config.rb', line 88

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