brew/Library/Homebrew/formula_assertions.rb
Issy Long ecdd45e73e
formula_assertions: Fix type of cmd param in shell_output
- This can be either a String or a Pathname, per
  the part of the `noseyparker` test that failed
  (in a different part of the test, the command is
  passed as a string).

```
  ==> Testing noseyparker
  ==> /opt/homebrew/Cellar/noseyparker/0.18.1/bin/noseyparker -V
  Error: noseyparker: failed
  An exception occurred within a child process:
    TypeError: Parameter 'cmd': Expected type String, got type Pathname with value #<Pathname:/opt/homebrew/Ce...ps://github.com/Homebrew/brew>
  Caller: /opt/homebrew/Library/Taps/homebrew/homebrew-core/Formula/n/noseyparker.rb:35
```
2024-07-12 16:28:35 -04:00

55 lines
1.4 KiB
Ruby

# typed: strict
# frozen_string_literal: true
module Homebrew
# Helper functions available in formula `test` blocks.
module Assertions
include Context
require "minitest"
require "minitest/assertions"
include ::Minitest::Assertions
sig { params(assertions: Integer).returns(Integer) }
attr_writer :assertions
sig { returns(Integer) }
def assertions
@assertions ||= T.let(0, T.nilable(Integer))
end
# Returns the output of running cmd and asserts the exit status.
#
# @api public
sig { params(cmd: T.any(Pathname, String), result: Integer).returns(String) }
def shell_output(cmd, result = 0)
ohai cmd
output = `#{cmd}`
assert_equal result, $CHILD_STATUS.exitstatus
output
rescue Minitest::Assertion
puts output if verbose?
raise
end
# Returns the output of running the cmd with the optional input and
# optionally asserts the exit status.
#
# @api public
sig { params(cmd: String, input: T.nilable(String), result: T.nilable(Integer)).returns(String) }
def pipe_output(cmd, input = nil, result = nil)
ohai cmd
output = IO.popen(cmd, "w+") do |pipe|
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
end
end
end