Class: Spec::Rake::SpecTask

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

Overview

A Rake task that runs a set of RSpec contexts.

Example:

Rake::SpecTask.new do |t|
  t.warning = true
end

This will create a task that can be run with:

rake spec

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a specing task.

Yields:

  • (_self)

Yield Parameters:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/spec/rake/spectask.rb', line 76

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 = []
  @out = nil
  @fail_on_error = true
  @rcov = false
  @rcov_opts = ['--exclude', 'lib\/spec,bin\/spec']
  @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.



62
63
64
# File 'lib/spec/rake/spectask.rb', line 62

def fail_on_error
  @fail_on_error
end

#failure_messageObject

A message to print to stdout when there are failures.



65
66
67
# File 'lib/spec/rake/spectask.rb', line 65

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>‘]



30
31
32
# File 'lib/spec/rake/spectask.rb', line 30

def libs
  @libs
end

#nameObject

Name of spec task. (default is :spec)



26
27
28
# File 'lib/spec/rake/spectask.rb', line 26

def name
  @name
end

#outObject

Where RSpec’s output is written. Defaults to STDOUT.



43
44
45
# File 'lib/spec/rake/spectask.rb', line 43

def out
  @out
end

#patternObject

Glob pattern to match spec files. (default is ‘spec/spec*.rb’)



37
38
39
# File 'lib/spec/rake/spectask.rb', line 37

def pattern
  @pattern
end

#rcovObject

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



47
48
49
# File 'lib/spec/rake/spectask.rb', line 47

def rcov
  @rcov
end

#rcov_dirObject

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



55
56
57
# File 'lib/spec/rake/spectask.rb', line 55

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



51
52
53
# File 'lib/spec/rake/spectask.rb', line 51

def rcov_opts
  @rcov_opts
end

#ruby_optsObject

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



58
59
60
# File 'lib/spec/rake/spectask.rb', line 58

def ruby_opts
  @ruby_opts
end

#spec_optsObject

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



40
41
42
# File 'lib/spec/rake/spectask.rb', line 40

def spec_opts
  @spec_opts
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.



34
35
36
# File 'lib/spec/rake/spectask.rb', line 34

def warning
  @warning
end

Instance Method Details

#defineObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/spec/rake/spectask.rb', line 95

def define
  #raise "No spec files found." if file_list.empty?
  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 RSpec for #{actual_name}" + (@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}"
      # ruby [ruby_opts] -Ilib -S rcov [rcov_opts] bin/spec -- [spec_opts] examples
      # 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_option_list + " " +
          file_list.collect { |fn| %["#{fn}"] }.join(' ') + " " + 
          redirect
        )
      rescue => e
         puts @failure_message if @failure_message
         raise e if @fail_on_error
      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

#file_listObject

:nodoc:



156
157
158
159
160
161
162
163
164
165
# File 'lib/spec/rake/spectask.rb', line 156

def 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

#rcov_option_listObject

:nodoc:



147
148
149
150
# File 'lib/spec/rake/spectask.rb', line 147

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

#spec_files=(list) ⇒ Object

Explicitly define the list of spec files to be included in a spec. list 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.



71
72
73
# File 'lib/spec/rake/spectask.rb', line 71

def spec_files=(list)
  @spec_files = list
end

#spec_option_listObject

:nodoc:



152
153
154
# File 'lib/spec/rake/spectask.rb', line 152

def spec_option_list # :nodoc:
  ENV['RSPECOPTS'] || @spec_opts.join(" ") || ""
end