/
test_bot.rb
140 lines (115 loc) · 4.51 KB
/
test_bot.rb
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
# frozen_string_literal: true
require_relative "step"
require_relative "test_runner"
require "date"
require "json"
require "development_tools"
require "formula"
require "formula_installer"
require "os"
require "tap"
require "utils"
require "utils/bottles"
module Homebrew
module TestBot
module_function
GIT = "/usr/bin/git"
HOMEBREW_TAP_REGEX = %r{^([\w-]+)/homebrew-([\w-]+)$}
def resolve_test_tap(tap = nil)
return Tap.fetch(tap) if tap
# Get tap from GitHub Actions GITHUB_REPOSITORY
git_url = ENV.fetch("GITHUB_REPOSITORY", nil)
return if git_url.blank?
url_path = git_url.sub(%r{^https?://.*github\.com/}, "")
.chomp("/")
.sub(/\.git$/, "")
return CoreTap.instance if url_path == CoreTap.instance.full_name
begin
Tap.fetch(url_path) if url_path.match?(HOMEBREW_TAP_REGEX)
rescue
# Don't care if tap fetch fails
nil
end
end
def run!(args)
$stdout.sync = true
$stderr.sync = true
if Pathname.pwd == HOMEBREW_PREFIX && args.cleanup?
raise UsageError, "cannot use --cleanup from HOMEBREW_PREFIX as it will delete all output."
end
ENV["HOMEBREW_BOOTSNAP"] = "1"
ENV["HOMEBREW_DEVELOPER"] = "1"
ENV["HOMEBREW_NO_AUTO_UPDATE"] = "1"
ENV["HOMEBREW_NO_EMOJI"] = "1"
ENV["HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK"] = "1"
ENV["HOMEBREW_FAIL_LOG_LINES"] = "150"
ENV["HOMEBREW_CURL_PATH"] = "/usr/bin/curl"
ENV["HOMEBREW_GIT_PATH"] = GIT
ENV["HOMEBREW_DISALLOW_LIBNSL1"] = "1"
ENV["HOMEBREW_NO_ENV_HINTS"] = "1"
ENV["HOMEBREW_PATH"] = ENV["PATH"] =
"#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:#{ENV.fetch("PATH")}"
# https://github.com/Homebrew/brew/issues/14274
ENV["PYTHONDONTWRITEBYTECODE"] = "1"
if args.local?
home = "#{Dir.pwd}/home"
logs = "#{Dir.pwd}/logs"
gitconfig = "#{Dir.home}/.gitconfig"
ENV["HOMEBREW_HOME"] = ENV["HOME"] = home
ENV["HOMEBREW_LOGS"] = logs
FileUtils.mkdir_p home
FileUtils.mkdir_p logs
FileUtils.cp gitconfig, home if File.exist?(gitconfig)
end
tap = resolve_test_tap(args.tap)
ENV["HOMEBREW_NO_INSTALL_FROM_API"] = "1" if tap.to_s == CoreTap.instance.name
# Tap repository if required, this is done before everything else
# because Formula parsing and/or git commit hash lookup depends on it.
# At the same time, make sure Tap is not a shallow clone.
# bottle rebuild and bottle upload rely on full clone.
if tap
if !tap.path.exist?
safe_system "brew", "tap", tap.name
elsif (tap.path/".git/shallow").exist?
raise unless quiet_system GIT, "-C", tap.path, "fetch", "--unshallow"
end
end
test_bot_tap = Tap.fetch("homebrew/test-bot")
if test_bot_tap != tap
test_bot_revision = Utils.safe_popen_read(
GIT, "-C", test_bot_tap.path.to_s,
"log", "-1", "--format=%h (%s)"
).strip
puts Formatter.headline("Using Homebrew/homebrew-test-bot #{test_bot_revision}", color: :cyan)
end
brew_version = Utils.safe_popen_read(
GIT, "-C", HOMEBREW_REPOSITORY.to_s,
"describe", "--tags", "--abbrev", "--dirty"
).strip
brew_commit_subject = Utils.safe_popen_read(
GIT, "-C", HOMEBREW_REPOSITORY.to_s,
"log", "-1", "--format=%s"
).strip
puts Formatter.headline("Using Homebrew/brew #{brew_version} (#{brew_commit_subject})", color: :cyan)
if tap.to_s != CoreTap.instance.name && CoreTap.instance.installed?
core_revision = Utils.safe_popen_read(
GIT, "-C", CoreTap.instance.path.to_s,
"log", "-1", "--format=%h (%s)"
).strip
puts Formatter.headline("Using #{CoreTap.instance.full_name} #{core_revision}", color: :cyan)
end
if tap
tap_github = " (#{ENV["GITHUB_REPOSITORY"]}" if tap.full_name != ENV["GITHUB_REPOSITORY"]
tap_revision = Utils.safe_popen_read(
GIT, "-C", tap.path.to_s,
"log", "-1", "--format=%h (%s)"
).strip
puts Formatter.headline("Testing #{tap.full_name}#{tap_github} #{tap_revision}:", color: :cyan)
end
ENV["HOMEBREW_GIT_NAME"] = args.git_name || "BrewTestBot"
ENV["HOMEBREW_GIT_EMAIL"] = args.git_email ||
Homebrew.failed = !TestRunner.run!(tap, git: GIT, args:)
end
end
end