Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/cucumber/formatter/message.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# frozen_string_literal: true

require 'cucumber/formatter/io'
require 'cucumber/formatter/message_builder'
require 'cucumber/query'

module Cucumber
module Formatter
# The formatter used for <tt>--format message</tt>
class Message < MessageBuilder
class Message
include Io

def initialize(config)
@io = ensure_io(config.out_stream, config.error_stream)
super(config)
@repository = Cucumber::Repository.new
@query = Cucumber::Query.new(@repository)
config.on_event :envelope, &method(:output_envelope)
end

def output_envelope(envelope)
def output_envelope(event)
envelope = event.envelope
@repository.update(envelope)
@io.write(envelope.to_json)
@io.write("\n")
Expand Down
38 changes: 13 additions & 25 deletions lib/cucumber/formatter/message_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require 'base64'
require 'cucumber/formatter/backtrace_filter'

require 'cucumber/query'

module Cucumber
module Formatter
class MessageBuilder
Expand All @@ -14,7 +12,6 @@ class MessageBuilder
def initialize(config)
@config = config
@repository = Cucumber::Repository.new
@query = Cucumber::Query.new(@repository)

@test_run_started_id = config.id_generator.new_id

Expand All @@ -27,15 +24,13 @@ def initialize(config)
@step_match_arguments_by_test_step_id = {}

# Ensure all handlers for events occur after all ivars are instantiated
config.on_event :envelope, &method(:on_envelope)

config.on_event :gherkin_source_parsed, &method(:on_gherkin_source_parsed)
config.on_event :gherkin_source_read, &method(:on_gherkin_source_read)

config.on_event :hook_test_step_created, &method(:on_hook_test_step_created)

config.on_event :step_activated, &method(:on_step_activated)
config.on_event :step_definition_registered, &method(:on_step_definition_registered)

config.on_event :test_case_created, &method(:on_test_case_created)
config.on_event :test_case_ready, &method(:on_test_case_ready)
Expand Down Expand Up @@ -84,15 +79,11 @@ def attach(src, media_type, filename)
end

message = Cucumber::Messages::Envelope.new(attachment: Cucumber::Messages::Attachment.new(**attachment_data))
output_envelope(message)
@config.event_bus.envelope(message)
end

private

def on_envelope(event)
output_envelope(event.envelope)
end

def on_gherkin_source_parsed(_event)
# TODO: Handle GherkinSourceParsed
end
Expand All @@ -106,7 +97,7 @@ def on_gherkin_source_read(event)
)
)

output_envelope(message)
@config.event_bus.envelope(message)
end

def on_hook_test_step_created(event)
Expand All @@ -118,10 +109,6 @@ def on_step_activated(event)
@step_match_arguments_by_test_step_id[event.test_step.id] = event.step_match.step_arguments
end

def on_step_definition_registered(event)
output_envelope(event.step_definition.to_envelope)
end

def on_test_case_created(event)
@pickle_id_by_test_case_id[event.test_case.id] = event.pickle.id
end
Expand All @@ -139,7 +126,7 @@ def on_test_case_ready(event)
# TODO: This may be a redundant update. But for now we're leaving this in whilst we're in the transitory phase
@repository.update(message)

output_envelope(message)
@config.event_bus.envelope(message)
end

def on_test_case_started(event)
Expand All @@ -164,7 +151,8 @@ def on_test_case_started(event)
)
)

output_envelope(message)
@config.event_bus.envelope(message)
@repository.update(message)
end

def on_test_case_finished(event)
Expand All @@ -187,7 +175,7 @@ def on_test_case_finished(event)
)
)

output_envelope(message)
@config.event_bus.envelope(message)
end

def on_test_run_started(*)
Expand All @@ -198,7 +186,7 @@ def on_test_run_started(*)
)
)

output_envelope(message)
@config.event_bus.envelope(message)
end

def on_test_run_finished(event)
Expand All @@ -210,7 +198,7 @@ def on_test_run_finished(event)
)
)

output_envelope(message)
@config.event_bus.envelope(message)
end

def on_test_run_hook_started(event)
Expand All @@ -225,7 +213,7 @@ def on_test_run_hook_started(event)
)
)

output_envelope(message)
@config.event_bus.envelope(message)
end

def on_test_run_hook_finished(event)
Expand All @@ -249,7 +237,7 @@ def on_test_run_hook_finished(event)
)
)

output_envelope(message)
@config.event_bus.envelope(message)
end

def on_test_step_created(event)
Expand Down Expand Up @@ -278,7 +266,7 @@ def on_test_step_started(event)
)
)

output_envelope(message)
@config.event_bus.envelope(message)
end

def on_test_step_finished(event)
Expand Down Expand Up @@ -316,7 +304,7 @@ def on_test_step_finished(event)
)
)

output_envelope(message)
@config.event_bus.envelope(message)
end

def on_undefined_parameter_type(event)
Expand All @@ -327,7 +315,7 @@ def on_undefined_parameter_type(event)
)
)

output_envelope(message)
@config.event_bus.envelope(message)
end

def test_step_to_message(step)
Expand Down
14 changes: 10 additions & 4 deletions lib/cucumber/formatter/rerun.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
# frozen_string_literal: true

require 'cucumber/formatter/io'
require 'cucumber/formatter/message_builder'
require 'cucumber/query'

module Cucumber
module Formatter
class Rerun < MessageBuilder
class Rerun
include Io

def initialize(config)
@config = config
@io = ensure_io(config.out_stream, config.error_stream)
super(config)
@repository = Cucumber::Repository.new
@query = Cucumber::Query.new(@repository)
config.on_event :envelope, &method(:output_envelope)
end

def output_envelope(envelope)
def output_envelope(event)
envelope = event.envelope
@repository.update(envelope)
finish_report if envelope.test_run_finished
end
Expand Down
1 change: 1 addition & 0 deletions lib/cucumber/glue/registry_and_more.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def register_rb_step_definition(string_or_regexp, proc_or_sym, options)
step_definition = StepDefinition.new(@configuration.id_generator.new_id, self, string_or_regexp, proc_or_sym, options)
@step_definitions << step_definition
@configuration.notify :step_definition_registered, step_definition
@configuration.notify :envelope, step_definition.to_envelope
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this out into a separate PR targeting the v11 migration branch

step_definition
rescue Cucumber::CucumberExpressions::UndefinedParameterTypeError => e
# TODO: add a way to extract the parameter type directly from the error.
Expand Down
6 changes: 5 additions & 1 deletion lib/cucumber/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,16 @@ def set_encoding
def report
return @report if @report

reports = [summary_report, global_hooks_summary_report] + formatters
reports = [message_builder, summary_report, global_hooks_summary_report] + formatters
reports << fail_fast_report if @configuration.fail_fast?
reports << publish_banner_printer unless @configuration.publish_quiet?
@report ||= Formatter::Fanout.new(reports)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I start to think that what we see here is technical debt from the time before we started to use event i CucumberRuby. Then the formatters had a lot of methods beforeScenario, afterScenario, beforeStep etc. The Fanout was utility to "iterate" over all the formatters and call these methods, so the "runtime" called beforeScenario on the Fanout which resulted in that beforeScenario was called on each formatter.

Now when we are using events what is important is that the right formatters are created. When they are created they register for the events they are interested in on the event bus (they get the Configuration object which has the event bus passed to the initialize method). Then the get event from the event bus and do their job.

So the remaining purpose of the code here (which is call from line 77 self.visitor = report) is to create the right formatter. Summary_report we always want to create as the runtime uses it to determine the exit code. Now we always want to create the message builder so that is can listen to event and create message, send the to the event bus so that message users can get all messages from the event bus. Then we shall create the formatters specified by the cli options etc.

end

def message_builder
@message_builder ||= Formatter::MessageBuilder.new(@configuration)
end

def summary_report
@summary_report ||= Core::Report::Summary.new(@configuration.event_bus)
end
Expand Down
6 changes: 6 additions & 0 deletions spec/cucumber/formatter/rerun_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module Formatter
end

described_class.new(config)
MessageBuilder.new(config)
execute [gherkin], [StandardStepActions.new, Filters::BroadcastTestRunStartedEvent.new(config), Filters::BroadcastTestCaseReadyEvent.new(config)], config.event_bus
config.event_bus.test_run_finished

Expand Down Expand Up @@ -70,6 +71,7 @@ module Formatter
end

described_class.new(config)
MessageBuilder.new(config)
execute [foo, bar], [StandardStepActions.new, Filters::BroadcastTestRunStartedEvent.new(config), Filters::BroadcastTestCaseReadyEvent.new(config)], config.event_bus
config.event_bus.test_run_finished

Expand All @@ -88,6 +90,7 @@ module Formatter
end

described_class.new(config)
MessageBuilder.new(config)
execute [gherkin], [StandardStepActions.new, Filters::BroadcastTestRunStartedEvent.new(config), Filters::BroadcastTestCaseReadyEvent.new(config)], config.event_bus
config.event_bus.test_run_finished

Expand All @@ -107,6 +110,7 @@ module Formatter
end

described_class.new(config)
MessageBuilder.new(config)
execute [gherkin], [FakeObjects::FlakyStepActions.new, Filters::BroadcastTestRunStartedEvent.new(config), Filters::BroadcastTestCaseReadyEvent.new(config)], config.event_bus

config.event_bus.test_run_finished
Expand All @@ -128,6 +132,7 @@ module Formatter
end

described_class.new(config)
MessageBuilder.new(config)
execute [gherkin], [FakeObjects::FlakyStepActions.new, Filters::BroadcastTestRunStartedEvent.new(config), Filters::BroadcastTestCaseReadyEvent.new(config)], config.event_bus
config.event_bus.test_run_finished

Expand All @@ -144,6 +149,7 @@ module Formatter
end

described_class.new(config)
MessageBuilder.new(config)
execute [gherkin, gherkin], [StandardStepActions.new, Filters::BroadcastTestRunStartedEvent.new(config), Filters::BroadcastTestCaseReadyEvent.new(config)], config.event_bus
config.event_bus.test_run_finished

Expand Down
2 changes: 1 addition & 1 deletion spec/cucumber/formatter/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module SpecHelper

def run_defined_feature
define_steps
actual_runtime.visitor = Fanout.new([@formatter])
actual_runtime.visitor = Fanout.new([actual_runtime.send(:message_builder), @formatter])
receiver = Test::Runner.new(event_bus)

event_bus.gherkin_source_read(gherkin_doc.uri, gherkin_doc.body)
Expand Down
Loading