diff options
author | Gabriel Mazetto <brodock@gmail.com> | 2019-07-10 21:26:52 +0300 |
---|---|---|
committer | Gabriel Mazetto <brodock@gmail.com> | 2019-07-11 00:14:50 +0300 |
commit | c1fe76f15ba8d2418c428d2c9e88c133bfc69a6f (patch) | |
tree | 56cbce8d0e3a0f3c161b65cc74f23c01bae2b486 /spec | |
parent | 6769aab08af1602905b82604ea26cc3bd758c5c2 (diff) |
Added the `abort_execution` matcher
Diffstat (limited to 'spec')
-rw-r--r-- | spec/support/matchers/abort_matcher.rb | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/spec/support/matchers/abort_matcher.rb b/spec/support/matchers/abort_matcher.rb new file mode 100644 index 00000000000..ce1dd140210 --- /dev/null +++ b/spec/support/matchers/abort_matcher.rb @@ -0,0 +1,46 @@ +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 |