require 'helix_web_services_client' require 'uri' require 'P4' #------------------------------------------------------------------------------ # Configuration #------------------------------------------------------------------------------ WS_URL = URI(ENV['WS_URL'] || 'http://localhost:9000/') # This is used to prepare a shelved changelist for the 'submit_change' method 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!' #------------------------------------------------------------------------------ # Helpers #------------------------------------------------------------------------------ def client_as_jdoe client = HelixWebServicesClient.new( url: WS_URL, user: 'jdoe', password: 'johndoe1A!', ssl: {verify: false} ) yield client ensure client.close if client end def client_as_super client = HelixWebServicesClient.new( url: WS_URL, user: 'super', password: 'superuser1A!', ssl: {verify: false} ) yield client ensure client.close if client 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.user = SUPER_USER p4.password = SUPER_PASSWORD results = p4.run_login('-p') p4.password = results.first p4.run_client('-d', '-f', 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.user = SUPER_USER p4.password = SUPER_PASSWORD results = p4.run_login('-p') p4.password = results.first p4.run_client('-d', '-f', name) FileUtils.rmtree(root) end end end