Class: Covered::Config

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

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

Returns a new instance of Config.



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

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

Instance Attribute Details

#coverageObject (readonly)

Returns the value of attribute coverage.



54
55
56
# File 'lib/covered/config.rb', line 54

def coverage
  @coverage
end

Class Method Details

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



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/covered/config.rb', line 28

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



16
17
18
19
20
21
22
# File 'lib/covered/config.rb', line 16

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

.reportsObject



24
25
26
# File 'lib/covered/config.rb', line 24

def self.reports
  ENV["COVERAGE"]
end

.rootObject



12
13
14
# File 'lib/covered/config.rb', line 12

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

Instance Method Details

#call(output) ⇒ Object

Generate coverage reports to the given output.

Parameters:

  • output (IO)

    The output stream to write the coverage report to.



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

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

#each(&block) ⇒ Object



90
91
92
# File 'lib/covered/config.rb', line 90

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

#finishObject

Finish coverage tracking.



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

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.



96
97
98
# File 'lib/covered/config.rb', line 96

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

#include_patternsObject

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



102
103
104
# File 'lib/covered/config.rb', line 102

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

#make_policy(policy) ⇒ Object

Override this method to implement your own policy.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/covered/config.rb', line 107

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



60
61
62
# File 'lib/covered/config.rb', line 60

def output
  policy.output
end

#policyObject



56
57
58
# File 'lib/covered/config.rb', line 56

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

#report?Boolean Also known as: record?

Returns:

  • (Boolean)


48
49
50
# File 'lib/covered/config.rb', line 48

def report?
  !!@reports
end

#startObject

Start coverage tracking.



65
66
67
68
69
70
71
72
# File 'lib/covered/config.rb', line 65

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