Class: Spec::Rake::SpecTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb

Overview

A Rake task that runs a set of specs.

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

If rake is invoked with a “SPEC=filename” command line option, then the list of spec files will be overridden to include only the filename specified on the command line. This provides an easy way to run just one spec.

If rake is invoked with a “SPEC_OPTS=options” command line option, then the given options will override the value of the spec_opts attribute.

If rake is invoked with a “RCOV_OPTS=options” command line option, then the given options will override the value of the rcov_opts attribute.

Examples:

rake spec                                      # run specs normally
rake spec SPEC=just_one_file.rb                # run just one spec file.
rake spec SPEC_OPTS="--diff"                   # enable diffing
rake spec RCOV_OPTS="--aggregate myfile.txt"   # see rcov --help for details

Each attribute of this task may be a proc. This allows for lazy evaluation, which is sometimes handy if you want to defer the evaluation of an attribute value until the task is run (as opposed to when it is defined).

This task can also be used to run existing Test::Unit tests and get RSpec output, for example like this:

require 'rubygems'
require 'spec/rake/spectask'
Spec::Rake::SpecTask.new do |t|
  t.ruby_opts = ['-rtest/unit']
  t.spec_files = FileList['test/**/*_test.rb']
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :spec) {|_self| ... } ⇒ SpecTask

Defines a new task, using the name name.

Yields:

  • (_self)

Yield Parameters:



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 126

def initialize(name=:spec)
  @name = name
  @libs = [File.expand_path(File.dirname(__FILE__) + '/../../../lib')]
  @pattern = nil
  @spec_files = nil
  @spec_opts = []
  @warning = false
  @ruby_opts = []
  @fail_on_error = true
  @rcov = false
  @rcov_opts = ['--exclude', 'lib\/spec,bin\/spec,config\/boot.rb']
  @rcov_dir = "coverage"

  yield self if block_given?
  @pattern = 'spec/**/*_spec.rb' if pattern.nil? && spec_files.nil?
  define
end

Instance Attribute Details

#fail_on_errorObject

Whether or not to fail Rake when an error occurs (typically when specs fail). Defaults to true.



105
106
107
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 105

def fail_on_error
  @fail_on_error
end

#failure_messageObject

A message to print to stderr when there are failures.



108
109
110
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 108

def failure_message
  @failure_message
end

#libsObject

Array of directories to be added to $LOAD_PATH before running the specs. Defaults to [‘<the absolute path to RSpec’s lib directory>‘]



73
74
75
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 73

def libs
  @libs
end

#nameObject

Name of spec task. (default is :spec)



69
70
71
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 69

def name
  @name
end

#outObject

Where RSpec’s output is written. Defaults to $stdout. DEPRECATED. Use –format FORMAT:WHERE in spec_opts.



112
113
114
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 112

def out
  @out
end

#patternObject

Glob pattern to match spec files. (default is ‘spec/*/_spec.rb’) Setting the SPEC environment variable overrides this.



81
82
83
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 81

def pattern
  @pattern
end

#rcovObject

Whether or not to use RCov (default is false) See eigenclass.org/hiki.rb?rcov



89
90
91
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 89

def rcov
  @rcov
end

#rcov_dirObject

Directory where the RCov report is written. Defaults to “coverage” Ignored if rcov=false



98
99
100
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 98

def rcov_dir
  @rcov_dir
end

#rcov_optsObject

Array of commandline options to pass to RCov. Defaults to [‘–exclude’, ‘lib/spec,bin/spec’]. Ignored if rcov=false Setting the RCOV_OPTS environment variable overrides this.



94
95
96
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 94

def rcov_opts
  @rcov_opts
end

#ruby_optsObject

Array of commandline options to pass to ruby. Defaults to [].



101
102
103
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 101

def ruby_opts
  @ruby_opts
end

#spec_filesObject

Explicitly define the list of spec files to be included in a spec. spec_files 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. Setting the SPEC environment variable overrides this.



119
120
121
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 119

def spec_files
  @spec_files
end

#spec_optsObject

Array of commandline options to pass to RSpec. Defaults to []. Setting the SPEC_OPTS environment variable overrides this.



85
86
87
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 85

def spec_opts
  @spec_opts
end

#verboseObject

Use verbose output. If this is set to true, the task will print the executed spec command to stdout. Defaults to false.



123
124
125
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 123

def verbose
  @verbose
end

#warningObject

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.



77
78
79
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 77

def warning
  @warning
end

Class Method Details

.attr_accessor(*names) ⇒ Object



60
61
62
63
64
65
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 60

def attr_accessor(*names)
  super(*names)
  names.each do |name|
    module_eval "def #{name}() evaluate(@#{name}) end" # Allows use of procs
  end
end

Instance Method Details

#defineObject

:nodoc:



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 144

def define # :nodoc:
  spec_script = File.expand_path(File.dirname(__FILE__) + '/../../../bin/spec')

  lib_path = libs.join(File::PATH_SEPARATOR)
  actual_name = Hash === name ? name.keys.first : name
  unless ::Rake.application.last_comment
    desc "Run specs" + (rcov ? " using RCov" : "")
  end
  task name do
    RakeFileUtils.verbose(verbose) do
      unless spec_file_list.empty?
        # ruby [ruby_opts] -Ilib -S rcov [rcov_opts] bin/spec -- examples [spec_opts]
        # or
        # ruby [ruby_opts] -Ilib bin/spec examples [spec_opts]
        cmd_parts = [RUBY]
        cmd_parts += ruby_opts
        cmd_parts << %[-I"#{lib_path}"]
        cmd_parts << "-S rcov" if rcov
        cmd_parts << "-w" if warning
        cmd_parts << rcov_option_list
        cmd_parts << %[-o "#{rcov_dir}"] if rcov
        cmd_parts << %["#{spec_script}"]
        cmd_parts << "--" if rcov
        cmd_parts += spec_file_list.collect { |fn| %["#{fn}"] }
        cmd_parts << spec_option_list
        if out
          cmd_parts << %[> "#{out}"]
          STDERR.puts "The Spec::Rake::SpecTask#out attribute is DEPRECATED and will be removed in a future version. Use --format FORMAT:WHERE instead."
        end
        cmd = cmd_parts.join(" ")
        puts cmd if verbose
        unless system(cmd)
          STDERR.puts failure_message if failure_message
          raise("Command #{cmd} failed") if fail_on_error
        end
      end
    end
  end

  if rcov
    desc "Remove rcov products for #{actual_name}"
    task paste("clobber_", actual_name) do
      rm_r rcov_dir rescue nil
    end

    clobber_task = paste("clobber_", actual_name)
    task :clobber => [clobber_task]

    task actual_name => clobber_task
  end
  self
end

#evaluate(o) ⇒ Object

:nodoc:



207
208
209
210
211
212
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 207

def evaluate(o) # :nodoc:
  case o
    when Proc then o.call
    else o
  end
end

#rcov_option_listObject

:nodoc:



197
198
199
200
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 197

def rcov_option_list # :nodoc:
  return "" unless rcov
  ENV['RCOV_OPTS'] || rcov_opts.join(" ") || ""
end

#spec_file_listObject

:nodoc:



214
215
216
217
218
219
220
221
222
223
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 214

def spec_file_list # :nodoc:
  if ENV['SPEC']
    FileList[ ENV['SPEC'] ]
  else
    result = []
    result += spec_files.to_a if spec_files
    result += FileList[ pattern ].to_a if pattern
    FileList[result]
  end
end

#spec_option_listObject

:nodoc:



202
203
204
205
# File 'lib/gems/rspec-1.1.11/lib/spec/rake/spectask.rb', line 202

def spec_option_list # :nodoc:
  STDERR.puts "RSPECOPTS is DEPRECATED and will be removed in a future version. Use SPEC_OPTS instead." if ENV['RSPECOPTS']
  ENV['SPEC_OPTS'] || ENV['RSPECOPTS'] || spec_opts.join(" ") || ""
end