forked from Homebrew/brew
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cask_spec.rb
350 lines (303 loc) · 12.6 KB
/
cask_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
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
# typed: false
# frozen_string_literal: true
describe Cask::Cask, :cask do
let(:cask) { described_class.new("versioned-cask") }
context "when multiple versions are installed" do
describe "#versions" do
context "when there are duplicate versions" do
it "uses the last unique version" do
allow(cask).to receive(:timestamped_versions).and_return([
["1.2.2", "0999"],
["1.2.3", "1000"],
["1.2.2", "1001"],
])
expect(cask).to receive(:timestamped_versions)
expect(cask.versions).to eq([
"1.2.3",
"1.2.2",
])
end
end
end
end
describe "load" do
let(:tap_path) { Tap.default_cask_tap.path }
let(:file_dirname) { Pathname.new(__FILE__).dirname }
let(:relative_tap_path) { tap_path.relative_path_from(file_dirname) }
it "returns an instance of the Cask for the given token" do
c = Cask::CaskLoader.load("local-caffeine")
expect(c).to be_a(described_class)
expect(c.token).to eq("local-caffeine")
end
it "returns an instance of the Cask from a specific file location" do
c = Cask::CaskLoader.load("#{tap_path}/Casks/local-caffeine.rb")
expect(c).to be_a(described_class)
expect(c.token).to eq("local-caffeine")
end
it "returns an instance of the Cask from a JSON file" do
c = Cask::CaskLoader.load("#{tap_path}/caffeine.json")
expect(c).to be_a(described_class)
expect(c.token).to eq("caffeine")
end
it "returns an instance of the Cask from a URL" do
c = Cask::CaskLoader.load("file://#{tap_path}/Casks/local-caffeine.rb")
expect(c).to be_a(described_class)
expect(c.token).to eq("local-caffeine")
end
it "raises an error when failing to download a Cask from a URL" do
expect do
Cask::CaskLoader.load("file://#{tap_path}/Casks/notacask.rb")
end.to raise_error(Cask::CaskUnavailableError)
end
it "returns an instance of the Cask from a relative file location" do
c = Cask::CaskLoader.load(relative_tap_path/"Casks/local-caffeine.rb")
expect(c).to be_a(described_class)
expect(c.token).to eq("local-caffeine")
end
it "uses exact match when loading by token" do
expect(Cask::CaskLoader.load("test-opera").token).to eq("test-opera")
expect(Cask::CaskLoader.load("test-opera-mail").token).to eq("test-opera-mail")
end
it "raises an error when attempting to load a Cask that doesn't exist" do
expect do
Cask::CaskLoader.load("notacask")
end.to raise_error(Cask::CaskUnavailableError)
end
end
describe "metadata" do
it "proposes a versioned metadata directory name for each instance" do
cask_token = "local-caffeine"
c = Cask::CaskLoader.load(cask_token)
metadata_timestamped_path = Cask::Caskroom.path.join(cask_token, ".metadata", c.version)
expect(c.metadata_versioned_path.to_s).to eq(metadata_timestamped_path.to_s)
end
end
describe "outdated" do
it "ignores the Casks that have auto_updates true (without --greedy)" do
c = Cask::CaskLoader.load("auto-updates")
expect(c).not_to be_outdated
expect(c.outdated_versions).to be_empty
end
it "ignores the Casks that have version :latest (without --greedy)" do
c = Cask::CaskLoader.load("version-latest-string")
expect(c).not_to be_outdated
expect(c.outdated_versions).to be_empty
end
describe "versioned casks" do
subject { cask.outdated_versions }
let(:cask) { described_class.new("basic-cask") }
shared_examples "versioned casks" do |tap_version, expectations|
expectations.each do |installed_versions, expected_output|
context "when versions #{installed_versions.inspect} are installed and the tap version is #{tap_version}" do
it {
allow(cask).to receive(:versions).and_return(installed_versions)
allow(cask).to receive(:version).and_return(Cask::DSL::Version.new(tap_version))
expect(cask).to receive(:outdated_versions).and_call_original
expect(subject).to eq expected_output
}
end
end
end
describe "installed version is equal to tap version => not outdated" do
include_examples "versioned casks", "1.2.3",
["1.2.3"] => [],
["1.2.4", "1.2.3"] => []
end
describe "installed version is different than tap version => outdated" do
include_examples "versioned casks", "1.2.4",
["1.2.3"] => ["1.2.3"],
["1.2.4", "1.2.3"] => ["1.2.3"],
["1.2.2", "1.2.3"] => ["1.2.2", "1.2.3"],
["1.2.2", "1.2.4", "1.2.3"] => ["1.2.2", "1.2.3"]
end
end
describe ":latest casks" do
let(:cask) { described_class.new("basic-cask") }
shared_examples ":latest cask" do |greedy, outdated_sha, tap_version, expectations|
expectations.each do |installed_version, expected_output|
context "when versions #{installed_version} are installed and the " \
"tap version is #{tap_version}, #{"not " unless greedy}greedy " \
"and sha is #{"not " unless outdated_sha}outdated" do
subject { cask.outdated_versions(greedy: greedy) }
it {
allow(cask).to receive(:versions).and_return(installed_version)
allow(cask).to receive(:version).and_return(Cask::DSL::Version.new(tap_version))
allow(cask).to receive(:outdated_download_sha?).and_return(outdated_sha)
expect(cask).to receive(:outdated_versions).and_call_original
expect(subject).to eq expected_output
}
end
end
end
describe ":latest version installed, :latest version in tap" do
include_examples ":latest cask", false, false, "latest",
["latest"] => []
include_examples ":latest cask", true, false, "latest",
["latest"] => []
include_examples ":latest cask", true, true, "latest",
["latest"] => ["latest"]
end
describe "numbered version installed, :latest version in tap" do
include_examples ":latest cask", false, false, "latest",
["1.2.3"] => []
include_examples ":latest cask", true, false, "latest",
["1.2.3"] => []
include_examples ":latest cask", true, true, "latest",
["1.2.3"] => ["1.2.3"]
end
describe "latest version installed, numbered version in tap" do
include_examples ":latest cask", false, false, "1.2.3",
["latest"] => ["latest"]
include_examples ":latest cask", true, false, "1.2.3",
["latest"] => ["latest"]
include_examples ":latest cask", true, true, "1.2.3",
["latest"] => ["latest"]
end
end
end
describe "full_name" do
context "when it is a core cask" do
it "is the cask token" do
c = Cask::CaskLoader.load("local-caffeine")
expect(c.full_name).to eq("local-caffeine")
end
end
context "when it is from a non-core tap" do
it "returns the fully-qualified name of the cask" do
c = Cask::CaskLoader.load("third-party/tap/third-party-cask")
expect(c.full_name).to eq("third-party/tap/third-party-cask")
end
end
context "when it is from no known tap" do
it "returns the cask token" do
file = Tempfile.new(%w[tapless-cask .rb])
begin
cask_name = File.basename(file.path, ".rb")
file.write "cask '#{cask_name}'"
file.close
c = Cask::CaskLoader.load(file.path)
expect(c.full_name).to eq(cask_name)
ensure
file.close
file.unlink
end
end
end
end
describe "#to_h" do
let(:expected_json) { (TEST_FIXTURE_DIR/"cask/everything.json").read.strip }
context "when loaded from cask file" do
it "returns expected hash" do
allow(MacOS).to receive(:version).and_return(MacOS::Version.new("13"))
hash = Cask::CaskLoader.load("everything").to_h
expect(hash).to be_a(Hash)
expect(JSON.pretty_generate(hash)).to eq(expected_json)
end
end
context "when loaded from json file" do
it "returns expected hash" do
expect(Homebrew::API::Cask).not_to receive(:fetch_source)
hash = Cask::CaskLoader::FromAPILoader.new(
"everything", from_json: JSON.parse(expected_json)
).load(config: nil).to_h
expect(hash).to be_a(Hash)
expect(JSON.pretty_generate(hash)).to eq(expected_json)
end
end
end
describe "#to_hash_with_variations" do
let!(:original_macos_version) { MacOS.full_version.to_s }
let(:expected_versions_variations) do
<<~JSON
{
"arm64_big_sur": {
"url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/darwin-arm64/1.2.0/arm.zip",
"version": "1.2.0",
"sha256": "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b"
},
"monterey": {
"url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/darwin/1.2.3/intel.zip"
},
"big_sur": {
"url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/darwin/1.2.0/intel.zip",
"version": "1.2.0",
"sha256": "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b"
},
"catalina": {
"url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/darwin/1.0.0/intel.zip",
"version": "1.0.0",
"sha256": "1866dfa833b123bb8fe7fa7185ebf24d28d300d0643d75798bc23730af734216"
},
"mojave": {
"url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine/darwin/1.0.0/intel.zip",
"version": "1.0.0",
"sha256": "1866dfa833b123bb8fe7fa7185ebf24d28d300d0643d75798bc23730af734216"
}
}
JSON
end
let(:expected_sha256_variations) do
<<~JSON
{
"monterey": {
"url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine-intel.zip",
"sha256": "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b"
},
"big_sur": {
"url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine-intel.zip",
"sha256": "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b"
},
"catalina": {
"url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine-intel.zip",
"sha256": "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b"
},
"mojave": {
"url": "file://#{TEST_FIXTURE_DIR}/cask/caffeine-intel.zip",
"sha256": "8c62a2b791cf5f0da6066a0a4b6e85f62949cd60975da062df44adf887f4370b"
}
}
JSON
end
before do
# Use a more limited symbols list to shorten the variations hash
symbols = {
monterey: "12",
big_sur: "11",
catalina: "10.15",
mojave: "10.14",
}
stub_const("MacOSVersions::SYMBOLS", symbols)
# For consistency, always run on Monterey and ARM
MacOS.full_version = "12"
allow(Hardware::CPU).to receive(:type).and_return(:arm)
end
after do
MacOS.full_version = original_macos_version
end
it "returns the correct variations hash for a cask with multiple versions" do
c = Cask::CaskLoader.load("multiple-versions")
h = c.to_hash_with_variations
expect(h).to be_a(Hash)
expect(JSON.pretty_generate(h["variations"])).to eq expected_versions_variations.strip
end
it "returns the correct variations hash for a cask different sha256s on each arch" do
c = Cask::CaskLoader.load("sha256-arch")
h = c.to_hash_with_variations
expect(h).to be_a(Hash)
expect(JSON.pretty_generate(h["variations"])).to eq expected_sha256_variations.strip
end
# @note The calls to `Cask.generating_hash!` and `Cask.generated_hash!`
# are not idempotent so they can only be used in one test.
it "returns the correct hash placeholders" do
described_class.generating_hash!
expect(described_class).to be_generating_hash
c = Cask::CaskLoader.load("placeholders")
h = c.to_hash_with_variations
described_class.generated_hash!
expect(described_class).not_to be_generating_hash
expect(h).to be_a(Hash)
expect(h["artifacts"].first[:binary].first).to eq "$APPDIR/some/path"
expect(h["caveats"]).to eq "$HOMEBREW_PREFIX and /$HOME\n"
end
end
end