Class: Homebrew::TestBot::Step Private
- Includes:
- SystemCommand::Mixin
- Defined in:
- test_bot/step.rb
Overview
This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.
Wraps command invocations. Instantiated by Test#test. Handles logging and pretty-printing.
Instance Attribute Summary collapse
- #command ⇒ Object readonly private
- #end_time ⇒ Object readonly private
- #name ⇒ Object readonly private
- #output ⇒ Object readonly private
- #start_time ⇒ Object readonly private
- #status ⇒ Object readonly private
Instance Method Summary collapse
- #annotation_location(name) ⇒ Object private
- #command_short ⇒ Object private
- #command_trimmed ⇒ Object private
- #failed? ⇒ Boolean private
- #ignored? ⇒ Boolean private
- #in_github_actions? ⇒ Boolean private
-
#initialize(command, env:, verbose:, named_args: nil, ignore_failures: false, repository: nil) ⇒ Step
constructor
private
Instantiates a Step object.
- #output? ⇒ Boolean private
- #passed? ⇒ Boolean private
- #puts_command ⇒ Object private
- #puts_full_output ⇒ Object private
- #puts_github_actions_annotation(message, title, file, line) ⇒ Object private
- #puts_in_github_actions_group(title) ⇒ Object private
- #puts_result ⇒ Object private
- #run(dry_run: false, fail_fast: false) ⇒ Object private
-
#time ⇒ Float
private
The execution time of the task.
- #truncate_output(output, max_kb:, context_lines:) ⇒ Object private
Methods included from SystemCommand::Mixin
#system_command, #system_command!
Constructor Details
#initialize(command, env:, verbose:, named_args: nil, ignore_failures: false, repository: nil) ⇒ Step
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.
Instantiates a Step object.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'test_bot/step.rb', line 18 def initialize(command, env:, verbose:, named_args: nil, ignore_failures: false, repository: nil) @named_args = [named_args].flatten.compact.map(&:to_s) @command = command + @named_args @env = env @verbose = verbose @ignore_failures = ignore_failures @repository = repository @name = command[1]&.delete("-") @status = :running @output = nil end |
Instance Attribute Details
#command ⇒ Object (readonly)
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.
13 14 15 |
# File 'test_bot/step.rb', line 13 def command @command end |
#end_time ⇒ Object (readonly)
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.
13 14 15 |
# File 'test_bot/step.rb', line 13 def end_time @end_time end |
#name ⇒ Object (readonly)
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.
13 14 15 |
# File 'test_bot/step.rb', line 13 def name @name end |
#output ⇒ Object (readonly)
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.
13 14 15 |
# File 'test_bot/step.rb', line 13 def output @output end |
#start_time ⇒ Object (readonly)
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.
13 14 15 |
# File 'test_bot/step.rb', line 13 def start_time @start_time end |
#status ⇒ Object (readonly)
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.
13 14 15 |
# File 'test_bot/step.rb', line 13 def status @status end |
Instance Method Details
#annotation_location(name) ⇒ Object
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.
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'test_bot/step.rb', line 122 def annotation_location(name) formula = Formulary.factory(name) method_sym = command.second.to_sym method_location = formula.method(method_sym).source_location if formula.respond_to?(method_sym) if method_location.present? && (method_location.first == formula.path.to_s) method_location else [formula.path, nil] end rescue FormulaUnavailableError [@repository.glob("**/#{name}*").first, nil] end |
#command_short ⇒ Object
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.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'test_bot/step.rb', line 39 def command_short (@command - %W[ brew -C #{HOMEBREW_PREFIX} #{HOMEBREW_REPOSITORY} #{@repository} #{Dir.pwd} --force --retry --verbose --json ].freeze).join(" ") .gsub(HOMEBREW_PREFIX.to_s, "") .gsub(HOMEBREW_REPOSITORY.to_s, "") .gsub(@repository.to_s, "") .gsub(Dir.pwd, "") end |
#command_trimmed ⇒ Object
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.
31 32 33 34 35 36 37 |
# File 'test_bot/step.rb', line 31 def command_trimmed command.reject { |arg| arg.to_s.start_with?("--exclude") } .join(" ") .delete_prefix("#{HOMEBREW_LIBRARY}/Taps/") .delete_prefix("#{HOMEBREW_PREFIX}/") .delete_prefix("/usr/bin/") end |
#failed? ⇒ Boolean
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.
62 63 64 |
# File 'test_bot/step.rb', line 62 def failed? @status == :failed end |
#ignored? ⇒ Boolean
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.
66 67 68 |
# File 'test_bot/step.rb', line 66 def ignored? @status == :ignored end |
#in_github_actions? ⇒ Boolean
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.
78 79 80 |
# File 'test_bot/step.rb', line 78 def in_github_actions? ENV["GITHUB_ACTIONS"].present? end |
#output? ⇒ Boolean
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.
103 104 105 |
# File 'test_bot/step.rb', line 103 def output? @output.present? end |
#passed? ⇒ Boolean
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.
58 59 60 |
# File 'test_bot/step.rb', line 58 def passed? @status == :passed end |
#puts_command ⇒ Object
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.
70 71 72 |
# File 'test_bot/step.rb', line 70 def puts_command puts Formatter.headline(command_trimmed, color: :blue) end |
#puts_full_output ⇒ Object
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.
114 115 116 117 118 119 120 |
# File 'test_bot/step.rb', line 114 def puts_full_output return if @output.blank? || @verbose puts_in_github_actions_group("Full #{command_short} output") do puts @output end end |
#puts_github_actions_annotation(message, title, file, line) ⇒ Object
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.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'test_bot/step.rb', line 82 def puts_github_actions_annotation(, title, file, line) return unless in_github_actions? type = if passed? :notice elsif ignored? :warning else :error end annotation = GitHub::Actions::Annotation.new(type, , title:, file:, line:) puts annotation end |
#puts_in_github_actions_group(title) ⇒ Object
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.
97 98 99 100 101 |
# File 'test_bot/step.rb', line 97 def puts_in_github_actions_group(title) puts "::group::#{title}" if in_github_actions? yield puts "::endgroup::" if in_github_actions? end |
#puts_result ⇒ Object
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.
74 75 76 |
# File 'test_bot/step.rb', line 74 def puts_result puts Formatter.headline(Formatter.error("FAILED"), color: :red) unless passed? end |
#run(dry_run: false, fail_fast: false) ⇒ Object
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.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'test_bot/step.rb', line 162 def run(dry_run: false, fail_fast: false) @start_time = Time.now puts_command if dry_run @status = :passed puts_result return end raise "git should always be called with -C!" if command[0] == "git" && %w[-C clone].exclude?(command[1]) executable, *args = command result = system_command executable, args:, print_stdout: @verbose, print_stderr: @verbose, env: @env @end_time = Time.now @status = if result.success? :passed elsif @ignore_failures :ignored else :failed end puts_result output = result.merged_output # ActiveSupport can barf on some Unicode so don't use .present? if output.empty? puts if @verbose exit 1 if fail_fast && failed? return end output.force_encoding(Encoding::UTF_8) @output = if output.valid_encoding? output else output.encode!(Encoding::UTF_16, invalid: :replace) output.encode!(Encoding::UTF_8) end return if passed? puts_full_output unless in_github_actions? puts exit 1 if fail_fast && failed? return end # TODO: move to extend/os # rubocop:todo Homebrew/MoveToExtendOS os_string = if OS.mac? str = "macOS #{MacOS.version.pretty_name} (#{MacOS.version})" str << " on Apple Silicon" if Hardware::CPU.arm? str else "#{OS.kernel_name} #{Hardware::CPU.arch}" end # rubocop:enable Homebrew/MoveToExtendOS @named_args.each do |name| next if name.blank? path, line = annotation_location(name) next if path.blank? # GitHub Actions has a 4KB maximum for annotations. annotation_output = truncate_output(@output, max_kb: 4, context_lines: 5) annotation_title = "`#{command_trimmed}` failed on #{os_string}!" file = path.to_s.delete_prefix("#{@repository}/") puts_in_github_actions_group("Truncated #{command_short} output") do puts_github_actions_annotation(annotation_output, annotation_title, file, line) end end exit 1 if fail_fast && failed? end |
#time ⇒ Float
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.
The execution time of the task. Precondition: Step#run has been called.
110 111 112 |
# File 'test_bot/step.rb', line 110 def time end_time - start_time end |
#truncate_output(output, max_kb:, context_lines:) ⇒ Object
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.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'test_bot/step.rb', line 136 def truncate_output(output, max_kb:, context_lines:) output_lines = output.lines first_error_index = output_lines.find_index do |line| !line.strip.match?(/^::error( .*)?::/) && (line.match?(/\berror:\s+/i) || line.match?(/\bcmake error\b/i)) end if first_error_index.blank? output = [] # Collect up to max_kb worth of the last lines of output. output_lines.reverse_each do |line| # Check output.present? so that we at least have _some_ output. break if line.length + output.join.length > max_kb && output.present? output.unshift line end output.join else start = [first_error_index - context_lines, 0].max # Let GitHub Actions truncate us to 4KB if needed. output_lines[start..].join end end |