forked from benhoskings/babushka
/
source_spec.rb
351 lines (336 loc) · 12.2 KB
/
source_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
require 'spec_helper'
require 'source_support'
describe Source, "arguments" do
it "should reject non-hash options" do
L{
Source.new 'a', 'b'
}.should raise_error(ArgumentError, 'Source.new options must be passed as a hash, not as "b".')
end
end
describe Source, '.discover_uri_and_type' do
it "should label nil paths as implicit" do
Source.discover_uri_and_type(nil).should == [nil, :implicit]
end
it "should work for public uris" do
[
'git://github.com/benhoskings/babushka-deps.git',
'http://github.com/benhoskings/babushka-deps.git',
'file://Users/ben/babushka/deps'
].each {|uri|
Source.discover_uri_and_type(uri).should == [URI.parse(uri), :public]
}
end
it "should work for private uris" do
[
'git@github.com:benhoskings/babushka-deps.git',
'benhoskin.gs:~ben/babushka-deps.git'
].each {|uri|
Source.discover_uri_and_type(uri).should == [uri, :private]
}
end
it "should work for local paths" do
Source.discover_uri_and_type('~/.babushka/deps').should == ['~/.babushka/deps'.p, :local]
Source.discover_uri_and_type('/tmp/babushka-deps').should == ['/tmp/babushka-deps', :local]
end
end
describe Source, '#uri_matches?' do
it "should match on equivalent URIs" do
Source.new(nil).uri_matches?(nil).should be_true
Source.new('~/.babushka/deps').uri_matches?('~/.babushka/deps').should be_true
Source.new('git://github.com/benhoskings/babushka-deps.git').uri_matches?('git://github.com/benhoskings/babushka-deps.git').should be_true
Source.new('git@github.com:benhoskings/babushka-deps.git').uri_matches?('git@github.com:benhoskings/babushka-deps.git').should be_true
end
it "should not match on differing URIs" do
Source.new(nil).uri_matches?('').should be_false
Source.new('~/.babushka/deps').uri_matches?('~/.babushka/babushka-deps').should be_false
Source.new('git://github.com/benhoskings/babushka-deps.git').uri_matches?('http://github.com/benhoskings/babushka-deps.git').should be_false
Source.new('git://github.com/benhoskings/babushka-deps.git').uri_matches?('git://github.com/benhoskings/babushka-deps').should be_false
Source.new('git@github.com:benhoskings/babushka-deps.git').uri_matches?('github.com:benhoskings/babushka-deps.git').should be_false
Source.new('git@github.com:benhoskings/babushka-deps.git').uri_matches?('git@github.com:benhoskings/babushka-deps').should be_false
end
end
describe Source, '#path' do
it "should work for implicit sources" do
Source.new(nil).path.should == nil
end
it "should work for local sources" do
Source.new('~/.babushka/deps').path.should == '~/.babushka/deps'.p
end
context "cloneable repos" do
context "without names" do
it "should work for public sources" do
Source.new('git://github.com/benhoskings/babushka-deps.git').path.should == tmp_prefix / 'sources/babushka-deps'
end
it "should work for private sources" do
Source.new('git@github.com:benhoskings/babushka-deps.git').path.should == tmp_prefix / 'sources/babushka-deps'
end
end
context "with names" do
it "should work for public sources" do
Source.new('git://github.com/benhoskings/babushka-deps.git', :name => 'custom_public_deps').path.should == tmp_prefix / 'sources/custom_public_deps'
end
it "should work for private sources" do
Source.new('git@github.com:benhoskings/babushka-deps.git', :name => 'custom_private_deps').path.should == tmp_prefix / 'sources/custom_private_deps'
end
end
end
end
describe "loading deps" do
context "with a good source" do
before {
@source = Source.new('spec/deps/good')
Base.task.verb = Babushka::Base::PassedVerb.new nil, {}, [], {}
@source.stub!(:define_deps!)
@source.load!
}
it "should load deps from a file" do
@source.deps.names.should include('test dep 1')
@source.deps.names.should include('test dep 2')
end
it "should not have defined the deps" do
dep = @source.deps.for('test dep 1')
dep.dep_defined?.should be_false
end
it "should store the source the dep was loaded from" do
@source.deps.for('test dep 1').dep_source.should == @source
end
end
context "with a source with errors" do
before {
@source = Source.new('spec/deps/bad')
@source.stub!(:define_deps!)
@source.load!
}
it "should recover from load errors" do
@source.deps.names.should include('broken test dep 1')
@source.deps.names.should include('test dep 1')
end
end
end
describe "defining deps" do
before {
@source = Source.new('spec/deps/good')
@source.load!
}
context "after loading" do
before {
@dep = @source.deps.for('test dep 1')
}
it "should not have defined the deps" do
@dep.dep_defined?.should == nil
end
end
end
describe "equality" do
before {
@remote_1 = test_dep_source 'equality_1'
@remote_2 = test_dep_source 'equality_2'
}
it "should be equal when uri, name and type are the same" do
(Source.new(*@remote_1) == Source.new(*@remote_1)).should be_true
end
it "shouldn't be equal when the name differs" do
(Source.new(*@remote_1) == Source.new(@remote_1.first, :name => 'remote_other')).should be_false
end
it "shouldn't be equal when the uri differs" do
(Source.new(*@remote_1) == Source.new(@remote_2.first, :name => 'remote_1')).should be_false
end
end
describe Source, ".for_path" do
context "on a file" do
before {
`mkdir -p "#{tmp_prefix / 'sources'}"`
`touch "#{tmp_prefix / 'sources/regular_file'}"`
}
it "should raise when called on a file" do
L{
Source.for_path(Source.source_prefix / 'regular_file')
}.should raise_error(ArgumentError, "The path #{Source.source_prefix / 'regular_file'} isn't a directory.")
end
end
context "on a dir" do
before {
`mkdir -p "#{tmp_prefix / 'ad_hoc_source'}"`
@source = Source.for_path(tmp_prefix / 'ad_hoc_source')
}
it "should work on a dir" do
@source.should be_present
@source.path.should == tmp_prefix / 'ad_hoc_source'
@source.name.should == 'ad_hoc_source'
end
end
context "on a git repo" do
before {
remote = test_dep_source 'path_remote_test'
Source.new(remote.first).add!
@source = Source.for_path(Source.source_prefix / 'path_remote_test')
}
it "should work on a git repo" do
@source.should be_present
@source.path.should == Source.source_prefix / 'path_remote_test'
@source.name.should == 'path_remote_test'
end
after { @source.remove! }
end
context "on a git repo with a custom name" do
before {
remote = test_dep_source 'custom_path_remote_test'
Source.new(remote.first, :name => 'custom_name_test').add!
@source = Source.for_path(Source.source_prefix / 'custom_name_test')
}
it "should work on a git repo" do
@source.should be_present
@source.path.should == Source.source_prefix / 'custom_name_test'
@source.name.should == 'custom_name_test'
end
after { @source.remove! }
end
end
describe Source, '.present' do
before {
@source_1 = Source.new(*test_dep_source('present_remote_1_test'))
@source_1.add!
@source_2 = Source.new(*test_dep_source('present_remote_2_test'))
@source_2.add!
@source_3 = Source.new(*test_dep_source('present_remote_3_test'))
}
it "should return the sources that are present" do
Source.present.should == [@source_1, @source_2]
end
end
describe "finding" do
before {
@source = Source.new('spec/deps/good')
@source.load!
}
it "should find the specified dep" do
@source.find('test dep 1').should be_an_instance_of(Dep)
@source.deps.deps.include?(@source.find('test dep 1')).should be_true
end
it "should find the specified template" do
@source.find_template('test meta 1').should be_an_instance_of(MetaDep)
@source.templates.templates.include?(@source.find_template('test meta 1')).should be_true
end
end
describe Source, "#present?" do
context "for local repos" do
it "should be true for valid paths" do
Source.new('spec/deps/good').should be_present
end
it "should be false for invalid paths" do
Source.new('spec/deps/nonexistent').should_not be_present
end
end
context "for remote repos" do
before { @present_source = test_dep_source 'present_test' }
it "should be false" do
Source.new(@present_source.first).should_not be_present
end
context "after cloning" do
before { Source.new(@present_source.first).add! }
it "should be true" do
Source.new(@present_source.first).should be_present
end
end
end
end
describe "cloning" do
context "unreadable sources" do
before {
@source = Source.new(tmp_prefix / "nonexistent.git", :name => 'unreadable')
@source.add!
}
it "shouldn't work" do
@source.path.should_not be_exists
end
end
context "readable sources" do
before {
@source = Source.new(*test_dep_source('clone_test'))
}
it "should clone a git repo" do
@source.path.should_not be_exists
@source.add!
@source.path.should be_exists
end
it "should not be available in Base.sources" do
Base.sources.current.include?(@source).should be_false
end
it "should be cloned into the source prefix" do
@source.path.to_s.starts_with?((tmp_prefix / 'sources').p.to_s).should be_true
end
context "without a name" do
before {
@nameless = Source.new(test_dep_source('nameless_test').first)
@nameless.add!
}
it "should use the basename as the name" do
File.directory?(tmp_prefix / 'sources/nameless_test').should be_true
end
it "should set the name in the source" do
@nameless.name.should == 'nameless_test'
end
end
context "with a name" do
before {
@fancypath_name = 'aliased_source_test'.p.basename
@aliased = Source.new(test_dep_source('aliased').first, :name => @fancypath_name)
@aliased.add!
}
it "should override the name" do
File.directory?(tmp_prefix / 'sources/aliased_source_test').should be_true
end
it "should set the name in the source" do
@aliased.name.should == 'aliased_source_test'
end
it "should stringify the name" do
@fancypath_name.should be_an_instance_of(Fancypath)
@aliased.name.should be_an_instance_of(String)
end
end
context "duplication" do
before {
@remote = test_dep_source 'duplicate_test'
@dup_remote = test_dep_source 'duplicate_dup'
@source = Source.new @remote.first
@source.add!
}
context "with the same name and URL" do
before {
@dup_source = Source.new(@remote.first, :name => 'duplicate_test')
}
it "should work" do
L{ @dup_source.add! }.should_not raise_error
@dup_source.should == @source
end
end
context "with the same name and different URLs" do
it "should raise an exception and not add anything" do
@dup_source = Source.new(@dup_remote.first, :name => 'duplicate_test')
L{
@dup_source.add!
}.should raise_error(SourceError, "There is already a source called '#{@source.name}' (it contains #{@source.uri}).")
end
end
context "with the same URL and different names" do
it "should raise an exception and not add anything" do
@dup_source = Source.new(@remote.first, :name => 'duplicate_test_different_name')
L{
@dup_source.add!
}.should raise_error(SourceError, "The source #{@source.uri} is already present (as '#{@source.name}').")
end
end
end
end
end
describe "classification" do
before { @source = test_dep_source 'classification_test' }
it "should treat file:// as public" do
(source = Source.new(*@source)).add!
[source.uri, source.name, source.type].should == [URI.parse(@source.first), 'classification_test', :public]
end
it "should treat local paths as local" do
(source = Source.new(@source.first.gsub(/^file:\//, ''), @source.last)).add!
[source.uri, source.name, source.type].should == [@source.first.gsub(/^file:\//, ''), 'classification_test', :local]
end
end