-
-
Notifications
You must be signed in to change notification settings - Fork 752
Command line --require are not visible in spec_helper.rb configuration block #3116
Description
Files required on command line are not visible or available in spec_helper.rb configuration block
I'm working on a project where BigDecimal support is optional and ideally tests would work both with require "bigdecimal" and without it. To that end, it would be useful to skip tests with BigDecimal depending on whether "bigdecimal" was required. However, there seems to be no clean way to do it using metadata and --require, which I thought would be the correct way to go about this.
There are actually two different issues I've noticed:
- With default
.rspecfiles required on command line are required after configuration block has already been executed. RSpec.configuration.requiresseems to always be empty inconfigureblock.
Your environment
- Ruby version:
ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [x86_64-linux],ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [x86_64-linux] - rspec-core version:
rspec-core 3.13.1
Steps to reproduce
Variation 1
These steps reproduce both issues 1 and 2.
.rspec
--require spec_helper
spec_helper.rb
RSpec.configure do |config|
config.disable_monkey_patching!
config.expect_with :rspec do |c|
c.syntax = :expect
end
puts RSpec.configuration.requires.inspect
# Skip tests using BigDecimal if no support is available.
unless defined?(BigDecimal)
config.filter_run_excluding(bigdecimal: true)
warn "no BigDecimal support detected, BigDecimal tests will not be run"
end
endspec/kernel_spec.rb
RSpec.describe Kernel do
describe "#BigDecimal", :bigdecimal do
puts RSpec.configuration.requires.inspect
let(:conversion) { BigDecimal(number) }
context "with an object" do
let(:number) { Object.new }
it "raises an error" do
expect { conversion }.to raise_error TypeError
end
end
end
describe "#puts" do
it "outputs a string" do
expect { puts "string" }.to output("string\n").to_stdout
end
end
endRunning rspec shows that bigdecimal was not required when needed, though it is present in requires in examples:
$ rspec --require bigdecimal
[]
no BigDecimal support detected, BigDecimal tests will not be run
["spec_helper", "bigdecimal"]
Run options: exclude {:bigdecimal=>true}
.
Finished in 0.0011 seconds (files took 0.08006 seconds to load)
1 example, 0 failuresVariation 2
This variation reproduces issue 2 (empty requires).
.rspec
--require bigdecimal
--require spec_helper
Running rspec shows that "bigdecimal" was required and executes previously excluded test, but requires is still empty in configure and filled in specs themselves:
$ rspec
[]
["bigdecimal", "spec_helper"]
..
Finished in 0.00314 seconds (files took 0.07711 seconds to load)
2 examples, 0 failuresExpected behavior
RSpec.configureshould probably be ran after processing allrequires.config.requiresinconfigureblock should not be empty.
Actual behavior
configureis executed immediately on loading spec_helper.config.requiresis filled afterconfigurewas already executed.
While requiring a library in .rspec works for actually running tests, it is very inconvenient, as the same result could be achieved by changing spec_helper.rb, which is exactly the problem I was trying to avoid.