2014-07-27 10:34:22 -07:00
|
|
|
module Homebrew
|
|
|
|
module Assertions
|
2017-06-25 13:04:36 +01:00
|
|
|
require "test/unit/assertions"
|
2016-01-19 15:42:37 +01:00
|
|
|
include ::Test::Unit::Assertions
|
2014-07-27 10:34:22 -07:00
|
|
|
|
2017-06-29 17:38:44 -07:00
|
|
|
# Custom name here for cross-version compatibility.
|
|
|
|
# In Ruby 2.0, Test::Unit::Assertions raise a MiniTest::Assertion,
|
|
|
|
# but they raise Test::Unit::AssertionFailedError in 2.3.
|
|
|
|
# If neither is defined, this might be a completely different
|
|
|
|
# version of Ruby.
|
|
|
|
if defined?(MiniTest::Assertion)
|
|
|
|
AssertionFailed = MiniTest::Assertion
|
|
|
|
elsif defined?(Test::Unit::AssertionFailedError)
|
|
|
|
AssertionFailed = Test::Unit::AssertionFailedError
|
|
|
|
else
|
|
|
|
raise NameError, "Unable to find an assertion class for this version of Ruby (#{RUBY_VERSION})"
|
|
|
|
end
|
|
|
|
|
2014-07-27 10:34:22 -07:00
|
|
|
# Returns the output of running cmd, and asserts the exit status
|
2015-08-03 13:09:07 +01:00
|
|
|
def shell_output(cmd, result = 0)
|
2014-07-29 21:59:49 -07:00
|
|
|
ohai cmd
|
2014-07-27 10:34:22 -07:00
|
|
|
output = `#{cmd}`
|
2017-06-10 20:12:55 +03:00
|
|
|
assert_equal result, $CHILD_STATUS.exitstatus
|
2014-07-27 10:34:22 -07:00
|
|
|
output
|
|
|
|
end
|
|
|
|
|
2015-01-19 10:20:49 +01:00
|
|
|
# Returns the output of running the cmd with the optional input, and
|
|
|
|
# optionally asserts the exit status
|
2015-08-03 13:09:07 +01:00
|
|
|
def pipe_output(cmd, input = nil, result = nil)
|
2014-07-29 21:59:49 -07:00
|
|
|
ohai cmd
|
2015-01-19 10:20:49 +01:00
|
|
|
output = IO.popen(cmd, "w+") do |pipe|
|
2014-07-27 10:34:22 -07:00
|
|
|
pipe.write(input) unless input.nil?
|
|
|
|
pipe.close_write
|
|
|
|
pipe.read
|
|
|
|
end
|
2017-06-10 20:12:55 +03:00
|
|
|
assert_equal result, $CHILD_STATUS.exitstatus unless result.nil?
|
2015-01-19 10:20:49 +01:00
|
|
|
output
|
2014-07-27 10:34:22 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|