Class Spec::Rake::SpecTask
In: lib/spec/rake/spectask.rb
Parent: ::Rake::TaskLib

A Rake task that runs a set of RSpec contexts.

Example:

  Spec::Rake::SpecTask.new do |t|
    t.warning = true
    t.rcov = true
  end

This will create a task that can be run with:

  rake spec

Methods

define   new   spec_files=  

Attributes

drb  [RW]  Whether or not to run specs via DRb. Setting this to true may run specs faster, especially in a Rails environment. Defaults to false
fail_on_error  [RW]  Whether or not to fail Rake when an error occurs (typically when specs fail). Defaults to true.
failure_message  [RW]  A message to print to stdout when there are failures.
libs  [RW]  Array of directories to be added to $LOAD_PATH before running the specs. Defaults to [’<the absolute path to RSpec‘s lib directory>’]
name  [RW]  Name of spec task. (default is :spec)
out  [RW]  Where RSpec‘s output is written. Defaults to STDOUT.
pattern  [RW]  Glob pattern to match spec files. (default is ‘spec/spec*.rb’)
rcov  [RW]  Whether or not to use RCov (default is false) See eigenclass.org/hiki.rb?rcov
rcov_dir  [RW]  Directory where the RCov report is written. Defaults to "coverage" Ignored if rcov=false
rcov_opts  [RW]  Array of commandline options to pass to RCov. Defaults to [’—exclude’, ‘lib\/spec,bin\/spec’]. Ignored if rcov=false
ruby_opts  [RW]  Array of commandline options to pass to ruby. Defaults to [].
spec_opts  [RW]  Array of commandline options to pass to RSpec. Defaults to [].
warning  [RW]  If true, requests that the specs be run with the warning flag set. E.g. warning=true implies "ruby -w" used to run the specs. Defaults to false.

Public Class methods

Create a specing task.

[Source]

    # File lib/spec/rake/spectask.rb, line 82
82:       def initialize(name=:spec)
83:         @name = name
84:         @libs = [File.expand_path(File.dirname(__FILE__) + '/../../../lib')]
85:         @pattern = nil
86:         @spec_files = nil
87:         @spec_opts = []
88:         @warning = false
89:         @ruby_opts = []
90:         @out = nil
91:         @fail_on_error = true
92:         @rcov = false
93:         @rcov_opts = ['--exclude', 'lib\/spec,bin\/spec']
94:         @rcov_dir = "coverage"
95: 
96:         yield self if block_given?
97:         @pattern = 'spec/**/*_spec.rb' if @pattern.nil? && @spec_files.nil?
98:         define
99:       end

Public Instance methods

[Source]

     # File lib/spec/rake/spectask.rb, line 101
101:       def define
102:         spec_script = File.expand_path(File.dirname(__FILE__) + '/../../../bin/' + (drb ? 'drbspec' : 'spec'))
103: 
104:         lib_path = @libs.join(File::PATH_SEPARATOR)
105:         actual_name = Hash === name ? name.keys.first : name
106:         unless ::Rake.application.last_comment
107:           desc "Run RSpec for #{actual_name}" + (@rcov ? " using RCov" : "")
108:         end
109:         task @name do
110:           RakeFileUtils.verbose(@verbose) do
111:             ruby_opts = @ruby_opts.clone
112:             ruby_opts.push( "-I\"#{lib_path}\"" )
113:             ruby_opts.push( "-S rcov" ) if @rcov
114:             ruby_opts.push( "-w" ) if @warning
115: 
116:             redirect = @out.nil? ? "" : " > #{@out}"
117: 
118:             unless file_list.empty?
119:               # ruby [ruby_opts] -Ilib -S rcov [rcov_opts] bin/spec -- [spec_opts] examples
120:               # or
121:               # ruby [ruby_opts] -Ilib bin/spec [spec_opts] examples
122:               begin
123:                 ruby(
124:                   ruby_opts.join(" ") + " " + 
125:                   rcov_option_list +
126:                   (@rcov ? %[ -o "#{@rcov_dir}" ] : "") + 
127:                   '"' + spec_script + '"' + " " +
128:                   (@rcov ? "-- " : "") + 
129:                   file_list.collect { |fn| %["#{fn}"] }.join(' ') + " " + 
130:                   spec_option_list + " " +
131:                   redirect
132:                 )
133:               rescue => e
134:                  puts @failure_message if @failure_message
135:                  raise e if @fail_on_error
136:               end
137:             end
138:           end
139:         end
140: 
141:         if @rcov
142:           desc "Remove rcov products for #{actual_name}"
143:           task paste("clobber_", actual_name) do
144:             rm_r @rcov_dir rescue nil
145:           end
146: 
147:           clobber_task = paste("clobber_", actual_name)
148:           task :clobber => [clobber_task]
149: 
150:           task actual_name => clobber_task
151:         end
152:         self
153:       end

Explicitly define the list of spec files to be included in a spec. list is expected to be an array of file names (a FileList is acceptable). If both pattern and spec_files are used, then the list of spec files is the union of the two.

[Source]

    # File lib/spec/rake/spectask.rb, line 77
77:       def spec_files=(list)
78:         @spec_files = list
79:       end

[Validate]