forked from Homebrew/brew
-
Notifications
You must be signed in to change notification settings - Fork 1
/
named_args_spec.rb
338 lines (266 loc) 路 10.8 KB
/
named_args_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
# typed: false
# frozen_string_literal: true
require "cli/named_args"
def setup_unredable_formula(name)
error = FormulaUnreadableError.new(name, "testing")
allow(Formulary).to receive(:factory).with(name, force_bottle: false, flags: []).and_raise(error)
end
def setup_unredable_cask(name)
error = Cask::CaskUnreadableError.new(name, "testing")
allow(Cask::CaskLoader).to receive(:load).with(name).and_raise(error)
allow(Cask::CaskLoader).to receive(:load).with(name, config: nil).and_raise(error)
config = instance_double(Cask::Config)
allow(Cask::Config).to receive(:from_args).and_return(config)
allow(Cask::CaskLoader).to receive(:load).with(name, config: config).and_raise(error)
end
describe Homebrew::CLI::NamedArgs do
let(:foo) do
formula "foo" do
url "https://brew.sh"
version "1.0"
end
end
let(:bar) do
formula "bar" do
url "https://brew.sh"
version "1.0"
end
end
let(:baz) do
Cask::CaskLoader.load(+<<~RUBY)
cask "baz" do
version "1.0"
end
RUBY
end
let(:foo_cask) do
Cask::CaskLoader.load(+<<~RUBY)
cask "foo" do
version "1.0"
end
RUBY
end
describe "#to_formulae" do
it "returns formulae" do
stub_formula_loader foo, call_original: true
stub_formula_loader bar
expect(described_class.new("foo", "bar").to_formulae).to eq [foo, bar]
end
it "raises an error when a Formula is unavailable" do
expect { described_class.new("mxcl").to_formulae }.to raise_error FormulaUnavailableError
end
it "returns an empty array when there are no Formulae" do
expect(described_class.new.to_formulae).to be_empty
end
end
describe "#to_formulae_and_casks" do
it "returns formulae and casks" do
stub_formula_loader foo, call_original: true
stub_cask_loader baz, call_original: true
expect(described_class.new("foo", "baz").to_formulae_and_casks).to eq [foo, baz]
end
context "when both formula and cask are present" do
before do
stub_formula_loader foo
stub_cask_loader foo_cask
end
it "returns formula by default" do
expect(described_class.new("foo").to_formulae_and_casks).to eq [foo]
end
it "returns formula if loading formula only" do
expect(described_class.new("foo").to_formulae_and_casks(only: :formula)).to eq [foo]
end
it "returns cask if loading cask only" do
expect(described_class.new("foo").to_formulae_and_casks(only: :cask)).to eq [foo_cask]
end
end
context "when both formula and cask are unreadable" do
before do
setup_unredable_formula "foo"
setup_unredable_cask "foo"
end
it "raises an error" do
expect { described_class.new("foo").to_formulae_and_casks }.to raise_error(FormulaUnreadableError)
end
it "raises an error if loading formula only" do
expect { described_class.new("foo").to_formulae_and_casks(only: :formula) }
.to raise_error(FormulaUnreadableError)
end
it "raises an error if loading cask only" do
expect { described_class.new("foo").to_formulae_and_casks(only: :cask) }
.to raise_error(Cask::CaskUnreadableError)
end
end
it "raises an error when neither formula nor cask is present" do
expect { described_class.new("foo").to_formulae_and_casks }.to raise_error(FormulaOrCaskUnavailableError)
end
it "returns formula when formula is present and cask is unreadable" do
stub_formula_loader foo
setup_unredable_cask "foo"
expect(described_class.new("foo").to_formulae_and_casks).to eq [foo]
expect { described_class.new("foo").to_formulae_and_casks }.to output(/Failed to load cask: foo/).to_stderr
end
it "returns cask when formula is unreadable and cask is present" do
setup_unredable_formula "foo"
stub_cask_loader foo_cask
expect(described_class.new("foo").to_formulae_and_casks).to eq [foo_cask]
expect { described_class.new("foo").to_formulae_and_casks }.to output(/Failed to load formula: foo/).to_stderr
end
it "raises an error when formula is absent and cask is unreadable" do
setup_unredable_cask "foo"
expect { described_class.new("foo").to_formulae_and_casks }.to raise_error(Cask::CaskUnreadableError)
end
it "raises an error when formula is unreadable and cask is absent" do
setup_unredable_formula "foo"
expect { described_class.new("foo").to_formulae_and_casks }.to raise_error(FormulaUnreadableError)
end
end
describe "#to_resolved_formulae" do
it "returns resolved formulae" do
allow(Formulary).to receive(:resolve).and_return(foo, bar)
expect(described_class.new("foo", "bar").to_resolved_formulae).to eq [foo, bar]
end
end
describe "#to_resolved_formulae_to_casks" do
it "returns resolved formulae, as well as casks" do
allow(Formulary).to receive(:resolve).and_call_original
allow(Formulary).to receive(:resolve).with("foo", any_args).and_return foo
stub_cask_loader baz, call_original: true
resolved_formulae, casks = described_class.new("foo", "baz").to_resolved_formulae_to_casks
expect(resolved_formulae).to eq [foo]
expect(casks).to eq [baz]
end
end
describe "#to_casks" do
it "returns casks" do
stub_cask_loader baz
expect(described_class.new("baz").to_casks).to eq [baz]
end
end
describe "#to_kegs" do
before do
(HOMEBREW_CELLAR/"foo/1.0").mkpath
(HOMEBREW_CELLAR/"foo/2.0").mkpath
(HOMEBREW_CELLAR/"bar/1.0").mkpath
end
it "resolves kegs with #resolve_kegs" do
expect(described_class.new("foo", "bar").to_kegs.map(&:name)).to eq ["foo", "foo", "bar"]
end
it "resolves kegs with multiple versions with #resolve_keg" do
expect(described_class.new("foo").to_kegs.map { |k| k.version.version.to_s }.sort).to eq ["1.0", "2.0"]
end
it "when there are no matching kegs returns an empty array" do
expect(described_class.new.to_kegs).to be_empty
end
end
describe "#to_default_kegs" do
before do
(HOMEBREW_CELLAR/"foo/1.0").mkpath
(HOMEBREW_CELLAR/"bar/1.0").mkpath
linked_path = (HOMEBREW_CELLAR/"foo/2.0")
linked_path.mkpath
Keg.new(linked_path).link
end
it "resolves kegs with #resolve_default_keg" do
expect(described_class.new("foo", "bar").to_default_kegs.map(&:name)).to eq ["foo", "bar"]
end
it "resolves the default keg" do
expect(described_class.new("foo").to_default_kegs.map { |k| k.version.version.to_s }).to eq ["2.0"]
end
it "when there are no matching kegs returns an empty array" do
expect(described_class.new.to_default_kegs).to be_empty
end
end
describe "#to_latest_kegs" do
before do
(HOMEBREW_CELLAR/"foo/1.0").mkpath
(HOMEBREW_CELLAR/"foo/2.0").mkpath
(HOMEBREW_CELLAR/"bar/1.0").mkpath
end
it "resolves the latest kegs with #resolve_latest_keg" do
latest_kegs = described_class.new("foo", "bar").to_latest_kegs
expect(latest_kegs.map(&:name)).to eq ["foo", "bar"]
expect(latest_kegs.map { |k| k.version.version.to_s }).to eq ["2.0", "1.0"]
end
it "when there are no matching kegs returns an empty array" do
expect(described_class.new.to_latest_kegs).to be_empty
end
end
describe "#to_kegs_to_casks" do
before do
(HOMEBREW_CELLAR/"foo/1.0").mkpath
end
it "returns kegs, as well as casks" do
stub_cask_loader baz, call_original: true
kegs, casks = described_class.new("foo", "baz").to_kegs_to_casks
expect(kegs.map(&:name)).to eq ["foo"]
expect(casks).to eq [baz]
end
end
describe "#homebrew_tap_cask_names" do
it "returns an array of casks from homebrew-cask" do
expect(described_class.new("foo", "homebrew/cask/local-caffeine").homebrew_tap_cask_names)
.to eq ["homebrew/cask/local-caffeine"]
end
it "returns an empty array when there are no matching casks" do
expect(described_class.new("foo").homebrew_tap_cask_names).to be_empty
end
end
describe "#to_paths" do
let(:existing_path) { mktmpdir }
let(:formula_path) { Pathname("/path/to/foo.rb") }
let(:cask_path) { Pathname("/path/to/baz.rb") }
before do
allow(formula_path).to receive(:exist?).and_return(true)
allow(cask_path).to receive(:exist?).and_return(true)
allow(Formulary).to receive(:path).and_call_original
allow(Cask::CaskLoader).to receive(:path).and_call_original
end
it "returns taps, cask formula and existing paths" do
expect(Formulary).to receive(:path).with("foo").and_return(formula_path)
expect(Cask::CaskLoader).to receive(:path).with("baz").and_return(cask_path)
expect(described_class.new("homebrew/core", "foo", "baz", existing_path.to_s).to_paths)
.to eq [Tap.fetch("homebrew/core").path, formula_path, cask_path, existing_path]
end
it "returns both cask and formula paths if they exist" do
expect(Formulary).to receive(:path).with("foo").and_return(formula_path)
expect(Cask::CaskLoader).to receive(:path).with("baz").and_return(cask_path)
expect(described_class.new("foo", "baz").to_paths).to eq [formula_path, cask_path]
end
it "returns only formulae when `only: :formula` is specified" do
expect(Formulary).to receive(:path).with("foo").and_return(formula_path)
expect(described_class.new("foo", "baz").to_paths(only: :formula)).to eq [formula_path, Formulary.path("baz")]
end
it "returns only casks when `only: :cask` is specified" do
expect(Cask::CaskLoader).to receive(:path).with("foo").and_return(cask_path)
expect(described_class.new("foo", "baz").to_paths(only: :cask)).to eq [cask_path, Cask::CaskLoader.path("baz")]
end
end
describe "#to_taps" do
it "returns taps" do
taps = described_class.new("homebrew/foo", "bar/baz")
expect(taps.to_taps.map(&:name)).to eq %w[homebrew/foo bar/baz]
end
it "raises an error for invalid tap" do
taps = described_class.new("homebrew/foo", "barbaz")
expect { taps.to_taps }.to raise_error(RuntimeError, /Invalid tap name/)
end
end
describe "#to_installed_taps" do
before do
(HOMEBREW_REPOSITORY/"Library/Taps/homebrew/homebrew-foo").mkpath
end
it "returns installed taps" do
taps = described_class.new("homebrew/foo")
expect(taps.to_installed_taps.map(&:name)).to eq %w[homebrew/foo]
end
it "raises an error for uninstalled tap" do
taps = described_class.new("homebrew/foo", "bar/baz")
expect { taps.to_installed_taps }.to raise_error(TapUnavailableError)
end
it "raises an error for invalid tap" do
taps = described_class.new("homebrew/foo", "barbaz")
expect { taps.to_installed_taps }.to raise_error(RuntimeError, /Invalid tap name/)
end
end
end