Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit feead38

Browse files
committed
Refactor: remove special cases from configuration options.
It's easiest to leverage the same "order then set configs" logic of `process_options_into` rather than treating libs and requires as special.
1 parent 519cd43 commit feead38

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

lib/rspec/core/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ def safe_include(mod, host)
897897
end
898898

899899
# @private
900-
def setup_load_path_and_require(paths)
900+
def requires=(paths)
901901
directories = ['lib', default_path].select { |p| File.directory? p }
902902
RSpec::Core::RubyProject.add_to_load_path(*directories)
903903
paths.each {|path| require path}

lib/rspec/core/configuration_options.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ def initialize(args)
1616
def configure(config)
1717
config.filter_manager = filter_manager
1818

19-
config.libs = options[:libs] || []
20-
config.setup_load_path_and_require(options[:requires] || [])
21-
2219
process_options_into config
2320
load_formatters_into config
2421
end
@@ -29,7 +26,7 @@ def parse_options
2926
filter_manager.include opts.delete(:inclusion_filter) if opts.has_key?(:inclusion_filter)
3027
filter_manager.exclude opts.delete(:exclusion_filter) if opts.has_key?(:exclusion_filter)
3128
}.
32-
inject {|h, opts|
29+
inject(:libs => [], :requires => []) {|h, opts|
3330
h.merge(opts) {|k, oldval, newval|
3431
[:libs, :requires].include?(k) ? oldval + newval : newval
3532
}
@@ -51,7 +48,7 @@ def filter_manager
5148
:line_numbers, :full_description, :full_backtrace, :tty
5249
].to_set
5350

54-
UNPROCESSABLE_OPTIONS = [:libs, :formatters, :requires].to_set
51+
UNPROCESSABLE_OPTIONS = [:formatters].to_set
5552

5653
def force?(key)
5754
!UNFORCED_OPTIONS.include?(key)
@@ -67,7 +64,7 @@ def order(keys, *ordered)
6764
def process_options_into(config)
6865
opts = options.reject { |k, _| UNPROCESSABLE_OPTIONS.include? k }
6966

70-
order(opts.keys, :default_path, :pattern).each do |key|
67+
order(opts.keys, :libs, :requires, :default_path, :pattern).each do |key|
7168
force?(key) ? config.force(key => opts[key]) : config.__send__("#{key}=", opts[key])
7269
end
7370
end

spec/rspec/core/configuration_options_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@
2626
opts = config_options_object(*%w[--require a/path -I a/lib])
2727
config = double("config").as_null_object
2828
expect(config).to receive(:libs=).ordered
29-
expect(config).to receive(:setup_load_path_and_require).ordered
29+
expect(config).to receive(:requires=).ordered
3030
opts.configure(config)
3131
end
3232

3333
it "sends loads requires before loading specs" do
3434
opts = config_options_object(*%w[-rspec_helper])
3535
config = double("config").as_null_object
36-
expect(config).to receive(:setup_load_path_and_require).ordered
36+
expect(config).to receive(:requires=).ordered
3737
expect(config).to receive(:files_or_directories_to_run=).ordered
3838
opts.configure(config)
3939
end
4040

4141
it "sets up load path and requires before formatter" do
4242
opts = config_options_object(*%w[--require a/path -f a/formatter])
4343
config = double("config").as_null_object
44-
expect(config).to receive(:setup_load_path_and_require).ordered
44+
expect(config).to receive(:requires=).ordered
4545
expect(config).to receive(:add_formatter).ordered
4646
opts.configure(config)
4747
end

spec/rspec/core/configuration_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module RSpec::Core
5353
end
5454
end
5555

56-
describe "#setup_load_path_and_require" do
56+
describe "#requires=" do
5757
include_context "isolate load path mutation"
5858

5959
def absolute_path_to(dir)
@@ -65,7 +65,7 @@ def absolute_path_to(dir)
6565
$LOAD_PATH.delete(lib_dir)
6666

6767
expect($LOAD_PATH).not_to include(lib_dir)
68-
config.setup_load_path_and_require []
68+
config.requires = []
6969
expect($LOAD_PATH).to include(lib_dir)
7070
end
7171

@@ -74,20 +74,20 @@ def absolute_path_to(dir)
7474
foo_dir = absolute_path_to("features")
7575

7676
expect($LOAD_PATH).not_to include(foo_dir)
77-
config.setup_load_path_and_require []
77+
config.requires = []
7878
expect($LOAD_PATH).to include(foo_dir)
7979
end
8080

8181
it 'stores the required files' do
8282
expect(config).to receive(:require).with('a/path')
83-
config.setup_load_path_and_require ['a/path']
83+
config.requires = ['a/path']
8484
expect(config.requires).to eq ['a/path']
8585
end
8686

8787
context "when `default_path` refers to a file rather than a directory" do
8888
it 'does not add it to the load path' do
8989
config.default_path = 'Rakefile'
90-
config.setup_load_path_and_require []
90+
config.requires = []
9191
expect($LOAD_PATH).not_to include(match(/Rakefile/))
9292
end
9393
end

0 commit comments

Comments
 (0)