Skip to content

Commit

Permalink
rb - Use a guard clause instead of wrapping the code inside a conditi…
Browse files Browse the repository at this point in the history
…onal expression
  • Loading branch information
titusfortner committed Jun 16, 2016
1 parent fed0476 commit 26ce4eb
Show file tree
Hide file tree
Showing 21 changed files with 78 additions and 126 deletions.
26 changes: 5 additions & 21 deletions rb/.rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-06-08 15:23:02 -0700 using RuboCop version 0.37.2.
# on 2016-06-08 15:50:57 -0700 using RuboCop version 0.37.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -11,7 +11,7 @@ Lint/AmbiguousRegexpLiteral:
Exclude:
- 'spec/integration/selenium/webdriver/firefox/marionette_spec.rb'

# Offense count: 13
# Offense count: 12
# Configuration parameters: AllowSafeAssignment.
Lint/AssignmentInCondition:
Exclude:
Expand Down Expand Up @@ -175,13 +175,12 @@ Style/Alias:
- 'lib/selenium/webdriver/support/color.rb'
- 'spec/integration/selenium/webdriver/spec_support/guards.rb'

# Offense count: 7
# Offense count: 6
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: with_first_parameter, with_fixed_indentation
Style/AlignParameters:
Exclude:
- 'lib/selenium/webdriver/common/keyboard.rb'
- 'lib/selenium/webdriver/remote/bridge.rb'
- 'spec/integration/selenium/webdriver/element_spec.rb'
- 'spec/integration/selenium/webdriver/spec_support/test_environment.rb'
Expand Down Expand Up @@ -373,15 +372,14 @@ Style/EmptyLinesAroundMethodBody:
Style/EmptyLinesAroundModuleBody:
Enabled: false

# Offense count: 30
# Offense count: 29
# Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment.
Style/ExtraSpacing:
Exclude:
- 'lib/selenium/webdriver/chrome/profile.rb'
- 'lib/selenium/webdriver/common/action_builder.rb'
- 'lib/selenium/webdriver/common/error.rb'
- 'lib/selenium/webdriver/common/keyboard.rb'
- 'lib/selenium/webdriver/common/log_entry.rb'
- 'lib/selenium/webdriver/common/mouse.rb'
- 'lib/selenium/webdriver/common/target_locator.rb'
Expand Down Expand Up @@ -418,28 +416,14 @@ Style/FormatString:
- 'lib/selenium/webdriver/safari/server.rb'
- 'lib/selenium/webdriver/support/color.rb'

# Offense count: 7
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'lib/selenium/server.rb'
- 'lib/selenium/webdriver/common/**/*'
- 'lib/selenium/webdriver/firefox/*'
- 'lib/selenium/webdriver/phantomjs/service.rb'
- 'lib/selenium/webdriver/remote/*'
- 'spec/integration/selenium/webdriver/spec_support/*'

# Offense count: 18
# Offense count: 14
# Cop supports --auto-correct.
# Configuration parameters: MaxLineLength.
Style/IfUnlessModifier:
Exclude:
- 'lib/selenium/server.rb'
- 'lib/selenium/webdriver/common/driver.rb'
- 'lib/selenium/webdriver/common/html5/shared_web_storage.rb'
- 'lib/selenium/webdriver/edge/legacy_support.rb'
- 'lib/selenium/webdriver/firefox/service.rb'
- 'lib/selenium/webdriver/phantomjs/service.rb'
- 'lib/selenium/webdriver/remote/bridge.rb'
- 'lib/selenium/webdriver/remote/response.rb'
- 'lib/selenium/webdriver/remote/w3c_bridge.rb'
Expand Down
10 changes: 4 additions & 6 deletions rb/lib/selenium/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,13 @@ def process
end

def poll_for_service
unless socket.connected?
raise Error, "remote server not launched in #{@timeout} seconds"
end
return if socket.connected?
raise Error, "remote server not launched in #{@timeout} seconds"
end

def poll_for_shutdown
unless socket.closed?
raise Error, "remote server not stopped in #{@timeout} seconds"
end
return if socket.closed?
raise Error, "remote server not stopped in #{@timeout} seconds"
end

def socket
Expand Down
5 changes: 2 additions & 3 deletions rb/lib/selenium/webdriver/common/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ def initialize(bridge)
@bridge = bridge

# TODO: refactor this away
unless bridge.driver_extensions.empty?
extend(*bridge.driver_extensions)
end
return if bridge.driver_extensions.empty?
extend(*bridge.driver_extensions)
end

def inspect
Expand Down
12 changes: 3 additions & 9 deletions rb/lib/selenium/webdriver/common/html5/shared_web_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,9 @@ def key?(key)
alias_method :has_key?, :key?

def fetch(key, &blk)
if self.key? key
return self[key]
end

if block_given?
yield key
else
raise KeyError, "missing key #{key.inspect}"
end
return self[key] if self.key? key
return yield(key) if block_given?
raise KeyError, "missing key #{key.inspect}"
end

def empty?
Expand Down
8 changes: 3 additions & 5 deletions rb/lib/selenium/webdriver/common/keyboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,10 @@ def release(key)
MODIFIERS = [:control, :shift, :alt, :command, :meta]

def assert_modifier(key)
unless MODIFIERS.include? key
raise ArgumentError,
"#{key.inspect} is not a modifier key, expected one of #{MODIFIERS.inspect}"
end
return if MODIFIERS.include? key
raise ArgumentError, "#{key.inspect} is not a modifier key, expected one of #{MODIFIERS.inspect}"
end

end # Keyboard
end # WebDriver
end # Selenium
end # Selenium
5 changes: 2 additions & 3 deletions rb/lib/selenium/webdriver/common/mouse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ def move_if_needed(element)
end

def assert_element(element)
unless element.kind_of? Element
raise TypeError, "expected #{Element}, got #{element.inspect}:#{element.class}"
end
return if element.kind_of? Element
raise TypeError, "expected #{Element}, got #{element.inspect}:#{element.class}"
end
end # Mouse
end # WebDriver
Expand Down
10 changes: 4 additions & 6 deletions rb/lib/selenium/webdriver/common/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,15 @@ def make_writable(file)
end

def assert_file(path)
unless File.file? path
raise Error::WebDriverError, "not a file: #{path.inspect}"
end
return if File.file? path
raise Error::WebDriverError, "not a file: #{path.inspect}"
end

def assert_executable(path)
assert_file(path)

unless File.executable? path
raise Error::WebDriverError, "not executable: #{path.inspect}"
end
return if File.executable? path
raise Error::WebDriverError, "not executable: #{path.inspect}"
end

def exit_hook(&blk)
Expand Down
5 changes: 2 additions & 3 deletions rb/lib/selenium/webdriver/common/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ def initialize(opts = {})
self.socks_username = opts.delete(:socks_username) if opts.has_key? :socks_username
self.socks_password = opts.delete(:socks_password) if opts.has_key? :socks_password

unless opts.empty?
raise ArgumentError, "unknown option#{'s' if opts.size != 1}: #{opts.inspect}"
end
return if opts.empty?
raise ArgumentError, "unknown option#{'s' if opts.size != 1}: #{opts.inspect}"
end

def ==(other)
Expand Down
5 changes: 2 additions & 3 deletions rb/lib/selenium/webdriver/common/socket_lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ def lock
sleep 0.1
end

unless did_lock?
raise Error::WebDriverError, "unable to bind to locking port #{@port} within #{@timeout} seconds"
end
return if did_lock?
raise Error::WebDriverError, "unable to bind to locking port #{@port} within #{@timeout} seconds"
end

def release
Expand Down
7 changes: 2 additions & 5 deletions rb/lib/selenium/webdriver/common/socket_poller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,8 @@ def listening?
begin
sock.connect_nonblock sockaddr
rescue Errno::EINPROGRESS
if IO.select(nil, [sock], nil, CONNECT_TIMEOUT)
retry
else
raise Errno::ECONNREFUSED
end
retry if IO.select(nil, [sock], nil, CONNECT_TIMEOUT)
raise Errno::ECONNREFUSED
rescue *CONNECTED_ERRORS
# yay!
end
Expand Down
5 changes: 2 additions & 3 deletions rb/lib/selenium/webdriver/common/touch_screen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ def coords_from(x, y)
end

def assert_element(element)
unless element.kind_of? Element
raise TypeError, "expected #{Element}, got #{element.inspect}:#{element.class}"
end
return if element.kind_of? Element
raise TypeError, "expected #{Element}, got #{element.inspect}:#{element.class}"
end

end # TouchScreen
Expand Down
5 changes: 2 additions & 3 deletions rb/lib/selenium/webdriver/firefox/binary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ def cope_with_mac_strangeness(args)

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

def modify_link_library_path(profile_path)
Expand Down
7 changes: 3 additions & 4 deletions rb/lib/selenium/webdriver/firefox/launcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ def start
def connect_until_stable
poller = SocketPoller.new(@host, @port, STABLE_CONNECTION_TIMEOUT)

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

def fetch_profile
Expand Down
5 changes: 2 additions & 3 deletions rb/lib/selenium/webdriver/firefox/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ def log_file=(file)
end

def add_webdriver_extension
unless @extensions.has_key?(:webdriver)
add_extension(WEBDRIVER_EXTENSION_PATH, :webdriver)
end
return if @extensions.has_key?(:webdriver)
add_extension(WEBDRIVER_EXTENSION_PATH, :webdriver)
end

#
Expand Down
5 changes: 2 additions & 3 deletions rb/lib/selenium/webdriver/firefox/profiles_ini.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ def parse
end
end

if p = path_for(name, is_relative, path)
@profile_paths[name] = p
end
return unless p = path_for(name, is_relative, path)
@profile_paths[name] = p
end

def path_for(name, is_relative, path)
Expand Down
5 changes: 2 additions & 3 deletions rb/lib/selenium/webdriver/firefox/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ def start_process

def stop_process
super
if Platform.windows? && !$DEBUG
@process.io.close rescue nil
end
return unless Platform.windows? && !$DEBUG
@process.io.close rescue nil
end

def stop_server
Expand Down
5 changes: 2 additions & 3 deletions rb/lib/selenium/webdriver/phantomjs/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ def start_process

def stop_process
super
if Platform.jruby? && !$DEBUG
@process.io.close rescue nil
end
return unless Platform.jruby? && !$DEBUG
@process.io.close rescue nil
end

def stop_server
Expand Down
53 changes: 25 additions & 28 deletions rb/lib/selenium/webdriver/remote/http/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ class Default < Common

def http
@http ||= (
http = new_http_client
if server_url.scheme == "https"
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
http = new_http_client
if server_url.scheme == "https"
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

if @timeout
http.open_timeout = @timeout
http.read_timeout = @timeout
end
if @timeout
http.open_timeout = @timeout
http.read_timeout = @timeout
end

http
http
)
end

Expand Down Expand Up @@ -76,11 +76,8 @@ def request(verb, url, headers, payload, redirects = 0)
retry

rescue Errno::ECONNREFUSED => ex
if use_proxy?
raise ex.class, "using proxy: #{proxy.http}"
else
raise
end
raise ex.class, "using proxy: #{proxy.http}" if use_proxy?
raise
end

if response.kind_of? Net::HTTPRedirection
Expand Down Expand Up @@ -124,13 +121,13 @@ def new_http_client

def proxy
@proxy ||= (
proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
no_proxy = ENV['no_proxy'] || ENV['NO_PROXY']
proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
no_proxy = ENV['no_proxy'] || ENV['NO_PROXY']

if proxy
proxy = "http://#{proxy}" unless proxy.start_with?("http://")
Proxy.new(http: proxy, no_proxy: no_proxy)
end
if proxy
proxy = "http://#{proxy}" unless proxy.start_with?("http://")
Proxy.new(http: proxy, no_proxy: no_proxy)
end
)
end

Expand All @@ -140,13 +137,13 @@ def use_proxy?
if proxy.no_proxy
ignored = proxy.no_proxy.split(",").any? do |host|
host == "*" ||
host == server_url.host || (
begin
IPAddr.new(host).include?(server_url.host)
rescue ArgumentError
false
end
)
host == server_url.host || (
begin
IPAddr.new(host).include?(server_url.host)
rescue ArgumentError
false
end
)

end

Expand Down
Loading

0 comments on commit 26ce4eb

Please sign in to comment.