diff --git a/lib/cucumber/formatter/event_stream.rb b/lib/cucumber/formatter/event_stream.rb index 6c2d1ae33d..0cdc8a4f7a 100644 --- a/lib/cucumber/formatter/event_stream.rb +++ b/lib/cucumber/formatter/event_stream.rb @@ -1,3 +1,6 @@ +require 'securerandom' +require 'date' + module Cucumber module Formatter class EventStream @@ -5,6 +8,7 @@ class EventStream def initialize(config) @config = config + @series_id = SecureRandom.uuid set_up_event_handlers end @@ -18,134 +22,33 @@ def emit(event_data) @config.out_stream.puts(event_data.to_json) end + def ms_since_epoch + Integer(DateTime.now.strftime('%Q')) + end + def set_up_event_handlers on(:test_run_started) do emit({ - event: "TestRunStarted", - protocol_version: PROTOCOL_VERSION + type: "start", + timestamp: ms_since_epoch, + series: @series_id }) end on(:gherkin_source_read) do |event| emit({ - event: "GherkinSourceRead", - id: "#{event.path}:1", - source: event.source - }) - end - - on(:step_match) do - emit({ - event: "StepDefinitionMatched", - }) - end - - on(:test_case_starting) do |event| - emit({ - event: "TestCaseStarted", - id: event.test_case.location - }) - end - - on(:test_step_starting) do |event| - emit({ - event: "TestStepStarted", - id: event.test_step.location - }) - end - - on(:test_step_finished) do |test_step, result| - event_builder = TestStepResultEvent.new(@config) - result.describe_to event_builder, test_step - emit(event_builder) - end - - on(:test_case_finished) do |test_case, result| - event_builder = TestCaseResultEvent.new(@config) - result.describe_to event_builder, test_case - emit(event_builder) - end - - on(:test_run_finished) do - emit({ - event: "TestRunFinished" + type: "source", + timestamp: ms_since_epoch, + series: @series_id, + uri: event.path, # TODO: should be uri + data: event.source, + media: { + encoding: "utf-8", + type: "text/vnd.cucumber.gherkin+plain" + } }) end end - - class TestStepResultEvent - def initialize(config) - @config = config - @data = {} - end - - def passed(test_step) - data[:event] = "TestStepPassed" - data[:id] = test_step.location - end - - def failed(test_step) - data[:event] = "TestStepPassed" - data[:id] = test_step.location - end - - def duration(duration, test_step) - data[:duration] = duration.nanoseconds - end - - def exception(exception, test_case) - data[:error_summary] = exception.message - data[:error_detail] = exception.backtrace - end - - def to_json - data.to_json - end - - private - - attr_reader :data - private :data - - def emit(event_data) - @config.out_stream.puts(event_data.to_json) - end - end - - class TestCaseResultEvent - def initialize(config) - @config = config - @data = {} - end - - def passed(test_step) - data[:event] = "TestCasePassed" - data[:id] = test_step.location - end - - def failed(test_step) - data[:event] = "TestCasePassed" - data[:id] = test_step.location - end - - def duration(duration, test_step) - data[:duration] = duration.nanoseconds - end - - def to_json - data.to_json - end - - private - - attr_reader :data - private :data - - def emit(event_data) - @config.out_stream.puts(event_data.to_json) - end - end - end end end