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:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a specing task.

Yields:

  • (_self)

Yield Parameters:



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

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,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.



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

def fail_on_error
  @fail_on_error
end

#failure_messageObject

A message to print to stdout when there are failures.



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

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



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

def libs
  @libs
end

#nameObject

Name of spec task. (default is :spec)



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

def name
  @name
end

#outObject

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



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

def out
  @out
end

#patternObject

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



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

def pattern
  @pattern
end

#rcovObject

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



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

def rcov
  @rcov
end

#rcov_dirObject

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



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

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



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

def rcov_opts
  @rcov_opts
end

#ruby_optsObject

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



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

def ruby_opts
  @ruby_opts
end

#spec_optsObject

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



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

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.



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

def warning
  @warning
end

Instance Method Details

#defineObject



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
146
147
148
# File 'lib/spec/rake/spectask.rb', line 96

def define
  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}\""

      unless spec_file_list.empty?
        # 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_file_list.collect { |fn| %["#{fn}"] }.join(' ') + " " + 
            spec_option_list + " " +
            redirect
          )
        rescue => e
           puts @failure_message if @failure_message
           raise e 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

#rcov_option_listObject

:nodoc:



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

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

#spec_file_listObject

:nodoc:



159
160
161
162
163
164
165
166
167
168
# File 'lib/spec/rake/spectask.rb', line 159

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_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.



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

def spec_files=(list)
  @spec_files = list
end

#spec_option_listObject

:nodoc:



155
156
157
# File 'lib/spec/rake/spectask.rb', line 155

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