-
Notifications
You must be signed in to change notification settings - Fork 154
/
coverband.rb
117 lines (104 loc) · 3.72 KB
/
coverband.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true
require "logger"
require "json"
require "redis"
require "coverband/version"
require "coverband/at_exit"
require "coverband/configuration"
require "coverband/utils/relative_file_converter"
require "coverband/utils/absolute_file_converter"
require "coverband/adapters/base"
require "coverband/adapters/redis_store"
require "coverband/adapters/hash_redis_store"
require "coverband/adapters/file_store"
require "coverband/adapters/stdout_store"
require "coverband/utils/file_hasher"
require "coverband/utils/html_formatter"
require "coverband/utils/result"
require "coverband/utils/file_list"
require "coverband/utils/source_file"
require "coverband/utils/lines_classifier"
require "coverband/utils/results"
require "coverband/collectors/coverage"
require "coverband/collectors/view_tracker"
require "coverband/reporters/base"
require "coverband/reporters/html_report"
require "coverband/reporters/console_report"
require "coverband/reporters/web"
require "coverband/integrations/background"
require "coverband/integrations/background_middleware"
require "coverband/integrations/rack_server_check"
Coverband::Adapters::RedisStore = Coverband::Adapters::HashRedisStore if ENV["COVERBAND_HASH_REDIS_STORE"]
module Coverband
@@configured = false
CONFIG_FILE = "./config/coverband.rb"
RUNTIME_TYPE = :runtime
EAGER_TYPE = :eager_loading
MERGED_TYPE = :merged
TYPES = [RUNTIME_TYPE, EAGER_TYPE]
ALL_TYPES = TYPES + [:merged]
def self.configure(file = nil)
configuration_file = file || ENV.fetch("COVERBAND_CONFIG", CONFIG_FILE)
configuration
if block_given?
yield(configuration)
elsif File.exist?(configuration_file)
load configuration_file
else
configuration.logger.debug("using default configuration")
end
@@configured = true
coverage_instance.reset_instance
end
def self.configured?
@@configured
end
def self.report_coverage
coverage_instance.report_coverage
end
def self.configuration
@configuration ||= Configuration.new
end
def self.start
Coverband::Collectors::Coverage.instance
# TODO: Railtie sets up at_exit after forks, via middleware, perhaps this should be
# added if not rails or if rails but not rackserverrunning
AtExit.register unless tasks_to_ignore?
Background.start if configuration.background_reporting_enabled && !RackServerCheck.running? && !tasks_to_ignore?
end
def self.tasks_to_ignore?
(defined?(Rake) &&
Rake.respond_to?(:application) &&
(Rake&.application&.top_level_tasks || []).any? { |task| Coverband::Configuration::IGNORE_TASKS.include?(task) })
end
def self.eager_loading_coverage!
coverage_instance.eager_loading!
end
def self.eager_loading_coverage(&block)
coverage_instance.eager_loading(&block)
end
def self.runtime_coverage!
coverage_instance.runtime!
end
private_class_method def self.coverage_instance
Coverband::Collectors::Coverage.instance
end
unless ENV["COVERBAND_DISABLE_AUTO_START"]
begin
# Coverband should be setup as early as possible
# to capture usage of things loaded by initializers or other Rails engines
# but after gems are loaded to avoid slowing down gem usage
# best is in application.rb after the bundler line but we get close with Railtie
if defined? ::Rails::Railtie
require "coverband/utils/railtie"
else
configure
start
end
require "coverband/integrations/resque" if defined? ::Resque
rescue Redis::CannotConnectError => error
Coverband.configuration.logger.info "Redis is not available (#{error}), Coverband not configured"
Coverband.configuration.logger.info "If this is a setup task like assets:precompile feel free to ignore"
end
end
end