Module: Webhookdb::SpecHelpers::Citest

Defined in:
lib/webhookdb/spec_helpers/citest.rb

Defined Under Namespace

Classes: RSpecResult

Constant Summary collapse

INTEGRATION_TESTS_DIR =
Pathname(__FILE__).dirname.parent.parent.parent + "integration"

Class Method Summary collapse

Class Method Details

.parse_rspec_html(output) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/webhookdb/spec_helpers/citest.rb', line 39

def self.parse_rspec_html(output)
  result = RSpecResult.new
  html = []
  output.lines.each do |line|
    next if line.strip.start_with?("Run options") || line.strip.starts_with?("All examples were")
    html << line
    captures = line.match(/innerHTML = "(\d+ examples?), (\d+ failures?)(, )?(\d+ pending)?/)&.captures
    next unless captures
    result.examples = captures[0].to_i
    result.failures = captures[1].to_i
    result.pending = captures[3].to_i
  end
  result.html = html.join
  return result
end

.put_results(html, key: "integration") ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/webhookdb/spec_helpers/citest.rb', line 55

def self.put_results(html, key: "integration")
  now = Time.now
  key = "test-results/#{key}/#{now.year}/#{now.month}/#{now.in_time_zone('UTC').iso8601}.html"
  doc = Webhookdb::DatabaseDocument.create(
    key:,
    content: html,
    content_type: "text/html",
  )
  url = doc.presigned_view_url(expire_at: 1.week.from_now)
  return url
end

.result_to_payload(result, html_url, prefix: "Integration Tests") ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/webhookdb/spec_helpers/citest.rb', line 67

def self.result_to_payload(result, html_url, prefix: "Integration Tests")
  color = "good"
  color = "warning" if result.pending.nonzero?
  color = "danger" if result.failures.nonzero?

  return {
    text: "#{prefix}: #{result.examples} examples, #{result.failures} failures, #{result.pending} pending",
    attachments: [
      {
        color:,
        fallback: "View results at #{html_url}",
        actions: [
          {
            type: "button",
            text: "View Results 🔎",
            url: html_url,
          },
        ],
      },
    ],
  }
end

.run_tests(folders) ⇒ Object

Run RSpec against the given folders, create a DatabaseDocument for the html results, and POST to Slack about it.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/webhookdb/spec_helpers/citest.rb', line 11

def self.run_tests(folders)
  out = StringIO.new
  err = StringIO.new
  folders = [folders] unless folders.respond_to?(:to_ary)
  args = folders.map { |f| "#{f}/" }
  args << "--format"
  args << "html"
  RSpec::Core::Runner.run(args, err, out)

  notifier = Webhookdb::Slack.new_notifier(
    force_channel: "#webhookdb-notifications",
    username: "CI Tests",
    icon_emoji: ":female-detective:",
  )
  outstring = out.string
  result = Webhookdb::SpecHelpers::Citest.parse_rspec_html(outstring)
  unless result.ok?
    msg = "Errored or unparseable output running #{folders.join(', ')} tests:" \
          "\nerror: #{err.string}\nout: #{outstring}"
    notifier.post text: msg
    return
  end

  url = self.put_results(result.html)
  payload = self.result_to_payload(result, url)
  notifier.post(payload)
end