lib/lopata/observers/web_logger.rb in lopata-0.1.1 vs lib/lopata/observers/web_logger.rb in lopata-0.1.2
- old
+ new
@@ -1,7 +1,8 @@
require 'httparty'
require 'json'
+require_relative 'backtrace_formatter'
module Lopata
module Observers
class WebLogger < BaseObserver
def started(world)
@@ -9,50 +10,24 @@
@client = Lopata::Client.new(Lopata::Config.build_number)
@client.start(world.scenarios.count)
end
def scenario_finished(scenario)
- if scenario.failed?
- backtrace = backtrace_for(scenario)
- @client.add_attempt(scenario, Lopata::FAILED, error_message_for(scenario), backtrace)
- else
- @client.add_attempt(scenario, Lopata::PASSED)
- end
+ @client.add_attempt(scenario)
end
# def example_pending(notification)
# example = notification.example
# @client.add_attempt(example, Lopata::PENDING, example.execution_result.pending_message)
# end
-
- private
-
- def error_message_for(scenario)
- exception = scenario.steps.map(&:exception).compact.last
- msg = ''
- if exception
- msg << "#{exception.class.name}: " unless exception.class.name =~ /RSpec/
- msg << "#{exception.message.to_s}" if exception.message
- end
- (msg.length == 0) ? 'Empty message' : msg
- end
-
- def backtrace_for(scenario)
- exception = scenario.steps.map(&:exception).compact.last
- msg = ''
- if exception
- msg = exception.backtrace.join("\n")
- msg << "\n"
- end
- msg
- end
end
end
PASSED = 0
FAILED = 1
PENDING = 2
+ SKIPPED = 5
class Client
include HTTParty
base_uri Lopata::Config.lopata_host
@@ -64,19 +39,28 @@
def start(count)
@launch_id = JSON.parse(post("/projects/#{project_code}/builds/#{build_number}/launches.json", body: {total: count}).body)['id']
end
- def add_attempt(scenario, status, msg = nil, backtrace = nil)
+ def add_attempt(scenario)
+ status = scenario.failed? ? Lopata::FAILED : Lopata::PASSED
+ steps = scenario.steps_in_running_order.map { |s| step_hash(s) }
+ request = { status: status, steps: steps }
test = test_id(scenario)
- request = { status: status}
- request[:message] = msg if msg
- request[:backtrace] = backtrace if backtrace
post("/tests/#{test}/attempts.json", body: request)
inc_finished
end
+ def step_hash(step)
+ hash = { status: step.status, title: step.title }
+ if step.failed?
+ hash[:message] = error_message_for(step)
+ hash[:backtrace] = backtrace_for(step)
+ end
+ hash
+ end
+
def test_id(scenario)
request = {
test: {
project_code: project_code,
title: scenario.title,
@@ -122,8 +106,29 @@
end
end
def project_code
Lopata::Config.lopata_code
+ end
+
+ def error_message_for(step)
+ if step.exception
+ backtrace_formatter.error_message(step.exception)
+ else
+ 'Empty error message'
+ end
+ end
+
+ def backtrace_for(step)
+ msg = ''
+ if step.exception
+ msg = backtrace_formatter.format(step.exception.backtrace).join("\n")
+ msg << "\n"
+ end
+ msg
+ end
+
+ def backtrace_formatter
+ @backtrace_formatter ||= Lopata::Observers::BacktraceFormatter.new
end
end
end
\ No newline at end of file