summaryrefslogtreecommitdiffstats
path: root/test/lib/spec/rake
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2007-08-23 00:56:42 -0500
committerLuke Kanies <luke@madstop.com>2007-08-23 00:56:42 -0500
commit5601ecf75d3854a66d087a108e1b06885fa2be12 (patch)
tree28d5892bab14c9296bcd4232075f3658ee1224a0 /test/lib/spec/rake
parent7c4d39ec09c10871d7eb234fe4392381245ff443 (diff)
Upgrading rspec to version 1.0.8. This only includes the contents of the lib directory, and even then only the spec-related stuff, not the autotest stuff.
Diffstat (limited to 'test/lib/spec/rake')
-rw-r--r--test/lib/spec/rake/spectask.rb136
-rw-r--r--test/lib/spec/rake/verify_rcov.rb9
2 files changed, 97 insertions, 48 deletions
diff --git a/test/lib/spec/rake/spectask.rb b/test/lib/spec/rake/spectask.rb
index 5c9b365c1..f8c6809a9 100644
--- a/test/lib/spec/rake/spectask.rb
+++ b/test/lib/spec/rake/spectask.rb
@@ -21,7 +21,38 @@ module Spec
#
# 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).
class SpecTask < ::Rake::TaskLib
+ class << self
+ def attr_accessor(*names)
+ super(*names)
+ names.each do |name|
+ module_eval "def #{name}() evaluate(@#{name}) end" # Allows use of procs
+ end
+ end
+ end
# Name of spec task. (default is :spec)
attr_accessor :name
@@ -35,20 +66,20 @@ module Spec
attr_accessor :warning
# Glob pattern to match spec files. (default is 'spec/**/*_spec.rb')
+ # Setting the SPEC environment variable overrides this.
attr_accessor :pattern
# Array of commandline options to pass to RSpec. Defaults to [].
+ # Setting the SPEC_OPTS environment variable overrides this.
attr_accessor :spec_opts
- # Where RSpec's output is written. Defaults to STDOUT.
- attr_accessor :out
-
# Whether or not to use RCov (default is false)
# See http://eigenclass.org/hiki.rb?rcov
attr_accessor :rcov
# 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.
attr_accessor :rcov_opts
# Directory where the RCov report is written. Defaults to "coverage"
@@ -62,18 +93,21 @@ module Spec
# Defaults to true.
attr_accessor :fail_on_error
- # A message to print to stdout when there are failures.
+ # A message to print to stderr when there are failures.
attr_accessor :failure_message
+ # Where RSpec's output is written. Defaults to STDOUT.
+ # DEPRECATED. Use --format FORMAT:WHERE in spec_opts.
+ attr_accessor :out
+
# Explicitly define the list of spec files to be included in a
- # spec. +list+ is expected to be an array of file names (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.
- def spec_files=(list)
- @spec_files = list
- end
+ # Setting the SPEC environment variable overrides this.
+ attr_accessor :spec_files
- # Create a specing task.
+ # Defines a new task, using the name +name+.
def initialize(name=:spec)
@name = name
@libs = [File.expand_path(File.dirname(__FILE__) + '/../../../lib')]
@@ -82,61 +116,63 @@ module Spec
@spec_opts = []
@warning = false
@ruby_opts = []
- @out = nil
@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?
+ @pattern = 'spec/**/*_spec.rb' if pattern.nil? && spec_files.nil?
define
end
- def define
+ def define # :nodoc:
spec_script = File.expand_path(File.dirname(__FILE__) + '/../../../bin/spec')
- lib_path = @libs.join(File::PATH_SEPARATOR)
+ lib_path = libs.join(File::PATH_SEPARATOR)
actual_name = Hash === name ? name.keys.first : name
unless ::Rake.application.last_comment
- desc "Run RSpec for #{actual_name}" + (@rcov ? " using RCov" : "")
+ desc "Run specs" + (rcov ? " using RCov" : "")
end
- task @name do
- RakeFileUtils.verbose(@verbose) do
- ruby_opts = @ruby_opts.clone
- ruby_opts.push( "-I\"#{lib_path}\"" )
- ruby_opts.push( "-S rcov" ) if @rcov
- ruby_opts.push( "-w" ) if @warning
-
- redirect = @out.nil? ? "" : " > \"#{@out}\""
-
+ task name do
+ RakeFileUtils.verbose(verbose) do
unless spec_file_list.empty?
- # ruby [ruby_opts] -Ilib -S rcov [rcov_opts] bin/spec -- [spec_opts] examples
+ # ruby [ruby_opts] -Ilib -S rcov [rcov_opts] bin/spec -- examples [spec_opts]
# or
- # ruby [ruby_opts] -Ilib bin/spec [spec_opts] examples
- begin
- ruby(
- ruby_opts.join(" ") + " " +
- rcov_option_list +
- (@rcov ? %[ -o "#{@rcov_dir}" ] : "") +
- '"' + spec_script + '"' + " " +
- (@rcov ? "-- " : "") +
- spec_file_list.collect { |fn| %["#{fn}"] }.join(' ') + " " +
- spec_option_list + " " +
- redirect
- )
- rescue => e
- puts @failure_message if @failure_message
- raise e if @fail_on_error
+ # ruby [ruby_opts] -Ilib bin/spec examples [spec_opts]
+ cmd = "ruby "
+
+ rb_opts = ruby_opts.clone
+ rb_opts << "-I\"#{lib_path}\""
+ rb_opts << "-S rcov" if rcov
+ rb_opts << "-w" if warning
+ cmd << rb_opts.join(" ")
+ cmd << " "
+ cmd << rcov_option_list
+ cmd << %[ -o "#{rcov_dir}" ] if rcov
+ cmd << %Q|"#{spec_script}"|
+ cmd << " "
+ cmd << "-- " if rcov
+ cmd << spec_file_list.collect { |fn| %["#{fn}"] }.join(' ')
+ cmd << " "
+ cmd << spec_option_list
+ if out
+ cmd << " "
+ cmd << %Q| > "#{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
+ unless system(cmd)
+ STDERR.puts failure_message if failure_message
+ raise("Command #{cmd} failed") if fail_on_error
end
end
end
end
- if @rcov
+ if rcov
desc "Remove rcov products for #{actual_name}"
task paste("clobber_", actual_name) do
- rm_r @rcov_dir rescue nil
+ rm_r rcov_dir rescue nil
end
clobber_task = paste("clobber_", actual_name)
@@ -148,12 +184,20 @@ module Spec
end
def rcov_option_list # :nodoc:
- return "" unless @rcov
- ENV['RCOVOPTS'] || @rcov_opts.join(" ") || ""
+ return "" unless rcov
+ ENV['RCOV_OPTS'] || rcov_opts.join(" ") || ""
end
def spec_option_list # :nodoc:
- ENV['RSPECOPTS'] || @spec_opts.join(" ") || ""
+ 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
+
+ def evaluate(o) # :nodoc:
+ case o
+ when Proc then o.call
+ else o
+ end
end
def spec_file_list # :nodoc:
@@ -161,8 +205,8 @@ module Spec
FileList[ ENV['SPEC'] ]
else
result = []
- result += @spec_files.to_a if @spec_files
- result += FileList[ @pattern ].to_a if @pattern
+ result += spec_files.to_a if spec_files
+ result += FileList[ pattern ].to_a if pattern
FileList[result]
end
end
diff --git a/test/lib/spec/rake/verify_rcov.rb b/test/lib/spec/rake/verify_rcov.rb
index a05153e99..9715744e9 100644
--- a/test/lib/spec/rake/verify_rcov.rb
+++ b/test/lib/spec/rake/verify_rcov.rb
@@ -19,10 +19,14 @@ module RCov
# exception.
attr_accessor :threshold
+ # Require the threshold value be met exactly. This is the default.
+ attr_accessor :require_exact_threshold
+
def initialize(name=:verify_rcov)
@name = name
@index_html = 'coverage/index.html'
@verbose = true
+ @require_exact_threshold = true
yield self if block_given?
raise "Threshold must be set" if @threshold.nil?
define
@@ -32,6 +36,7 @@ module RCov
desc "Verify that rcov coverage is at least #{threshold}%"
task @name do
total_coverage = nil
+
File.open(index_html).each_line do |line|
if line =~ /<tt.*>(\d+\.\d+)%<\/tt>&nbsp;<\/td>/
total_coverage = eval($1)
@@ -40,8 +45,8 @@ module RCov
end
puts "Coverage: #{total_coverage}% (threshold: #{threshold}%)" if verbose
raise "Coverage must be at least #{threshold}% but was #{total_coverage}%" if total_coverage < threshold
- raise "Coverage has increased above the threshold of #{threshold}% to #{total_coverage}%. You should update your threshold value." if total_coverage > threshold
+ raise "Coverage has increased above the threshold of #{threshold}% to #{total_coverage}%. You should update your threshold value." if (total_coverage > threshold) and require_exact_threshold
end
end
end
-end \ No newline at end of file
+end