Class: RSpec::Core::Configuration

Inherits:
Object
  • Object
show all
Includes:
Hooks
Defined in:
lib/rspec/core/configuration.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hooks

#after, #around, #before, #find_hook, #hooks, #run_hook, #run_hook!, #run_hook_filtered

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rspec/core/configuration.rb', line 37

def initialize
  @color_enabled = false
  self.include_or_extend_modules = []
  self.files_to_run = []
  self.backtrace_clean_patterns = [
    /\/lib\d*\/ruby\//,
    /bin\//,
    /gems/,
    /spec\/spec_helper\.rb/,
    /lib\/rspec\/(core|expectations|matchers|mocks)/
  ]

  filter_run_excluding(
    :if     => lambda { |value, | .has_key?(:if) && !value },
    :unless => lambda { |value| value }
  )
end

Class Method Details

.add_setting(name, opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rspec/core/configuration.rb', line 8

def self.add_setting(name, opts={})
  if opts[:alias]
    alias_method name, opts[:alias]
    alias_method "#{name}=", "#{opts[:alias]}="
    alias_method "#{name}?", "#{opts[:alias]}?"
  else
    define_method("#{name}=") {|val| settings[name] = val}
    define_method(name)       { settings.has_key?(name) ? settings[name] : opts[:default] }
    define_method("#{name}?") { send name }
  end
end

Instance Method Details

#add_formatter(formatter_to_use, output = nil) ⇒ Object Also known as: formatter=



266
267
268
269
270
271
272
273
# File 'lib/rspec/core/configuration.rb', line 266

def add_formatter(formatter_to_use, output=nil)
  formatter_class =
    built_in_formatter(formatter_to_use) ||
    custom_formatter(formatter_to_use) ||
    (raise ArgumentError, "Formatter '#{formatter_to_use}' unknown - maybe you meant 'documentation' or 'progress'?.")

  formatters << formatter_class.new(output ? File.new(output, 'w') : self.output)
end

#add_setting(name, opts = {}) ⇒ Object

:call-seq:

add_setting(:name)
add_setting(:name, :default => "default_value")
add_setting(:name, :alias => :other_setting)

Use this to add custom settings to the RSpec.configuration object.

RSpec.configuration.add_setting :foo

Creates three methods on the configuration object, a setter, a getter, and a predicate:

RSpec.configuration.foo=(value)
RSpec.configuration.foo()
RSpec.configuration.foo?() # returns true if foo returns anything but nil or false

Intended for extension frameworks like rspec-rails, so they can add config settings that are domain specific. For example:

RSpec.configure do |c|
  c.add_setting :use_transactional_fixtures, :default => true
  c.add_setting :use_transactional_examples, :alias => :use_transactional_fixtures
end

Options

add_setting takes an optional hash that supports the following keys:

:default => "default value"

This sets the default value for the getter and the predicate (which will return true as long as the value is not false or nil).

:alias => :other_setting

Aliases its setter, getter, and predicate, to those for the other_setting.



93
94
95
# File 'lib/rspec/core/configuration.rb', line 93

def add_setting(name, opts={})
  self.class.add_setting(name, opts)
end

#alias_example_to(new_name, extra_options = {}) ⇒ Object

E.g. alias_example_to :crazy_slow, :speed => ‘crazy_slow’ defines crazy_slow as an example variant that has the crazy_slow speed option



307
308
309
# File 'lib/rspec/core/configuration.rb', line 307

def alias_example_to(new_name, extra_options={})
  RSpec::Core::ExampleGroup.alias_example_to(new_name, extra_options)
end

#alias_it_should_behave_like_to(new_name, report_label = '') ⇒ Object

Define an alias for it_should_behave_like that allows different language (like “it_has_behavior” or “it_behaves_like”) to be employed when including shared examples.

Example:

alias_it_should_behave_like_to(:it_has_behavior, 'has behavior:')

allows the user to include a shared example group like:

describe Entity do
  it_has_behavior 'sortability' do
    let(:sortable) { Entity.new }
  end
end

which is reported in the output as:

Entity
  has behavior: sortability
    # sortability examples here


332
333
334
# File 'lib/rspec/core/configuration.rb', line 332

def alias_it_should_behave_like_to(new_name, report_label = '')
  RSpec::Core::ExampleGroup.alias_it_should_behave_like_to(new_name, report_label)
end

#cleaned_from_backtrace?(line) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/rspec/core/configuration.rb', line 109

def cleaned_from_backtrace?(line)
  backtrace_clean_patterns.any? { |regex| line =~ regex }
end

#clear_inclusion_filterObject

:nodoc:



105
106
107
# File 'lib/rspec/core/configuration.rb', line 105

def clear_inclusion_filter # :nodoc:
  self.filter = nil
end

#color_enabledObject



212
213
214
# File 'lib/rspec/core/configuration.rb', line 212

def color_enabled
  @color_enabled && output_to_tty?
end

#color_enabled=(bool) ⇒ Object



220
221
222
223
224
225
226
227
228
229
# File 'lib/rspec/core/configuration.rb', line 220

def color_enabled=(bool)
  return unless bool
  @color_enabled = true
  if bool && ::RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
    unless ENV['ANSICON']
      warn "You must use ANSICON 1.31 or later (http://adoxa.110mb.com/ansicon/) to use colour on Windows"
      @color_enabled = false
    end
  end
end

#color_enabled?Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/rspec/core/configuration.rb', line 216

def color_enabled?
  color_enabled
end

#configure_expectation_frameworkObject



380
381
382
383
384
# File 'lib/rspec/core/configuration.rb', line 380

def configure_expectation_framework
  expectation_frameworks.each do |framework|
    RSpec::Core::ExampleGroup.send(:include, framework)
  end
end

#configure_group(group) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/rspec/core/configuration.rb', line 363

def configure_group(group)
  modules = {
    :include => group.included_modules.dup,
    :extend  => group.ancestors.dup
  }

  include_or_extend_modules.each do |include_or_extend, mod, filters|
    next unless filters.empty? || group.apply?(:any?, filters)
    next if self.class < mod
    group.send(include_or_extend, mod)
  end
end

#configure_mock_frameworkObject



376
377
378
# File 'lib/rspec/core/configuration.rb', line 376

def configure_mock_framework
  RSpec::Core::ExampleGroup.send(:include, mock_framework)
end

#debug=(bool) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/rspec/core/configuration.rb', line 239

def debug=(bool)
  return unless bool
  begin
    require 'ruby-debug'
    Debugger.start
  rescue LoadError
    raise <<-EOM

#{'*'*50}
You must install ruby-debug to run rspec with the --debug option.

If you have ruby-debug installed as a ruby gem, then you need to either
require 'rubygems' or configure the RUBYOPT environment variable with
the value 'rubygems'.
#{'*'*50}
EOM
  end
end

#expect_with(*frameworks) ⇒ Object

Sets the expectation framework module(s).

frameworks can be :rspec, :stdlib, or both

Given :rspec, configures rspec/expectations. Given :stdlib, configures test/unit/assertions Given both, configures both



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/rspec/core/configuration.rb', line 190

def expect_with(*frameworks)
  settings[:expectation_frameworks] = []
  frameworks.each do |framework|
    case framework
    when Symbol
      case framework
      when :rspec
        require 'rspec/core/expecting/with_rspec'
      when :stdlib
        require 'rspec/core/expecting/with_stdlib'
      else
        raise ArgumentError, "#{framework.inspect} is not supported"
      end
      settings[:expectation_frameworks] << RSpec::Core::ExpectationFrameworkAdapter
    end
  end
end

#expectation_framework=(framework) ⇒ Object

Delegates to expect_with=()



179
180
181
# File 'lib/rspec/core/configuration.rb', line 179

def expectation_framework=(framework)
  expect_with([framework])
end

#expectation_frameworksObject

Returns the configured expectation framework adapter module(s)



171
172
173
174
175
176
# File 'lib/rspec/core/configuration.rb', line 171

def expectation_frameworks
  settings[:expectation_frameworks] ||= begin
                                         require 'rspec/core/expecting/with_rspec'
                                         [RSpec::Core::ExpectationFrameworkAdapter]
                                       end
end

#extend(mod, filters = {}) ⇒ Object



359
360
361
# File 'lib/rspec/core/configuration.rb', line 359

def extend(mod, filters={})
  include_or_extend_modules << [:extend, mod, filters]
end

#files_or_directories_to_run=(*files) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/rspec/core/configuration.rb', line 288

def files_or_directories_to_run=(*files)
  self.files_to_run = files.flatten.collect do |file|
    if File.directory?(file)
      filename_pattern.split(",").collect do |pattern|
        Dir["#{file}/#{pattern.strip}"]
      end
    else
      if file =~ /(\:(\d+))$/
        self.line_number = $2
        file.sub($1,'')
      else
        file
      end
    end
  end.flatten
end

#filter_run_excluding(options = {}) ⇒ Object



351
352
353
# File 'lib/rspec/core/configuration.rb', line 351

def filter_run_excluding(options={})
  self.exclusion_filter = (exclusion_filter || {}).merge(options)
end

#filter_run_including(options = {}, force_overwrite = false) ⇒ Object Also known as: filter_run



336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/rspec/core/configuration.rb', line 336

def filter_run_including(options={}, force_overwrite = false)
  if filter and filter[:line_number] || filter[:full_description]
    warn "Filtering by #{options.inspect} is not possible since " \
         "you are already filtering by #{filter.inspect}"
  else
    if force_overwrite
      self.filter = options
    else
      self.filter = (filter || {}).merge(options)
    end
  end
end

#formattersObject



277
278
279
# File 'lib/rspec/core/configuration.rb', line 277

def formatters
  @formatters ||= []
end

#full_backtrace=(bool) ⇒ Object



208
209
210
# File 'lib/rspec/core/configuration.rb', line 208

def full_backtrace=(bool)
  settings[:backtrace_clean_patterns] = []
end

#full_description=(description) ⇒ Object



262
263
264
# File 'lib/rspec/core/configuration.rb', line 262

def full_description=(description)
  filter_run({ :full_description => /#{description}/ }, true)
end

#include(mod, filters = {}) ⇒ Object



355
356
357
# File 'lib/rspec/core/configuration.rb', line 355

def include(mod, filters={})
  include_or_extend_modules << [:include, mod, filters]
end

#libs=(libs) ⇒ Object



231
232
233
# File 'lib/rspec/core/configuration.rb', line 231

def libs=(libs)
  libs.map {|lib| $LOAD_PATH.unshift lib}
end

#line_number=(line_number) ⇒ Object



258
259
260
# File 'lib/rspec/core/configuration.rb', line 258

def line_number=(line_number)
  filter_run({ :line_number => line_number.to_i }, true)
end

#load_spec_filesObject



386
387
388
389
# File 'lib/rspec/core/configuration.rb', line 386

def load_spec_files
  files_to_run.map {|f| load File.expand_path(f) }
  raise_if_rspec_1_is_loaded
end

#mock_frameworkObject

Returns the configured mock framework adapter module



114
115
116
117
118
119
# File 'lib/rspec/core/configuration.rb', line 114

def mock_framework
  settings[:mock_framework] ||= begin
                                  require 'rspec/core/mocking/with_rspec'
                                  RSpec::Core::MockFrameworkAdapter
                                end
end

#mock_framework=(framework) ⇒ Object

Sets the mock framework adapter module.

framework can be a Symbol or a Module.

Given any of :rspec, :mocha, :flexmock, or :rr, configures the named framework.

Given :nothing, configures no framework. Use this if you don’t use any mocking framework to save a little bit of overhead.

Given a Module, includes that module in every example group. The module should adhere to RSpec’s mock framework adapter API:

setup_mocks_for_rspec
  - called before each example

verify_mocks_for_rspec
  - called after each example. Framework should raise an exception
    when expectations fail

teardown_mocks_for_rspec
  - called after verify_mocks_for_rspec (even if there are errors)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rspec/core/configuration.rb', line 148

def mock_framework=(framework)
  case framework
  when Module
    settings[:mock_framework] = framework
  when String, Symbol
    require case framework.to_s
            when /rspec/i
              'rspec/core/mocking/with_rspec'
            when /mocha/i
              'rspec/core/mocking/with_mocha'
            when /rr/i
              'rspec/core/mocking/with_rr'
            when /flexmock/i
              'rspec/core/mocking/with_flexmock'
            else
              'rspec/core/mocking/with_absolutely_nothing'
            end
    settings[:mock_framework] = RSpec::Core::MockFrameworkAdapter
  else
  end
end

#mock_with(framework) ⇒ Object

Delegates to mock_framework=(framework)



122
123
124
# File 'lib/rspec/core/configuration.rb', line 122

def mock_with(framework)
  self.mock_framework = framework
end

#puts(message) ⇒ Object



97
98
99
# File 'lib/rspec/core/configuration.rb', line 97

def puts(message)
  output_stream.puts(message)
end

#reporterObject



281
282
283
284
285
286
# File 'lib/rspec/core/configuration.rb', line 281

def reporter
  @reporter ||= begin
                  add_formatter('progress') if formatters.empty?
                  Reporter.new(*formatters)
                end
end

#requires=(paths) ⇒ Object



235
236
237
# File 'lib/rspec/core/configuration.rb', line 235

def requires=(paths)
  paths.map {|path| require path}
end

#settingsObject



101
102
103
# File 'lib/rspec/core/configuration.rb', line 101

def settings
  @settings ||= {}
end