#!/usr/bin/env ruby # Copyright:: Copyright (c) 2005 Uttk Team. All rights reserved. # Author:: Nicolas Pouillard . # License:: Gnu General Public License. # Revision:: $Id$ # FIXME add a configuration file # conf: # load_path: ../lib # relative to the place of the configuration file # requires: class UttkUnit def initialize ( argv ) @argv = argv end def parse_options @options = OpenStruct.new(:load_path => [], :actions => [], :uttk => nil, :verbose => true, :mode => :require) OptionParser.new do |o| o.on('-I', '--include DIR', 'Add this directory to $LOAD_PATH') do |dir| @options.load_path << dir @options.actions << proc { dir.to_path.load_path! if defined? CoreEx } end o.on('-r', '--require FEATURE', 'Require this file') do |file| @options.actions << proc { require file } end o.on('--import NAME', 'Import this constant') do |const| @options.actions << proc { const.constantize.import! } end o.on('--mode import|require|nothing', [:import, :require, :nothing], 'Set the loading mode for unit tests [default: require]') do |mode| @options.mode = mode end o.on('-w', '--[no-]verbose', 'Turn on/off the verbose mode [default: on]') do |b| @options.verbose = b end o.on('--uttk FILE', 'Require this Uttk (`no\' to disable)') do |file| @options.uttk = file end end.parse!(@argv) end def requires require 'pathname' require 'ostruct' require 'optparse' end def run raise 'Do not load CoreEx before uttk-unit' if defined? CoreEx requires parse_options files = [] options = false argv = ARGV.dup ARGV.replace([]) argv.each do |arg| if options ARGV << arg next end if arg == '--' options = true else files << Pathname.new(arg).cleanpath end end quoted_paths = files.map { |s| Regexp.quote(s) } re_paths = Regexp.new("(#{quoted_paths.join '|'})") Kernel.const_set(:EMBEDDED_TEST_MODE, re_paths) @options.load_path.each { |dir| $LOAD_PATH << dir } root = Pathname.new(__FILE__).dirname.parent if @options.uttk.nil? require((root + 'lib' + 'uttk').to_s) elsif @options.uttk != 'no' require @options.uttk end @options.actions.each { |block| block[] } raise 'CoreEx not loaded' unless defined? CoreEx Test::Unit::UI::Yaml::TestRunner.import! $VERBOSE = @options.verbose files.each do |arg| path = Pathname.new(arg).ext.to_s case @options.mode when :import path = path.camelize if path !~ /[A-Z]/ or path =~ /[.\/]/ path.constantize.import! when :require require path when :nothing else raise "Bad loading mode: #{@options.mode}" end end run_embedded_test_sections end end # class UttkUnit UttkUnit.new(ARGV).run