]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/lib/spec/rake/spectask.rb
c59e226f56bb0983504d75c3ec56be5247353968
[rails.git] / vendor / gems / rspec-1.1.2 / lib / spec / rake / spectask.rb
1 #!/usr/bin/env ruby
2
3 # Define a task library for running RSpec contexts.
4
5 require 'rake'
6 require 'rake/tasklib'
7
8 module Spec
9   module Rake
10
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     #
48     # This task can also be used to run existing Test::Unit tests and get RSpec
49     # output, for example like this:
50     #
51     #   require 'rubygems'
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']
56     #   end
57     #
58     class SpecTask < ::Rake::TaskLib
59       class << self
60         def attr_accessor(*names)
61           super(*names)
62           names.each do |name|
63             module_eval "def #{name}() evaluate(@#{name}) end" # Allows use of procs
64           end
65         end
66       end
67
68       # Name of spec task. (default is :spec)
69       attr_accessor :name
70
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>']
73       attr_accessor :libs
74
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
78
79       # Glob pattern to match spec files. (default is 'spec/**/*_spec.rb')
80       # Setting the SPEC environment variable overrides this.
81       attr_accessor :pattern
82
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
86
87       # Whether or not to use RCov (default is false)
88       # See http://eigenclass.org/hiki.rb?rcov
89       attr_accessor :rcov
90       
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
95
96       # Directory where the RCov report is written. Defaults to "coverage"
97       # Ignored if rcov=false
98       attr_accessor :rcov_dir
99
100       # Array of commandline options to pass to ruby. Defaults to [].
101       attr_accessor :ruby_opts
102
103       # Whether or not to fail Rake when an error occurs (typically when specs fail).
104       # Defaults to true.
105       attr_accessor :fail_on_error
106
107       # A message to print to stderr when there are failures.
108       attr_accessor :failure_message
109
110       # Where RSpec's output is written. Defaults to STDOUT.
111       # DEPRECATED. Use --format FORMAT:WHERE in spec_opts.
112       attr_accessor :out
113
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
120       
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
124
125       # Defines a new task, using the name +name+.
126       def initialize(name=:spec)
127         @name = name
128         @libs = [File.expand_path(File.dirname(__FILE__) + '/../../../lib')]
129         @pattern = nil
130         @spec_files = nil
131         @spec_opts = []
132         @warning = false
133         @ruby_opts = []
134         @fail_on_error = true
135         @rcov = false
136         @rcov_opts = ['--exclude', 'lib\/spec,bin\/spec,config\/boot.rb']
137         @rcov_dir = "coverage"
138
139         yield self if block_given?
140         @pattern = 'spec/**/*_spec.rb' if pattern.nil? && spec_files.nil?
141         define
142       end
143
144       def define # :nodoc:
145         spec_script = File.expand_path(File.dirname(__FILE__) + '/../../../bin/spec')
146
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" : "")
151         end
152         task name do
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]
156               # or
157               # ruby [ruby_opts] -Ilib bin/spec examples [spec_opts]
158               cmd = "ruby "
159
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(" ")
165               cmd << " "
166               cmd << rcov_option_list
167               cmd << %[ -o "#{rcov_dir}" ] if rcov
168               cmd << %Q|"#{spec_script}"|
169               cmd << " "
170               cmd << "-- " if rcov
171               cmd << spec_file_list.collect { |fn| %["#{fn}"] }.join(' ')
172               cmd << " "
173               cmd << spec_option_list
174               if out
175                 cmd << " "
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."
178               end
179               if verbose
180                 puts cmd
181               end
182               unless system(cmd)
183                 STDERR.puts failure_message if failure_message
184                 raise("Command #{cmd} failed") if fail_on_error
185               end
186             end
187           end
188         end
189
190         if rcov
191           desc "Remove rcov products for #{actual_name}"
192           task paste("clobber_", actual_name) do
193             rm_r rcov_dir rescue nil
194           end
195
196           clobber_task = paste("clobber_", actual_name)
197           task :clobber => [clobber_task]
198
199           task actual_name => clobber_task
200         end
201         self
202       end
203
204       def rcov_option_list # :nodoc:
205         return "" unless rcov
206         ENV['RCOV_OPTS'] || rcov_opts.join(" ") || ""
207       end
208
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(" ") || ""
212       end
213       
214       def evaluate(o) # :nodoc:
215         case o
216           when Proc then o.call
217           else o
218         end
219       end
220
221       def spec_file_list # :nodoc:
222         if ENV['SPEC']
223           FileList[ ENV['SPEC'] ]
224         else
225           result = []
226           result += spec_files.to_a if spec_files
227           result += FileList[ pattern ].to_a if pattern
228           FileList[result]
229         end
230       end
231
232     end
233   end
234 end
235