3 # Define a task library for running RSpec contexts.
11 # A Rake task that runs a set of specs.
15 # Spec::Rake::SpecTask.new do |t|
20 # This will create a task that can be run with:
24 # If rake is invoked with a "SPEC=filename" command line option,
25 # then the list of spec files will be overridden to include only the
26 # filename specified on the command line. This provides an easy way
27 # to run just one spec.
29 # If rake is invoked with a "SPEC_OPTS=options" command line option,
30 # then the given options will override the value of the +spec_opts+
33 # If rake is invoked with a "RCOV_OPTS=options" command line option,
34 # then the given options will override the value of the +rcov_opts+
39 # rake spec # run specs normally
40 # rake spec SPEC=just_one_file.rb # run just one spec file.
41 # rake spec SPEC_OPTS="--diff" # enable diffing
42 # rake spec RCOV_OPTS="--aggregate myfile.txt" # see rcov --help for details
44 # Each attribute of this task may be a proc. This allows for lazy evaluation,
45 # which is sometimes handy if you want to defer the evaluation of an attribute value
46 # until the task is run (as opposed to when it is defined).
48 # This task can also be used to run existing Test::Unit tests and get RSpec
49 # output, for example like this:
52 # require 'spec/rake/spectask'
53 # Spec::Rake::SpecTask.new do |t|
54 # t.ruby_opts = ['-rtest/unit']
55 # t.spec_files = FileList['test/**/*_test.rb']
58 class SpecTask < ::Rake::TaskLib
60 def attr_accessor(*names)
63 module_eval "def #{name}() evaluate(@#{name}) end" # Allows use of procs
68 # Name of spec task. (default is :spec)
71 # Array of directories to be added to $LOAD_PATH before running the
72 # specs. Defaults to ['<the absolute path to RSpec's lib directory>']
75 # If true, requests that the specs be run with the warning flag set.
76 # E.g. warning=true implies "ruby -w" used to run the specs. Defaults to false.
77 attr_accessor :warning
79 # Glob pattern to match spec files. (default is 'spec/**/*_spec.rb')
80 # Setting the SPEC environment variable overrides this.
81 attr_accessor :pattern
83 # Array of commandline options to pass to RSpec. Defaults to [].
84 # Setting the SPEC_OPTS environment variable overrides this.
85 attr_accessor :spec_opts
87 # Whether or not to use RCov (default is false)
88 # See http://eigenclass.org/hiki.rb?rcov
91 # Array of commandline options to pass to RCov. Defaults to ['--exclude', 'lib\/spec,bin\/spec'].
92 # Ignored if rcov=false
93 # Setting the RCOV_OPTS environment variable overrides this.
94 attr_accessor :rcov_opts
96 # Directory where the RCov report is written. Defaults to "coverage"
97 # Ignored if rcov=false
98 attr_accessor :rcov_dir
100 # Array of commandline options to pass to ruby. Defaults to [].
101 attr_accessor :ruby_opts
103 # Whether or not to fail Rake when an error occurs (typically when specs fail).
105 attr_accessor :fail_on_error
107 # A message to print to stderr when there are failures.
108 attr_accessor :failure_message
110 # Where RSpec's output is written. Defaults to STDOUT.
111 # DEPRECATED. Use --format FORMAT:WHERE in spec_opts.
114 # Explicitly define the list of spec files to be included in a
115 # spec. +spec_files+ is expected to be an array of file names (a
116 # FileList is acceptable). If both +pattern+ and +spec_files+ are
117 # used, then the list of spec files is the union of the two.
118 # Setting the SPEC environment variable overrides this.
119 attr_accessor :spec_files
121 # Use verbose output. If this is set to true, the task will print
122 # the executed spec command to stdout. Defaults to false.
123 attr_accessor :verbose
125 # Defines a new task, using the name +name+.
126 def initialize(name=:spec)
128 @libs = [File.expand_path(File.dirname(__FILE__) + '/../../../lib')]
134 @fail_on_error = true
136 @rcov_opts = ['--exclude', 'lib\/spec,bin\/spec,config\/boot.rb']
137 @rcov_dir = "coverage"
139 yield self if block_given?
140 @pattern = 'spec/**/*_spec.rb' if pattern.nil? && spec_files.nil?
145 spec_script = File.expand_path(File.dirname(__FILE__) + '/../../../bin/spec')
147 lib_path = libs.join(File::PATH_SEPARATOR)
148 actual_name = Hash === name ? name.keys.first : name
149 unless ::Rake.application.last_comment
150 desc "Run specs" + (rcov ? " using RCov" : "")
153 RakeFileUtils.verbose(verbose) do
154 unless spec_file_list.empty?
155 # ruby [ruby_opts] -Ilib -S rcov [rcov_opts] bin/spec -- examples [spec_opts]
157 # ruby [ruby_opts] -Ilib bin/spec examples [spec_opts]
160 rb_opts = ruby_opts.clone
161 rb_opts << "-I\"#{lib_path}\""
162 rb_opts << "-S rcov" if rcov
163 rb_opts << "-w" if warning
164 cmd << rb_opts.join(" ")
166 cmd << rcov_option_list
167 cmd << %[ -o "#{rcov_dir}" ] if rcov
168 cmd << %Q|"#{spec_script}"|
171 cmd << spec_file_list.collect { |fn| %["#{fn}"] }.join(' ')
173 cmd << spec_option_list
176 cmd << %Q| > "#{out}"|
177 STDERR.puts "The Spec::Rake::SpecTask#out attribute is DEPRECATED and will be removed in a future version. Use --format FORMAT:WHERE instead."
183 STDERR.puts failure_message if failure_message
184 raise("Command #{cmd} failed") if fail_on_error
191 desc "Remove rcov products for #{actual_name}"
192 task paste("clobber_", actual_name) do
193 rm_r rcov_dir rescue nil
196 clobber_task = paste("clobber_", actual_name)
197 task :clobber => [clobber_task]
199 task actual_name => clobber_task
204 def rcov_option_list # :nodoc:
205 return "" unless rcov
206 ENV['RCOV_OPTS'] || rcov_opts.join(" ") || ""
209 def spec_option_list # :nodoc:
210 STDERR.puts "RSPECOPTS is DEPRECATED and will be removed in a future version. Use SPEC_OPTS instead." if ENV['RSPECOPTS']
211 ENV['SPEC_OPTS'] || ENV['RSPECOPTS'] || spec_opts.join(" ") || ""
214 def evaluate(o) # :nodoc:
216 when Proc then o.call
221 def spec_file_list # :nodoc:
223 FileList[ ENV['SPEC'] ]
226 result += spec_files.to_a if spec_files
227 result += FileList[ pattern ].to_a if pattern