#!/usr/bin/ruby $:.unshift( "." ) $:.unshift( "./lib" ) require "P4" require "getoptlong" require 'test/unit' require 'test/unit/ui/console/testrunner' # # Array to hold a list of Test::Unit::TestCase subclasses in the order in # which they are subclassed. See below for the black magic. # $p4ruby_test_list = Array.new # # Reopen the Test::Unit::TestCase class, and define TestCase.inherited, so # we can see whenever a subclass is created. As they're created, we append # them to a list of subclasses, and this will dictate the order of the # tests when they're run. # module Test module Unit class TestCase def TestCase.inherited( sub ) $p4ruby_test_list.push( sub ) end end end end # # Define some common methods and attributes that can be included # by our Test::Unit::TestCase subclasses. # module P4RubyTest @@STARTDIR = Dir.getwd @@ROOTDIR = "testroot" @@P4D = "p4d" # Assume it's in the PATH attr_accessor :p4 # # Rather ugly, but very effective... # def initialize( test_method_name ) super( test_method_name ) Dir.chdir( client_root ) ENV.delete( 'PWD' ) init_client() end # # Set up the test tree - cleaning out any old data # def create_test_tree() if( File.directory?( server_root ) ) puts( "Removing old test tree!" ) cleanup_test_tree() end Dir.mkdir( server_root ) Dir.mkdir( client_root ) create_p4config_file() true end def init_client() # Create a P4 object for all tests. Makes life simple. @p4 = P4.new @p4.port = "rsh:#{@@P4D} -r #{server_root} -L log -vserver=3 -i" end # # Cleanup the test tree # def cleanup_test_tree() rmdir( server_root ) end # # Return the path of the server root # def server_root @@STARTDIR + '/' + @@ROOTDIR end # # Return the client root # def client_root server_root + '/' + 'workspace' end private # # Nuke an entire tree # def rmdir( top ) Dir.entries( top ).each do |ent| next if( ent == '.' || ent == '..' ) path = top + '/' + ent if( File.directory?( path ) ) rmdir( path ) else File.chmod( 0666, path ) File.unlink( path ) end end Dir.delete( top ) end def create_p4config_file return unless ( ENV.has_key?( 'P4CONFIG' ) ) File.open( server_root + '/' + ENV[ 'P4CONFIG' ], "w" ) do |f| f.puts( "P4PORT=#{@p4.port}" ) end end end $save_test_root = false opts = GetoptLong.new( [ "--keep", GetoptLong::NO_ARGUMENT ] ) opts.each do |opt,arg| if( opt == '--keep' ) $save_test_root = true end end # # Load each test in the test/ subdirectory in alphabetical order of filename. # This (through the magic of overriding Test::Unit::TestCase.inherited) will # also be the execution order. # Dir.entries( "test" ).sort.each do |ent| next if( ent == "." || ent == ".." ) next unless ent =~ /^\d+_\w+.rb/ puts( "Loading test/#{ent}..." ) require "test/#{ent}" end # # Build the test suite by adding in the tests, in the order that they # were loaded. Note that what we're adding is the classes rather than # the filenames, so we use the global $p4ruby_test_list to access the # Test::Unit::TestCase subclasses. # class TestSuite def self.suite suite = Test::Unit::TestSuite.new( "P4Ruby Tests" ) $p4ruby_test_list.each do |t| suite << t.suite end suite end end # # Now run them # Test::Unit::UI::Console::TestRunner.run( TestSuite, 3 )