Path: blob/trunk/rb/lib/selenium/webdriver/common/platform.rb
3987 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819require 'rbconfig'20require 'socket'2122module Selenium23module WebDriver24# @api private25module Platform26module_function2728def home29@home ||= Dir.home30end3132def engine33@engine ||= RUBY_ENGINE.to_sym34end3536def os37host_os = RbConfig::CONFIG['host_os']38@os ||= case host_os39when /mswin|msys|mingw|cygwin|bccwin|wince|emc/40:windows41when /darwin|mac os/42:macosx43when /linux/44:linux45when /solaris|bsd/46:unix47else48raise Error::WebDriverError, "unknown os: #{host_os.inspect}"49end50end5152def ci53if ENV['JENKINS']54:jenkins55elsif ENV['APPVEYOR']56:appveyor57elsif ENV['GITHUB_ACTIONS']58:github59end60end6162def jruby?63engine == :jruby64end6566def truffleruby?67engine == :truffleruby68end6970def ruby_version71RUBY_VERSION72end7374def windows?75os == :windows76end7778def mac?79os == :macosx80end8182def linux?83os == :linux84end8586def unix?87os == :unix88end8990def wsl?91return false unless linux?9293File.read('/proc/version').downcase.include?('microsoft')94rescue Errno::EACCES95# the file cannot be accessed on Linux on DeX96false97end9899def cygwin?100RUBY_PLATFORM.include?('cygwin')101end102103def null_device104File::NULL105end106107def wrap_in_quotes_if_necessary(str)108windows? && !cygwin? ? %("#{str}") : str109end110111def cygwin_path(path, only_cygwin: false, **opts)112return path if only_cygwin && !cygwin?113114flags = []115opts.each { |k, v| flags << "--#{k}" if v }116117`cygpath #{flags.join ' '} "#{path}"`.strip118end119120def unix_path(path)121path.tr(File::ALT_SEPARATOR, File::SEPARATOR)122end123124def windows_path(path)125path.tr(File::SEPARATOR, File::ALT_SEPARATOR)126end127128def make_writable(file)129File.chmod 0o766, file130end131132def assert_file(path)133return if File.file? path134135raise Error::WebDriverError, "not a file: #{path.inspect}"136end137138def assert_executable(path)139assert_file(path)140141return if File.executable? path142143raise Error::WebDriverError, "not executable: #{path.inspect}"144end145146def exit_hook147pid = Process.pid148149at_exit { yield if Process.pid == pid }150end151152def localhost153info = Socket.getaddrinfo 'localhost', 80, Socket::AF_INET, Socket::SOCK_STREAM154155return info[0][3] unless info.empty?156157raise Error::WebDriverError, "unable to translate 'localhost' for TCP + IPv4"158end159160def ip161orig = Socket.do_not_reverse_lookup162Socket.do_not_reverse_lookup = true163164begin165UDPSocket.open do |s|166s.connect '8.8.8.8', 53167return s.addr.last168end169ensure170Socket.do_not_reverse_lookup = orig171end172rescue Errno::ENETUNREACH, Errno::EHOSTUNREACH173# no external ip174end175176def interfaces177interfaces = Socket.getaddrinfo('localhost', 8080).map { |e| e[3] }178interfaces += ['0.0.0.0', Platform.ip]179180interfaces.compact.uniq181end182end # Platform183end # WebDriver184end # Selenium185186187