#!/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 RUBY_VERSION.split('.')[1] > '8' require 'rubygems' gem 'test-unit' end 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} -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. # 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 # if( RbConfig::CONFIG[ 'MAJOR' ].to_i == 1 && RbConfig::CONFIG[ 'MINOR' ].to_i <= 8 ) exit( Test::Unit::UI::Console::TestRunner.run( TestSuite, 3 ).passed? ) else exit( Test::Unit::UI::Console::TestRunner.run( TestSuite, :output_level => 3 ).passed? ) end