Skip to content

Commit

Permalink
Get control of driver/server registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed May 30, 2020
1 parent 3842679 commit 6fcd43a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
9 changes: 5 additions & 4 deletions lib/capybara.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'xpath'
require 'forwardable'
require 'capybara/config'
require 'capybara/registration_container'

module Capybara
class CapybaraError < StandardError; end
Expand Down Expand Up @@ -126,7 +127,7 @@ def configure
# @yieldreturn [Capybara::Driver::Base] A Capybara driver instance
#
def register_driver(name, &block)
drivers[name] = block
drivers.send(:register, name, block)
end

##
Expand All @@ -145,7 +146,7 @@ def register_driver(name, &block)
# @yieldparam host The host/ip to bind to
#
def register_server(name, &block)
servers[name.to_sym] = block
servers.send(:register, name.to_sym, block)
end

##
Expand Down Expand Up @@ -199,11 +200,11 @@ def modify_selector(name, &block)
end

def drivers
@drivers ||= {}
@drivers ||= RegistrationContainer.new
end

def servers
@servers ||= {}
@servers ||= RegistrationContainer.new
end

# Wraps the given string, which should contain an HTML document or fragment
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/cucumber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
Before do |scenario|
scenario.source_tag_names.each do |tag|
driver_name = tag.sub(/^@/, '').to_sym
Capybara.current_driver = driver_name if Capybara.drivers.key?(driver_name)
Capybara.current_driver = driver_name if Capybara.drivers[driver_name]
end
end
44 changes: 44 additions & 0 deletions lib/capybara/registration_container.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

module Capybara
# @api private
class RegistrationContainer
def names
@registered.keys
end

def [](name)
@registered[name]
end

def []=(name, value)
warn 'DEPRECATED: Directly setting drivers/servers is deprecated, please use Capybara.register_driver/register_server instead'
@registered[name] = value
end

def method_missing(method_name, *args, **options, &block)
if @registered.respond_to?(method_name)
warn "DEPRECATED: Calling '#{method_name}' on the drivers/servers container is deprecated without replacement"
# RUBY 2.6 will send an empty hash rather than nothing with **options so fix that
return @registered.public_send(method_name, *args, &block) if options.empty?

return @registered.public_send(method_name, *args, **options, &block)
end
super
end

def respond_to_missing?(method_name, include_private = false)
@registered.respond_to?(method_name) || super
end

private

def initialize
@registered = {}
end

def register(name, block)
@registered[name] = block
end
end
end
4 changes: 2 additions & 2 deletions lib/capybara/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ def initialize(mode, app = nil)

def driver
@driver ||= begin
unless Capybara.drivers.key?(mode)
other_drivers = Capybara.drivers.keys.map(&:inspect)
unless Capybara.drivers[mode]
other_drivers = Capybara.drivers.names.map(&:inspect)
raise Capybara::DriverNotFoundError, "no driver called #{mode.inspect} was found, available drivers: #{other_drivers.join(', ')}"
end
driver = Capybara.drivers[mode].call(app)
Expand Down
2 changes: 1 addition & 1 deletion spec/capybara_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# do nothing
end

expect(described_class.servers).to have_key(:blob)
expect(described_class.servers[:blob]).to be_truthy
end
end

Expand Down

0 comments on commit 6fcd43a

Please sign in to comment.