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/}'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize new Config instance.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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

Class Method Details

.assertionlessObject



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

def self.assertionless
  @assertionless
end

.assertionless=(boolean) ⇒ Object



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

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

.dotindexHash

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

Returns:

  • (Hash)

    YAML loaded ‘.index` file, or empty hash.



77
78
79
80
81
82
83
84
85
86
87
# 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_setupObject

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



92
93
94
95
96
97
98
99
100
101
102
103
# 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

.rootString

Find and cache project root directory.

Returns:

  • (String)

    Project’s root path.



60
61
62
63
64
65
66
67
68
69
70
71
72
# 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

Instance Method Details

#after(&proc) ⇒ Proc?

Procedure to call, just after running tests.

Returns:

  • (Proc, nil)


346
347
348
349
# File 'lib/rubytest/config.rb', line 346

def after(&proc)
  @after = proc if proc
  @after
end

#apply(hash = {}, &block) ⇒ Object

Evaluate configuration block.

Returns:

  • nothing



133
134
135
136
137
138
# 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!Object

Apply lazy block.



126
127
128
# File 'lib/rubytest/config.rb', line 126

def apply!
  @block.call(self) if @block
end

#apply_environment_defaultsObject

Apply environment as underlying defaults for unset configuration settings.

Returns:

  • nothing



409
410
411
412
413
414
415
416
417
418
# 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_overridesObject

TODO:

Better name for this method?

Apply environment, overriding any previous configuration settings.

Returns:

  • nothing



394
395
396
397
398
399
400
401
402
403
# 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) ⇒ Boolean

Automatically modify the ‘$LOAD_PATH`?

Returns:

  • (Boolean)


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

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

#autopath?Boolean

Automatically modify the ‘$LOAD_PATH`?

Returns:

  • (Boolean)


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

def autopath?
  @autopath
end

#before(&proc) ⇒ Proc?

Procedure to call, just before running tests.

Returns:

  • (Proc, nil)


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

def before(&proc)
  @before = proc if proc
  @before
end

#chdir(dir = nil) ⇒ String

Change to this directory before running tests.

Returns:



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

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

#chdir=(dir) ⇒ String

Set directory to change to before running tests.

Returns:



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

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

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

List of test files to run.

Returns:



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

def files(*list)
  @files.concat(makelist(list)) unless list.empty?
  @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:



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

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

#format(name = nil) ⇒ String

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

Returns:



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

def format(name=nil)
  @format = name.to_s if name
  @format || DEFAULT_FORMAT
end

#format=(name) ⇒ String

Set test report format.

Parameters:

  • name (String)

    Name of the report format.

Returns:



241
242
243
# File 'lib/rubytest/config.rb', line 241

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

#hard=(boolean) ⇒ Boolean

Hard is a synonym for assertionless.

Returns:

  • (Boolean)


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

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

#hard?Boolean

Hard is a synonym for assertionless.

Returns:

  • (Boolean)


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

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

#load_config(file) ⇒ Boolean

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.

Returns:

  • (Boolean)

    true if file was required



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# 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

#loadpath(*list) ⇒ Array<String> Also known as: load_path

Paths to add to $LOAD_PATH.

Returns:



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

def loadpath(*list)
  @loadpath.concat(makelist(list)) unless list.empty?
  @loadpath
end

#loadpath=(list) ⇒ Array<String> Also known as: load_path=

Set paths to add to $LOAD_PATH.

Returns:



206
207
208
# File 'lib/rubytest/config.rb', line 206

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

#match(*list) ⇒ Array<String>

Description match for filtering tests.

Returns:



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

def match(*list)
  @match.concat(makelist(list)) unless list.empty?
  @match
end

#match=(list) ⇒ Array<String>

Set the description matches for filtering tests.

Returns:



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

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:



356
357
358
# File 'lib/rubytest/config.rb', line 356

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:



365
366
367
# File 'lib/rubytest/config.rb', line 365

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

#nameObject



141
142
143
# File 'lib/rubytest/config.rb', line 141

def name
  @name
end

#name=(name) ⇒ Object



146
147
148
# File 'lib/rubytest/config.rb', line 146

def name=(name)
  @name = name.to_s if name
end

#requires(*list) ⇒ Array<String>

Scripts to require prior to tests.

Returns:



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

def requires(*list)
  @requires.concat(makelist(list)) unless list.empty?
  @requires
end

#requires=(list) ⇒ Array<String>

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

Returns:



223
224
225
# File 'lib/rubytest/config.rb', line 223

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

#suiteArray

Default test suite ($TEST_SUITE).

Returns:

  • (Array)


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

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.



159
160
161
# File 'lib/rubytest/config.rb', line 159

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

#tags(*list) ⇒ Array<String>

Selection of tags for filtering tests.

Returns:



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

def tags(*list)
  @tags.concat(makelist(list)) unless list.empty?
  @tags
end

#tags=(list) ⇒ Array<String>

Set the list of tags for filtering tests.

Returns:



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

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

#to_shellwordsArray<String>

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

DEPRECATE: Shell command is considered bad approach.

Returns:



375
376
377
378
379
380
381
382
383
384
385
386
387
388
# 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) ⇒ Array<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:



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

def units(*list)
  @units.concat(makelist(list)) unless list.empty?
  @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:



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

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

#verbose=(boolean) ⇒ Boolean

Set verbose mode.

Returns:

  • (Boolean)


255
256
257
# File 'lib/rubytest/config.rb', line 255

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

#verbose?Boolean

Provide extra details in reports?

Returns:

  • (Boolean)


248
249
250
# File 'lib/rubytest/config.rb', line 248

def verbose?
  @verbose
end