#!/usr/bin/ruby #------------------------------------------------------------------------------- # Copyright (c) 2001-2008, Perforce Software, Inc. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE # SOFTWARE, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH # DAMAGE. #------------------------------------------------------------------------------- $:.unshift( "." ) $:.unshift( "./lib" ) require "fileutils.rb" require "rbconfig" require "P4" require "getoptlong" if RbConfig MAJOR_VERSION = RbConfig::CONFIG[ 'MAJOR' ].to_i MINOR_VERSION = RbConfig::CONFIG[ 'MINOR' ].to_i if MAJOR_VERSION == 1 require 'test/unit' if MINOR_VERSION <= 8 require 'test/unit/ui/console/testrunner' end elsif MAJOR_VERSION == 2 require 'minitest/unit' require 'minitest/autorun' 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} -C1 -L log -vserver=3 -i" end # # Cleanup the test tree # def cleanup_test_tree() # Go back to where we started, or we can't remove the tree. Dir.chdir( @@STARTDIR ) FileUtils.rm_rf( server_root ) true 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 def enable_unicode cmd = "#{@@P4D} -r #{server_root} -C1 -L log -vserver=3 -xi" # Using IO.popen stops the output from polluting the test # output. IO.popen( cmd ) { |p| p.read } end private 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. # tests = Array.new Dir.entries( "test" ).sort.each do |ent| next if( ent == "." || ent == ".." ) next unless ent =~ /^\d+_\w+.rb/ tests.push( "test/#{ent}" ) end # # Now run them # if( MAJOR_VERSION == 1 && MINOR_VERSION >= 8 ) runner = Test::Unit::AutoRunner.new( false, '.', tests ) exit runner.run end