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

Commit b342ffb

Browse files
committed
Allow files_to_run to be used in spec_helper when loaded early via -r.
This changes it to be lazily rather than eagerly computed. This is important to allow `pattern` to be set from a `spec_helper` file loaded via `-r`.
1 parent b2fea38 commit b342ffb

File tree

6 files changed

+136
-50
lines changed

6 files changed

+136
-50
lines changed

features/command_line/init.feature

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,36 @@ Feature: --init option
2222
Given I have a brand new project with no files
2323
And I have run `rspec --init`
2424
When I accept the recommended settings by removing `=begin` and `=end` from `spec/spec_helper.rb`
25-
And I create a spec file with the following content:
26-
"""
25+
And I create "spec/addition_spec.rb" with the following content:
26+
"""ruby
2727
RSpec.describe "Addition" do
2828
it "works" do
2929
expect(1 + 1).to eq(2)
3030
end
3131
end
3232
"""
33-
And I run `rspec`
33+
And I create "spec/subtraction_spec.rb" with the following content:
34+
"""ruby
35+
RSpec.describe "Subtraction" do
36+
it "works" do
37+
expect(1 - 1).to eq(0)
38+
end
39+
end
40+
"""
41+
42+
When I run `rspec`
43+
Then the examples should all pass
44+
And the output should not contain:
45+
"""
46+
Addition
47+
works
48+
"""
49+
50+
When I run `rspec spec/addition_spec.rb`
3451
Then the examples should all pass
52+
And the output should contain:
53+
"""
54+
Addition
55+
works
56+
"""
3557

features/step_definitions/additional_cli_steps.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
end
7474
end
7575

76-
When /^I create a spec file with the following content:$/ do |content|
77-
write_file("spec/example_spec.rb", content)
76+
When /^I create "([^"]*)" with the following content:$/ do |file_name, content|
77+
write_file(file_name, content)
7878
end
7979

lib/rspec/core/configuration.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def treat_symbols_as_metadata_keys_with_true_values=(value)
231231
# @private
232232
add_setting :include_or_extend_modules
233233
# @private
234-
add_setting :files_to_run
234+
attr_writer :files_to_run
235235
# @private
236236
add_setting :expecting_with_rspec
237237
# @private
@@ -248,7 +248,7 @@ def initialize
248248
@expectation_frameworks = []
249249
@include_or_extend_modules = []
250250
@mock_framework = nil
251-
@files_to_run = []
251+
@files_or_directories_to_run = []
252252
@color = false
253253
@pattern = '**/*_spec.rb'
254254
@failure_exit_code = 1
@@ -620,7 +620,12 @@ def profile_examples
620620
def files_or_directories_to_run=(*files)
621621
files = files.flatten
622622
files << default_path if (command == 'rspec' || Runner.running_in_drb?) && default_path && files.empty?
623-
self.files_to_run = get_files_to_run(files)
623+
@files_or_directories_to_run = files
624+
@files_to_run = nil
625+
end
626+
627+
def files_to_run
628+
@files_to_run ||= get_files_to_run(@files_or_directories_to_run)
624629
end
625630

626631
# Creates a method that delegates to `example` including the submitted

lib/rspec/core/configuration_options.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,38 @@ def force?(key)
5252
!UNFORCED_OPTIONS.include?(key)
5353
end
5454

55-
def order(keys, *ordered)
56-
ordered.reverse.each do |key|
55+
def order(keys)
56+
OPTIONS_ORDER.reverse.each do |key|
5757
keys.unshift(key) if keys.delete(key)
5858
end
5959
keys
6060
end
6161

62+
OPTIONS_ORDER = [
63+
# load paths depend on nothing, but must be set before `requires`
64+
# to support load-path-relative requires.
65+
:libs,
66+
67+
# `files_or_directories_to_run` uses `default_path` so it must be
68+
# set before it.
69+
:default_path,
70+
71+
# must be set before `requires` to support checking `config.files_to_run`
72+
# from within `spec_helper.rb` when a `-rspec_helper` option is used.
73+
:files_or_directories_to_run,
74+
75+
# In general, we want to require the specified files as early as possible.
76+
# The `--require` option is specifically intended to allow early requires.
77+
# For later requires, they can just put the require in their spec files, but
78+
# `--require` provides a unique opportunity for users to instruct RSpec to
79+
# load an extension file early for maximum flexibility.
80+
:requires
81+
]
82+
6283
def process_options_into(config)
6384
opts = options.reject { |k, _| UNPROCESSABLE_OPTIONS.include? k }
6485

65-
order(opts.keys, :libs, :requires, :default_path, :pattern).each do |key|
86+
order(opts.keys).each do |key|
6687
force?(key) ? config.force(key => opts[key]) : config.__send__("#{key}=", opts[key])
6788
end
6889
end

spec/rspec/core/configuration_options_spec.rb

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@
2929
opts.configure(config)
3030
end
3131

32-
it "sends loads requires before loading specs" do
32+
it "loads requires before loading specs" do
3333
opts = config_options_object(*%w[-rspec_helper])
34-
config = double("config").as_null_object
34+
config = RSpec::Core::Configuration.new
3535
expect(config).to receive(:requires=).ordered
36-
expect(config).to receive(:files_or_directories_to_run=).ordered
36+
expect(config).to receive(:get_files_to_run).ordered
3737
opts.configure(config)
38+
config.files_to_run
3839
end
3940

4041
it "sets up load path and requires before formatter" do
@@ -45,20 +46,38 @@
4546
opts.configure(config)
4647
end
4748

48-
it "sends default_path before files_or_directories_to_run" do
49+
it "sets default_path before loading specs" do
4950
opts = config_options_object(*%w[--default_path spec])
50-
config = double("config").as_null_object
51+
config = RSpec::Core::Configuration.new
5152
expect(config).to receive(:force).with(:default_path => 'spec').ordered
53+
expect(config).to receive(:get_files_to_run).ordered
54+
opts.configure(config)
55+
config.files_to_run
56+
end
57+
58+
it "sets `files_or_directories_to_run` before `requires` so users can check `files_to_run` in a spec_helper loaded by `--require`" do
59+
opts = config_options_object(*%w[--require spec_helper])
60+
config = RSpec::Core::Configuration.new
5261
expect(config).to receive(:files_or_directories_to_run=).ordered
62+
expect(config).to receive(:requires=).ordered
5363
opts.configure(config)
5464
end
5565

56-
it "sends pattern before files_or_directories_to_run" do
66+
it "sets default_path before `files_or_directories_to_run` since it relies on it" do
67+
opts = config_options_object(*%w[--default_path spec])
68+
config = RSpec::Core::Configuration.new
69+
expect(config).to receive(:force).with(:default_path => 'spec').ordered
70+
expect(config).to receive(:files_or_directories_to_run=).ordered
71+
opts.configure(config)
72+
end
73+
74+
it "sets pattern before loading specs" do
5775
opts = config_options_object(*%w[--pattern **/*.spec])
58-
config = double("config").as_null_object
76+
config = RSpec::Core::Configuration.new
5977
expect(config).to receive(:force).with(:pattern => '**/*.spec').ordered
60-
expect(config).to receive(:files_or_directories_to_run=).ordered
78+
expect(config).to receive(:get_files_to_run).ordered
6179
opts.configure(config)
80+
config.files_to_run
6281
end
6382

6483
it "assigns inclusion_filter" do
@@ -300,11 +319,12 @@
300319

301320
describe "default_path" do
302321
it "gets set before files_or_directories_to_run" do
303-
config = double("config").as_null_object
322+
config = RSpec::Core::Configuration.new
304323
expect(config).to receive(:force).with(:default_path => 'foo').ordered
305-
expect(config).to receive(:files_or_directories_to_run=).ordered
324+
expect(config).to receive(:get_files_to_run).ordered
306325
opts = config_options_object("--default_path", "foo")
307326
opts.configure(config)
327+
config.files_to_run
308328
end
309329
end
310330

0 commit comments

Comments
 (0)