Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / rspec / lib / spec / rake / spectask.rb
blob74b682cdb20317e2db1a04272fec38528cf9c18b
1 #!/usr/bin/env ruby
3 # Define a task library for running RSpec contexts.
5 require 'rake'
6 require 'rake/tasklib'
8 module Spec
9   module Rake
11     # A Rake task that runs a set of specs.
12     #
13     # Example:
14     #  
15     #   Spec::Rake::SpecTask.new do |t|
16     #     t.warning = true
17     #     t.rcov = true
18     #   end
19     #
20     # This will create a task that can be run with:
21     #
22     #   rake spec
23     #
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.
28     #
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+
31     # attribute.
32     #
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+
35     # attribute.
36     #
37     # Examples:
38     #
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
43     #
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
48       class << self
49         def attr_accessor(*names)
50           super(*names)
51           names.each do |name|
52             module_eval "def #{name}() evaluate(@#{name}) end" # Allows use of procs
53           end
54         end
55       end
57       # Name of spec task. (default is :spec)
58       attr_accessor :name
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>']
62       attr_accessor :libs
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
78       attr_accessor :rcov
79       
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).
93       # Defaults to true.
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.
101       attr_accessor :out
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
109       
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)
116         @name = name
117         @libs = [File.expand_path(File.dirname(__FILE__) + '/../../../lib')]
118         @pattern = nil
119         @spec_files = nil
120         @spec_opts = []
121         @warning = false
122         @ruby_opts = []
123         @fail_on_error = true
124         @rcov = false
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?
130         define
131       end
133       def define # :nodoc:
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" : "")
140         end
141         task name do
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]
145               # or
146               # ruby [ruby_opts] -Ilib bin/spec examples [spec_opts]
147               cmd = "ruby "
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(" ")
154               cmd << " "
155               cmd << rcov_option_list
156               cmd << %[ -o "#{rcov_dir}" ] if rcov
157               cmd << %Q|"#{spec_script}"|
158               cmd << " "
159               cmd << "-- " if rcov
160               cmd << spec_file_list.collect { |fn| %["#{fn}"] }.join(' ')
161               cmd << " "
162               cmd << spec_option_list
163               if out
164                 cmd << " "
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."
167               end
168               if verbose
169                 puts cmd
170               end
171               unless system(cmd)
172                 STDERR.puts failure_message if failure_message
173                 raise("Command #{cmd} failed") if fail_on_error
174               end
175             end
176           end
177         end
179         if rcov
180           desc "Remove rcov products for #{actual_name}"
181           task paste("clobber_", actual_name) do
182             rm_r rcov_dir rescue nil
183           end
185           clobber_task = paste("clobber_", actual_name)
186           task :clobber => [clobber_task]
188           task actual_name => clobber_task
189         end
190         self
191       end
193       def rcov_option_list # :nodoc:
194         return "" unless rcov
195         ENV['RCOV_OPTS'] || rcov_opts.join(" ") || ""
196       end
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(" ") || ""
201       end
202       
203       def evaluate(o) # :nodoc:
204         case o
205           when Proc then o.call
206           else o
207         end
208       end
210       def spec_file_list # :nodoc:
211         if ENV['SPEC']
212           FileList[ ENV['SPEC'] ]
213         else
214           result = []
215           result += spec_files.to_a if spec_files
216           result += FileList[ pattern ].to_a if pattern
217           FileList[result]
218         end
219       end
221     end
222   end