Class: Rpruby::RSpec::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rpruby/rspec/formatter.rb

Constant Summary collapse

MAX_DESCRIPTION_LENGTH =
255
MIN_DESCRIPTION_LENGTH =
3

Instance Method Summary collapse

Constructor Details

#initialize(_output) ⇒ Formatter

Returns a new instance of Formatter.



19
20
21
# File 'lib/rpruby/rspec/formatter.rb', line 19

def initialize(_output)
  ENV['REPORT_PORTAL_USED'] = 'true'
end

Instance Method Details

#example_failed(notification) ⇒ Object



87
88
89
90
91
92
# File 'lib/rpruby/rspec/formatter.rb', line 87

def example_failed(notification)
  exception = notification.exception
  Rpruby.send_log(:failed, %(#{exception.class}: #{exception.message}\n\nStacktrace: #{notification.formatted_backtrace.join("\n")}), Rpruby.now)
  Rpruby.finish_item(Rpruby.current_scenario, :failed) unless Rpruby.current_scenario.nil?
  Rpruby.current_scenario = nil
end

#example_group_finished(_group_notification) ⇒ Object



53
54
55
56
57
58
# File 'lib/rpruby/rspec/formatter.rb', line 53

def example_group_finished(_group_notification)
  unless @current_group_node.nil?
    Rpruby.finish_item(@current_group_node.content)
    @current_group_node = @current_group_node.parent
  end
end

#example_group_started(group_notification) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rpruby/rspec/formatter.rb', line 30

def example_group_started(group_notification)
  description = group_notification.group.description
  if description.size < MIN_DESCRIPTION_LENGTH
    p "Group description should be at least #{MIN_DESCRIPTION_LENGTH} characters ('group_notification': #{group_notification.inspect})"
    return
  end
  item = Rpruby::TestItem.new(name: description[0..MAX_DESCRIPTION_LENGTH - 1],
                                    type: :TEST,
                                    id: nil,
                                    start_time: Rpruby.now,
                                    description: '',
                                    closed: false,
                                    tags: [])
  group_node = Tree::TreeNode.new(SecureRandom.hex, item)
  if group_node.nil?
    p "Group node is nil for item #{item.inspect}"
  else
    @current_group_node << group_node unless @current_group_node.nil? # make @current_group_node parent of group_node
    @current_group_node = group_node
    group_node.content.id = ReportPortal.start_item(group_node)
  end
end

#example_passed(_notification) ⇒ Object



82
83
84
85
# File 'lib/rpruby/rspec/formatter.rb', line 82

def example_passed(_notification)
  Rpruby.finish_item(Rpruby.current_scenario, :passed) unless Rpruby.current_scenario.nil?
  Rpruby.current_scenario = nil
end

#example_pending(_notification) ⇒ Object



94
95
96
97
# File 'lib/rpruby/rspec/formatter.rb', line 94

def example_pending(_notification)
  Rpruby.finish_item(Rpruby.current_scenario, :skipped) unless Rpruby.current_scenario.nil?
  Rpruby.current_scenario = nil
end

#example_started(notification) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rpruby/rspec/formatter.rb', line 60

def example_started(notification)
  description = notification.example.description
  if description.size < MIN_DESCRIPTION_LENGTH
    p "Example description should be at least #{MIN_DESCRIPTION_LENGTH} characters ('notification': #{notification.inspect})"
    return
  end
  Rpruby.current_scenario = Rpruby::TestItem.new(name: description[0..MAX_DESCRIPTION_LENGTH - 1],
                                                             type: :STEP,
                                                             id: nil,
                                                             start_time: Rpruby.now,
                                                             description: '',
                                                             closed: false,
                                                             tags: [])
  example_node = Tree::TreeNode.new(SecureRandom.hex, Rpruby.current_scenario)
  if example_node.nil?
    p "Example node is nil for scenario #{Rpruby.current_scenario.inspect}"
  else
    @current_group_node << example_node
    example_node.content.id = Rpruby.start_item(example_node)
  end
end

#message(notification) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/rpruby/rspec/formatter.rb', line 99

def message(notification)
  if notification.message.respond_to?(:read)
    Rpruby.send_file(:passed, notification.message)
  else
    Rpruby.send_log(:passed, notification.message, Rpruby.now)
  end
end

#start(_start_notification) ⇒ Object



23
24
25
26
27
28
# File 'lib/rpruby/rspec/formatter.rb', line 23

def start(_start_notification)
  cmd_args = ARGV.map { |arg| arg.include?('rp_uuid=') ? 'rp_uuid=[FILTERED]' : arg }.join(' ')
  Rpruby.start_launch(cmd_args)
  @root_node = Tree::TreeNode.new(SecureRandom.hex)
  @current_group_node = @root_node
end

#stop(_notification) ⇒ Object



107
108
109
# File 'lib/rpruby/rspec/formatter.rb', line 107

def stop(_notification)
  Rpruby.finish_launch
end