lib/lopata/observers/web_logger.rb in lopata-0.1.5 vs lib/lopata/observers/web_logger.rb in lopata-0.1.6
- old
+ new
@@ -2,54 +2,54 @@
require 'json'
require_relative 'backtrace_formatter'
module Lopata
module Observers
+ # @private
class WebLogger < BaseObserver
def started(world)
- raise "Build number is not initailzed in Lopata::Config" unless Lopata::Config.build_number
- @client = Lopata::Client.new(Lopata::Config.build_number)
+ @client = Lopata::Client.new
@client.start(world.scenarios.count)
+ @finished = 0
end
def scenario_finished(scenario)
- @client.add_attempt(scenario)
+ @finished += 1
+ @client.add_attempt(scenario, @finished)
end
-
- # def example_pending(notification)
- # example = notification.example
- # @client.add_attempt(example, Lopata::PENDING, example.execution_result.pending_message)
- # end
end
end
PASSED = 0
FAILED = 1
PENDING = 2
SKIPPED = 5
+ # @private
class Client
include HTTParty
- base_uri Lopata::Config.lopata_host
- attr_accessor :build_number
+ attr_reader :url, :project_code, :build_number
- def initialize(build_number)
- @build_number = build_number
+ def initialize
+ params = Lopata.configuration.web_logging_params
+ raise "Web logging is not initailzed" unless params
+ @url = HTTParty.normalize_base_uri(params[:url])
+ @project_code = params[:project_code]
+ @build_number = params[:build_number]
end
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)
+ def add_attempt(scenario, finished)
status = scenario.failed? ? Lopata::FAILED : Lopata::PASSED
- steps = scenario.steps_in_running_order.map { |s| step_hash(s) }
- request = { status: status, steps: steps }
+ steps = scenario.steps.map { |s| step_hash(s) }
+ request = { status: status, steps: steps, launch: { id: @launch_id, finished: finished } }
test = test_id(scenario)
post("/tests/#{test}/attempts.json", body: request)
- inc_finished
end
def step_hash(step)
hash = { status: step.status, title: step.title }
if step.failed?
@@ -80,35 +80,28 @@
to_rerun + get_json("/projects/#{project_code}/builds/#{build_number}/failures.json")
end
private
- def get_json(url)
- JSON.parse(self.class.get(url).body)
+ def get_json(path)
+ JSON.parse(self.class.get(path, base_uri: url).body)
end
def post(*args)
- self.class.post(*args)
+ self.class.post(*with_base_uri(args))
end
def patch(*args)
- self.class.patch(*args)
+ self.class.patch(*with_base_uri(args))
end
- def inc_finished
- @finished ||= 0
- @finished += 1
- response = patch("/launches/#{@launch_id}",
- body: { finished: @finished }.to_json,
- headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' })
- if response.code == 404
- puts 'Launch has been cancelled. Exit.'
- exit!
+ def with_base_uri(args = [])
+ if args.last.is_a? Hash
+ args.last[:base_uri] = url
+ else
+ args << { base_uri: url }
end
- end
-
- def project_code
- Lopata::Config.lopata_code
+ args
end
def error_message_for(step)
if step.exception
backtrace_formatter.error_message(step.exception)
\ No newline at end of file