Skip to content

Commit

Permalink
rb - reduce module, class, method, line lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Jun 16, 2016
1 parent 4d140b5 commit 2f048df
Show file tree
Hide file tree
Showing 26 changed files with 307 additions and 322 deletions.
46 changes: 40 additions & 6 deletions rb/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 2.0

Expand All @@ -20,6 +18,9 @@ Style/PercentLiteralDelimiters:
'%i': '[]'
'%w': '[]'

Style/RegexpLiteral:
EnforcedStyle: mixed

# Consider documenting all top-level classes and modules
Style/Documentation:
Enabled: false
Expand All @@ -43,10 +44,43 @@ Style/MutableConstant:
Style/MethodName:
Enabled: false

# Refactor Chrome::Bridge#create_capabilities
# TODO: Refactor Chrome::Bridge#create_capabilities
Metrics/PerceivedComplexity:
Max: 16
Max: 11
Exclude:
- 'lib/selenium/webdriver/chrome/bridge.rb'

# Refactor Chrome::Bridge#create_capabilities
Metrics/CyclomaticComplexity:
Max: 16
Max: 9
Exclude:
- 'lib/selenium/webdriver/chrome/bridge.rb'
- 'lib/selenium/webdriver/common/driver.rb'
- 'spec/integration/selenium/webdriver/spec_support/test_environment.rb'

Metrics/ClassLength:
Max: 140
Exclude:
- 'lib/selenium/webdriver/firefox/profile.rb'
- 'lib/selenium/webdriver/remote/bridge.rb'
- 'lib/selenium/webdriver/remote/capabilities.rb'
- 'lib/selenium/webdriver/remote/w3c_bridge.rb'
- 'spec/integration/selenium/webdriver/spec_support/test_environment.rb'

Metrics/AbcSize:
Max: 33
Exclude:
- 'lib/selenium/webdriver/chrome/bridge.rb'
- 'lib/selenium/webdriver/support/color.rb'

Metrics/LineLength:
Max: 130

Metrics/ModuleLength:
Exclude:
- 'lib/selenium/webdriver/common/platform.rb'
- 'spec/**/*'

Metrics/MethodLength:
Max: 33
Exclude:
- 'lib/selenium/webdriver/chrome/bridge.rb'
32 changes: 0 additions & 32 deletions rb/.rubocop_todo.yml

This file was deleted.

5 changes: 3 additions & 2 deletions rb/lib/selenium/webdriver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ def self.root
# WebDriver.for :firefox, :profile => Profile.new
# WebDriver.for :remote, :url => "http://localhost:4444/wd/hub", :desired_capabilities => caps
#
# One special argument is not passed on to the bridges, :listener. You can pass a listener for this option
# to get notified of WebDriver events. The passed object must respond to #call or implement the methods from AbstractEventListener.
# One special argument is not passed on to the bridges, :listener.
# You can pass a listener for this option to get notified of WebDriver events.
# The passed object must respond to #call or implement the methods from AbstractEventListener.
#
# @see Selenium::WebDriver::Support::AbstractEventListener
#
Expand Down
6 changes: 5 additions & 1 deletion rb/lib/selenium/webdriver/chrome.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
module Selenium
module WebDriver
module Chrome
MISSING_TEXT = 'Unable to find the chromedriver executable. Please download the server from http://chromedriver.storage.googleapis.com/index.html and place it somewhere on your PATH. More info at https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver.'.freeze
MISSING_TEXT = <<-ERROR.tr("\n", '').freeze
Unable to find chromedriver. Please download the server from
http://chromedriver.storage.googleapis.com/index.html and place it
somewhere on your PATH. More info at https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver.
ERROR

def self.driver_path=(path)
Platform.assert_executable path
Expand Down
87 changes: 33 additions & 54 deletions rb/lib/selenium/webdriver/common/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,53 +32,40 @@ def home
end

def engine
@engine ||= (
if defined? RUBY_ENGINE
RUBY_ENGINE.to_sym
else
:ruby
end
)
@engine ||= defined?(RUBY_ENGINE) ? RUBY_ENGINE.to_sym : :ruby
end

def os
@os ||= (
host_os = RbConfig::CONFIG['host_os']
case host_os
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
:windows
when /darwin|mac os/
:macosx
when /linux/
:linux
when /solaris|bsd/
:unix
else
raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
end
)
host_os = RbConfig::CONFIG['host_os']
@os ||= case host_os
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
:windows
when /darwin|mac os/
:macosx
when /linux/
:linux
when /solaris|bsd/
:unix
else
raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
end
end

def ci
if ENV['TRAVIS']
:travis
elsif ENV['JENKINS']
:jenkins
end
return :travis if ENV['TRAVIS']
:jenkins if ENV['JENKINS']
end

def bitsize
@bitsize ||= (
if defined?(FFI::Platform::ADDRESS_SIZE)
FFI::Platform::ADDRESS_SIZE
elsif defined?(FFI)
FFI.type_size(:pointer) == 4 ? 32 : 64
elsif jruby?
Integer(ENV_JAVA['sun.arch.data.model'])
else
1.size == 4 ? 32 : 64
end
)
@bitsize ||= if defined?(FFI::Platform::ADDRESS_SIZE)
FFI::Platform::ADDRESS_SIZE
elsif defined?(FFI)
FFI.type_size(:pointer) == 4 ? 32 : 64
elsif jruby?
Integer(ENV_JAVA['sun.arch.data.model'])
else
1.size == 4 ? 32 : 64
end
end

def jruby?
Expand Down Expand Up @@ -111,13 +98,11 @@ def cygwin?
end

def null_device
@null_device ||= (
if defined?(File::NULL)
File::NULL
else
Platform.windows? ? 'NUL' : '/dev/null'
end
)
@null_device ||= if defined?(File::NULL)
File::NULL
else
Platform.windows? ? 'NUL' : '/dev/null'
end
end

def wrap_in_quotes_if_necessary(str)
Expand Down Expand Up @@ -150,14 +135,11 @@ def assert_executable(path)
def exit_hook
pid = Process.pid

at_exit do
yield if Process.pid == pid
end
at_exit { yield if Process.pid == pid }
end

def find_binary(*binary_names)
paths = ENV['PATH'].split(File::PATH_SEPARATOR)
binary_names.map! { |n| "#{n}.exe" } if windows?

binary_names.each do |binary_name|
paths.each do |path|
Expand Down Expand Up @@ -191,11 +173,8 @@ def find_in_program_files(*binary_names)
def localhost
info = Socket.getaddrinfo 'localhost', 80, Socket::AF_INET, Socket::SOCK_STREAM

if info.empty?
raise Error::WebDriverError, "unable to translate 'localhost' for TCP + IPv4"
end

info[0][3]
return info[0][3] unless info.empty?
raise Error::WebDriverError, "unable to translate 'localhost' for TCP + IPv4"
end

def ip
Expand Down
6 changes: 5 additions & 1 deletion rb/lib/selenium/webdriver/edge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
module Selenium
module WebDriver
module Edge
MISSING_TEXT = 'Unable to find MicrosoftWebDriver. Please download the server from https://www.microsoft.com/en-us/download/details.aspx?id=48212. More info at https://github.com/SeleniumHQ/selenium/wiki/MicrosoftWebDriver.'.freeze
MISSING_TEXT = <<-ERROR.tr("\n", '').freeze
Unable to find MicrosoftWebDriver. Please download the server from
https://www.microsoft.com/en-us/download/details.aspx?id=48212 and place it
somewhere on your PATH. More info at https://github.com/SeleniumHQ/selenium/wiki/MicrosoftWebDriver.
ERROR

def self.driver_path=(path)
Platform.assert_executable path
Expand Down
14 changes: 9 additions & 5 deletions rb/lib/selenium/webdriver/firefox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@
module Selenium
module WebDriver
module Firefox
DEFAULT_PORT = 7055
DEFAULT_ENABLE_NATIVE_EVENTS = Platform.os == :windows
DEFAULT_SECURE_SSL = false
DEFAULT_PORT = 7055
DEFAULT_ENABLE_NATIVE_EVENTS = Platform.os == :windows
DEFAULT_SECURE_SSL = false
DEFAULT_ASSUME_UNTRUSTED_ISSUER = true
DEFAULT_LOAD_NO_FOCUS_LIB = false
DEFAULT_LOAD_NO_FOCUS_LIB = false

MISSING_TEXT = 'Unable to find Mozilla geckodriver. Please download the executable from https://github.com/mozilla/geckodriver/releases'.freeze
MISSING_TEXT = <<-ERROR.tr("\n", '').freeze
Unable to find Mozilla geckodriver. Please download the server from
https://github.com/mozilla/geckodriver/releases and place it
somewhere on your PATH. More info at https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver.
ERROR

def self.driver_path=(path)
Platform.assert_executable path
Expand Down
42 changes: 16 additions & 26 deletions rb/lib/selenium/webdriver/firefox/binary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ module Firefox
# @api private
class Binary
NO_FOCUS_LIBRARY_NAME = 'x_ignore_nofocus.so'.freeze
NO_FOCUS_LIBRARIES = [
["#{WebDriver.root}/selenium/webdriver/firefox/native/linux/amd64/#{NO_FOCUS_LIBRARY_NAME}", "amd64/#{NO_FOCUS_LIBRARY_NAME}"],
["#{WebDriver.root}/selenium/webdriver/firefox/native/linux/x86/#{NO_FOCUS_LIBRARY_NAME}", "x86/#{NO_FOCUS_LIBRARY_NAME}"]
NO_FOCUS_LIBRARIES = [
["#{WebDriver.root}/selenium/webdriver/firefox/native/linux/amd64/#{NO_FOCUS_LIBRARY_NAME}",
"amd64/#{NO_FOCUS_LIBRARY_NAME}"],
["#{WebDriver.root}/selenium/webdriver/firefox/native/linux/x86/#{NO_FOCUS_LIBRARY_NAME}",
"x86/#{NO_FOCUS_LIBRARY_NAME}"]
].freeze

WAIT_TIMEOUT = 90
Expand All @@ -38,18 +40,17 @@ def start_with(profile, profile_path, *args)
profile_path = profile_path.tr('/', '\\')
end

ENV['XRE_CONSOLE_LOG'] = profile.log_file if profile.log_file
ENV['XRE_PROFILE_PATH'] = profile_path
ENV['MOZ_NO_REMOTE'] = '1' # able to launch multiple instances
ENV['XRE_CONSOLE_LOG'] = profile.log_file if profile.log_file
ENV['XRE_PROFILE_PATH'] = profile_path
ENV['MOZ_NO_REMOTE'] = '1' # able to launch multiple instances
ENV['MOZ_CRASHREPORTER_DISABLE'] = '1' # disable breakpad
ENV['NO_EM_RESTART'] = '1' # prevent the binary from detaching from the console
ENV['NO_EM_RESTART'] = '1' # prevent the binary from detaching from the console

if Platform.linux? && (profile.native_events? || profile.load_no_focus_lib?)
modify_link_library_path profile_path
end

execute(*args)
cope_with_mac_strangeness(args) if Platform.mac?
end

def quit
Expand Down Expand Up @@ -80,21 +81,6 @@ def execute(*extra_args)
@process.start
end

def cope_with_mac_strangeness(args)
sleep 0.3

if @process.crashed?
# ok, trying a restart
sleep 7
execute(*args)
end

# ensure we're ok
sleep 0.3
return unless @process.crashed?
raise Error::WebDriverError, "unable to start Firefox cleanly, args: #{args.inspect}"
end

def modify_link_library_path(profile_path)
paths = []

Expand All @@ -109,7 +95,7 @@ def modify_link_library_path(profile_path)
paths += ENV['LD_LIBRARY_PATH'].to_s.split(File::PATH_SEPARATOR)

ENV['LD_LIBRARY_PATH'] = paths.uniq.join(File::PATH_SEPARATOR)
ENV['LD_PRELOAD'] = NO_FOCUS_LIBRARY_NAME
ENV['LD_PRELOAD'] = NO_FOCUS_LIBRARY_NAME
end

class << self
Expand Down Expand Up @@ -143,7 +129,9 @@ def path
@path = Platform.cygwin_path(@path) if Platform.cygwin?

unless File.file?(@path.to_s)
raise Error::WebDriverError, "Could not find Firefox binary (os=#{Platform.os}). Make sure Firefox is installed or set the path manually with #{self}.path="
error = "Could not find Firefox binary (os=#{Platform.os}). "
error << "Make sure Firefox is installed or set the path manually with #{self}.path="
raise Error::WebDriverError, error
end

@path
Expand All @@ -165,7 +153,9 @@ def version
private

def windows_path
windows_registry_path || Platform.find_in_program_files('\\Mozilla Firefox\\firefox.exe') || Platform.find_binary('firefox')
windows_registry_path ||
Platform.find_in_program_files('\\Mozilla Firefox\\firefox.exe') ||
Platform.find_binary('firefox')
end

def macosx_path
Expand Down
3 changes: 2 additions & 1 deletion rb/lib/selenium/webdriver/firefox/launcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def connect_until_stable

return if poller.connected?
@binary.quit
raise Error::WebDriverError, "unable to obtain stable firefox connection in #{STABLE_CONNECTION_TIMEOUT} seconds (#{@host}:#{@port})"
error = "unable to obtain stable firefox connection in #{STABLE_CONNECTION_TIMEOUT} seconds (#{@host}:#{@port})"
raise Error::WebDriverError, error
end

def fetch_profile
Expand Down
3 changes: 2 additions & 1 deletion rb/lib/selenium/webdriver/firefox/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def initialize(model = nil)
@native_events = model_prefs.delete(WEBDRIVER_PREFS[:native_events]) == 'true'
@secure_ssl = model_prefs.delete(WEBDRIVER_PREFS[:untrusted_certs]) != 'true'
@untrusted_issuer = model_prefs.delete(WEBDRIVER_PREFS[:untrusted_issuer]) == 'true'
@load_no_focus_lib = model_prefs.delete(WEBDRIVER_PREFS[:load_no_focus_lib]) == 'true' # not stored in profile atm, so will always be false.
# not stored in profile atm, so will always be false.
@load_no_focus_lib = model_prefs.delete(WEBDRIVER_PREFS[:load_no_focus_lib]) == 'true'
@additional_prefs = model_prefs
end

Expand Down
Loading

0 comments on commit 2f048df

Please sign in to comment.