Skip to content

Commit 427b2bc

Browse files
committed
Add before and after blocks for config.
1 parent 993bcb8 commit 427b2bc

3 files changed

Lines changed: 46 additions & 16 deletions

File tree

lib/rubytest/cli.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def options
8686
config.format = 'tapj'
8787
end
8888

89+
# tempted to change -T
8990
opt.on '-t', '--tag TAG', 'select tests by tag' do |tag|
9091
config.tags.concat makelist(tag)
9192
end
@@ -118,6 +119,9 @@ def options
118119
opt.on '-c', '--config FILE', "require local config file (immediately)" do |file|
119120
config.load_config(file)
120121
end
122+
#opt.on '-T', '--tests GLOB', "tests to run (if none given as arguments)" do |glob|
123+
# config.files << glob
124+
#end
121125
opt.on '-V' , '--verbose', 'provide extra detail in reports' do
122126
config.verbose = true
123127
end

lib/rubytest/config.rb

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,22 @@ def chdir=(dir)
323323
@chdir = dir.to_s
324324
end
325325

326+
# Procedure to call, just before running tests.
327+
#
328+
# @return [Proc,nil]
329+
def before(&proc)
330+
@before = proc if proc
331+
@before
332+
end
333+
334+
# Procedure to call, just after running tests.
335+
#
336+
# @return [Proc,nil]
337+
def after(&proc)
338+
@after = proc if proc
339+
@after
340+
end
341+
326342
# The mode is only useful for specialied purposes, such as how
327343
# to run tests via the Rake task. It has no general purpose
328344
# and can be ignored in most cases.
@@ -390,27 +406,26 @@ def apply_environment_defaults
390406
@loadpath = env(:loadpath, @loadpath) if @loadpath.empty?
391407
end
392408

393-
# Load configuration file.
409+
# Load configuration file for project.
410+
#
411+
# File names are prefixed with `./` to ensure they are from a local
412+
# source. An extension of `.rb` is assumed if the file lacks an one.
394413
#
395414
# @return [Boolean] true if file was required
396415
def load_config(file)
397-
try_paths = ['etc', 'config']
398-
try_paths.concat loadpath
399-
try_paths << '.'
400-
try_paths = try_paths.uniq
416+
file = file + '.rb' if File.extname(file) == ''
401417

402418
if chdir
403-
try_paths = try_paths.map{ |path| File.join(chdir, path) }
419+
file = File.join(chdir, file)
420+
else
421+
file = File.join('.', file)
404422
end
405423

406-
hold_path = $LOAD_PATH.dup
407-
$LOAD_PATH.replace(try_paths)
408-
begin
409-
success = require file
410-
ensure
411-
$LOAD_PATH.replace(hold_path)
424+
if File.exist?(file)
425+
return require(file)
426+
else
427+
raise "config file not found -- `#{file}'"
412428
end
413-
success
414429
end
415430

416431
private

lib/rubytest/runner.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
module Test
22

3-
# Currently this is an alias for configure, however it is likely
4-
# to become an alias for `Runner.run` in the future.
3+
# TODO: Support config profile's again?
4+
5+
# Alias for `Test.configure`.
6+
# Use #run! to run tests immediately.
57
#
6-
# @deprecated Will probably change behavior in future.
78
def self.run(config=nil, &config_proc)
89
$stderr.puts "configuration profiles no longer supported." if config
910
configure(&config_proc)
1011
end
1112

13+
# Configure and run immediately.
14+
#
15+
def run!(config=nil, &config_proc)
16+
Runner.run(config, &config_proc)
17+
end
18+
1219
# The Test::Runner class handles the execution of tests.
1320
#
1421
class Runner
@@ -123,6 +130,8 @@ def run
123130
require test_file
124131
end
125132

133+
config.before.call if config.before
134+
126135
@reporter = reporter_load(format)
127136
@recorder = Recorder.new
128137

@@ -131,6 +140,8 @@ def run
131140
observers.each{ |o| o.begin_suite(suite) }
132141
run_thru(suite)
133142
observers.each{ |o| o.end_suite(suite) }
143+
144+
config.after.call if config.after
134145
end
135146

136147
recorder.success?

0 commit comments

Comments
 (0)