Class: Wukong::SpecHelpers::ExitCodeMatcher

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

Overview

A matcher for the exit code of a command.

Instance Attribute Summary

Attributes inherited from IntegrationTestMatcher

#expectations, #failed_expectation, #runner

Instance Method Summary collapse

Methods inherited from IntegrationTestMatcher

#formatted_command, #formatted_env, #formatted_error_output, #formatted_output, #match_function, #match_type

Constructor Details

#initialize(code) ⇒ ExitCodeMatcher

Initialize this matcher with the given code.

If code is the symbol :non_zero then the expectation will be any non-zero exit code.

Parameters:

  • code (Integer, Symbol)


143
144
145
146
147
148
149
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 143

def initialize code
  if code ==  :non_zero
    @expected_code = :non_zero
  else
    @expected_code = code.to_i
  end
end

Instance Method Details

#descriptionObject

:nodoc:



197
198
199
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 197

def description
  "exit with #{expected_exit_code_description}"
end

#expected_exit_codeObject

:nodoc:



183
184
185
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 183

def expected_exit_code
  (@expected_code || 0).to_i
end

#expected_exit_code_descriptionObject

:nodoc:



188
189
190
191
192
193
194
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 188

def expected_exit_code_description
  if non_zero_exit_code?
    "a non-zero exit code"
  else
    "an exit code of #{expected_exit_code}"
  end
end

#failure_messageObject

:nodoc:



168
169
170
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 168

def failure_message
  "From within #{runner.cwd} ran\n\n#{formatted_env}\n#{formatted_command}\n\nexpecting #{expected_exit_code_description}  Got #{runner.exit_code} instead.#{formatted_error_output}"
end

#matches?(runner) ⇒ true, false

Return whether or not the given command's exit code matches the expectation.

Parameters:

Returns:

  • (true, false)


156
157
158
159
160
161
162
163
164
165
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 156

def matches?(runner)
  self.runner = runner
  runner.run!
  if non_zero_exit_code?
    @failed = true if runner.exit_code == 0
  else
    @failed = true if runner.exit_code != expected_exit_code
  end
  @failed ? false : true
end

#negative_failure_messageObject

:nodoc:



173
174
175
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 173

def negative_failure_message
  "From within #{runner.cwd} ran\n\n#{formatted_env}\n#{formatted_command}\n\nNOT expecting #{expected_exit_code_description}.#{formatted_error_output}"
end

#non_zero_exit_code?Boolean

:nodoc:

Returns:

  • (Boolean)


178
179
180
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 178

def non_zero_exit_code?
  @expected_code == :non_zero
end

#output_descriptionObject

:nodoc:



202
203
204
# File 'lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb', line 202

def output_description
  "STDOUT"
end