testlib.rb #1

  • //
  • p15-1/
  • guest/
  • perforce_software/
  • p4ruby/
  • p15-1/
  • test/
  • testlib.rb
  • View
  • Commits
  • Open Download .zip Download (6 KB)
# vim:ts=2:sw=2:et:
#-------------------------------------------------------------------------------
# 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.
#-------------------------------------------------------------------------------

require 'fileutils.rb'
require 'rbconfig'
require 'P4'
require 'getoptlong'
require 'test/unit'

# 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 = ENV['P4D_BIN'] || 'p4d'

  attr_accessor :p4

  #
  # Common setup for tests
  #
  def setup
    super
    # The P4CONFIG value is often set by developers, we shouldn't depend on it
    ENV.delete('P4CONFIG')
    ENV.delete('P4ENVIRO')
    ENV['P4ENVIRO'] = enviro_file
    create_workspace_tree
    Dir.chdir(client_root)
    ENV.delete('PWD')
    init_client
  end

  #
  # Common cleanup
  #
  def teardown
    @p4.disconnect if @p4.connected?
    @p4 = nil
    # Go back to where we started, or we can't remove the tree.
    Dir.chdir(@@STARTDIR)
    unless ENV['P4RUBY_TEST_NOCLEANUP']
      FileUtils.rm_rf(server_root)
      FileUtils.rm_rf(client_root)
      File.unlink(enviro_file) if File.exist?(enviro_file)
      begin
        Dir.rmdir(test_root) if File.directory?(test_root)
        Dir.rmdir(@@ROOTDIR) if File.directory?(@@ROOTDIR)
      rescue Errno::ENOTEMPTY
      end
    end
    true
  end

  #
  # Set up the client workspace for the test
  #
  def create_workspace_tree
    init_client
    FileUtils.mkdir_p(server_root) unless File.directory?(server_root)
    FileUtils.mkdir_p(client_root) unless File.directory?(client_root)
    create_p4config_file
    create_enviro_file
    true
  end

  def init_client
    # Create a P4 object for a test
    @p4 = P4.new
    @p4.charset = nil # Disable auto-detection?
    # If P4RUBY_TEST_PORT is defined, use that instead of rsh
    if ENV.key?('P4RUBY_TEST_PORT')
      @p4.port = ENV['P4RUBY_TEST_PORT']
      puts "using test port: #{@p4.port}"
    else
      @p4.port = %(rsh:#{@@P4D} -r "#{server_root}" -C1 -L log -vserver=3 -vrpc=5 -i)
    end
    @p4.client = name.downcase.gsub(' ', '-')
  end

  # Create the Perforce client for a test
  def create_client
    @p4.connect unless @p4.connected?
    spec = @p4.fetch_client
    spec._root = client_root
    @p4.save_client(spec)
  end

  #
  # Return the path to this test's root
  #
  def test_root
    [@@STARTDIR, @@ROOTDIR, name].join('/')
  end

  def enviro_file
    File.expand_path(File.join(test_root, 'p4enviro'))
  end

  #
  # Return the path of the server root
  #
  def server_root
    File.expand_path('server', test_root)
  end

  #
  # Return the client root
  #
  def client_root
    [test_root, 'workspace'].join('/')
  end

  def enable_unicode
    # On windows, using the -r command to do an upgrade just seems to return
    # an error of 'unexpected arguments'
    #cmd = "#{@@P4D} -r '#{server_root}' -C1 -L log -vserver=3 -xi"
    start_dir = Dir.pwd
    Dir.chdir(server_root)
    cmd = "#{@@P4D} -C1 -L log -vserver=3 -xi"
    # Using IO.popen stops the output from polluting the test
    # output.
    IO.popen(cmd) { |p| p.read }
    #system(cmd)
    Dir.chdir(start_dir)
  end

  #
  # Common method for adding some test files to work with
  #
  def add_sample_content
    Dir.mkdir('test_files')
    %w(foo bar baz).each do
    |fn|
      fn = "test_files/#{fn}.txt"
      File.open(fn, 'w') do
      |f|
        f.puts('This is a test file')
      end
      p4.run_add(fn)
    end
    change = p4.fetch_change
    change._description = 'Test files'
    p4.run_submit(change)
    true
  end

  # Note: when security level changes, it's likely that nothing will happen
  # until you change password
  def configure_security(level)
    results = p4.run_configure('set', "security=#{level}")
    puts "results #{results}"
    true
  end

  private

  def create_p4config_file
    return unless ENV.key?('P4CONFIG')
    File.open(client_root + '/' + ENV['P4CONFIG'], 'w') do
    |f|
      f.puts("P4PORT=#{@p4.port}")
    end
  end

  def create_enviro_file
    return unless ENV.key?('P4ENVIRO')
    File.open(ENV['P4ENVIRO'], 'w+') do |_f|
    end
  end
end
# Change User Description Committed
#1 16214 perforce_software Move the p15-1 line.
//guest/perforce_software/p4rub/p15-1/test/testlib.rb
#1 16213 perforce_software Move files to proper depot path to view files correctly from project page.
//guest/perforce_software/p4ruby/p15.1/test/testlib.rb
#1 14756 tjuricek p15.1 branch of p4ruby.
//guest/perforce_software/p4ruby/main/test/testlib.rb
#6 14719 tjuricek Mirroring the current state of the workshop.

From this point, we will try to rebuild the history of p4-ruby in the workshop from this codeline.
#5 14685 tjuricek First merge into main of the p4ruby-check project, used to validate the p4ruby gem (without any access to source).

This does require using p4util:

   bundle exec p4util download p4d

See tools/test_p4ruby_gem.sh for the exact process used to validate the gem.

Imported from Git
 Author: Tristan Juricek <[email protected]> 1415838551 -0800
 Committer: Tristan Juricek <[email protected]> 1415838551 -0800
 sha1: bc41f30d8c51a652defeee59f438d1446091ee61
 push-state: complete
 parent-changes: 97f4a83740ecd006070c19775bd9a3abba2f9804=[946222]/1256d8281e6fbfe9f2019fd3756d0b4730beb5c5=[960938]
#4 14684 Git Fusion Git Fusion branch management

Imported from Git
 ghost-of-change-num: 946222
 ghost-of-sha1: 97f4a83740ecd006070c19775bd9a3abba2f9804
 ghost-precedes-sha1: bc41f30d8c51a652defeee59f438d1446091ee61
 push-state: incomplete
#3 14682 Git Fusion Git Fusion branch management

Imported from Git
 ghost-of-change-num: 960958
 ghost-of-sha1: 005052ae424bd69f426f7209e741ca1c8c3253c7
 ghost-precedes-sha1: ad052c71a568ef12165e143a6866ad9ceffbb4a1
 parent-branch: None@960958
 push-state: incomplete
#2 14679 tony Fix p4ruby test run so it also includes the current directory in the
library search path. This helps us pick up the P4.so we're testing.

Also fixed some minor gripes and backed out a previous change that
obviously didn't work under 1.8
#1 14678 tony Add file missed in previous change