#!/usr/bin/env ruby # encoding: UTF-8 require 'tins/xt' require 'tins/lines_file' include Tins::GO require 'utils' include Utils def usage puts <<-EOT Usage: #{File.basename($0)} [OPTS] FILENAME[:LINENO] [FILENAME] Options are -n TESTNAME run the test TESTNAME in file FILENAME -t FRAMEWORK use test framework FRAMEWORK (rspec or test-unit) -h display this help Version is #{File.basename($0)} #{Utils::VERSION}. EOT exit 1 end def cmd(*args) puts args * ' ' system(*args) end $config = Utils::Config::ConfigFile.new $config.parse_config_file File.expand_path('~/.utilsrc') testrunner_args = [] if i = ARGV.index('--') testrunner_args.concat ARGV[(i + 1)..-1] args = ARGV[0...i] else args = ARGV.dup end $opt = go 't:n:h', args args.empty? and fail "require filename or filename:linenumber as arguments" $opt['h'] and usage puts "Running tests in #{args.inspect}" case ($opt['t'] || $config.probe.test_framework).to_sym when :rspec rspec = `which rspec`.full?(:chomp) || `which spec`.full?(:chomp) or raise fail "no spec or rspec command found" if linenumber = $opt['n'] cmd 'ruby', '-I', $config.probe.include_dirs_argument, rspec, '-l', linenumber, *(args + testrunner_args) else args = args.map do |a| if Utils::Editor::FILE_LINENUMBER_REGEXP =~ a $~.captures * ':' else a end end cmd 'ruby', '-I', $config.probe.include_dirs_argument, rspec, *(args + testrunner_args) end when :'test-unit' testrb = `which testrb`.full?(:chomp) or raise fail "no testrb command found" if testname = $opt['n'] cmd 'ruby', '-I', $config.probe.include_dirs_argument, '-S', testrb, '-n', testname, *(args + testrunner_args) else for filename in args sl = filename.source_location if sl.linenumber lf = Tins::LinesFile.for_filename(*sl) if testname = lf.match_backward(/def\s+(\S+?)(?:\(|\s*$)/).full?(:first) cmd 'ruby', '-I', $config.probe.include_dirs_argument, testrb, '-n', testname, sl.filename, *testrunner_args else warn "no test found before line #{sl.linenumber}" end else cmd 'ruby', '-I', $config.probe.include_dirs_argument, '-S', testrb, sl.filename, *testrunner_args end end end end