1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
158
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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
# Comprehensively test a formula or pull request.
#
# Usage: brew test-bot [options...] <pull-request|formula>
#
# Options:
# --keep-logs: Write and keep log files under ./brewbot/
# --cleanup: Clean the Homebrew directory. Very dangerous. Use with care.
# --skip-setup: Don't check the local system is setup correctly.
# --junit: Generate a JUnit XML test results file.
require 'formula'
require 'utils'
require 'date'
require 'erb'
HOMEBREW_CONTRIBUTED_CMDS = HOMEBREW_REPOSITORY + "Library/Contributions/cmd/"
class Step
attr_reader :command, :name, :status, :output, :time
def initialize test, command, options={}
@test = test
@category = test.category
@command = command
@puts_output_on_success = options[:puts_output_on_success]
@name = command.split[1].delete '-'
@status = :running
@repository = HOMEBREW_REPOSITORY
@time = 0
end
def log_file_path full_path=true
file = "#{@category}.#{@name}.txt"
return file unless @test.log_root and full_path
@test.log_root + file
end
def status_colour
case @status
when :passed then "green"
when :running then "orange"
when :failed then "red"
end
end
def status_upcase
@status.to_s.upcase
end
def passed?
@status == :passed
end
def failed?
@status == :failed
end
def puts_command
print "#{Tty.blue}==>#{Tty.white} #{@command}#{Tty.reset}"
tabs = (80 - "PASSED".length + 1 - @command.length) / 8
tabs.times{ print "\t" }
$stdout.flush
end
def puts_result
puts " #{Tty.send status_colour}#{status_upcase}#{Tty.reset}"
end
def has_output?
@output and @output.any?
end
def run
puts_command
start_time = Time.now
run_command = "#{@command} &>#{log_file_path}"
if run_command.start_with? 'git '
Dir.chdir @repository do
`#{run_command}`
end
else
`#{run_command}`
end
end_time = Time.now
@time = end_time - start_time
success = $?.success?
@status = success ? :passed : :failed
puts_result
return unless File.exists?(log_file_path)
@output = IO.read(log_file_path)
if has_output? and (not success or @puts_output_on_success)
puts @output
end
FileUtils.rm log_file_path unless ARGV.include? "--keep-logs"
end
end
class Test
attr_reader :log_root, :category, :name, :formulae, :steps
def initialize argument
@hash = nil
@url = nil
@formulae = []
url_match = argument.match HOMEBREW_PULL_OR_COMMIT_URL_REGEX
formula = Formula.factory argument rescue FormulaUnavailableError
git "rev-parse --verify #{argument} &>/dev/null"
if $?.success?
@hash = argument
elsif url_match
@url = url_match[0]
elsif formula
@formulae = [argument]
else
odie "#{argument} is not a pull request URL, commit URL or formula name."
end
@category = __method__
@steps = []
@brewbot_root = Pathname.pwd + "brewbot"
FileUtils.mkdir_p @brewbot_root
end
def git arguments
Dir.chdir HOMEBREW_REPOSITORY do
`git #{arguments}`
end
end
def download
def shorten_revision revision
git("rev-parse --short #{revision}").strip
end
def current_sha1
shorten_revision 'HEAD'
end
def current_branch
git('symbolic-ref HEAD').gsub('refs/heads/', '').strip
end
def single_commit? start_revision, end_revision
git("rev-list --count #{start_revision}..#{end_revision}").to_i == 1
end
@category = __method__
@start_branch = current_branch
# Use Jenkins environment variables if present.
if ENV['GIT_PREVIOUS_COMMIT'] and ENV['GIT_COMMIT'] \
and not ENV['ghprbPullId']
diff_start_sha1 = shorten_revision ENV['GIT_PREVIOUS_COMMIT']
diff_end_sha1 = shorten_revision ENV['GIT_COMMIT']
test "brew update" if current_branch == "master"
elsif @hash or @url
diff_start_sha1 = current_sha1
test "brew update" if current_branch == "master"
diff_end_sha1 = current_sha1
end
# Handle Jenkins pull request builder plugin.
if ENV['ghprbPullId'] and ENV['GIT_URL']
git_url = ENV['GIT_URL']
git_match = git_url.match %r{.*github.com[:/](\w+/\w+).*}
if git_match
github_repo = git_match[1]
pull_id = ENV['ghprbPullId']
@url = "https://github.com/#{github_repo}/pull/#{pull_id}"
@hash = nil
else
puts "Invalid 'ghprbPullId' environment variable value!"
end
end
if @hash == 'HEAD'
if diff_start_sha1 == diff_end_sha1 or \
single_commit?(diff_start_sha1, diff_end_sha1)
@name = diff_end_sha1
else
@name = "#{diff_start_sha1}-#{diff_end_sha1}"
end
elsif @hash
test "git checkout #{@hash}"
diff_start_sha1 = "#{@hash}^"
diff_end_sha1 = @hash
@name = @hash
elsif @url
test "git checkout #{current_sha1}"
test "brew pull --clean #{@url}"
diff_end_sha1 = current_sha1
@short_url = @url.gsub('https://github.com/', '')
if @short_url.include? '/commit/'
# 7 characters should be enough for a commit (not 40).
@short_url.gsub!(/(commit\/\w{7}).*/, '\1')
@name = @short_url
else
@name = "#{@short_url}-#{diff_end_sha1}"
end
else
diff_start_sha1 = diff_end_sha1 = current_sha1
@name = "#{@formulae.first}-#{diff_end_sha1}"
end
@log_root = @brewbot_root + @name
FileUtils.mkdir_p @log_root
return unless diff_start_sha1 != diff_end_sha1
return if @url and not steps.last.passed?
diff_stat = git "diff #{diff_start_sha1}..#{diff_end_sha1} --name-status"
diff_stat.each_line do |line|
status, filename = line.split
# Don't try and do anything to removed files.
if (status == 'A' or status == 'M')
if filename.include? '/Formula/'
@formulae << File.basename(filename, '.rb')
end
end
end
end
def setup
@category = __method__
test "brew doctor"
test "brew --env"
test "brew --config"
end
def formula formula
@category = __method__.to_s + ".#{formula}"
dependencies = `brew deps #{formula}`.split("\n")
dependencies -= `brew list`.split("\n")
dependencies = dependencies.join(' ')
formula_object = Formula.factory(formula)
requirements = formula_object.recursive_requirements
unsatisfied_requirements = requirements.reject {|r| r.satisfied?}
unless unsatisfied_requirements.empty?
puts "#{Tty.blue}==>#{Tty.white} SKIPPING: #{formula}#{Tty.reset}"
unsatisfied_requirements.each {|r| puts r.message}
return
end
test "brew audit #{formula}"
test "brew fetch #{dependencies}" unless dependencies.empty?
test "brew fetch --build-bottle #{formula}"
test "brew install --verbose #{dependencies}" unless dependencies.empty?
test "brew install --verbose --build-bottle #{formula}"
return unless steps.last.passed?
bottle_step = test "brew bottle #{formula}", :puts_output_on_success => true
bottle_revision = bottle_new_revision(formula_object)
bottle_filename = bottle_filename(formula_object, bottle_revision)
if bottle_step.passed? and bottle_step.has_output?
bottle_base = bottle_filename.gsub(bottle_suffix(bottle_revision), '')
bottle_output = bottle_step.output.gsub /.*(bottle do.*end)/m, '\1'
File.open "#{bottle_base}.bottle.rb", 'w' do |file|
file.write bottle_output
end
end
test "brew uninstall #{formula}"
test "brew install #{bottle_filename}"
test "brew test #{formula}" if formula_object.test_defined?
test "brew uninstall #{formula}"
test "brew uninstall #{dependencies}" unless dependencies.empty?
end
def homebrew
@category = __method__
test "brew tests"
test "brew readall"
end
def cleanup_before
@category = __method__
return unless ARGV.include? '--cleanup'
git 'stash'
git 'am --abort 2>/dev/null'
git 'rebase --abort 2>/dev/null'
git 'checkout -f master'
git 'reset --hard'
git 'clean --force -dx'
end
def cleanup_after
@category = __method__
force_flag = ''
if ARGV.include? '--cleanup'
test 'brew cleanup'
test 'git clean --force -dx'
force_flag = '-f'
end
if ARGV.include? '--cleanup' or @url or @hash
test "git checkout #{force_flag} #{@start_branch}"
end
if ARGV.include? '--cleanup'
test 'git reset --hard'
test 'git gc'
git 'stash pop 2>/dev/null'
end
FileUtils.rm_rf @brewbot_root unless ARGV.include? "--keep-logs"
end
def test cmd, options={}
step = Step.new self, cmd, options
step.run
steps << step
step
end
def check_results
message = "All tests passed and raring to brew."
status = :passed
steps.each do |step|
case step.status
when :passed then next
when :running then raise
when :failed then
if status == :passed
status = :failed
message = ""
end
message += "#{step.command}: #{step.status.to_s.upcase}\n"
end
end
status == :passed
end
def run
cleanup_before
download
setup unless ARGV.include? "--skip-setup"
homebrew
formulae.each do |f|
formula(f)
end
cleanup_after
check_results
end
end
if Pathname.pwd == HOMEBREW_PREFIX and ARGV.include? "--cleanup"
odie 'cannot use --cleanup from HOMEBREW_PREFIX as it will delete all output.'
end
tests = []
any_errors = false
if ARGV.named.empty?
# With no arguments just build the most recent commit.
test = Test.new('HEAD')
any_errors = test.run
tests << test
else
ARGV.named.each do |argument|
test = Test.new(argument)
any_errors = test.run or any_errors
tests << test
end
end
if ARGV.include? "--junit"
xml_erb = HOMEBREW_CONTRIBUTED_CMDS + "brew-test-bot.xml.erb"
erb = ERB.new IO.read xml_erb
open("brew-test-bot.xml", "w") do |xml|
# Remove empty lines and null characters from ERB result.
xml.write erb.result(binding).gsub(/^\s*$\n|\000/, '')
end
end
exit any_errors ? 0 : 1
|