Module: Homebrew::Assertions Private
- Extended by:
- T::Helpers
- Includes:
- Context, Minitest::Assertions, Utils::Output::Mixin
- Defined in:
- formula_assertions.rb
Overview
This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.
Helper functions available in formula test blocks.
Instance Attribute Summary collapse
- #assertions ⇒ Integer private
Instance Method Summary collapse
-
#pipe_output(cmd, input = nil, result = nil) ⇒ String
Returns the output of running the cmd with the optional input and optionally asserts the exit status.
-
#shell_output(cmd, result = 0) ⇒ String
Returns the output of running cmd and asserts the exit status.
-
#with_test_process(executable, *args, signal: "TERM", **spawn_options, &block) ⇒ void
Runs a command in the background for the duration of a test block.
Methods included from Utils::Output::Mixin
#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_once, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_incompatible, #pretty_install_status, #pretty_installed, #pretty_removed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning
Methods included from Context
current, current=, #debug?, #deferred_environment_expansion?, #quiet?, #verbose?, #with_context
Instance Attribute Details
#assertions ⇒ Integer
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
27 28 29 |
# File 'formula_assertions.rb', line 27 def assertions @assertions ||= T.let(0, T.nilable(Integer)) end |
Instance Method Details
#pipe_output(cmd, input = nil, result = nil) ⇒ String
Returns the output of running the cmd with the optional input and optionally asserts the exit status.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'formula_assertions.rb', line 128 def pipe_output(cmd, input = nil, result = nil) ohai cmd.to_s assert_path_exists cmd, "Pathname '#{cmd}' does not exist!" if cmd.is_a?(Pathname) 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 |
#shell_output(cmd, result = 0) ⇒ String
Returns the output of running cmd and asserts the exit status.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'formula_assertions.rb', line 108 def shell_output(cmd, result = 0) ohai cmd.to_s assert_path_exists cmd, "Pathname '#{cmd}' does not exist!" if cmd.is_a?(Pathname) output = if cmd.is_a?(Pathname) Utils.popen_read_text("/bin/sh", "-c", 'exec "$1"', "shell_output", cmd..to_s, err: :err) else Utils.popen_read_text("/bin/sh", "-c", cmd, err: :err) end assert_equal result, $CHILD_STATUS.exitstatus output rescue Minitest::Assertion puts output if verbose? raise end |
#with_test_process(executable, *args, signal: "TERM", **spawn_options, &block) ⇒ void
This method returns an undefined value.
Runs a command in the background for the duration of a test block.
Pass the executable and its arguments separately. For shell setup, use exec
to replace the shell with the process the helper should clean up.
Yields the child process ID and terminates and reaps the child on exit.
The block may also reap the child itself.
Use a minus-prefixed signal with pgroup: true to send both signals to the process group.
Sends KILL if the child or selected group is still running five seconds after the requested signal.
Extra keyword arguments are forwarded to Process.spawn.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'formula_assertions.rb', line 50 def with_test_process(executable, *args, signal: "TERM", **, &block) signal_number = Signal.list.fetch(signal.delete_prefix("-").delete_prefix("SIG"), 0) raise ArgumentError, "Unsupported signal: #{signal.inspect}" if signal_number.zero? executable = executable.to_s pid = Process.spawn([executable, executable], *args, **) target = signal.start_with?("-") ? -pid : pid begin yield(pid) ensure reaped = T.let(reap_test_process(pid, Process::WNOHANG), T::Boolean) if target.negative? || !reaped begin Process.kill(signal_number, target) rescue Errno::ESRCH, Errno::EPERM => e raise if target.positive? && e.is_a?(Errno::EPERM) target = pid end deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + TEST_PROCESS_GRACE_PERIOD loop do reaped ||= reap_test_process(pid, Process::WNOHANG) Process.kill(0, target) if target.negative? break if reaped && target.positive? remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC) break if remaining <= 0 sleep [0.1, remaining].min rescue Errno::ESRCH, Errno::EPERM # On macOS, probing a group with only zombies may raise EPERM. target = pid end begin Process.kill("KILL", target) if target.negative? rescue Errno::ESRCH, Errno::EPERM # The group may have exited since the last check. end unless reaped begin # The child may have left the original process group. Process.kill("KILL", pid) rescue Errno::ESRCH # The child exited between the non-blocking wait and the signal. end reap_test_process(pid) end end end end |