class RSpec::Core::ConfigurationOptions

Responsible for utilizing externally provided configuration options, whether via the command line, ‘.rspec`, `~/.rspec`, `$XDG_CONFIG_HOME/rspec/options`, `.rspec-local` or a custom options file.

Constants

OPTIONS_ORDER
UNFORCED_OPTIONS
UNPROCESSABLE_OPTIONS

Attributes

args[R]

@return [Array<String>] the original command-line arguments

options[R]

@return [Hash] the final merged options, drawn from all external sources

Public Class Methods

new(args) click to toggle source

@param args [Array<String>] command line arguments

# File lib/rspec/core/configuration_options.rb, line 12
def initialize(args)
  @args = args.dup
  organize_options
end

Public Instance Methods

configure(config) click to toggle source

Updates the provided {Configuration} instance based on the provided external configuration options.

@param config [Configuration] the configuration instance to update

# File lib/rspec/core/configuration_options.rb, line 21
def configure(config)
  process_options_into config
  configure_filter_manager config.filter_manager
  load_formatters_into config
end
configure_filter_manager(filter_manager) click to toggle source

@api private Updates the provided {FilterManager} based on the filter options. @param filter_manager [FilterManager] instance to update

# File lib/rspec/core/configuration_options.rb, line 30
def configure_filter_manager(filter_manager)
  @filter_manager_options.each do |command, value|
    filter_manager.__send__ command, value
  end
end

Private Instance Methods

args_from_options_file(path) click to toggle source
# File lib/rspec/core/configuration_options.rb, line 169
def args_from_options_file(path)
  return [] unless path && File.exist?(path)
  config_string = options_file_as_erb_string(path)
  FlatMap.flat_map(config_string.split(/\n+/), &:shellsplit)
end
command_line_options() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 138
def command_line_options
  @command_line_options ||= Parser.parse(@args)
end
custom_options() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 142
def custom_options
  options_from(custom_options_file)
end
custom_options_file() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 183
def custom_options_file
  command_line_options[:custom_options_file]
end
env_options() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 129
def env_options
  return {} unless ENV['SPEC_OPTS']

  parse_args_ignoring_files_or_dirs_to_run(
    Shellwords.split(ENV["SPEC_OPTS"]),
    "ENV['SPEC_OPTS']"
  )
end
file_options() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 121
def file_options
  if custom_options_file
    [custom_options]
  else
    [global_options, project_options, local_options]
  end
end
force?(key) click to toggle source
# File lib/rspec/core/configuration_options.rb, line 66
def force?(key)
  !UNFORCED_OPTIONS.include?(key)
end
global_options() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 154
def global_options
  @global_options ||= options_from(global_options_file)
end
global_options_file() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 195
def global_options_file
  xdg_options_file_if_exists || home_options_file_path
end
home_options_file_path() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 206
def home_options_file_path
  File.join(File.expand_path("~"), ".rspec")
rescue ArgumentError
  # :nocov:
  RSpec.warning "Unable to find ~/.rspec because the HOME environment variable is not set"
  nil
  # :nocov:
end
load_formatters_into(config) click to toggle source
# File lib/rspec/core/configuration_options.rb, line 117
def load_formatters_into(config)
  options[:formatters].each { |pair| config.add_formatter(*pair) } if options[:formatters]
end
local_options() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 146
def local_options
  @local_options ||= options_from(local_options_file)
end
local_options_file() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 191
def local_options_file
  "./.rspec-local"
end
options_file_as_erb_string(path) click to toggle source
# File lib/rspec/core/configuration_options.rb, line 175
def options_file_as_erb_string(path)
  if RUBY_VERSION >= '2.6'
    ERB.new(File.read(path), :trim_mode => '-').result(binding)
  else
    ERB.new(File.read(path), nil, '-').result(binding)
  end
end
options_from(path) click to toggle source
# File lib/rspec/core/configuration_options.rb, line 158
def options_from(path)
  args = args_from_options_file(path)
  parse_args_ignoring_files_or_dirs_to_run(args, path)
end
order(keys) click to toggle source
# File lib/rspec/core/configuration_options.rb, line 70
def order(keys)
  OPTIONS_ORDER.reverse_each do |key|
    keys.unshift(key) if keys.delete(key)
  end
  keys
end
organize_options() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 44
def organize_options
  @filter_manager_options = []

  @options = (file_options << command_line_options << env_options).each do |opts|
    @filter_manager_options << [:include, opts.delete(:inclusion_filter)] if opts.key?(:inclusion_filter)
    @filter_manager_options << [:exclude, opts.delete(:exclusion_filter)] if opts.key?(:exclusion_filter)
  end

  @options = @options.inject(:libs => [], :requires => []) do |hash, opts|
    hash.merge(opts) do |key, oldval, newval|
      [:libs, :requires].include?(key) ? oldval + newval : newval
    end
  end
end
parse_args_ignoring_files_or_dirs_to_run(args, source) click to toggle source
# File lib/rspec/core/configuration_options.rb, line 163
def parse_args_ignoring_files_or_dirs_to_run(args, source)
  options = Parser.parse(args, source)
  options.delete(:files_or_directories_to_run)
  options
end
process_options_into(config) click to toggle source
# File lib/rspec/core/configuration_options.rb, line 109
def process_options_into(config)
  opts = options.reject { |k, _| UNPROCESSABLE_OPTIONS.include? k }

  order(opts.keys).each do |key|
    force?(key) ? config.force(key => opts[key]) : config.__send__("#{key}=", opts[key])
  end
end
project_options() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 150
def project_options
  @project_options ||= options_from(project_options_file)
end
project_options_file() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 187
def project_options_file
  "./.rspec"
end
resolve_xdg_config_home() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 222
def resolve_xdg_config_home
  File.expand_path(ENV.fetch("XDG_CONFIG_HOME", "~/.config"))
rescue ArgumentError
  # :nocov:
  # On Ruby 2.4, `File.expand("~")` works even if `ENV['HOME']` is not set.
  # But on earlier versions, it fails.
  nil
  # :nocov:
end
xdg_options_file_if_exists() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 199
def xdg_options_file_if_exists
  path = xdg_options_file_path
  if path && File.exist?(path)
    path
  end
end
xdg_options_file_path() click to toggle source
# File lib/rspec/core/configuration_options.rb, line 215
def xdg_options_file_path
  xdg_config_home = resolve_xdg_config_home
  if xdg_config_home
    File.join(xdg_config_home, "rspec", "options")
  end
end