Class: Spec::Runner::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/plugins/rspec/lib/spec/runner/options.rb

Constant Summary collapse

FILE_SORTERS =
{
  'mtime' => lambda {|file_a, file_b| File.mtime(file_b) <=> File.mtime(file_a)}
}
EXAMPLE_FORMATTERS =

Load these lazily for better speed

{ # Load these lazily for better speed
          'silent' => ['spec/runner/formatter/silent_formatter',                 'Formatter::SilentFormatter'],
               'l' => ['spec/runner/formatter/silent_formatter',                 'Formatter::SilentFormatter'],
         'specdoc' => ['spec/runner/formatter/specdoc_formatter',                'Formatter::SpecdocFormatter'],
               's' => ['spec/runner/formatter/specdoc_formatter',                'Formatter::SpecdocFormatter'],
          'nested' => ['spec/runner/formatter/nested_text_formatter',            'Formatter::NestedTextFormatter'],
               'n' => ['spec/runner/formatter/nested_text_formatter',            'Formatter::NestedTextFormatter'],
            'html' => ['spec/runner/formatter/html_formatter',                   'Formatter::HtmlFormatter'],
               'h' => ['spec/runner/formatter/html_formatter',                   'Formatter::HtmlFormatter'],
        'progress' => ['spec/runner/formatter/progress_bar_formatter',           'Formatter::ProgressBarFormatter'],
               'p' => ['spec/runner/formatter/progress_bar_formatter',           'Formatter::ProgressBarFormatter'],
'failing_examples' => ['spec/runner/formatter/failing_examples_formatter',       'Formatter::FailingExamplesFormatter'],
               'e' => ['spec/runner/formatter/failing_examples_formatter',       'Formatter::FailingExamplesFormatter'],
'failing_example_groups' => ['spec/runner/formatter/failing_example_groups_formatter', 'Formatter::FailingExampleGroupsFormatter'],
               'g' => ['spec/runner/formatter/failing_example_groups_formatter', 'Formatter::FailingExampleGroupsFormatter'],
         'profile' => ['spec/runner/formatter/profile_formatter',                'Formatter::ProfileFormatter'],
               'o' => ['spec/runner/formatter/profile_formatter',                'Formatter::ProfileFormatter'],
        'textmate' => ['spec/runner/formatter/text_mate_formatter',              'Formatter::TextMateFormatter']
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_stream, output_stream) ⇒ Options

Returns a new instance of Options.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 54

def initialize(error_stream, output_stream)
  @error_stream = error_stream
  @output_stream = output_stream
  @filename_pattern = "**/*_spec.rb"
  @backtrace_tweaker = QuietBacktraceTweaker.new
  @examples = []
  @colour = false
  @profile = false
  @dry_run = false
  @debug = false
  @reporter = Reporter.new(self)
  @context_lines = 3
  @diff_format  = :unified
  @files = []
  @example_groups = []
  @result = nil
  @examples_run = false
  @examples_should_be_run = nil
  @user_input_for_runner = nil
  @after_suite_parts = []
  @files_loaded = false
  @out_used = nil
end

Instance Attribute Details

#argvObject

Returns the value of attribute argv.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def argv
  @argv
end

#autospecObject

Returns the value of attribute autospec.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def autospec
  @autospec
end

#backtrace_tweakerObject

Returns the value of attribute backtrace_tweaker.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def backtrace_tweaker
  @backtrace_tweaker
end

#colourObject

Returns the value of attribute colour.



52
53
54
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 52

def colour
  @colour
end

#context_linesObject

Returns the value of attribute context_lines.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def context_lines
  @context_lines
end

#debugObject

Returns the value of attribute debug.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def debug
  @debug
end

#diff_formatObject

Returns the value of attribute diff_format.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def diff_format
  @diff_format
end

#differ_classObject

Returns the value of attribute differ_class.



52
53
54
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 52

def differ_class
  @differ_class
end

#dry_runObject

Returns the value of attribute dry_run.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def dry_run
  @dry_run
end

#error_streamObject

Returns the value of attribute error_stream.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def error_stream
  @error_stream
end

#example_groupsObject (readonly)

Returns the value of attribute example_groups.



52
53
54
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 52

def example_groups
  @example_groups
end

#examplesObject (readonly)

Returns the value of attribute examples.



52
53
54
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 52

def examples
  @examples
end

#filename_patternObject

Returns the value of attribute filename_pattern.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def filename_pattern
  @filename_pattern
end

#filesObject (readonly)

Returns the value of attribute files.



52
53
54
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 52

def files
  @files
end

#heckle_runnerObject

Returns the value of attribute heckle_runner.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def heckle_runner
  @heckle_runner
end

#line_numberObject

Returns the value of attribute line_number.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def line_number
  @line_number
end

#loadbyObject

Returns the value of attribute loadby.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def loadby
  @loadby
end

#output_streamObject

Returns the value of attribute output_stream.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def output_stream
  @output_stream
end

#profileObject

Returns the value of attribute profile.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def profile
  @profile
end

#reporterObject

Returns the value of attribute reporter.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def reporter
  @reporter
end

#reverseObject

Returns the value of attribute reverse.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def reverse
  @reverse
end

#timeoutObject

Returns the value of attribute timeout.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def timeout
  @timeout
end

#user_input_for_runnerObject

Returns the value of attribute user_input_for_runner.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def user_input_for_runner
  @user_input_for_runner
end

#verboseObject

Returns the value of attribute verbose.



30
31
32
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 30

def verbose
  @verbose
end

Instance Method Details

#add_dir_from_project_root_to_load_path(dir, load_path = $LOAD_PATH) ⇒ Object

:nodoc:



115
116
117
118
119
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 115

def add_dir_from_project_root_to_load_path(dir, load_path=$LOAD_PATH) # :nodoc:
  return if project_root.nil?
  full_dir = File.join(project_root, dir)
  load_path.unshift full_dir unless load_path.include?(full_dir)
end

#add_example_group(example_group) ⇒ Object



78
79
80
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 78

def add_example_group(example_group)
  @example_groups << example_group
end

#after_suite_partsObject



167
168
169
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 167

def after_suite_parts
  Spec::Example::BeforeAndAfterHooks.after_suite_parts
end

#before_suite_partsObject



163
164
165
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 163

def before_suite_parts
  Spec::Example::BeforeAndAfterHooks.before_suite_parts
end

#determine_project_rootObject

:nodoc:



104
105
106
107
108
109
110
111
112
113
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 104

def determine_project_root # :nodoc:
  # This is borrowed (slightly modified) from Scott Taylors
  # project_path project:
  #   http://github.com/smtlaissezfaire/project_path
  Pathname(File.expand_path('.')).ascend do |path|
    if File.exists?(File.join(path, "spec"))
      return path
    end
  end
end

#dry_run?Boolean

Returns:

  • (Boolean)


288
289
290
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 288

def dry_run?
  @dry_run == true
end

#example_lineObject



86
87
88
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 86

def example_line
  Spec::Runner::LineNumberQuery.new(self).example_line_for(files.first, line_number)
end

#examples_run?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 171

def examples_run?
  @examples_run
end

#examples_should_not_be_runObject



175
176
177
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 175

def examples_should_not_be_run
  @examples_should_be_run = false
end

#files_to_loadObject



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 272

def files_to_load
  result = []
  sorted_files.each do |file|
    if File.directory?(file)
      filename_pattern.split(",").each do |pattern|
        result += Dir[File.expand_path("#{file}/#{pattern.strip}")]
      end
    elsif File.file?(file)
      result << file
    else
      raise "File or directory not found: #{file}"
    end
  end
  result
end

#formatter_optionsObject



249
250
251
252
253
254
255
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 249

def formatter_options
  @formatter_options ||= OpenStruct.new(
    :colour   => colour,
    :autospec => autospec,
    :dry_run  => dry_run
  )
end

#formattersObject



232
233
234
235
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 232

def formatters
  @format_options ||= [['progress', @output_stream]]
  @formatters ||= load_formatters(@format_options, EXAMPLE_FORMATTERS)
end

#line_number_requested?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 82

def line_number_requested?
  !!line_number
end

#load_formatters(format_options, formatters) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 237

def load_formatters(format_options, formatters)
  format_options.map do |format, where|
    formatter_type = if formatters[format]
      require formatters[format][0]
      eval(formatters[format][1], binding, __FILE__, __LINE__)
    else
      load_class(format, 'formatter', '--format')
    end
    formatter_type.new(formatter_options, where)
  end
end

#load_heckle_runner(heckle) ⇒ Object



261
262
263
264
265
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 261

def load_heckle_runner(heckle)
  @format_options ||= [['silent', @output_stream]]
  require which_heckle_runner
  @heckle_runner = ::Spec::Runner::HeckleRunner.new(heckle)
end

#mock_frameworkObject



179
180
181
182
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 179

def mock_framework
  # TODO - don't like this dependency - perhaps store this in here instead?
  Spec::Runner.configuration.mock_framework
end

#number_of_examplesObject



267
268
269
270
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 267

def number_of_examples
  return examples.size unless examples.empty?
  @example_groups.inject(0) {|sum, group| sum + group.number_of_examples}
end

#parse_diff(format) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 199

def parse_diff(format)
  case format
  when :context, 'context', 'c'
    @diff_format  = :context
    default_differ
  when :unified, 'unified', 'u', '', nil
    @diff_format  = :unified
    default_differ
  else
    @diff_format  = :custom
    self.differ_class = load_class(format, 'differ', '--diff')
  end
end

#parse_example(example) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 213

def parse_example(example)
  if(File.file?(example))
    @examples = [File.open(example).read.split("\n")].flatten
  else
    @examples = [example]
  end
end

#parse_format(format_arg) ⇒ Object



221
222
223
224
225
226
227
228
229
230
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 221

def parse_format(format_arg)
  format, where = ClassAndArgumentsParser.parse(format_arg)
  unless where
    raise "When using several --format options only one of them can be without a file" if @out_used
    where = @output_stream
    @out_used = true
  end
  @format_options ||= []
  @format_options << [format, where]
end

#project_rootObject

:nodoc:



99
100
101
102
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 99

def project_root # :nodoc:
  require 'pathname'
  @project_root ||= determine_project_root
end

#remove_example_group(example_group) ⇒ Object



90
91
92
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 90

def remove_example_group(example_group)
  @example_groups.delete(example_group)
end

#require_ruby_debugObject



94
95
96
97
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 94

def require_ruby_debug
  require 'rubygems' unless ENV['NO_RUBYGEMS']
  require 'ruby-debug'
end

#run_examplesObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 121

def run_examples
  require_ruby_debug if debug
  return true unless examples_should_be_run?
  success = true
  begin
    runner = custom_runner || ExampleGroupRunner.new(self)

    unless @files_loaded
      ['spec','lib'].each do |dir|
        add_dir_from_project_root_to_load_path(dir)
      end
      runner.load_files(files_to_load)
      @files_loaded = true
    end

    define_predicate_matchers
    plugin_mock_framework
    ignore_backtrace_patterns

    # TODO - this has to happen after the files get loaded,
    # otherwise the before_suite_parts are not populated
    # from the configuration. There is no spec for this
    # directly, but features/before_and_after_blocks/before_and_after_blocks.story
    # will fail if this happens before the files are loaded.
    before_suite_parts.each { |part| part.call }

    if example_groups.empty?
      true
    else
      set_spec_from_line_number if line_number
      success = runner.run
      @examples_run = true
      heckle if heckle_runner
      success
    end
  ensure
    after_suite_parts.each do |part|
      part.arity < 1 ? part.call : part.call(success)
    end
  end
end

#which_heckle_runnerObject



257
258
259
# File 'lib/vendor/plugins/rspec/lib/spec/runner/options.rb', line 257

def which_heckle_runner
  ([/mswin/, /java/].detect{|p| p =~ RUBY_PLATFORM} || Spec::Ruby.version.to_f == 1.9) ? "spec/runner/heckle_runner_unsupported" : "spec/runner/heckle_runner"
end