Class: Homebrew::TestBot::Step Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from SystemCommand::Mixin

#system_command, #system_command!

Constructor Details

#initialize(command, env:, verbose:, named_args: nil, ignore_failures: false, repository: nil) ⇒ void

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.

Parameters:

  • command (Array<String>)

    Command to execute and arguments.

  • env (Hash{String => String})

    Environment variables to set when running command.

  • verbose (Boolean)
  • named_args (String, Array<String>, nil) (defaults to: nil)
  • ignore_failures (Boolean) (defaults to: false)
  • repository (Pathname, nil) (defaults to: nil)


42
43
44
45
46
47
48
49
50
51
52
53
# File 'test_bot/step.rb', line 42

def initialize(command, env:, verbose:, named_args: nil, ignore_failures: false, repository: nil)
  @named_args = T.let([named_args].flatten.compact.map(&:to_s), T::Array[String])
  @command = T.let(command + @named_args, T::Array[String])
  @env = env
  @verbose = verbose
  @ignore_failures = ignore_failures
  @repository = repository

  @name = T.let(command[1]&.delete("-"), T.nilable(String))
  @status = T.let(:running, Symbol)
  @output = T.let(nil, T.nilable(String))
end

Instance Attribute Details

#commandArray<String> (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.

Returns:



15
16
17
# File 'test_bot/step.rb', line 15

def command
  @command
end

#end_timeTime? (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.

Returns:



27
28
29
# File 'test_bot/step.rb', line 27

def end_time
  @end_time
end

#nameString? (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.

Returns:



18
19
20
# File 'test_bot/step.rb', line 18

def name
  @name
end

#outputString? (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.

Returns:



24
25
26
# File 'test_bot/step.rb', line 24

def output
  @output
end

#start_timeTime? (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.

Returns:



27
28
29
# File 'test_bot/step.rb', line 27

def start_time
  @start_time
end

#statusSymbol (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.

Returns:



21
22
23
# File 'test_bot/step.rb', line 21

def status
  @status
end

Instance Method Details

#annotation_location(name) ⇒ Array<([String, nil], [Integer, nil])>

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.

Parameters:

Returns:



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'test_bot/step.rb', line 155

def annotation_location(name)
  formula = Formulary.factory(name)
  method_sym = command.fetch(1).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.to_s, nil]
  end
rescue FormulaUnavailableError
  glob_result = @repository ? @repository.glob("**/#{name}*").first&.to_s : nil
  [glob_result, nil]
end

#command_shortString

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.

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'test_bot/step.rb', line 65

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_trimmedString

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.

Returns:



56
57
58
59
60
61
62
# File 'test_bot/step.rb', line 56

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.

Returns:

  • (Boolean)


90
91
92
# File 'test_bot/step.rb', line 90

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.

Returns:

  • (Boolean)


95
96
97
# File 'test_bot/step.rb', line 95

def ignored?
  @status == :ignored
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.

Returns:

  • (Boolean)


133
134
135
# File 'test_bot/step.rb', line 133

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.

Returns:

  • (Boolean)


85
86
87
# File 'test_bot/step.rb', line 85

def passed?
  @status == :passed
end

#puts_commandvoid

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.

This method returns an undefined value.



100
101
102
# File 'test_bot/step.rb', line 100

def puts_command
  puts Formatter.headline(command_trimmed, color: :blue)
end

#puts_full_outputvoid

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.

This method returns an undefined value.



146
147
148
149
150
151
152
# File 'test_bot/step.rb', line 146

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) ⇒ void

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.

This method returns an undefined value.

Parameters:



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'test_bot/step.rb', line 110

def puts_github_actions_annotation(message, title, file, line)
  return unless GitHub::Actions.env_set?

  type = if passed?
    :notice
  elsif ignored?
    :warning
  else
    :error
  end

  annotation = GitHub::Actions::Annotation.new(type, message, title:, file:, line:)
  puts annotation
end

#puts_in_github_actions_group(title, &_block) ⇒ void

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.

This method returns an undefined value.

Parameters:

  • title (String)
  • _block (T.proc.void)


126
127
128
129
130
# File 'test_bot/step.rb', line 126

def puts_in_github_actions_group(title, &_block)
  puts "::group::#{title}" if GitHub::Actions.env_set?
  yield
  puts "::endgroup::" if GitHub::Actions.env_set?
end

#puts_resultvoid

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.

This method returns an undefined value.



105
106
107
# File 'test_bot/step.rb', line 105

def puts_result
  puts Formatter.headline(Formatter.error("FAILED"), color: :red) unless passed?
end

#run(dry_run: false, fail_fast: false) ⇒ void

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.

This method returns an undefined value.

Parameters:

  • dry_run (Boolean) (defaults to: false)
  • fail_fast (Boolean) (defaults to: false)


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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'test_bot/step.rb', line 198

def run(dry_run: false, fail_fast: false)
  @start_time = T.let(Time.now, T.nilable(Time))

  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 T.must(executable), args:,
                                              print_stdout: @verbose,
                                              print_stderr: @verbose,
                                              env:          @env

  @end_time = T.let(Time.now, T.nilable(Time))

  @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 GitHub::Actions.env_set?
    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.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

#timeFloat

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.

Returns:

  • (Float)

    execution time in seconds



141
142
143
# File 'test_bot/step.rb', line 141

def time
  T.must(end_time) - T.must(start_time)
end

#truncate_output(output, max_kb:, context_lines:) ⇒ String

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.

Parameters:

Returns:



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
# File 'test_bot/step.rb', line 171

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.
    T.must(output_lines[start..]).join
  end
end