forked from Homebrew/brew
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
analytics_spec.rb
221 lines (186 loc) 路 9.38 KB
/
analytics_spec.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
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
# typed: false
# frozen_string_literal: true
require "utils/analytics"
require "formula_installer"
describe Utils::Analytics do
describe "::label_google" do
before do
described_class.clear_additional_tags_cache
end
let(:ci) { ", CI" if ENV["CI"] }
it "returns OS_VERSION and prefix when HOMEBREW_PREFIX is a custom prefix on intel" do
allow(Hardware::CPU).to receive(:type).and_return(:intel)
allow(Hardware::CPU).to receive(:in_rosetta2?).and_return(false)
allow(Homebrew).to receive(:default_prefix?).and_return(false)
expect(described_class.label_google).to include described_class.custom_prefix_label_google
end
it "returns OS_VERSION, ARM and prefix when HOMEBREW_PREFIX is a custom prefix on arm" do
allow(Hardware::CPU).to receive(:type).and_return(:arm)
allow(Hardware::CPU).to receive(:in_rosetta2?).and_return(false)
allow(Homebrew).to receive(:default_prefix?).and_return(false)
expect(described_class.label_google).to include described_class.arch_label_google
expect(described_class.label_google).to include described_class.custom_prefix_label_google
end
it "returns OS_VERSION, Rosetta and prefix when HOMEBREW_PREFIX is a custom prefix on Rosetta", :needs_macos do
allow(Hardware::CPU).to receive(:type).and_return(:intel)
allow(Hardware::CPU).to receive(:in_rosetta2?).and_return(true)
allow(Homebrew).to receive(:default_prefix?).and_return(false)
expect(described_class.label_google).to include described_class.custom_prefix_label_google
end
it "does not include prefix when HOMEBREW_PREFIX is the default prefix" do
allow(Homebrew).to receive(:default_prefix?).and_return(true)
expect(described_class.label_google).not_to include HOMEBREW_PREFIX.to_s
end
it "includes CI when ENV['CI'] is set" do
ENV["CI"] = "1"
expect(described_class.label_google).to include "CI"
end
end
describe "::additional_tags_influx" do
before do
described_class.clear_additional_tags_cache
end
let(:ci) { ", CI" if ENV["CI"] }
it "returns OS_VERSION and prefix when HOMEBREW_PREFIX is a custom prefix on intel" do
allow(Hardware::CPU).to receive(:type).and_return(:intel)
allow(Hardware::CPU).to receive(:in_rosetta2?).and_return(false)
allow(Homebrew).to receive(:default_prefix?).and_return(false)
expect(described_class.additional_tags_influx).to have_key(:prefix)
expect(described_class.additional_tags_influx[:prefix]).to eq "custom-prefix"
end
it "returns OS_VERSION, ARM and prefix when HOMEBREW_PREFIX is a custom prefix on arm" do
allow(Hardware::CPU).to receive(:type).and_return(:arm)
allow(Hardware::CPU).to receive(:in_rosetta2?).and_return(false)
allow(Homebrew).to receive(:default_prefix?).and_return(false)
expect(described_class.additional_tags_influx).to have_key(:arch)
expect(described_class.additional_tags_influx[:arch]).to eq HOMEBREW_PHYSICAL_PROCESSOR
expect(described_class.additional_tags_influx).to have_key(:prefix)
expect(described_class.additional_tags_influx[:prefix]).to eq "custom-prefix"
end
it "returns OS_VERSION, Rosetta and prefix when HOMEBREW_PREFIX is a custom prefix on Rosetta", :needs_macos do
allow(Hardware::CPU).to receive(:type).and_return(:intel)
allow(Hardware::CPU).to receive(:in_rosetta2?).and_return(true)
allow(Homebrew).to receive(:default_prefix?).and_return(false)
expect(described_class.additional_tags_influx).to have_key(:prefix)
expect(described_class.additional_tags_influx[:prefix]).to eq "custom-prefix"
end
it "does not include prefix when HOMEBREW_PREFIX is the default prefix" do
allow(Homebrew).to receive(:default_prefix?).and_return(true)
expect(described_class.additional_tags_influx).to have_key(:prefix)
expect(described_class.additional_tags_influx[:prefix]).to eq HOMEBREW_PREFIX.to_s
end
it "includes CI when ENV['CI'] is set" do
ENV["CI"] = "1"
expect(described_class.additional_tags_influx).to have_key(:ci)
end
it "includes developer when ENV['HOMEBREW_DEVELOPER'] is set" do
ENV["HOMEBREW_DEVELOPER"] = "1"
expect(described_class.additional_tags_influx).to have_key(:developer)
end
end
describe "::report_event" do
let(:f) { formula { url "foo-1.0" } }
let(:options) { ["--head"].join }
let(:action) { "#{f.full_name} #{options}".strip }
before do
described_class.clear_additional_tags_cache
end
context "when ENV vars is set" do
it "returns nil when HOMEBREW_NO_ANALYTICS is true" do
ENV["HOMEBREW_NO_ANALYTICS"] = "true"
expect(described_class).not_to receive(:report_google)
expect(described_class).not_to receive(:report_influx)
described_class.report_event(:install, action)
end
it "returns nil when HOMEBREW_NO_ANALYTICS_THIS_RUN is true" do
ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "true"
expect(described_class).not_to receive(:report_google)
expect(described_class).not_to receive(:report_influx)
described_class.report_event(:install, action)
end
it "returns nil when HOMEBREW_ANALYTICS_DEBUG is true" do
ENV.delete("HOMEBREW_NO_ANALYTICS_THIS_RUN")
ENV.delete("HOMEBREW_NO_ANALYTICS")
ENV["HOMEBREW_ANALYTICS_DEBUG"] = "true"
expect(described_class).to receive(:report_google)
expect(described_class).to receive(:report_influx)
described_class.report_event(:install, action)
end
end
it "passes to the influxdb and google methods" do
ENV.delete("HOMEBREW_NO_ANALYTICS_THIS_RUN")
ENV.delete("HOMEBREW_NO_ANALYTICS")
ENV.delete("HOMEBREW_DEVELOPER")
ENV["HOMEBREW_ANALYTICS_DEBUG"] = "true"
expect(described_class).to receive(:report_google).with(:event, hash_including(ea: action)).once
expect(described_class).to receive(:report_influx).with(:install, "formula_name --head", false,
hash_including(developer: false)).once
described_class.report_event(:install, action)
end
it "sends to google twice on request" do
ENV.delete("HOMEBREW_NO_ANALYTICS_THIS_RUN")
ENV.delete("HOMEBREW_NO_ANALYTICS")
ENV.delete("HOMEBREW_DEVELOPER")
ENV["HOMEBREW_ANALYTICS_DEBUG"] = "true"
expect(described_class).to receive(:report_google).with(:event, hash_including(ea: action, ec: :install)).once
expect(described_class).to receive(:report_google).with(:event,
hash_including(ea: action,
ec: :install_on_request)).once
expect(described_class).to receive(:report_influx).with(:install, "formula_name --head", true,
hash_including(developer: false)).once
described_class.report_event(:install, action, on_request: true)
end
end
describe "::report_influx" do
let(:f) { formula { url "foo-1.0" } }
let(:options) { ["--head"].join }
let(:action) { "#{f.full_name} #{options}".strip }
it "outputs in debug mode" do
ENV.delete("HOMEBREW_NO_ANALYTICS_THIS_RUN")
ENV.delete("HOMEBREW_NO_ANALYTICS")
ENV["HOMEBREW_ANALYTICS_DEBUG"] = "true"
ENV["HOMEBREW_DEVELOPER"] = "1"
expect(described_class).to receive(:deferred_curl).once
described_class.report_influx(:install, action, true, developer: true, CI: true)
end
end
describe "::report_build_error" do
context "when tap is installed" do
let(:err) { BuildError.new(f, "badprg", %w[arg1 arg2], {}) }
let(:f) { formula { url "foo-1.0" } }
it "reports event if BuildError raised for a formula with a public remote repository" do
allow_any_instance_of(Tap).to receive(:custom_remote?).and_return(false)
expect(described_class).to respond_to(:report_event)
described_class.report_build_error(err)
end
it "does not report event if BuildError raised for a formula with a private remote repository" do
allow_any_instance_of(Tap).to receive(:private?).and_return(true)
expect(described_class).not_to receive(:report_event)
described_class.report_build_error(err)
end
end
context "when formula does not have a tap" do
let(:err) { BuildError.new(f, "badprg", %w[arg1 arg2], {}) }
let(:f) { instance_double(Formula, name: "foo", path: "blah", tap: nil) }
it "does not report event if BuildError is raised" do
expect(described_class).not_to receive(:report_event)
described_class.report_build_error(err)
end
end
context "when tap for a formula is not installed" do
let(:err) { BuildError.new(f, "badprg", %w[arg1 arg2], {}) }
let(:f) { instance_double(Formula, name: "foo", path: "blah", tap: CoreTap.instance) }
it "does not report event if BuildError is raised" do
allow_any_instance_of(Pathname).to receive(:directory?).and_return(false)
expect(described_class).not_to receive(:report_event)
described_class.report_build_error(err)
end
end
end
specify "::table_output" do
results = { ack: 10, wget: 100 }
expect { described_class.table_output("install", "30", results) }
.to output(/110 | 100.00%/).to_stdout
.and not_to_output.to_stderr
end
end