Class: Test::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytest/config.rb

Overview

Encapsulates test run configruation.

Constant Summary collapse

DEFAULT_FORMAT =

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

'dotprogress'
GLOB_ROOT =

Glob used to find project root directory.

'{.index,.gemspec,.git,.hg,_darcs,lib/}'
GLOB_CONFIG =
Deprecated.

Use manual -c/–config option instead.

RubyTest configuration file can be in ‘.test.rb`, etc/test.rb or config/test.rb, .test, in that order of precedence.

'{.test.rb,etc/test.rb,config/test.rb,.test}'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}, &block) ⇒ Config

Initialize new Config instance.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rubytest/config.rb', line 129

def initialize(settings={}, &block)
  @format   = nil
  @autopath = nil
  @chdir    = nil
  @files    = []
  @tags     = []
  @match    = []
  @units    = []
  @requires = []
  @loadpath = []

  #apply_environment

  settings.each do |k,v|
    send("#{k}=", v)
  end

  self.class.load_config # deprecated!!!

  apply(&block)
end

Instance Attribute Details

#loadpathArray<String>

Paths to add to $LOAD_PATH.

Returns:



204
205
206
# File 'lib/rubytest/config.rb', line 204

def loadpath
  @loadpath
end

#requiresArray<String>

Scripts to require prior to tests.

Returns:



216
217
218
# File 'lib/rubytest/config.rb', line 216

def requires
  @requires
end

Class Method Details

.assertionlessObject



49
50
51
# File 'lib/rubytest/config.rb', line 49

def self.assertionless
  @assertionless
end

.assertionless=(boolean) ⇒ Object



54
55
56
# File 'lib/rubytest/config.rb', line 54

def self.assertionless=(boolean)
  @assertionaless = !!boolean
end

.config_fileObject

Deprecated.

Find traditional configuration file.



76
77
78
# File 'lib/rubytest/config.rb', line 76

def self.config_file
  @config_file ||= Dir.glob(File.join(root, GLOB_CONFIG)).first
end

.dotindexHash

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

Returns:

  • (Hash)

    YAML loaded .index file, or empty hash.



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rubytest/config.rb', line 100

def self.dotindex
  @dotindex ||= (
    file = File.join(root, '.index')
    if File.exist?(file)
      require 'yaml'
      YAML.load_file(file) rescue {}
    else
      {}
    end
  )
end

.load_configObject

Deprecated.

Planned for deprecation in April 2013.

Load configuration file. An example file might look like:

Test.configure do |run|
  run.files << 'test/case_*.rb'
end


65
66
67
68
69
70
71
# File 'lib/rubytest/config.rb', line 65

def self.load_config
  if config_file
    file = config_file.sub(Dir.pwd+'/','')
    $stderr.puts "Automatic #{file} loading has been deprecated.\nUse -c option for future version."
    load config_file
  end
end

.load_path_setupObject

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



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rubytest/config.rb', line 115

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

.rootString

Find and cache project root directory.

Returns:

  • (String)

    Project’s root path.



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rubytest/config.rb', line 83

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

Instance Method Details

#apply(&block) ⇒ Object

Evaluate configuration block.

Returns:

  • nothing



154
155
156
# File 'lib/rubytest/config.rb', line 154

def apply(&block)
  block.call(self) if block
end

#apply_environment_defaultsObject

Apply environment as underlying defaults for unset configuration settings.

Returns:

  • nothing



382
383
384
385
386
387
388
389
390
391
# File 'lib/rubytest/config.rb', line 382

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_overridesObject

TODO:

Better name for this method?

Apply environment, overriding any previous configuration settings.

Returns:

  • nothing



367
368
369
370
371
372
373
374
375
376
# File 'lib/rubytest/config.rb', line 367

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) ⇒ Boolean

Automatically modify the ‘$LOAD_PATH`?

Returns:

  • (Boolean)


197
198
199
# File 'lib/rubytest/config.rb', line 197

def autopath=(boolean)
  @autopath = !! boolean
end

#autopath?Boolean

Automatically modify the ‘$LOAD_PATH`?

Returns:

  • (Boolean)


190
191
192
# File 'lib/rubytest/config.rb', line 190

def autopath?
  @autopath
end

#chdirString

Change to this directory before running tests.

Returns:



315
316
317
# File 'lib/rubytest/config.rb', line 315

def chdir
  @chdir
end

#chdir=(dir) ⇒ String

Set directory to change to before running tests.

Returns:



322
323
324
# File 'lib/rubytest/config.rb', line 322

def chdir=(dir)
  @chdir = dir.to_s
end

#filesArray<String> Also known as: test_files

List of test files to run.

Returns:



174
175
176
# File 'lib/rubytest/config.rb', line 174

def files
  @files
end

#files=(list) ⇒ Array<String> Also known as: test_files=

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

Returns:



182
183
184
# File 'lib/rubytest/config.rb', line 182

def files=(list)
  @files = makelist(list)
end

#formatString

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

Returns:



229
230
231
# File 'lib/rubytest/config.rb', line 229

def format
  @format || DEFAULT_FORMAT
end

#format=(format) ⇒ String

Set test report format.

Returns:



236
237
238
# File 'lib/rubytest/config.rb', line 236

def format=(format)
  @format = format.to_s
end

#hard=(boolean) ⇒ Boolean

Hard is a synonym for assertionless.

Returns:

  • (Boolean)


308
309
310
# File 'lib/rubytest/config.rb', line 308

def hard=(boolean)
  @hard = !! boolean
end

#hard?Boolean

Hard is a synonym for assertionless.

Returns:

  • (Boolean)


301
302
303
# File 'lib/rubytest/config.rb', line 301

def hard?
  @hard || self.class.assertionless
end

#load_config(file) ⇒ Boolean

Load configuration file.

Returns:

  • (Boolean)

    true if file was required



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/rubytest/config.rb', line 396

def load_config(file)
  try_paths = ['etc', 'config']
  try_paths.concat loadpath
  try_paths << '.'
  try_paths = try_paths.uniq

  if chdir
    try_paths = try_paths.map{ |path| File.join(chdir, path) }
  end

  hold_path = $LOAD_PATH.dup
  $LOAD_PATH.replace(try_paths)
  begin
    success = require file
  ensure
    $LOAD_PATH.replace(hold_path)
  end
  success
end

#matchArray<String>

Description match for filtering tests.

Returns:



271
272
273
# File 'lib/rubytest/config.rb', line 271

def match
  @match
end

#match=(list) ⇒ Array<String>

Set the description matches for filtering tests.

Returns:



278
279
280
# File 'lib/rubytest/config.rb', line 278

def match=(list)
  @match = makelist(list)
end

#modeString

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.

Returns:



331
332
333
# File 'lib/rubytest/config.rb', line 331

def mode
  @mode
end

#mode=(type) ⇒ String

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.

Returns:



340
341
342
# File 'lib/rubytest/config.rb', line 340

def mode=(type)
  @mode = type.to_s
end

#suiteArray

Default test suite ($TEST_SUITE).

Returns:

  • (Array)


161
162
163
# File 'lib/rubytest/config.rb', line 161

def suite
  @suite ||= $TEST_SUITE
end

#suite=(test_objects) ⇒ Object

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



167
168
169
# File 'lib/rubytest/config.rb', line 167

def suite=(test_objects)
  @suite = Array(test_objects)
end

#tagsArray<String>

Selection of tags for filtering tests.

Returns:



257
258
259
# File 'lib/rubytest/config.rb', line 257

def tags
  @tags
end

#tags=(list) ⇒ Array<String>

Set the list of tags for filtering tests.

Returns:



264
265
266
# File 'lib/rubytest/config.rb', line 264

def tags=(list)
  @tags = makelist(list)
end

#to_shellwordsArray<String>

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

Returns:



348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/rubytest/config.rb', line 348

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

#unitsArray<String>

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

Returns:



286
287
288
# File 'lib/rubytest/config.rb', line 286

def units
  @units
end

#units=(list) ⇒ Array<String>

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.

Returns:



294
295
296
# File 'lib/rubytest/config.rb', line 294

def units=(list)
  @units = makelist(list)
end

#verbose=(boolean) ⇒ Boolean

Set verbose mode.

Returns:

  • (Boolean)


250
251
252
# File 'lib/rubytest/config.rb', line 250

def verbose=(boolean)
  @verbose = !! boolean
end

#verbose?Boolean

Provide extra details in reports?

Returns:

  • (Boolean)


243
244
245
# File 'lib/rubytest/config.rb', line 243

def verbose?
  @verbose
end