Class: Lookout::Rake::Tasks::Test

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/lookout-rake-3.0/tasks/test.rb

Overview

Rake task for running expectation tests.

Constant Summary collapse

LoaderPath =
File.join(File.dirname(__FILE__), 'test/loader.rb')
Paths =
%w'lib'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|?| ... } ⇒ Test

Defines a Rake task for running expectation tests named NAME. Also defines a task for running expectations with coverage checking named NAME:coverage. The default task is set to depend on the NAME task, unless the default task has already been defined. The ‘:check` task is likewise set to depend on NAME. The NAME task itself and its NAME:coverage counterpart are set to depend on the `:compile` task if it’s been defined.

Optionally yields the TASK being created so that it may be adjusted further before being defined.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :name (Symbol) — default: :test

    The name of the task

  • :paths (Array<String>) — default: ['lib']

    The paths to add to ‘$LOAD_PATH`

  • :requires (Array<String>) — default: []

    The libraries to require

  • :files (Array<String>) — default: FileList[ENV['TEST']]

    The expectation files to load

  • :inventory (Inventory) — default: Inventory::Rake::Tasks.inventory

    The Inventory to look for :paths, :requires, and :files in, see #inventory= (the default is only used if ‘inventory/rake-1.0` has been required)

  • :specification (Gem::Specification)

    The Gem specification to look for :paths and :requires in, see #specification=

  • :options (Array<String>) — default: ['-w']

    The options to pass to ruby

Yields:

  • (?)

Yield Parameters:

  • task (self)


37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/lookout-rake-3.0/tasks/test.rb', line 37

def initialize(options = {})
  self.name = options.fetch(:name, :test)
  self.paths = options.fetch(:paths, Paths)
  self.requires = options.fetch(:requires, [])
  self.files = options.fetch(:files){ ENV.include?('TEST') ? FileList[ENV['TEST']] : nil }
  inventory = options[:inventory] ||
    ((provided? 'inventory/rake-1.0' or provided? 'inventory-rake-1.0') and Inventory::Rake::Tasks.inventory) and
    self.inventory = inventory
  self.specification = options.fetch(:specification) if options.include? :specification
  self.options = options.fetch(:options, %w'-w')
  yield self if block_given?
  define
end

Instance Attribute Details

#filesArray<String>

Returns The expectation files to load, defaulting to ‘FileList[’test/unit/*/.rb]‘.

Returns:

  • (Array<String>)

    The expectation files to load, defaulting to ‘FileList[’test/unit/*/.rb]‘



83
84
85
# File 'lib/lookout-rake-3.0/tasks/test.rb', line 83

def files
  @files ||= FileList['test/unit/**/*.rb']
end

#inventoryInventory

Returns The inventory to use.

Returns:

  • (Inventory)

    The inventory to use



92
93
94
# File 'lib/lookout-rake-3.0/tasks/test.rb', line 92

def inventory
  @inventory
end

#nameSymbol

Returns The name of the task.

Returns:

  • (Symbol)

    The name of the task



52
53
54
# File 'lib/lookout-rake-3.0/tasks/test.rb', line 52

def name
  @name
end

#optionsArray<String>

Returns The options to pass to ruby.

Returns:

  • (Array<String>)

    The options to pass to ruby



127
128
129
# File 'lib/lookout-rake-3.0/tasks/test.rb', line 127

def options
  @options
end

#pathsArray<String>

Returns The paths to add to ‘$LOAD_PATH`; may load #specification.

Returns:

  • (Array<String>)

    The paths to add to ‘$LOAD_PATH`; may load #specification



60
61
62
63
64
# File 'lib/lookout-rake-3.0/tasks/test.rb', line 60

def paths
  return @paths if @paths and not @paths.equal? Paths
  self.specification = specification
  @paths
end

#requiresArray<String>

Returns The libraries to require; may load #specification.

Returns:

  • (Array<String>)

    The libraries to require; may load #specification



71
72
73
74
75
# File 'lib/lookout-rake-3.0/tasks/test.rb', line 71

def requires
  return @requires unless @requires.empty?
  self.specification = specification
  @requires
end

Instance Method Details

#defineObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/lookout-rake-3.0/tasks/test.rb', line 133

def define
  desc @name == :test ? 'Run tests' : 'Run tests for %s' % @name
  task @name do
    run
  end

  coverage = :"#{@name}:coverage"
  desc @name == :test ? 'Check test coverage' : 'Check test coverage for %s' % @name
  task coverage do
    run %w'-c'
  end

  [@name, coverage].each do |name|
    task name => :compile if Rake::Task.task_defined? :compile
  end

  task :default => @name unless Rake::Task.task_defined? :default

  task :check => @name
end

#specificationGem::Specification

Returns The specification to use; will try to find one by looking for ‘*.gemspec` in the current directory.

Returns:

  • (Gem::Specification)

    The specification to use; will try to find one by looking for ‘*.gemspec` in the current directory

Raises:

  • (RuntimeError)

    If no specification has been set and one can’t be found in the current directory (the project root directory)



108
109
110
111
112
113
114
115
# File 'lib/lookout-rake-3.0/tasks/test.rb', line 108

def specification
  return @specification if defined? @specification
  return nil unless defined? ::Gem
  gemspec = Dir['*.gemspec'].first
  fail 'gem specification was not given and could not be found in project root: %s' %
    Dir.pwd unless gemspec
  @specification = Gem::Specification.load(gemspec)
end

#specification=(specification) ⇒ Gem::Specification

Returns The new specification to use for #paths and #requires: SPECIFICATION.

Parameters:

  • specification (Gem::Specification)

Returns:

  • (Gem::Specification)

    The new specification to use for #paths and #requires: SPECIFICATION



120
121
122
123
124
# File 'lib/lookout-rake-3.0/tasks/test.rb', line 120

def specification=(specification)
  self.paths = specification.require_paths
  self.requires = [specification.name.gsub('-', '/')]
  specification
end