Welcome to mirror list, hosted at ThFree Co, Russian Federation.

abort_matcher.rb « matchers « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 64fed2ca06905bf9c5307a1523769f66f1d39daa (plain)
1
2
3
4
5
6
7
8
9
10
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
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

RSpec::Matchers.define :abort_execution do
  match do |code_block|
    @captured_stderr = StringIO.new
    original_stderr = $stderr
    $stderr = @captured_stderr

    code_block.call

    false
  rescue SystemExit => e
    captured = @captured_stderr.string.chomp
    @actual_exit_code = e.status
    break false unless e.status == 1

    if @message
      if @message.is_a? String
        @message == captured
      elsif @message.is_a? Regexp
        @message.match?(captured)
      else
        raise ArgumentError, 'with_message must be either a String or a Regular Expression'
      end
    end

  ensure
    $stderr = original_stderr
  end

  chain :with_message do |message|
    @message = message
  end

  failure_message do |block|
    unless @actual_exit_code
      break "expected #{block} to abort with '#{@message}' but didnt call abort."
    end

    if @actual_exit_code != 1
      break "expected #{block} to abort with: '#{@message}' but exited with success instead."
    end

    "expected #{block} to abort with: '#{@message}' \n but received: '#{@captured_stderr.string.chomp}' instead."
  end

  supports_block_expectations
end