#!/usr/bin/env ruby
#-*-ruby-*-
require "dpklib/command"
require "dpklib/parsearg"
require "runit/cui/testrunner"

class DpkrbRunitProgram < Dpklib::CommandLineProgram
  VERSION_FILE = "VERSION"

  def start
    opts = Dpklib.parse_args(argv, "Lq", "p:") || usage
    @is_ignore_loaderror = opts[:L]
    @is_quiet = opts[:q]
    @pattern = opts[:p] 
    usage if opts.empty?
    
    RUNIT::CUI::TestRunner.quiet_mode = @is_quiet
    for file in opts
      with_protect(file) {
        load(file)
      }
    end

    # After quiting, test-runner works.
    argv.replace( @pattern ? [@pattern] : [] )
    require("rubyunit")
  end

  def with_protect(file, &block)
    if @is_ignore_loaderror
      begin
        yield
      rescue LoadError
        info("Skipped #{file}: #{$!}")
      end
    else
      yield
    end
  end
  
  def usage
    die "
usage: #{progname} [OPTIONS] RBFILE ...
OPTIONS:
  -L: Ignores LoadError.
  -q: Quiet mode.
  -p PATTERN:
    Only runs testcases or methods matches PATTERN.
    PATTERN can be STRING or /REGEXP/.
"
  end

  execute
end #/DpkrbRunitProgram
