require 'fileutils' require 'ostruct' require 'P4' #------------------------------------------------------------------------------ # Configuration #------------------------------------------------------------------------------ # Make sure everything Sinatra is in test mode ENV['RACK_ENV'] = 'test' ENABLE_GIT_FUSION = ENV['ENABLE_GIT_FUSION'] || false P4PORT = ENV['P4PORT'] || 'localhost:1666' TMP_FOLDER = '/tmp/clients' DELETE_CLIENT = !ENV.key?('KEEP_WORKSPACES') CHARSET = ENV['P4CHARSET'] || 'auto' JDOE_USER = 'jdoe' JDOE_PASSWORD = 'johndoe1A!' SUPER_USER = 'super' SUPER_PASSWORD = 'superuser1A!' # This is fairly special configuration for using rack-test # Most apps are tricky to test via mocking. We load the full stack. require 'rspec' require 'rack/test' CONFIG_RU = File.absolute_path('../../config.ru', __FILE__) HELIX_WEB_SERVICES_APP = Rack::Builder.parse_file(CONFIG_RU).first require 'hws_settings' HWSSettings.system_handle.WORKSPACE_DIR = TMP_FOLDER HWSSettings.system_handle.ENABLE_GIT_FUSION = ENABLE_GIT_FUSION #------------------------------------------------------------------------------ # Helpers #------------------------------------------------------------------------------ # These methods allow you to create temporary clients, etc. for tests that # work with lower-level (i.e. service) objects # # For most methods that interact with the HTTP API, you won't need these. def ticket_for_jdoe @webauth_for_jdoe ||= create_webauth_for_jdoe header 'X-Perforce-Helix-Web-Services-P4PASSWD', p4ticket_for_jdoe header 'X-Perforce-Helix-Web-Services-P4PORT', 'localhost:1666' @webauth_for_jdoe end def p4ticket_for_jdoe @ticket_for_jdoe ||= create_ticket_for_jdoe end def create_webauth_for_jdoe post '/projects/v1/login', {:user => JDOE_USER, :password => JDOE_PASSWORD} last_response.body end def create_ticket_for_jdoe p4 = P4.new # Make P4Ruby only raise exceptions if there are errors. Warnings # (such as 'no such file(s)' don't get the same treatment. p4.exception_level = P4::RAISE_ERRORS p4.port = P4PORT p4.user = JDOE_USER p4.password = JDOE_PASSWORD p4.charset = CHARSET unless CHARSET.nil? || CHARSET == 'none' p4.connect return Auth.ticket_from_login(p4) ensure p4.disconnect end def ticket_for_super @webauth_for_super ||= create_webauth_for_super @ticket_for_super ||= create_ticket_for_super header 'X-Perforce-Helix-Web-Services-P4PASSWD', @ticket_for_super @webauth_for_super end def create_webauth_for_super post '/projects/v1/login', {:user => SUPER_USER, :password => SUPER_PASSWORD} last_response.body end def create_ticket_for_super p4 = P4.new # Make P4Ruby only raise exceptions if there are errors. Warnings # (such as 'no such file(s)' don't get the same treatment. p4.exception_level = P4::RAISE_ERRORS p4.port = P4PORT p4.user = SUPER_USER p4.password = SUPER_PASSWORD p4.charset = CHARSET unless CHARSET.nil? || CHARSET == 'none' p4.connect return Auth.ticket_from_login(p4) ensure p4.disconnect end def p4_as_jdoe p4 = P4.new # Make P4Ruby only raise exceptions if there are errors. Warnings # (such as 'no such file(s)' don't get the same treatment. p4.exception_level = P4::RAISE_ERRORS p4.port = P4PORT p4.user = JDOE_USER p4.password = JDOE_PASSWORD p4.charset = CHARSET unless CHARSET.nil? || CHARSET == 'none' p4.connect results = p4.run_login('-p') p4.password = results.first yield p4 if block_given? return p4 ensure p4.disconnect if block_given? end def temp_client_as_jdoe(&block) p4_as_jdoe do |p4| unless Dir.exists?(TMP_FOLDER) FileUtils.makedirs(TMP_FOLDER) end name = (0...8).map { (65 + rand(26)).chr }.join root = File.join(TMP_FOLDER, name) Dir.mkdir(root) p4.client = name spec = p4.fetch_client spec._root = root spec._client = name spec._description = 'test client' # When there's one depot, the view generated maps //depot/... //view/... # instead of //depot/... //view/depot/... like it will with multiple depots. if (spec._view.length == 1) spec._view = ["//depot/... //#{name}/depot/..."] end p4.save_client(spec) p4.run_sync('//...') block.call(p4, root) if DELETE_CLIENT p4.run_client('-d', name) FileUtils.rmtree(root) end end end def temp_stream_client_as_jdoe(&block) p4_as_jdoe do |p4| name = (0...8).map { (65 + rand(26)).chr }.join # Create a stream in the stream-test depot stream_spec = p4.fetch_stream("//stream-test/main-#{name}") stream_spec._type = 'mainline' stream_spec._parent = 'none' p4.save_stream(stream_spec) unless Dir.exists?(TMP_FOLDER) FileUtils.makedirs(TMP_FOLDER) end root = File.join(TMP_FOLDER, name) Dir.mkdir(root) p4.client = name spec = p4.fetch_client spec._root = root spec._client = name spec._description = 'test client' spec._view = nil spec._stream = "//stream-test/main-#{name}" p4.save_client(spec) p4.run_sync('//...') block.call(p4, root, "//stream-test/main-#{name}") if DELETE_CLIENT p4.run_client('-d', name) FileUtils.rmtree(root) end end end