Class: Specular

Inherits:
Object
  • Object
show all
Defined in:
lib/specular.rb,
lib/specular/version.rb

Defined Under Namespace

Modules: Integration, Support Classes: Error

Constant Summary collapse

STATUS_LABEL =

Constants ============================================================

{
  failed: 'Failed 🛑',
  passed: 'Passed ✅',
  skipped: 'Skipped ⚠️'
}.freeze
MESSAGE_EVENTS =
%i[ default success failure ].freeze
MESSAGES =
{
  slack: {
    default: 'RSpec tests for {% if repo %}<{{repo}}|{{name}}>{% else %}{{name}}{% endif %} have {% if created_issues.github.url %}<{{created_issues.github.url}}|{{result}}>{% else %}{{result}}{% endif %}'
  },
  github: {
    default: 'RSpec test report ({{result}})'
  }
}
REF_DEFAULT =
'main'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results, **context) ⇒ Specular

Instance Methods =====================================================



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/specular.rb', line 44

def initialize(results, **context)
  @results = results
  @context = context

  if (@context[:repo] and !@context[:repo].end_with?('/'))
    @context[:repo] = '%s/' % @context[:repo]
  end

  @context[:result] = self.failures? ? :failed : :passed

  @context[:messages] = MESSAGES.keys.to_h do |type|
    [
      type,
      MESSAGE_EVENTS.to_h do |event|
        [
          event,
          @context.dig(:messages, type, event) || MESSAGES.dig(type, event)
        ]
      end
    ]
  end
end

Instance Attribute Details

#resultsObject (readonly)

Properties ===========================================================



38
39
40
# File 'lib/specular.rb', line 38

def results
  @results
end

Class Method Details

.versionObject



2
3
4
# File 'lib/specular/version.rb', line 2

def self.version
  File.readlines(File.expand_path('../../VERSION', __dir__)).first.chomp
end

Instance Method Details

#create_issue!(services = %i[ github ])) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/specular.rb', line 159

def create_issue!(services = %i[ github ])
  @context[:created_issues] = services.to_h do |service|
    [
      service,
      Specular::Integration.for(service, @context).create_issue!(
        title: self.message(service: service),
        body: self.report
      )
    ]
  end
end

#examplesObject



84
85
86
87
88
89
90
91
92
# File 'lib/specular.rb', line 84

def examples
  if (@context.dig(:show, :all))
    @results[:examples]
  else
    @results[:examples].select do |example|
      example[:status] == 'failed'
    end
  end
end

#failures?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/specular.rb', line 73

def failures?
  @results.dig(:summary, :failure_count) > 0
end

#git_refObject



67
68
69
70
71
# File 'lib/specular.rb', line 67

def git_ref
  @context[:ref] ||= Open3.popen3('git', 'rev-parse', 'HEAD') do |_sin, sout, _serr, _proc|
    sout.readlines.first.chomp
  end
end

#message(service: :github) ⇒ Object



77
78
79
80
81
82
# File 'lib/specular.rb', line 77

def message(service: :github)
  Liquid::Template.parse(
    @context.dig(:messages, service, @context[:result]) ||
      @context.dig(:messages, service, :default)
  ).render(Specular::Support.stringify_keys(@context))
end

#post_message!(services = %i[ slack ])) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/specular.rb', line 171

def post_message!(services = %i[ slack ])
  @context[:posted_messages] = services.to_h do |service|
    [
      service,
      Specular::Integration.for(service, @context).post_message!(
        message: self.message(service: service)
      )
    ]
  end
end

#report(title: false) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/specular.rb', line 128

def report(title: false)
  [
    *(title ? ("<!-- %s -->\n" % self.message(service: :github)) : ()),
    "## Test Results\n",
    self.examples.map do |example|
      case (example[:status])
      when 'failed'
        "### %s\n\n**Status:** %s\n**Source:** %s\n\n%s" % [
          example[:full_description],
          status_label(example[:status]),
          source_link(example[:file_path], example[:line_number]),
          result_format(example.dig(:exception, :message))
        ]
      else
        "### %s\n\n**Status:** %s\n**Source:** %s\n\n" % [
          example[:full_description],
          status_label(example[:status]),
          source_link(example[:file_path], example[:line_number])
        ]
      end
    end,
    "## Summary\n\n**Duration:** %.2fs\n**Failures:** %d\n" % [
      @results.dig(:summary, :duration),
      @results.dig(:summary, :failure_count)
    ],
    "## Git Context\n**Ref:** %s" % [
      self.git_ref
    ]
  ].join("\n")
end

#result_format(result) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/specular.rb', line 119

def result_format(result)
  return unless (result)

  "```\n%s%s```\n" % [
    result,
    result.end_with?("\n") ? "" : "\n"
  ]
end


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/specular.rb', line 98

def source_link(file, line)
  if (@context[:repo])
    '[`%s:%d`](%s#L%d)' % [
      file,
      line,
      URI.join(
        @context[:repo] + 'blob/%s/' % [
          self.git_ref,
        ],
        file
      ),
      line
    ]
  else
    '`%s:%d`' % [
      file,
      line
    ]
  end
end

#status_label(status) ⇒ Object



94
95
96
# File 'lib/specular.rb', line 94

def status_label(status)
  STATUS_LABEL[status.to_sym] || status
end