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).
47 class SpecTask < ::Rake::TaskLib
49 def attr_accessor(*names)
52 module_eval "def #{name}() evaluate(@#{name}) end" # Allows use of procs
57 # Name of spec task. (default is :spec)
60 # Array of directories to be added to $LOAD_PATH before running the
61 # specs. Defaults to ['<the absolute path to RSpec's lib directory>']
64 # If true, requests that the specs be run with the warning flag set.
65 # E.g. warning=true implies "ruby -w" used to run the specs. Defaults to false.
66 attr_accessor :warning
68 # Glob pattern to match spec files. (default is 'spec/**/*_spec.rb')
69 # Setting the SPEC environment variable overrides this.
70 attr_accessor :pattern
72 # Array of commandline options to pass to RSpec. Defaults to [].
73 # Setting the SPEC_OPTS environment variable overrides this.
74 attr_accessor :spec_opts
76 # Whether or not to use RCov (default is false)
77 # See http://eigenclass.org/hiki.rb?rcov
80 # Array of commandline options to pass to RCov. Defaults to ['--exclude', 'lib\/spec,bin\/spec'].
81 # Ignored if rcov=false
82 # Setting the RCOV_OPTS environment variable overrides this.
83 attr_accessor :rcov_opts
85 # Directory where the RCov report is written. Defaults to "coverage"
86 # Ignored if rcov=false
87 attr_accessor :rcov_dir
89 # Array of commandline options to pass to ruby. Defaults to [].
90 attr_accessor :ruby_opts
92 # Whether or not to fail Rake when an error occurs (typically when specs fail).
94 attr_accessor :fail_on_error
96 # A message to print to stderr when there are failures.
97 attr_accessor :failure_message
99 # Where RSpec's output is written. Defaults to STDOUT.
100 # DEPRECATED. Use --format FORMAT:WHERE in spec_opts.
103 # Explicitly define the list of spec files to be included in a
104 # spec. +spec_files+ is expected to be an array of file names (a
105 # FileList is acceptable). If both +pattern+ and +spec_files+ are
106 # used, then the list of spec files is the union of the two.
107 # Setting the SPEC environment variable overrides this.
108 attr_accessor :spec_files
110 # Use verbose output. If this is set to true, the task will print
111 # the executed spec command to stdout. Defaults to false.
112 attr_accessor :verbose
114 # Defines a new task, using the name +name+.
115 def initialize(name=:spec)
117 @libs = [File.expand_path(File.dirname(__FILE__) + '/../../../lib')]
123 @fail_on_error = true
125 @rcov_opts = ['--exclude', 'lib\/spec,bin\/spec,config\/boot.rb']
126 @rcov_dir = "coverage"
128 yield self if block_given?
129 @pattern = 'spec/**/*_spec.rb' if pattern.nil? && spec_files.nil?
134 spec_script = File.expand_path(File.dirname(__FILE__) + '/../../../bin/spec')
136 lib_path = libs.join(File::PATH_SEPARATOR)
137 actual_name = Hash === name ? name.keys.first : name
138 unless ::Rake.application.last_comment
139 desc "Run specs" + (rcov ? " using RCov" : "")
142 RakeFileUtils.verbose(verbose) do
143 unless spec_file_list.empty?
144 # ruby [ruby_opts] -Ilib -S rcov [rcov_opts] bin/spec -- examples [spec_opts]
146 # ruby [ruby_opts] -Ilib bin/spec examples [spec_opts]
149 rb_opts = ruby_opts.clone
150 rb_opts << "-I\"#{lib_path}\""
151 rb_opts << "-S rcov" if rcov
152 rb_opts << "-w" if warning
153 cmd << rb_opts.join(" ")
155 cmd << rcov_option_list
156 cmd << %[ -o "#{rcov_dir}" ] if rcov
157 cmd << %Q|"#{spec_script}"|
160 cmd << spec_file_list.collect { |fn| %["#{fn}"] }.join(' ')
162 cmd << spec_option_list
165 cmd << %Q| > "#{out}"|
166 STDERR.puts "The Spec::Rake::SpecTask#out attribute is DEPRECATED and will be removed in a future version. Use --format FORMAT:WHERE instead."
172 STDERR.puts failure_message if failure_message
173 raise("Command #{cmd} failed") if fail_on_error
180 desc "Remove rcov products for #{actual_name}"
181 task paste("clobber_", actual_name) do
182 rm_r rcov_dir rescue nil
185 clobber_task = paste("clobber_", actual_name)
186 task :clobber => [clobber_task]
188 task actual_name => clobber_task
193 def rcov_option_list # :nodoc:
194 return "" unless rcov
195 ENV['RCOV_OPTS'] || rcov_opts.join(" ") || ""
198 def spec_option_list # :nodoc:
199 STDERR.puts "RSPECOPTS is DEPRECATED and will be removed in a future version. Use SPEC_OPTS instead." if ENV['RSPECOPTS']
200 ENV['SPEC_OPTS'] || ENV['RSPECOPTS'] || spec_opts.join(" ") || ""
203 def evaluate(o) # :nodoc:
205 when Proc then o.call
210 def spec_file_list # :nodoc:
212 FileList[ ENV['SPEC'] ]
215 result += spec_files.to_a if spec_files
216 result += FileList[ pattern ].to_a if pattern