2020-10-10 14:16:11 +02:00
|
|
|
# typed: false
|
2019-04-19 15:38:03 +09:00
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# @api private
|
2014-07-27 10:34:22 -07:00
|
|
|
module Assertions
|
2020-08-02 14:32:31 +02:00
|
|
|
include Context
|
|
|
|
|
2021-02-24 18:01:47 +00:00
|
|
|
require "minitest"
|
|
|
|
require "minitest/assertions"
|
|
|
|
include ::Minitest::Assertions
|
|
|
|
|
|
|
|
attr_writer :assertions
|
|
|
|
|
|
|
|
def assertions
|
|
|
|
@assertions ||= 0
|
|
|
|
end
|
|
|
|
|
|
|
|
# Test::Unit backwards compatibility methods
|
|
|
|
{
|
2021-02-27 16:58:43 +00:00
|
|
|
assert_include: :assert_includes,
|
|
|
|
assert_path_exist: :assert_path_exists,
|
2021-02-24 18:01:47 +00:00
|
|
|
assert_raise: :assert_raises,
|
2021-02-27 16:58:43 +00:00
|
|
|
assert_throw: :assert_throws,
|
2021-02-24 18:01:47 +00:00
|
|
|
assert_not_empty: :refute_empty,
|
|
|
|
assert_not_equal: :refute_equal,
|
|
|
|
assert_not_in_delta: :refute_in_delta,
|
|
|
|
assert_not_in_epsilon: :refute_in_epsilon,
|
2021-02-27 16:58:43 +00:00
|
|
|
assert_not_include: :refute_includes,
|
2021-02-24 18:01:47 +00:00
|
|
|
assert_not_includes: :refute_includes,
|
|
|
|
assert_not_instance_of: :refute_instance_of,
|
|
|
|
assert_not_kind_of: :refute_kind_of,
|
2021-02-27 09:29:58 +01:00
|
|
|
assert_not_match: :refute_match,
|
2021-02-24 18:01:47 +00:00
|
|
|
assert_no_match: :refute_match,
|
|
|
|
assert_not_nil: :refute_nil,
|
|
|
|
assert_not_operator: :refute_operator,
|
2021-02-27 16:58:43 +00:00
|
|
|
assert_path_not_exist: :refute_path_exists,
|
2021-02-24 18:01:47 +00:00
|
|
|
assert_not_predicate: :refute_predicate,
|
|
|
|
assert_not_respond_to: :refute_respond_to,
|
|
|
|
assert_not_same: :refute_same,
|
|
|
|
}.each do |old_method, new_method|
|
|
|
|
define_method(old_method) do |*args|
|
2021-04-09 09:30:36 +01:00
|
|
|
odeprecated old_method, new_method
|
2021-02-24 18:01:47 +00:00
|
|
|
send(new_method, *args)
|
|
|
|
end
|
|
|
|
end
|
2014-07-27 10:34:22 -07:00
|
|
|
|
2021-02-26 22:39:34 +00:00
|
|
|
def assert_true(act, msg = nil)
|
2021-04-09 09:30:36 +01:00
|
|
|
odeprecated "assert_true", "assert(...) or assert_equal(true, ...)"
|
2021-02-26 22:39:34 +00:00
|
|
|
assert_equal(true, act, msg)
|
|
|
|
end
|
|
|
|
|
2021-02-27 16:58:43 +00:00
|
|
|
def assert_false(act, msg = nil)
|
2021-04-09 09:30:36 +01:00
|
|
|
odeprecated "assert_false", "assert(!...) or assert_equal(false, ...)"
|
2021-02-27 16:58:43 +00:00
|
|
|
assert_equal(false, act, msg)
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns the output of running cmd, and asserts the exit status.
|
2020-08-17 05:15:22 +02:00
|
|
|
# @api public
|
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
|
2021-02-24 18:01:47 +00:00
|
|
|
rescue Minitest::Assertion
|
2020-08-02 14:32:31 +02:00
|
|
|
puts output if verbose?
|
2020-03-10 09:21:30 +00:00
|
|
|
raise
|
2014-07-27 10:34:22 -07:00
|
|
|
end
|
|
|
|
|
2015-01-19 10:20:49 +01:00
|
|
|
# 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
|
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
|
2021-02-24 18:01:47 +00:00
|
|
|
rescue Minitest::Assertion
|
2020-08-02 14:32:31 +02:00
|
|
|
puts output if verbose?
|
2020-03-10 09:21:30 +00:00
|
|
|
raise
|
2014-07-27 10:34:22 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|