Skip to content

Commit 6000b0c

Browse files
committed
Lazy-load profiles + autodiscover plugin gems
Previously every bundled profile (rails, test_frameworks, root_filter, bundler_filter, hidden_filter) was eagerly required from defaults.rb, even when the user only needed one of them. Move to lazy loading via Profiles#fetch_proc, which on a miss tries: 1. require "simplecov/profiles/<name>" — bundled profiles 2. require "simplecov-profile-<name>" — third-party plugin gems This both shrinks the default load (rails / test_frameworks no longer get required unless used) and gives third-party profiles a published gem-naming convention so users can `SimpleCov.start "myprofile"` without a manual require beforehand. Update defaults_spec to use the new public Profiles#fetch_proc rather than poking the underlying hash directly.
1 parent e7ed0e8 commit 6000b0c

3 files changed

Lines changed: 32 additions & 8 deletions

File tree

lib/simplecov/defaults.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
require "pathname"
44
require_relative "formatter/html_formatter"
5-
require_relative "profiles/root_filter"
6-
require_relative "profiles/test_frameworks"
7-
require_relative "profiles/bundler_filter"
8-
require_relative "profiles/hidden_filter"
9-
require_relative "profiles/rails"
105

11-
# Default configuration
6+
# Default configuration. Profiles autoload on first reference via
7+
# `SimpleCov.profiles.fetch_proc`; the unused ones (e.g. "rails",
8+
# "test_frameworks") never get required unless a user opts in.
129
SimpleCov.configure do
1310
formatter SimpleCov::Formatter::HTMLFormatter
1411

lib/simplecov/profiles.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,37 @@ def define(name, &blk)
2626
# Applies the profile of given name on SimpleCov.configure
2727
#
2828
def load(name)
29+
SimpleCov.configure(&fetch_proc(name))
30+
end
31+
32+
#
33+
# Returns the proc registered for the given profile name, autoloading
34+
# bundled or plugin-gem profiles on first lookup. Raises if the profile
35+
# cannot be located.
36+
#
37+
# Lookup order:
38+
# 1. already registered via #define
39+
# 2. require "simplecov/profiles/<name>" (bundled profiles)
40+
# 3. require "simplecov-profile-<name>" (third-party plugin gems)
41+
#
42+
def fetch_proc(name)
2943
name = name.to_sym
44+
autoload_profile(name) unless key?(name)
3045
raise "Could not find SimpleCov Profile called '#{name}'" unless key?(name)
3146

32-
SimpleCov.configure(&self[name])
47+
self[name]
48+
end
49+
50+
private
51+
52+
def autoload_profile(name)
53+
require "simplecov/profiles/#{name}"
54+
rescue LoadError
55+
begin
56+
require "simplecov-profile-#{name}"
57+
rescue LoadError
58+
# fall through; #fetch_proc raises the user-facing error
59+
end
3360
end
3461
end
3562
end

spec/defaults_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
include SimpleCov::Configuration
1212

1313
def load_profile(name)
14-
configure(&SimpleCov.profiles[name.to_sym])
14+
configure(&SimpleCov.profiles.fetch_proc(name))
1515
end
1616
end
1717
end

0 commit comments

Comments
 (0)