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

Parameters:

  • command (Array<String>)

    Command to execute and arguments.

  • env (Hash)

    Environment variables to set when running command.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'test_bot/step.rb', line 19

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

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



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

def command
  @command
end

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



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

def end_time
  @end_time
end

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



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

def name
  @name
end

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



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

def output
  @output
end

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



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

def start_time
  @start_time
end

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



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

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.



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

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_shortObject

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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'test_bot/step.rb', line 40

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_trimmedObject

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.



32
33
34
35
36
37
38
# File 'test_bot/step.rb', line 32

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)


63
64
65
# File 'test_bot/step.rb', line 63

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)


67
68
69
# File 'test_bot/step.rb', line 67

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)


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

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)


59
60
61
# File 'test_bot/step.rb', line 59

def passed?
  @status == :passed
end

#puts_commandObject

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.



71
72
73
# File 'test_bot/step.rb', line 71

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

#puts_full_outputObject

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.



111
112
113
114
115
116
117
# File 'test_bot/step.rb', line 111

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.



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

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



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

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

#puts_resultObject

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.



75
76
77
# File 'test_bot/step.rb', line 75

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.



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

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 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.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

#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



107
108
109
# File 'test_bot/step.rb', line 107

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.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'test_bot/step.rb', line 133

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