How does Rspec read and load the configuration from .rspec, as well as cmd line

Stanley Meng
1 min readFeb 7, 2020

--

I spent some time on this topic because the code I got was a bit confused. I didn’t understand why it worked in this way. The code is really messy. I’m now doing the refactoring…

Well, anyway, I’ve been using Rspec for over past 2+ year, but I have to say, I’m still new…so, maybe, the following info is not 100% correct… let me know if you have any input.

  1. When you run command “rspec” in cmd, it calls Runner.invoke()
  2. Runner.invoke calls Runner.run()
  3. Runner.run() initializes a ConfigurationOption object and assign it to parameter ‘option’. This ConfigurationOption object reads and loads all the configuration from the command line, `.rspec`, `~/.rspec`, `$XDG_CONFIG_HOME/rspec/options`, `.rspec-local` or a custom options file
  4. Then, Runner.run() creates a Runner object, passing the ConfigurationOption object, a Rspec.configuration object, and Rspec.world object
  5. Runner object has a run() function, run() call setup(), setup() call configure(), configure() loads the content of ConfigurationOption object to the Rspec.configuration object (all these functions are in rspec-core/lib/rspec/core/runner.rb)
  6. Now, the global object Rspec.configuration has everything you pass via the order listed in Step 3
  7. A typical usage is to yield the global configuration object, and edit the settings, for instance:
RSpec.configure do |c|
c.drb = true
c.drb_port = 1234
c.default_path = 'behavior'
end

All details are in github repo: git://github.com/rspec/rspec-core.git
File names:
rspec-core/lib/rspec/core/runner.rb
rspec-core/lib/rspec/core.rb
rspec-core/lib/rspec/core/configuration_options.rb
rspec-core/lib/rspec/core/configuration.rb
rspec-core/exe/rspec

--

--

No responses yet