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
# File lib/rubytest/config.rb, line 48 def self.assertionless @assertionless end
# File lib/rubytest/config.rb, line 53 def self.assertionless=(boolean) @assertionaless = !!boolean end
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
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
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
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
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
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 lazy block.
# File lib/rubytest/config.rb, line 126 def apply! @block.call(self) if @block end
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, 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
Automatically modify the ‘$LOAD_PATH`?
@return [Boolean]
# File lib/rubytest/config.rb, line 190 def autopath=(boolean) @autopath = !! boolean end
Automatically modify the ‘$LOAD_PATH`?
@return [Boolean]
# File lib/rubytest/config.rb, line 183 def autopath? @autopath end
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
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
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
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
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
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
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 is a synonym for assertionless.
@return [Boolean]
# File lib/rubytest/config.rb, line 316 def hard=(boolean) @hard = !! boolean end
Hard is a synonym for assertionless.
@return [Boolean]
# File lib/rubytest/config.rb, line 309 def hard? @hard || self.class.assertionless end
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
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
Set paths to add to $LOAD_PATH.
@return [Array<String>]
# File lib/rubytest/config.rb, line 206 def loadpath=(list) @loadpath = makelist(list) end
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
Set the description matches for filtering tests.
@return [Array<String>]
# File lib/rubytest/config.rb, line 285 def match=(list) @match = makelist(list) end
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
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
# File lib/rubytest/config.rb, line 141 def name @name end
# File lib/rubytest/config.rb, line 146 def name=(name) @name = name.to_s if name end
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
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
Default test suite ($TEST_SUITE).
@return [Array]
# File lib/rubytest/config.rb, line 153 def suite @suite ||= $TEST_SUITE end
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
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
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
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
Set verbose mode.
@return [Boolean]
# File lib/rubytest/config.rb, line 255 def verbose=(boolean) @verbose = !! boolean end
Provide extra details in reports?
@return [Boolean]
# File lib/rubytest/config.rb, line 248 def verbose? @verbose end
Private Instance Methods
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
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