-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
app_spec.rb
389 lines (297 loc) 路 11.8 KB
/
app_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
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
380
381
382
383
384
385
386
387
388
389
# frozen_string_literal: true
RSpec.describe Cask::Artifact::App, :cask do
let(:cask) { Cask::CaskLoader.load(cask_path("local-caffeine")) }
let(:command) { NeverSudoSystemCommand }
let(:adopt) { false }
let(:force) { false }
let(:app) { cask.artifacts.find { |a| a.is_a?(described_class) } }
let(:source_path) { cask.staged_path.join("Caffeine.app") }
let(:target_path) { cask.config.appdir.join("Caffeine.app") }
let(:install_phase) { app.install_phase(command:, adopt:, force:) }
let(:uninstall_phase) { app.uninstall_phase(command:, force:) }
before do
InstallHelper.install_without_artifacts(cask)
end
describe "install_phase" do
it "installs the given app using the proper target directory" do
install_phase
expect(target_path).to be_a_directory
expect(source_path).to be_a_symlink
end
describe "when app is in a subdirectory" do
let(:cask) do
Cask::Cask.new("subdir") do
url "file://#{TEST_FIXTURE_DIR}/cask/caffeine.zip"
homepage "https://brew.sh/local-caffeine"
version "1.2.3"
sha256 "67cdb8a02803ef37fdbf7e0be205863172e41a561ca446cd84f0d7ab35a99d94"
app "subdir/Caffeine.app"
end
end
it "installs the given app using the proper target directory" do
appsubdir = cask.staged_path.join("subdir").tap(&:mkpath)
FileUtils.mv(source_path, appsubdir)
install_phase
expect(target_path).to be_a_directory
expect(appsubdir.join("Caffeine.app")).to be_a_symlink
end
end
it "only uses apps when they are specified" do
staged_app_copy = source_path.sub("Caffeine.app", "Caffeine Deluxe.app")
FileUtils.cp_r source_path, staged_app_copy
install_phase
expect(target_path).to be_a_directory
expect(source_path).to be_a_symlink
expect(cask.config.appdir.join("Caffeine Deluxe.app")).not_to exist
expect(cask.staged_path.join("Caffeine Deluxe.app")).to exist
end
describe "when the target already exists" do
before do
target_path.mkpath
end
it "avoids clobbering an existing app" do
expect { install_phase }.to raise_error(
Cask::CaskError,
"It seems there is already an App at '#{target_path}'.",
)
expect(source_path).to be_a_directory
expect(target_path).to be_a_directory
expect(File.identical?(source_path, target_path)).to be false
contents_path = target_path.join("Contents/Info.plist")
expect(contents_path).not_to exist
end
describe "given the adopt option" do
let(:adopt) { true }
describe "when the target compares different from the source" do
it "avoids clobbering the existing app" do
stdout = <<~EOS
==> Adopting existing App at '#{target_path}'
EOS
expect { install_phase }
.to output(stdout).to_stdout
.and raise_error(
Cask::CaskError,
"It seems the existing App is different from the one being installed.",
)
expect(source_path).to be_a_directory
expect(target_path).to be_a_directory
expect(File.identical?(source_path, target_path)).to be false
contents_path = target_path.join("Contents/Info.plist")
expect(contents_path).not_to exist
end
end
describe "when the target compares the same as the source" do
before do
target_path.delete
FileUtils.cp_r source_path, target_path
end
it "adopts the existing app" do
stdout = <<~EOS
==> Adopting existing App at '#{target_path}'
EOS
stderr = ""
expect { install_phase }
.to output(stdout).to_stdout
.and output(stderr).to_stderr
expect(source_path).to be_a_symlink
expect(target_path).to be_a_directory
contents_path = target_path.join("Contents/Info.plist")
expect(contents_path).to exist
end
end
end
describe "given the force option" do
let(:force) { true }
before do
allow(User).to receive(:current).and_return(User.new("fake_user"))
end
describe "target is both writable and user-owned" do
it "overwrites the existing app" do
stdout = <<~EOS
==> Removing App '#{target_path}'
==> Moving App 'Caffeine.app' to '#{target_path}'
EOS
stderr = <<~EOS
Warning: It seems there is already an App at '#{target_path}'; overwriting.
EOS
expect { install_phase }
.to output(stdout).to_stdout
.and output(stderr).to_stderr
expect(source_path).to be_a_symlink
expect(target_path).to be_a_directory
contents_path = target_path.join("Contents/Info.plist")
expect(contents_path).to exist
end
end
describe "target is user-owned but contains read-only files" do
before do
FileUtils.touch "#{target_path}/foo"
FileUtils.chmod 0555, target_path
end
after do
FileUtils.chmod 0755, target_path
end
it "overwrites the existing app" do
expect(command).to receive(:run).and_call_original.at_least(:once)
stdout = <<~EOS
==> Removing App '#{target_path}'
==> Moving App 'Caffeine.app' to '#{target_path}'
EOS
stderr = <<~EOS
Warning: It seems there is already an App at '#{target_path}'; overwriting.
EOS
expect { install_phase }
.to output(stdout).to_stdout
.and output(stderr).to_stderr
expect(source_path).to be_a_symlink
expect(target_path).to be_a_directory
contents_path = target_path.join("Contents/Info.plist")
expect(contents_path).to exist
end
end
end
end
describe "when the target is a broken symlink" do
let(:deleted_path) { cask.staged_path.join("Deleted.app") }
before do
deleted_path.mkdir
File.symlink(deleted_path, target_path)
deleted_path.rmdir
end
it "leaves the target alone" do
expect { install_phase }.to raise_error(
Cask::CaskError, "It seems there is already an App at '#{target_path}'."
)
expect(target_path).to be_a_symlink
end
describe "given the force option" do
let(:force) { true }
it "overwrites the existing app" do
stdout = <<~EOS
==> Removing App '#{target_path}'
==> Moving App 'Caffeine.app' to '#{target_path}'
EOS
stderr = <<~EOS
Warning: It seems there is already an App at '#{target_path}'; overwriting.
EOS
expect { install_phase }
.to output(stdout).to_stdout
.and output(stderr).to_stderr
expect(source_path).to be_a_symlink
expect(target_path).to be_a_directory
contents_path = target_path.join("Contents/Info.plist")
expect(contents_path).to exist
end
end
end
it "gives a warning if the source doesn't exist" do
source_path.rmtree
message = "It seems the App source '#{source_path}' is not there."
expect { install_phase }.to raise_error(Cask::CaskError, message)
end
end
describe "uninstall_phase" do
after do
FileUtils.chmod 0755, target_path if target_path.exist?
FileUtils.chmod 0755, source_path if source_path.exist?
end
it "deletes managed apps" do
install_phase
expect(target_path).to exist
uninstall_phase
expect(target_path).not_to exist
end
it "backs up read-only managed apps" do
install_phase
FileUtils.chmod 0544, target_path
uninstall_phase
expect(source_path).to be_a_directory
end
end
describe "summary" do
let(:description) { app.class.english_description }
let(:contents) { app.summarize_installed }
it "returns the correct english_description" do
expect(description).to eq("Apps")
end
describe "app is correctly installed" do
it "returns the path to the app" do
install_phase
expect(contents).to eq("#{target_path} (#{target_path.abv})")
end
end
describe "app is missing" do
it "returns a warning and the supposed path to the app" do
expect(contents).to match(/.*Missing App.*: #{target_path}/)
end
end
end
describe "upgrade" do
before do
install_phase
end
# Fix for https://github.com/Homebrew/homebrew-cask/issues/102721
it "reuses the same directory" do
contents_path = target_path.join("Contents/Info.plist")
expect(target_path).to exist
inode = target_path.stat.ino
expect(contents_path).to exist
app.uninstall_phase(command:, force:, successor: cask)
expect(target_path).to exist
expect(target_path.children).to be_empty
expect(contents_path).not_to exist
app.install_phase(command:, adopt:, force:, predecessor: cask)
expect(target_path).to exist
expect(target_path.stat.ino).to eq(inode)
expect(contents_path).to exist
end
describe "when the system blocks modifying apps" do
it "uninstalls and reinstalls the app" do
target_contents_path = target_path.join("Contents")
expect(File).to receive(:write).with(target_path / ".homebrew-write-test",
instance_of(String)).and_raise(Errno::EACCES)
app.uninstall_phase(command:, force:, successor: cask)
expect(target_path).not_to exist
app.install_phase(command:, adopt:, force:, predecessor: cask)
expect(target_contents_path).to exist
end
end
describe "when the directory is owned by root" do
before do
allow(app.target).to receive_messages(writable?: false, owned?: false)
end
it "reuses the same directory" do
source_contents_path = source_path.join("Contents")
target_contents_path = target_path.join("Contents")
allow(command).to receive(:run!).with(any_args).and_call_original
expect(command).to receive(:run!)
.with("/bin/cp", args: ["-pR", source_contents_path, target_path],
sudo: true)
.and_call_original
expect(FileUtils).not_to receive(:move).with(source_contents_path, an_instance_of(Pathname))
app.uninstall_phase(command:, force:, successor: cask)
expect(target_contents_path).not_to exist
expect(target_path).to exist
expect(source_contents_path).to exist
app.install_phase(command:, adopt:, force:, predecessor: cask)
expect(target_contents_path).to exist
end
describe "when the system blocks modifying apps" do
it "uninstalls and reinstalls the app" do
target_contents_path = target_path.join("Contents")
allow(command).to receive(:run!).with(any_args).and_call_original
expect(command).to receive(:run!)
.with("touch", args: [target_path / ".homebrew-write-test"],
print_stderr: false,
sudo: true)
.and_raise(ErrorDuringExecution.new([], status: 1,
output: [[:stderr, "touch: #{target_path}/.homebrew-write-test: Operation not permitted\n"]], secrets: []))
app.uninstall_phase(command:, force:, successor: cask)
expect(target_path).not_to exist
app.install_phase(command:, adopt:, force:, predecessor: cask)
expect(target_contents_path).to exist
end
end
end
end
end