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)


52
53
54
55
56
57
58
59
60
61
62
63
# File 'test_bot/step.rb', line 52

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:



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

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:



37
38
39
# File 'test_bot/step.rb', line 37

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:



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

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:



34
35
36
# File 'test_bot/step.rb', line 34

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:



37
38
39
# File 'test_bot/step.rb', line 37

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:



31
32
33
# File 'test_bot/step.rb', line 31

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:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'test_bot/step.rb', line 165

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:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'test_bot/step.rb', line 75

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:



66
67
68
69
70
71
72
# File 'test_bot/step.rb', line 66

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)


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

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)


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

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)


143
144
145
# File 'test_bot/step.rb', line 143

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)


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

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.



110
111
112
# File 'test_bot/step.rb', line 110

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.



156
157
158
159
160
161
162
# File 'test_bot/step.rb', line 156

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:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'test_bot/step.rb', line 120

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)


136
137
138
139
140
# File 'test_bot/step.rb', line 136

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.



115
116
117
# File 'test_bot/step.rb', line 115

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)


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

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

  @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 #{Homebrew::TestBot.runner_os_title_with_arch}!"
    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



151
152
153
# File 'test_bot/step.rb', line 151

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:



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

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