class Test::Config

Encapsulates test run configruation.

Constants

DEFAULT_FORMAT

Default report is in the old “dot-progress” format.

GLOB_ROOT

Glob used to find project root directory.

Public Class Methods

assertionless() click to toggle source
# File lib/rubytest/config.rb, line 48
def self.assertionless
  @assertionless
end
assertionless=(boolean) click to toggle source
# File lib/rubytest/config.rb, line 53
def self.assertionless=(boolean)
  @assertionaless = !!boolean
end
dotindex() click to toggle source

Load and cache a project’s ‘.index` file, if it has one.

@return [Hash] YAML loaded ‘.index` file, or empty hash.

# File lib/rubytest/config.rb, line 77
def self.dotindex
  @dotindex ||= (
    file = File.join(root, '.index')
    if File.exist?(file)
      require 'yaml'
      YAML.load_file(file) rescue {}
    else
      {}
    end
  )
end
load_path_setup() click to toggle source

Setup $LOAD_PATH based on project’s ‘.index` file, if an index file is not found, then default to `lib/` if it exists.

# File lib/rubytest/config.rb, line 92
def self.load_path_setup
  if load_paths = (dotindex['paths'] || {})['lib']
    load_paths.each do |path|
      $LOAD_PATH.unshift(File.join(root, path))
    end
  else
    typical_load_path = File.join(root, 'lib')
    if File.directory?(typical_load_path)
      $LOAD_PATH.unshift(typical_load_path) 
    end
  end
end
new(settings={}, &block) click to toggle source

Initialize new Config instance.

# File lib/rubytest/config.rb, line 106
def initialize(settings={}, &block)
  @format   = nil
  @autopath = nil
  @chdir    = nil
  @files    = []
  @tags     = []
  @match    = []
  @units    = []
  @requires = []
  @loadpath = []

  #apply_environment

  apply(settings)

  # save for lazy execution
  @block = block
end
root() click to toggle source

Find and cache project root directory.

@return [String] Project’s root path.

# File lib/rubytest/config.rb, line 60
def self.root
  @root ||= (
    glob    = GLOB_ROOT
    stop    = '/'
    default = Dir.pwd
    dir     = Dir.pwd
    until dir == stop
      break dir if Dir[File.join(dir, glob)].first
      dir = File.dirname(dir)
    end
    dir == stop ? default : dir
  )
end

Public Instance Methods

after(&proc) click to toggle source

Procedure to call, just after running tests.

@return [Proc,nil]

# File lib/rubytest/config.rb, line 346
def after(&proc)
  @after = proc if proc
  @after
end
apply(hash={}, &block) click to toggle source

Evaluate configuration block.

@return nothing

# File lib/rubytest/config.rb, line 133
def apply(hash={}, &block)
  hash.each do |k,v|
    send("#{k}=", v)
  end
  block.call(self) if block
end
apply!() click to toggle source

Apply lazy block.

# File lib/rubytest/config.rb, line 126
def apply!
  @block.call(self) if @block
end
apply_environment_defaults() click to toggle source

Apply environment as underlying defaults for unset configuration settings.

@return nothing

# File lib/rubytest/config.rb, line 409
def apply_environment_defaults
  @format   = env(:format,   @format)   if @format.nil?
  @autopath = env(:autopath, @autopath) if @autopath.nil?
  @files    = env(:files,    @files)    if @files.empty?
  @match    = env(:match,    @match)    if @match.empty?
  @tags     = env(:tags,     @tags)     if @tags.empty?
  @units    = env(:units,    @units)    if @units.empty?
  @requires = env(:requires, @requires) if @requires.empty?
  @loadpath = env(:loadpath, @loadpath) if @loadpath.empty?
end
apply_environment_overrides() click to toggle source

Apply environment, overriding any previous configuration settings.

@todo Better name for this method? @return nothing

# File lib/rubytest/config.rb, line 394
def apply_environment_overrides
  @format   = env(:format,   @format)
  @autopath = env(:autopath, @autopath)
  @files    = env(:files,    @files)
  @match    = env(:match,    @match)
  @tags     = env(:tags,     @tags)
  @units    = env(:units,    @units)
  @requires = env(:requires, @requires)
  @loadpath = env(:loadpath, @loadpath)
end
autopath=(boolean) click to toggle source

Automatically modify the ‘$LOAD_PATH`?

@return [Boolean]

# File lib/rubytest/config.rb, line 190
def autopath=(boolean)
  @autopath = !! boolean
end
autopath?() click to toggle source

Automatically modify the ‘$LOAD_PATH`?

@return [Boolean]

# File lib/rubytest/config.rb, line 183
def autopath?
  @autopath
end
before(&proc) click to toggle source

Procedure to call, just before running tests.

@return [Proc,nil]

# File lib/rubytest/config.rb, line 338
def before(&proc)
  @before = proc if proc
  @before
end
chdir(dir=nil) click to toggle source

Change to this directory before running tests.

@return [String]

# File lib/rubytest/config.rb, line 323
def chdir(dir=nil)
  @chdir = dir.to_s if dir
  @chdir
end
chdir=(dir) click to toggle source

Set directory to change to before running tests.

@return [String]

# File lib/rubytest/config.rb, line 331
def chdir=(dir)
  @chdir = dir.to_s
end
files(*list) click to toggle source

List of test files to run.

@return [Array<String>]

# File lib/rubytest/config.rb, line 166
def files(*list)
  @files.concat(makelist(list)) unless list.empty?
  @files
end
Also aliased as: test_files
files=(list) click to toggle source

Set the list of test files to run. Entries can be file glob patterns.

@return [Array<String>]

# File lib/rubytest/config.rb, line 175
def files=(list)
  @files = makelist(list)
end
Also aliased as: test_files=
format(name=nil) click to toggle source

Name of test report format, by default it is ‘dotprogress`.

@return [String] format

# File lib/rubytest/config.rb, line 230
def format(name=nil)
  @format = name.to_s if name
  @format || DEFAULT_FORMAT
end
format=(name) click to toggle source

Set test report format.

@param [String] name

Name of the report format.

@return [String] format

# File lib/rubytest/config.rb, line 241
def format=(name)
  @format = name.to_s
end
hard=(boolean) click to toggle source

Hard is a synonym for assertionless.

@return [Boolean]

# File lib/rubytest/config.rb, line 316
def hard=(boolean)
  @hard = !! boolean
end
hard?() click to toggle source

Hard is a synonym for assertionless.

@return [Boolean]

# File lib/rubytest/config.rb, line 309
def hard?
  @hard || self.class.assertionless
end
load_config(file) click to toggle source

Load configuration file for project.

File names are prefixed with ‘./` to ensure they are from a local source. An extension of `.rb` is assumed if the file lacks an one.

@return [Boolean] true if file was required

# File lib/rubytest/config.rb, line 426
def load_config(file)
  file = file + '.rb' if File.extname(file) == ''

  if chdir
    file = File.join(chdir, file)
  else
    file = File.join('.', file)
  end

  if File.exist?(file)
    return require(file)
  else
    raise "config file not found -- `#{file}'"
  end
end
load_path(*list)
Alias for: loadpath
load_path=(list)
Alias for: loadpath=
loadpath(*list) click to toggle source

Paths to add to $LOAD_PATH.

@return [Array<String>]

# File lib/rubytest/config.rb, line 197
def loadpath(*list)
  @loadpath.concat(makelist(list)) unless list.empty?
  @loadpath
end
Also aliased as: load_path
loadpath=(list) click to toggle source

Set paths to add to $LOAD_PATH.

@return [Array<String>]

# File lib/rubytest/config.rb, line 206
def loadpath=(list)
  @loadpath = makelist(list)
end
Also aliased as: load_path=
match(*list) click to toggle source

Description match for filtering tests.

@return [Array<String>]

# File lib/rubytest/config.rb, line 277
def match(*list)
  @match.concat(makelist(list)) unless list.empty?
  @match
end
match=(list) click to toggle source

Set the description matches for filtering tests.

@return [Array<String>]

# File lib/rubytest/config.rb, line 285
def match=(list)
  @match = makelist(list)
end
mode() click to toggle source

The mode is only useful for specialied purposes, such as how to run tests via the Rake task. It has no general purpose and can be ignored in most cases.

@return [String]

# File lib/rubytest/config.rb, line 356
def mode
  @mode
end
mode=(type) click to toggle source

The mode is only useful for specialied purposes, such as how to run tests via the Rake task. It has no general purpose and can be ignored in most cases.

@return [String]

# File lib/rubytest/config.rb, line 365
def mode=(type)
  @mode = type.to_s
end
name() click to toggle source
# File lib/rubytest/config.rb, line 141
def name
  @name
end
name=(name) click to toggle source
# File lib/rubytest/config.rb, line 146
def name=(name)
  @name = name.to_s if name
end
requires(*list) click to toggle source

Scripts to require prior to tests.

@return [Array<String>]

# File lib/rubytest/config.rb, line 214
def requires(*list)
  @requires.concat(makelist(list)) unless list.empty?
  @requires
end
requires=(list) click to toggle source

Set the features that need to be required before the test files.

@return [Array<String>]

# File lib/rubytest/config.rb, line 223
def requires=(list)
  @requires = makelist(list)
end
suite() click to toggle source

Default test suite ($TEST_SUITE).

@return [Array]

# File lib/rubytest/config.rb, line 153
def suite
  @suite ||= $TEST_SUITE
end
suite=(test_objects) click to toggle source

This is not really for general, but it is useful for Ruby Test’s own tests, to isolate tests.

# File lib/rubytest/config.rb, line 159
def suite=(test_objects)
  @suite = Array(test_objects)
end
tags(*list) click to toggle source

Selection of tags for filtering tests.

@return [Array<String>]

# File lib/rubytest/config.rb, line 262
def tags(*list)
  @tags.concat(makelist(list)) unless list.empty?
  @tags
end
tags=(list) click to toggle source

Set the list of tags for filtering tests.

@return [Array<String>]

# File lib/rubytest/config.rb, line 270
def tags=(list)
  @tags = makelist(list)
end
test_files(*list)
Alias for: files
test_files=(list)
Alias for: files=
to_shellwords() click to toggle source

Convert configuration to shell options, compatible with the rubytest command line.

DEPRECATE: Shell command is considered bad approach.

@return [Array<String>]

# File lib/rubytest/config.rb, line 375
def to_shellwords
  argv = []
  argv << %[--autopath] if autopath?
  argv << %[--verbose]  if verbose?
  argv << %[--format="#{format}"] if format
  argv << %[--chdir="#{chdir}"] if chdir
  argv << %[--tags="#{tags.join(';')}"]   unless tags.empty?
  argv << %[--match="#{match.join(';')}"] unless match.empty?
  argv << %[--units="#{units.join(';')}"] unless units.empty?
  argv << %[--loadpath="#{loadpath.join(';')}"] unless loadpath.empty?
  argv << %[--requires="#{requires.join(';')}"] unless requires.empty?
  argv << files.join(' ') unless files.empty?
  argv
end
units(*list) click to toggle source

List of units with which to filter tests. It is an array of strings which are matched against module, class and method names.

@return [Array<String>]

# File lib/rubytest/config.rb, line 293
def units(*list)
  @units.concat(makelist(list)) unless list.empty?
  @units
end
units=(list) click to toggle source

Set the list of units with which to filter tests. It is an array of strings which are matched against module, class and method names.

@return [Array<String>]

# File lib/rubytest/config.rb, line 302
def units=(list)
  @units = makelist(list)
end
verbose=(boolean) click to toggle source

Set verbose mode.

@return [Boolean]

# File lib/rubytest/config.rb, line 255
def verbose=(boolean)
  @verbose = !! boolean
end
verbose?() click to toggle source

Provide extra details in reports?

@return [Boolean]

# File lib/rubytest/config.rb, line 248
def verbose?
  @verbose
end

Private Instance Methods

env(name, default=nil) click to toggle source

Lookup environment variable with name ‘rubytest_{name}`, and transform in according to the type of the given default. If the environment variable is not set then returns the default.

@return [Object]

# File lib/rubytest/config.rb, line 450
def env(name, default=nil)
  value = ENV["rubytest_#{name}".downcase]

  case default
  when Array
    return makelist(value) if value 
  else
    return value if value
  end
  default
end
makelist(list) click to toggle source

If given a String then split up at ‘:` and `;` markers. Otherwise ensure the list is an Array and the entries are all strings and not empty.

@return [Array<String>]

# File lib/rubytest/config.rb, line 467
def makelist(list)
  case list
  when String
    list = list.split(/[:;]/)
  else
    list = Array(list).map{ |path| path.to_s }
  end
  list.reject{ |path| path.strip.empty? }
end