Path: blob/trunk/rb/lib/selenium/webdriver/common/platform.rb
1865 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['TRAVIS']54:travis55elsif ENV['JENKINS']56:jenkins57elsif ENV['APPVEYOR']58:appveyor59elsif ENV['GITHUB_ACTIONS']60:github61end62end6364def jruby?65engine == :jruby66end6768def truffleruby?69engine == :truffleruby70end7172def ruby_version73RUBY_VERSION74end7576def windows?77os == :windows78end7980def mac?81os == :macosx82end8384def linux?85os == :linux86end8788def unix?89os == :unix90end9192def wsl?93return false unless linux?9495File.read('/proc/version').downcase.include?('microsoft')96rescue Errno::EACCES97# the file cannot be accessed on Linux on DeX98false99end100101def cygwin?102RUBY_PLATFORM.include?('cygwin')103end104105def null_device106File::NULL107end108109def wrap_in_quotes_if_necessary(str)110windows? && !cygwin? ? %("#{str}") : str111end112113def cygwin_path(path, only_cygwin: false, **opts)114return path if only_cygwin && !cygwin?115116flags = []117opts.each { |k, v| flags << "--#{k}" if v }118119`cygpath #{flags.join ' '} "#{path}"`.strip120end121122def unix_path(path)123path.tr(File::ALT_SEPARATOR, File::SEPARATOR)124end125126def windows_path(path)127path.tr(File::SEPARATOR, File::ALT_SEPARATOR)128end129130def make_writable(file)131File.chmod 0o766, file132end133134def assert_file(path)135return if File.file? path136137raise Error::WebDriverError, "not a file: #{path.inspect}"138end139140def assert_executable(path)141assert_file(path)142143return if File.executable? path144145raise Error::WebDriverError, "not executable: #{path.inspect}"146end147148def exit_hook149pid = Process.pid150151at_exit { yield if Process.pid == pid }152end153154def localhost155info = Socket.getaddrinfo 'localhost', 80, Socket::AF_INET, Socket::SOCK_STREAM156157return info[0][3] unless info.empty?158159raise Error::WebDriverError, "unable to translate 'localhost' for TCP + IPv4"160end161162def ip163orig = Socket.do_not_reverse_lookup164Socket.do_not_reverse_lookup = true165166begin167UDPSocket.open do |s|168s.connect '8.8.8.8', 53169return s.addr.last170end171ensure172Socket.do_not_reverse_lookup = orig173end174rescue Errno::ENETUNREACH, Errno::EHOSTUNREACH175# no external ip176end177178def interfaces179interfaces = Socket.getaddrinfo('localhost', 8080).map { |e| e[3] }180interfaces += ['0.0.0.0', Platform.ip]181182interfaces.compact.uniq183end184end # Platform185end # WebDriver186end # Selenium187188189