Module: Kensho

Defined in:
lib/kensho/rspec/state.rb,
lib/kensho/rspec.rb,
lib/kensho/_schema.rb,
lib/kensho/rspec/helpers.rb,
lib/kensho/rspec/helpers.rb,
lib/kensho/rspec/version.rb,
lib/kensho/rspec/formatter.rb

Overview

Hook into RSpec’s example-group definition so we can track the currently-defining group. RSpec calls ‘subclass` for every nested describe; we wrap it to push/pop a stack on the State module.

Defined Under Namespace

Modules: RSpec, Schema

Class Method Summary collapse

Class Method Details

.apply_behavior(key, value) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/kensho/rspec/helpers.rb', line 140

def self.apply_behavior(key, value)
  group = Kensho::RSpec::State.current_group
  return unless group

  group.[key] = value.to_s
  nil
end

.attach(path, kind: nil, name: nil, mime_type: nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/kensho/rspec/helpers.rb', line 65

def attach(path, kind: nil, name: nil, mime_type: nil)
  scratch = Kensho::RSpec::State.current
  formatter = Kensho::RSpec::State.formatter
  return nil unless scratch && formatter

  record = formatter.register_attachment(
    scratch,
    path.to_s,
    kind: kind,
    name: name,
    mime_type: mime_type
  )
  return nil unless record

  if scratch.step_stack.last
    (scratch.step_stack.last['attachments'] ||= []) << record
  else
    scratch.attachments << record
  end
  record
end

.current_case_idObject



106
107
108
109
# File 'lib/kensho/rspec/helpers.rb', line 106

def current_case_id
  scratch = Kensho::RSpec::State.current
  scratch ? scratch.case_id : nil
end

.Epic(name) ⇒ Object Also known as: epic



132
133
134
# File 'lib/kensho/rspec/helpers.rb', line 132

def self.Epic(name)
  apply_behavior(:kensho_epic, name)
end

.Feature(name) ⇒ Object Also known as: feature

Top-level sugar so spec authors can write ‘Kensho::Feature(’Cart’)‘ etc. inside a describe block. These are module methods (uppercase method names are legal in Ruby) that walk the binding to find the example group whose `describe` block is currently executing and set metadata on it.



128
129
130
# File 'lib/kensho/rspec/helpers.rb', line 128

def self.Feature(name)
  apply_behavior(:kensho_feature, name)
end

.label(key, value) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/kensho/rspec/helpers.rb', line 87

def label(key, value)
  scratch = Kensho::RSpec::State.current
  return if scratch.nil? || key.nil? || key.to_s.empty?

  scratch.labels[key.to_s] = value.to_s
  nil
end


95
96
97
98
99
100
101
102
103
104
# File 'lib/kensho/rspec/helpers.rb', line 95

def link(url, kind: nil, label: nil)
  scratch = Kensho::RSpec::State.current
  return if scratch.nil? || url.nil? || url.to_s.empty?

  entry = { 'url' => url.to_s }
  entry['kind']  = kind.to_s  if kind
  entry['label'] = label.to_s if label
  scratch.links << entry
  nil
end

.step(title, action: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kensho/rspec/helpers.rb', line 23

def step(title, action: nil)
  scratch = Kensho::RSpec::State.current
  unless scratch
    return yield({}) if block_given?

    return {}
  end

  started_perf = monotonic_now
  step_obj = {
    'id'        => "step_#{SecureRandom.hex(5)}",
    'title'     => title.to_s,
    'status'    => 'pass',
    'startedAt' => Kensho::Schema.iso_now,
    'duration'  => 0,
    '_started_perf' => started_perf
  }
  step_obj['action'] = action.to_s if action

  parent = scratch.step_stack.last
  if parent
    (parent['children'] ||= []) << step_obj
  else
    scratch.steps << step_obj
  end
  scratch.step_stack << step_obj

  result = nil
  begin
    result = block_given? ? yield(step_obj) : nil
  rescue StandardError, ::RSpec::Expectations::ExpectationNotMetError => e
    step_obj['status'] = 'fail'
    close_step!(step_obj, started_perf)
    scratch.step_stack.pop if scratch.step_stack.last.equal?(step_obj)
    raise e
  end

  close_step!(step_obj, started_perf)
  scratch.step_stack.pop if scratch.step_stack.last.equal?(step_obj)
  result
end

.Story(name) ⇒ Object Also known as: story



136
137
138
# File 'lib/kensho/rspec/helpers.rb', line 136

def self.Story(name)
  apply_behavior(:kensho_story, name)
end