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
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. DEPRECATED. Use —format FORMAT:WHERE in spec_opts. |
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. |
Create a specing task.
# File lib/spec/rake/spectask.rb, line 78 78: def initialize(name=:spec) 79: @name = name 80: @libs = [File.expand_path(File.dirname(__FILE__) + '/../../../lib')] 81: @pattern = nil 82: @spec_files = nil 83: @spec_opts = [] 84: @warning = false 85: @ruby_opts = [] 86: @out = nil 87: @fail_on_error = true 88: @rcov = false 89: @rcov_opts = ['--exclude', 'lib\/spec,bin\/spec,config\/boot.rb'] 90: @rcov_dir = "coverage" 91: 92: yield self if block_given? 93: @pattern = 'spec/**/*_spec.rb' if @pattern.nil? && @spec_files.nil? 94: define 95: end
# File lib/spec/rake/spectask.rb, line 97 97: def define 98: spec_script = File.expand_path(File.dirname(__FILE__) + '/../../../bin/spec') 99: 100: lib_path = @libs.join(File::PATH_SEPARATOR) 101: actual_name = Hash === name ? name.keys.first : name 102: unless ::Rake.application.last_comment 103: desc "Run RSpec for #{actual_name}" + (@rcov ? " using RCov" : "") 104: end 105: task @name do 106: RakeFileUtils.verbose(@verbose) do 107: unless spec_file_list.empty? 108: # ruby [ruby_opts] -Ilib -S rcov [rcov_opts] bin/spec -- [spec_opts] examples 109: # or 110: # ruby [ruby_opts] -Ilib bin/spec [spec_opts] examples 111: cmd = "ruby " 112: 113: ruby_opts = @ruby_opts.clone 114: ruby_opts << "-I\"#{lib_path}\"" 115: ruby_opts << "-S rcov" if @rcov 116: ruby_opts << "-w" if @warning 117: cmd << ruby_opts.join(" ") 118: cmd << " " 119: cmd << rcov_option_list 120: cmd << %[ -o "#{@rcov_dir}" ] if @rcov 121: cmd << %Q|"#{spec_script}"| 122: cmd << " " 123: cmd << "-- " if @rcov 124: cmd << spec_file_list.collect { |fn| %["#{fn}"] }.join(' ') 125: cmd << " " 126: cmd << spec_option_list 127: cmd << " " 128: cmd << %Q| > "#{@out}"| if @out 129: 130: unless system(cmd) 131: puts @failure_message if @failure_message 132: raise("Command #{cmd} failed") if @fail_on_error 133: end 134: end 135: end 136: end 137: 138: if @rcov 139: desc "Remove rcov products for #{actual_name}" 140: task paste("clobber_", actual_name) do 141: rm_r @rcov_dir rescue nil 142: end 143: 144: clobber_task = paste("clobber_", actual_name) 145: task :clobber => [clobber_task] 146: 147: task actual_name => clobber_task 148: end 149: self 150: 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.
# File lib/spec/rake/spectask.rb, line 73 73: def spec_files=(list) 74: @spec_files = list 75: end