Module: Webhookdb::SpecHelpers::Citest

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

Defined Under Namespace

Classes: RSpecResult

Class Method Summary collapse

Class Method Details

.parse_rspec_html(output) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/webhookdb/spec_helpers/citest.rb', line 30

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(folder, html) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/webhookdb/spec_helpers/citest.rb', line 46

def self.put_results(folder, html)
  now = Time.now
  key = "test-results/#{folder}/#{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(folder, result, html_url) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/webhookdb/spec_helpers/citest.rb', line 58

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

  return {
    text: "Tests for #{folder}: #{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(folder) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/webhookdb/spec_helpers/citest.rb', line 7

def self.run_tests(folder)
  out = StringIO.new
  err = StringIO.new
  RSpec::Core::Runner.run([folder + "/", "--format", "html"], 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 #{folder} tests:\nerror: #{err.string}\nout: #{outstring}"
    notifier.post text: msg
    return
  end

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