expectrb

標準添付のtestrbみたいなののexpectations用をでっち上げる。
Test::Unit::AutoRunnerに相当するものはないのでそのへんを書いて、自分用に合せる。
pwdhoge/のときに expectrb -dtest でhoge/test/test_*.rbを実行してくれる辺りは気に入ったかも。

ちょこちょこと$stdoutあたりに細工してからtestrbを走らせるだけでもよかったのかもしれないとは思ったのだけど…。

# = expect.rb
# *testrb* command as a version which correspond to
# Unit test library *expectations*.
# 
# 
# == Usage
#   expectrb [ -p | --prefix= PATTERN ] [ -d | --chdir= DIR ] [ -v | --verbose | --no-verbose ] [ test-files ]
# 
# If it receives no arguments from command line, 
# it do glob "test_*.rb" to search test files.
# A prefix of this pattern, "test_", is enable to relay to
# a value of option prefix or $RUBY_TEST_PREFIX.
# 
# option prefix::
#   Assign test file search pattern's prefix.
# 
# option chdir::
#   At first, move directory to work.
# 
# option verbose::
#   Approve or not to echo application's messages.
#   In default, it's approved.
# 

require 'expectations'
require 'optparse'
require 'rdoc/usage'

def loaderror_ignore_consent(file)
  msg = "missing #{file}: Ignore and skip? (Y/n) > "
  begin
    $stderr.print msg
  end until /\A[yn]/i =~ $stdin.gets
  $&.downcase == 'y'
end

argv            = ARGV.dup
myself          = File.basename($0, '.rb')
testfile_prefix = nil
working_dir     = Dir.pwd
verbose_p       = true
echo            = lambda{|message| puts "#{myself}: #{message}" if verbose_p }

OptionParser.new.instance_eval do
  on('-pPAT', '--prefix=PAT'  ){|s| testfile_prefix = s }
  on('-dDIR', '--chdir=DIR'   ){|d| working_dir     = d }
  on('-v',    '--[no-]verbose'){|v| verbose_p       = v }
  on('-h',    '--help'        ){    RDoc.usage 'Usage'  }
  parse! argv
end

Dir.chdir working_dir

if argv.empty?
  prefix = testfile_prefix || ENV['RUBY_TEST_PREFIX'] || 'test_'
  pattern = "#{prefix}*.rb"
  echo["glob #{pattern}"]
  argv.push *Dir.glob(pattern)
end

if argv.empty?
  echo["no matched test."]
  RDoc.usage
end

argv.each do |testfile|
  echo["load #{testfile}"]
  begin
    load testfile, true
  rescue LoadError => ex
    verbose_p && $stdin.tty? && loaderror_ignore_consent(testfile) or abort(ex.message)
  end
end