brew/Library/Homebrew/formula_assertions.rb

49 lines
1.1 KiB
Ruby
Raw Normal View History

2023-03-07 17:24:20 -08:00
# typed: true
# frozen_string_literal: true
2014-07-27 10:34:22 -07:00
module Homebrew
2020-08-17 05:15:22 +02:00
# Helper functions available in formula `test` blocks.
2014-07-27 10:34:22 -07:00
module Assertions
include Context
require "minitest"
require "minitest/assertions"
include ::Minitest::Assertions
attr_writer :assertions
def assertions
@assertions ||= 0
end
# Returns the output of running cmd, and asserts the exit status.
2020-08-17 05:15:22 +02:00
# @api public
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}`
assert_equal result, $CHILD_STATUS.exitstatus
2014-07-27 10:34:22 -07:00
output
rescue Minitest::Assertion
puts output if verbose?
raise
2014-07-27 10:34:22 -07:00
end
# Returns the output of running the cmd with the optional input, and
2020-08-17 05:15:22 +02:00
# optionally asserts the exit status.
# @api public
def pipe_output(cmd, input = nil, result = nil)
2014-07-29 21:59:49 -07:00
ohai cmd
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
assert_equal result, $CHILD_STATUS.exitstatus unless result.nil?
output
rescue Minitest::Assertion
puts output if verbose?
raise
2014-07-27 10:34:22 -07:00
end
end
end