Class: Wukong::SpecHelpers::IntegrationTestMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb

Overview

A class for running commands and capturing their STDOUT, STDERR, and exit code. This class is designed to work with the matchers defined in IntegrationTestMatchers.

Direct Known Subclasses

ExitCodeMatcher, StderrMatcher, StdoutMatcher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*expectations) ⇒ IntegrationTestMatcher

Create a matcher on the given expectations. Each expectation can be either a String or a Regexp. Strings will be tested for inclusion in the output, Regexps will be tested for a match against the output.

Parameters:

  • expectations (Array<String,Regexp>)


44
45
46
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 44

def initialize *expectations
  self.expectations = expectations
end

Instance Attribute Details

#expectationsObject

An array of expectations about the output of the runner.



13
14
15
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 13

def expectations
  @expectations
end

#failed_expectationObject

The expectation which caused failure.



16
17
18
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 16

def failed_expectation
  @failed_expectation
end

#runnerObject

The runner used to run the actual commands.



10
11
12
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 10

def runner
  @runner
end

Instance Method Details

#failure_messageObject

:nodoc:



49
50
51
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 49

def failure_message
  "From within #{runner.cwd} ran\n\n#{formatted_env}\n#{formatted_command}\n\nand expected #{output_description}\n\n#{formatted_output}\n\nto #{match_type}\n\n  #{failed_expectation}#{formatted_error_output}"
end

#formatted_commandObject

:nodoc:



69
70
71
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 69

def formatted_command
  "  $ #{runner.cmd}"
end

#formatted_envObject

:nodoc:



74
75
76
77
78
79
80
81
82
83
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 74

def formatted_env
  ['  {'].tap do |lines|
    runner.env.each_pair do |key, value|
      if key =~ /^(BUNDLE_GEMFILE|PATH|RUBYLIB)$/
        lines << "    #{key} => #{value},"
      end
    end
    lines << '  }'
  end.join("\n")
end

#formatted_error_outputObject

:nodoc:



64
65
66
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 64

def formatted_error_output
  output_description.to_s =~ /stderr/ ? "\n\nSTDOUT was\n\n#{runner.stdout}" : "\n\nSTDERR was\n\n#{runner.stderr}"
end

#formatted_outputObject

:nodoc:



59
60
61
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 59

def formatted_output
  output.split("\n").map { |line| '  ' + line }.join("\n")
end

#match_function(expectation) ⇒ Object

:nodoc:



86
87
88
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 86

def match_function expectation
  expectation.is_a?(Regexp) ? :match : :include?
end

#match_typeObject

:nodoc:



91
92
93
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 91

def match_type
  failed_expectation.is_a?(Regexp) ? 'match' : 'include'
end

#matches?(runner) ⇒ true, false

Return whether or not the given command's output matches expectations.

If an expectation failes to match, the failed_expectation attribute will be set accordingly.

Parameters:

Returns:

  • (true, false)


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 26

def matches?(runner)
  self.runner = runner
  runner.run!
  expectations.each do |expectation|
    unless output.send(match_function(expectation), expectation)
      self.failed_expectation = expectation
      return false
    end
  end
  true
end

#negative_failure_messageObject

:nodoc:



54
55
56
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 54

def negative_failure_message
  "Expected #{output_description} of #{runner.cmd}\n\n#{output}\n\nto NOT #{match_type}\n\n#{self.failed_expectation}."
end