Skip to content

Commit

Permalink
Merge pull request #930 from cucumber/core-event-bus
Browse files Browse the repository at this point in the history
Use core cucumber event bus implementation
  • Loading branch information
mvz authored May 31, 2024
2 parents 62e788a + 1179a73 commit 356c7d7
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 323 deletions.
1 change: 0 additions & 1 deletion Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ lib/aruba/cucumber/parameter_types.rb
lib/aruba/cucumber/testing_frameworks.rb
lib/aruba/errors.rb
lib/aruba/event_bus.rb
lib/aruba/event_bus/name_resolver.rb
lib/aruba/events.rb
lib/aruba/file_size.rb
lib/aruba/generators/script_file.rb
Expand Down
6 changes: 0 additions & 6 deletions lib/aruba/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,4 @@ class CommandNotFoundError < ArgumentError; end
# Raised if command was already started, otherwise aruba forgets about the
# previous pid and you've got hidden commands run
class CommandAlreadyStartedError < Error; end

# Raised if an event name cannot be resolved
class EventNameResolveError < StandardError; end

# Raised if given object is not an event
class NoEventError < StandardError; end
end
53 changes: 6 additions & 47 deletions lib/aruba/event_bus.rb
Original file line number Diff line number Diff line change
@@ -1,63 +1,22 @@
# frozen_string_literal: true

require "aruba/event_bus/name_resolver"
require "aruba/errors"
require "cucumber/core/event_bus"

module Aruba
# Event bus
#
# Implements and in-process pub-sub events broadcaster allowing multiple observers
# to subscribe to different events that fire as your tests are executed.
#
class EventBus
# Create EventBus
#
# @param [#transform] resolver
# A resolver which transforms Symbol, String, Class into an event Class.
def initialize(resolver)
@resolver = resolver
@handlers = Hash.new { |h, k| h[k] = [] }
end

# Register for an event
#
# @param [String, Symbol, Class, Array] event_ids
# If Array, register multiple events witht the same handler. If String,
# Symbol, Class register handler for given event.
#
# @param [#call] handler_object
# The handler object, needs to have method `#call`. Either
# `handler_object` or `block` can be defined. The handler object gets the
# event passed to `#call`.
#
# @yield
# Handler block which gets the event passed as parameter.
def register(event_ids, handler_object = nil, &handler_proc)
handler = handler_proc || handler_object

if handler.nil? || !handler.respond_to?(:call)
raise ArgumentError, "Please pass either an object#call or a handler block"
class EventBus < Cucumber::Core::EventBus
def register(ids, handler_object = nil, &handler_proc)
Array(ids).each do |event_id|
on(event_id, handler_object, &handler_proc)
end

Array(event_ids).flatten.each do |id|
@handlers[
@resolver.transform(id).to_s
] << handler
end

nil
end

# Broadcast an event
#
# @param [Object] event
# An object of registered event class. This object is passed to the event
# handler.
#
def notify(event)
raise NoEventError, "Please pass an event object, not a class" if event.is_a?(Class)

@handlers[event.class.to_s].each { |handler| handler.call(event) }
broadcast(event)
end
end
end
160 changes: 0 additions & 160 deletions lib/aruba/event_bus/name_resolver.rb

This file was deleted.

20 changes: 13 additions & 7 deletions lib/aruba/events.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "cucumber/core/event"

# Aruba
module Aruba
# Events
Expand All @@ -10,13 +12,7 @@ module Events
# inherited by normal events
#
# @private
class BasicEvent
attr_reader :entity

def initialize(entity)
@entity = entity
end
end
class BasicEvent < Cucumber::Core::Event.new(:entity); end

# Command was stopped
class CommandStopped < BasicEvent; end
Expand All @@ -38,5 +34,15 @@ class ChangedWorkingDirectory < BasicEvent; end

# The configuration was changed
class ChangedConfiguration < BasicEvent; end

def self.registry
[CommandStarted,
CommandStopped,
AddedEnvironmentVariable,
ChangedEnvironmentVariable,
DeletedEnvironmentVariable,
ChangedWorkingDirectory,
ChangedConfiguration].to_h { |klass| [klass.event_id, klass] }
end
end
end
2 changes: 1 addition & 1 deletion lib/aruba/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Runtime
attr_accessor :config, :environment, :logger, :command_monitor, :announcer, :event_bus

def initialize(opts = {})
@event_bus = EventBus.new(EventBus::NameResolver.new(Aruba::Events))
@event_bus = EventBus.new(Aruba::Events.registry)
@announcer = opts.fetch(:announcer, Aruba.platform.announcer.new)
@config = opts.fetch(:config,
ConfigWrapper.new(Aruba.config.make_copy, @event_bus))
Expand Down
71 changes: 0 additions & 71 deletions spec/aruba/event_bus/name_resolver_spec.rb

This file was deleted.

Loading

0 comments on commit 356c7d7

Please sign in to comment.