Class: RSpec::Core::Configuration

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

Instance Attribute Summary collapse

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.



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

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)/
  ]
end

Instance Attribute Details

#formatter_classObject



193
194
195
196
197
198
# File 'lib/rspec/core/configuration.rb', line 193

def formatter_class
  @formatter_class ||= begin
                         require 'rspec/core/formatters/progress_formatter'
                         RSpec::Core::Formatters::ProgressFormatter
                       end
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_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 !!foo

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.



89
90
91
# File 'lib/rspec/core/configuration.rb', line 89

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



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

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


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

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)


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

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

#clear_inclusion_filterObject

:nodoc:



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

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

#color_enabledObject



132
133
134
# File 'lib/rspec/core/configuration.rb', line 132

def color_enabled
  @color_enabled && (output_to_tty? || autotest?)
end

#color_enabled=(bool) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rspec/core/configuration.rb', line 140

def color_enabled=(bool)
  return unless bool
  @color_enabled = true
  if bool && ::RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
    using_stdout = settings[:output_stream] == $stdout
    using_stderr = settings[:error_stream]  == $stderr
    begin
      require 'Win32/Console/ANSI'
      settings[:output_stream] = $stdout if using_stdout
      settings[:error_stream]  = $stderr if using_stderr
    rescue LoadError
      warn "You must 'gem install win32console' to use colour on Windows"
      @color_enabled = false
    end
  end
end

#color_enabled?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/rspec/core/configuration.rb', line 136

def color_enabled?
  !!color_enabled
end

#configure_group(group) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/rspec/core/configuration.rb', line 286

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

  include_or_extend_modules.each do |include_or_extend, mod, filters|
    next unless group.all_apply?(filters)
    next if modules[include_or_extend].include?(mod)
    modules[include_or_extend] << mod
    group.send(include_or_extend, mod)
  end
end

#configure_mock_frameworkObject



300
301
302
303
# File 'lib/rspec/core/configuration.rb', line 300

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

#debug=(bool) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rspec/core/configuration.rb', line 165

def debug=(bool)
  return unless bool
  begin
    require 'ruby-debug'
  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

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



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

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

#files_or_directories_to_run=(*files) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rspec/core/configuration.rb', line 215

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



274
275
276
# File 'lib/rspec/core/configuration.rb', line 274

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

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



263
264
265
266
267
268
269
270
# File 'lib/rspec/core/configuration.rb', line 263

def filter_run_including(options={})
  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
    self.filter = options
  end
end

#formatterObject



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

def formatter
  @formatter ||= formatter_class.new(output)
end

#formatter=(formatter_to_use) ⇒ Object



200
201
202
203
204
205
# File 'lib/rspec/core/configuration.rb', line 200

def formatter=(formatter_to_use)
  self.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'?.")
end

#full_backtrace=(bool) ⇒ Object



128
129
130
# File 'lib/rspec/core/configuration.rb', line 128

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

#full_description=(description) ⇒ Object



187
188
189
# File 'lib/rspec/core/configuration.rb', line 187

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

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



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

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

#libs=(libs) ⇒ Object



157
158
159
# File 'lib/rspec/core/configuration.rb', line 157

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

#line_number=(line_number) ⇒ Object



183
184
185
# File 'lib/rspec/core/configuration.rb', line 183

def line_number=(line_number)
  filter_run :line_number => line_number.to_i
end

#load_spec_filesObject



305
306
307
# File 'lib/rspec/core/configuration.rb', line 305

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

#mock_with(mock_framework) ⇒ Object



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

def mock_with(mock_framework)
  settings[:mock_framework] = mock_framework
end

#puts(message) ⇒ Object



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

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

#reporterObject



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

def reporter
  @reporter ||= Reporter.new(formatter)
end

#require_mock_framework_adapterObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rspec/core/configuration.rb', line 113

def require_mock_framework_adapter
  require case mock_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
end

#requires=(paths) ⇒ Object



161
162
163
# File 'lib/rspec/core/configuration.rb', line 161

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

#settingsObject



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

def settings
  @settings ||= {}
end