123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Core::RakeTask

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Rake::TaskLib
Instance Chain:
self, ShellEscape, Rake::DSL, Rake::DSL, Rake::TaskLib
Inherits: Rake::TaskLib
  • Object
Defined in: rspec-core/lib/rspec/core/rake_task.rb

Overview

::RSpec rake task

See Also:

  • Rakefile

Constant Summary

ShellEscape - Included

SHELLS_ALLOWING_UNQUOTED_IDS

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(*args, &task_block) ⇒ RakeTask

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 77

def initialize(*args, &task_block)
  @name          = args.shift || :spec
  @ruby_opts     = nil
  @rspec_opts    = nil
  @verbose       = true
  @fail_on_error = true
  @rspec_path    = DEFAULT_RSPEC_PATH
  @pattern       = DEFAULT_PATTERN

  define(args, &task_block)
end

Instance Attribute Details

#exclude_pattern (rw)

Files matching this pattern will be excluded. Defaults to ‘nil`.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 39

attr_accessor :exclude_pattern

#fail_on_error (rw)

Whether or not to fail Rake when an error occurs (typically when examples fail). Defaults to ‘true`.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 43

attr_accessor :fail_on_error

#failure_message (rw)

A message to print to stderr when there are failures.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 46

attr_accessor :failure_message

#name (rw)

Name of task. Defaults to ‘:spec`.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 31

attr_accessor :name

#pattern (rw)

Files matching this pattern will be loaded. Defaults to ‘’spec/**,//*/*_spec.rb’‘.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 35

attr_accessor :pattern

#rspec_opts (rw)

Command line options to pass to ::RSpec. Defaults to ‘nil`.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 75

attr_accessor :rspec_opts

#rspec_path (rw)

Path to ::RSpec. Defaults to the absolute path to the rspec binary from the loaded rspec-core gem.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 72

attr_accessor :rspec_path

#ruby_opts (rw)

Command line options to pass to ruby. Defaults to ‘nil`.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 68

attr_accessor :ruby_opts

#verbose (rw)

Use verbose output. If this is set to true, the task will print the executed spec command to stdout. Defaults to ‘true`.

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 65

attr_accessor :verbose

#with_clean_environment (rw)

Run RSpec with a clean (empty) environment is not supported

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 55

def with_clean_environment
  false
end

#with_clean_environment=(value) (rw)

Run RSpec with a clean (empty) environment is not supported

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 50

def with_clean_environment=(_value)
  raise ArgumentError, "Running in a clean environment is not supported on Ruby versions before 1.9.0"
end

Instance Method Details

#blank (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 173

def blank
  lambda { |s| s.nil? || s == "" }
end

#define(args, &task_block) (private)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 110

def define(args, &task_block)
  desc "Run RSpec code examples" unless ::Rake.application.last_description

  task name, *args do |_, task_args|
    RakeFileUtils.__send__(:verbose, verbose) do
      task_block.call(*[self, task_args].slice(0, task_block.arity)) if task_block
      run_task verbose
    end
  end
end

#file_exclusion_specification (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 157

def file_exclusion_specification
  " --exclude-pattern #{escape exclude_pattern}" if exclude_pattern
end

#file_inclusion_specification (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 121

def file_inclusion_specification
  if ENV['SPEC']
    FileList[ENV['SPEC']].sort
  elsif String === pattern && !File.exist?(pattern)
    return if [*rspec_opts].any? { |opt| opt =~ /--pattern/ }
    "--pattern #{escape pattern}"
  else
    # Before RSpec 3.1, we used `FileList` to get the list of matched
    # files, and then pass that along to the `rspec` command. Starting
    # with 3.1, we prefer to pass along the pattern as-is to the `rspec`
    # command, for 3 reasons:
    #
    #   * It's *much* less verbose to pass one `--pattern` option than a
    #     long list of files.
    #   * It ensures `task.pattern` and `--pattern` have the same
    #     behavior.
    #   * It fixes a bug, where
    #     `task.pattern = pattern_that_matches_no_files` would run *all*
    #     files because it would cause no pattern or file args to get
    #     passed to `rspec`, which causes all files to get run.
    #
    # However, `FileList` is *far* more flexible than the `--pattern`
    # option. Specifically, it supports individual files and directories,
    # as well as arrays of files, directories and globs, as well as other
    # `FileList` objects.
    #
    # For backwards compatibility, we have to fall back to using FileList
    # if the user has passed a `pattern` option that will not work with
    # `--pattern`.
    #
    # TODO: consider deprecating support for this and removing it in
    #   RSpec 4.
    FileList[pattern].sort.map { |file| escape file }
  end
end

#rspec_load_path (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 177

def rspec_load_path
  @rspec_load_path ||= begin
    core_and_support = $LOAD_PATH.grep(
      /#{File::SEPARATOR}rspec-(core|support)[^#{File::SEPARATOR}]*#{File::SEPARATOR}lib/
    ).uniq

    "-I#{core_and_support.map { |file| escape file }.join(File::PATH_SEPARATOR)}"
  end
end

#run_task(verbose)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 90

def run_task(verbose)
  command = spec_command
  puts command if verbose

  if with_clean_environment
    return if system({}, command, :unsetenv_others => true)
  else
    return if system(command)
  end

  puts failure_message if failure_message

  return unless fail_on_error
  $stderr.puts "#{command} failed" if verbose
  exit $?.exitstatus || 1
end

#spec_command (private)

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/rake_task.rb', line 161

def spec_command
  cmd_parts = []
  cmd_parts << RUBY
  cmd_parts << ruby_opts
  cmd_parts << rspec_load_path
  cmd_parts << escape(rspec_path)
  cmd_parts << file_inclusion_specification
  cmd_parts << file_exclusion_specification
  cmd_parts << rspec_opts
  cmd_parts.flatten.reject(&blank).join(" ")
end