cdnget 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,471 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'stringio'
4
+
5
+ require 'minitest'
6
+ require 'minitest/spec'
7
+ require 'minitest/autorun'
8
+ require 'minitest/ok'
9
+
10
+ require 'cdnget'
11
+
12
+
13
+ describe CDNGet do
14
+
15
+ def capture_io(stdin=nil)
16
+ bkup = [$stdin, $stdout, $stderr]
17
+ $stdin = StringIO.new(stdin || "")
18
+ $stdout = StringIO.new
19
+ $stderr = StringIO.new
20
+ begin
21
+ yield
22
+ return $stdout.string(), $stderr.string()
23
+ ensure
24
+ $stdin, $stdout, $stderr = bkup
25
+ end
26
+ end
27
+
28
+
29
+ describe 'cdnget [-h][--help]' do
30
+
31
+ it "prints help message." do
32
+ expected = CDNGet::Main.new("cdnget").help_message()
33
+ ok {CDNGet::Main.new("cdnget").run("-h")} == expected
34
+ ok {CDNGet::Main.new("cdnget").run("--help")} == expected
35
+ end
36
+
37
+ end
38
+
39
+
40
+ describe 'cdnget [-v][--version]' do
41
+
42
+ it "prints help message." do
43
+ expected = CDNGet::RELEASE + "\n"
44
+ ok {CDNGet::Main.new("cdnget").run("-v")} == expected
45
+ ok {CDNGet::Main.new("cdnget").run("--version")} == expected
46
+ end
47
+
48
+ end
49
+
50
+
51
+ describe 'cdnget' do
52
+
53
+ it "lists CDN." do
54
+ expected = <<END
55
+ cdnjs # https://cdnjs.com/
56
+ jsdelivr # https://www.jsdelivr.com/
57
+ google # https://developers.google.com/speed/libraries/
58
+ #jquery # https://code.jquery.com/
59
+ #aspnet # https://www.asp.net/ajax/cdn/
60
+ END
61
+ actual = CDNGet::Main.new().run()
62
+ ok {actual} == expected.gsub(/^\#.*\n/, '')
63
+ end
64
+
65
+ end
66
+
67
+
68
+ describe 'cdnget CDN' do
69
+
70
+ it "(cdnjs) lists librareis." do
71
+ actual = CDNGet::Main.new().run("cdnjs")
72
+ ok {actual} =~ /^jquery # jquery, library, ajax, framework, toolkit, popular$/
73
+ ok {actual} =~ /^angular\.js # framework, mvc, AngularJS, angular, angular2, angular\.js$/
74
+ ok {actual} =~ /^twitter-bootstrap # css, less, mobile-first, responsive, front-end, framework, web, twitter, bootstrap$/
75
+ ok {actual} =~ /^ember\.js # ember, ember.js$/
76
+ end
77
+
78
+ it "(google) lists librareis." do
79
+ actual = CDNGet::Main.new().run("google")
80
+ ok {actual} =~ /^jquery /
81
+ ok {actual} =~ /^angularjs /
82
+ ok {actual} =~ /^webfont /
83
+ end
84
+
85
+ it "(jsdelivr) lists librareis." do
86
+ actual = CDNGet::Main.new().run("jsdelivr")
87
+ ok {actual} =~ /^jquery /
88
+ ok {actual} =~ /^angularjs /
89
+ ok {actual} =~ /^bootstrap /
90
+ end
91
+
92
+ end
93
+
94
+
95
+ describe 'cdnget CDN "jquery*"' do
96
+
97
+ it "(cdnjs) lists libraries starting to pattern." do
98
+ actual = CDNGet::Main.new().run("cdnjs", "jquery*")
99
+ ok {actual} =~ /^jquery #/
100
+ ok {actual} =~ /^jqueryui #/ # match
101
+ ok {actual} !~ /^require-jquery #/ # not match
102
+ ok {actual} !~ /^angular/
103
+ ok {actual} !~ /^twitter-bootstrap/
104
+ ok {actual} !~ /^ember\.js/
105
+ end
106
+
107
+ it "(google) lists libraries starting to pattern." do
108
+ actual = CDNGet::Main.new().run("google", "jquery*")
109
+ ok {actual} =~ /^jquery #/
110
+ ok {actual} =~ /^jqueryui #/ # match
111
+ ok {actual} !~ /^angularjs/
112
+ end
113
+
114
+ it "(jsdelivr) lists libraries starting to pattern." do
115
+ actual = CDNGet::Main.new().run("jsdelivr", "jquery*")
116
+ ok {actual} =~ /^jquery #/
117
+ ok {actual} =~ /^jquery\.ui #/ # match
118
+ ok {actual} !~ /^angularjs/
119
+ ok {actual} !~ /^bootstrap/
120
+ end
121
+
122
+ end
123
+
124
+
125
+ describe 'cdnget cdnjs "*jquery"' do
126
+
127
+ it "(cdnjs) lists libraries ending to pattern." do
128
+ actual = CDNGet::Main.new().run("cdnjs", "*jquery")
129
+ ok {actual} =~ /^jquery #/
130
+ ok {actual} !~ /^jqueryui #/ # not match
131
+ ok {actual} =~ /^require-jquery #/ # match
132
+ ok {actual} !~ /^angular/
133
+ ok {actual} !~ /^twitter-bootstrap/
134
+ ok {actual} !~ /^ember\.js/
135
+ end
136
+
137
+ it "(google) lists libraries ending to pattern." do
138
+ actual = CDNGet::Main.new().run("google", "*jquery")
139
+ ok {actual} =~ /^jquery #/
140
+ ok {actual} !~ /^jqueryui #/ # not match
141
+ ok {actual} !~ /^angularjs/
142
+ end
143
+
144
+ it "(jsdelivr) lists libraries ending to pattern." do
145
+ actual = CDNGet::Main.new().run("jsdelivr", "*jquery")
146
+ ok {actual} =~ /^jquery #/
147
+ ok {actual} !~ /^jquery\.ui #/ # not match
148
+ ok {actual} !~ /^angularjs/
149
+ ok {actual} !~ /^bootstrap/
150
+ end
151
+
152
+ end
153
+
154
+
155
+ describe 'cdnget cdnjs "*jquery*"' do
156
+
157
+ it "(cdnjs) lists libraries including pattern." do
158
+ actual = CDNGet::Main.new().run("cdnjs", "*jquery*")
159
+ ok {actual} =~ /^jquery #/
160
+ ok {actual} =~ /^jqueryui #/ # match
161
+ ok {actual} =~ /^require-jquery #/ # match
162
+ ok {actual} !~ /^angular/
163
+ ok {actual} !~ /^twitter-bootstrap/
164
+ ok {actual} !~ /^ember\.js/
165
+ end
166
+
167
+ it "(google) lists libraries including pattern." do
168
+ actual = CDNGet::Main.new().run("google", "*o*")
169
+ ok {actual} !~ /^jquery /
170
+ ok {actual} !~ /^angularjs /
171
+ ok {actual} =~ /^mootools /
172
+ ok {actual} =~ /^swfobject /
173
+ end
174
+
175
+ it "(google) lists libraries including pattern." do
176
+ actual = CDNGet::Main.new().run("jsdelivr", "*jquery*")
177
+ ok {actual} =~ /^jquery /
178
+ ok {actual} =~ /^jasmine\.jquery /
179
+ ok {actual} =~ /^jquery\.zoom /
180
+ ok {actual} !~ /^angularjs/
181
+ ok {actual} !~ /^bootstrap/
182
+ end
183
+
184
+ end
185
+
186
+
187
+ describe "cdnget CDN jquery" do
188
+
189
+ it "(cdnjs) lists versions of library." do
190
+ actual = CDNGet::Main.new().run("cdnjs", "jquery")
191
+ text1 = <<END
192
+ name: jquery
193
+ desc: JavaScript library for DOM operations
194
+ tags: jquery, library, ajax, framework, toolkit, popular
195
+ versions:
196
+ END
197
+ ok {actual}.start_with?(text1)
198
+ text2 = <<END
199
+ - 1.3.2
200
+ - 1.3.1
201
+ - 1.3.0
202
+ - 1.2.6
203
+ - 1.2.3
204
+ END
205
+ ok {actual}.end_with?(text2)
206
+ ok {actual} =~ /^ - 2\.2\.0$/
207
+ ok {actual} =~ /^ - 1\.12\.0$/
208
+ end
209
+
210
+ it "(google) lists versions of library." do
211
+ actual = CDNGet::Main.new().run("google", "jquery")
212
+ text1 = <<END
213
+ name: jquery
214
+ site: http://jquery.com/
215
+ versions:
216
+ END
217
+ ok {actual}.start_with?(text1)
218
+ text2 = <<END
219
+ - 1.3.2
220
+ - 1.3.1
221
+ - 1.3.0
222
+ - 1.2.6
223
+ - 1.2.3
224
+ END
225
+ ok {actual}.end_with?(text2)
226
+ ok {actual} =~ /^ - 2\.2\.0$/
227
+ ok {actual} =~ /^ - 1\.12\.0$/
228
+ end
229
+
230
+ it "(jsdelivr) lists versions of library." do
231
+ actual = CDNGet::Main.new().run("jsdelivr", "jquery")
232
+ text1 = <<END
233
+ name: jquery
234
+ desc: jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.
235
+ site: http://jquery.com/
236
+ versions:
237
+ END
238
+ ok {actual}.start_with?(text1)
239
+ text2 = <<END
240
+ - 1.7.2
241
+ - 1.7.1
242
+ - 1.7
243
+ - 1.5.1
244
+ - 1.4.4
245
+ END
246
+ ok {actual}.end_with?(text2)
247
+ ok {actual} =~ /^ - 2\.2\.0$/
248
+ ok {actual} =~ /^ - 1\.12\.0$/
249
+ end
250
+
251
+ it "(cdnjs) raises error when library name is wrong." do
252
+ pr = proc { CDNGet::Main.new().run("cdnjs", "jquery-ui") }
253
+ ok {pr}.raise?(CDNGet::CommandError, "jquery-ui: Library not found.")
254
+ #
255
+ pr = proc { CDNGet::Main.new().run("cdnjs", "emberjs", "2.2.1") }
256
+ ok {pr}.raise?(CDNGet::CommandError, "emberjs: Library not found (maybe 'ember.js'?).")
257
+ end
258
+
259
+ it "(google) raises error when library name is wrong." do
260
+ pr = proc { CDNGet::Main.new().run("google", "jquery-ui") }
261
+ ok {pr}.raise?(CDNGet::CommandError, "jquery-ui: Library not found.")
262
+ end
263
+
264
+ it "(jsdelivr) raises error when library name is wrong." do
265
+ pr = proc { CDNGet::Main.new().run("google", "jquery-ui") }
266
+ ok {pr}.raise?(CDNGet::CommandError, "jquery-ui: Library not found.")
267
+ end
268
+
269
+ end
270
+
271
+
272
+ describe "cdnget cdnjs jquery 2.2.0" do
273
+
274
+ it "(cdnjs) lists files." do
275
+ expected = <<END
276
+ name: jquery
277
+ version: 2.2.0
278
+ urls:
279
+ - https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.js
280
+ - https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js
281
+ - https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.map
282
+ END
283
+ actual = CDNGet::Main.new().run("cdnjs", "jquery", "2.2.0")
284
+ ok {actual} == expected
285
+ end
286
+
287
+ it "(google) lists files." do
288
+ expected = <<END
289
+ name: jquery
290
+ version: 2.2.0
291
+ site: http://jquery.com/
292
+ urls:
293
+ - https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js
294
+ END
295
+ actual = CDNGet::Main.new().run("google", "jquery", "2.2.0")
296
+ ok {actual} == expected
297
+ end
298
+
299
+ it "(jsdelivr) lists files." do
300
+ expected = <<END
301
+ name: jquery
302
+ version: 2.2.0
303
+ urls:
304
+ - https://cdn.jsdelivr.net/jquery/2.2.0/jquery.js
305
+ - https://cdn.jsdelivr.net/jquery/2.2.0/jquery.min.js
306
+ - https://cdn.jsdelivr.net/jquery/2.2.0/jquery.min.map
307
+ END
308
+ actual = CDNGet::Main.new().run("jsdelivr", "jquery", "2.2.0")
309
+ ok {actual} == expected
310
+ end
311
+
312
+ it "(cdnjs) lists files containing subdirectory." do
313
+ expected = <<END
314
+ name: jqueryui
315
+ version: 1.9.2
316
+ urls:
317
+ - https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.9.2/i18n/jquery-ui-i18n.js
318
+ - https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.9.2/i18n/jquery-ui-i18n.min.js
319
+ - https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js
320
+ END
321
+ actual = CDNGet::Main.new().run("cdnjs", "jqueryui", "1.9.2")
322
+ ok {actual} == expected
323
+ end
324
+
325
+ it "(google) lists files containing subdirectory." do
326
+ skip("no libraries containing subdirectory")
327
+ end
328
+
329
+ it "(cdnjs) raises error when library name is wrong." do
330
+ pr = proc { CDNGet::Main.new().run("cdnjs", "jquery-ui", "1.9.2") }
331
+ ok {pr}.raise?(CDNGet::CommandError, "jquery-ui: Library not found.")
332
+ #
333
+ pr = proc { CDNGet::Main.new().run("cdnjs", "emberjs", "2.2.1") }
334
+ ok {pr}.raise?(CDNGet::CommandError, "emberjs: Library not found (maybe 'ember.js'?).")
335
+ end
336
+
337
+ it "(google) raises error when library name is wrong." do
338
+ pr = proc { CDNGet::Main.new().run("google", "jquery-ui", "1.9.2") }
339
+ ok {pr}.raise?(CDNGet::CommandError, "jquery-ui: Library not found.")
340
+ end
341
+
342
+ it "(jsdelivr) raises error when library name is wrong." do
343
+ pr = proc { CDNGet::Main.new().run("jsdelivr", "jquery-ui", "1.9.2") }
344
+ ok {pr}.raise?(CDNGet::CommandError, "jquery-ui: Library not found.")
345
+ end
346
+
347
+ end
348
+
349
+
350
+ describe "cdnget CDN jquery 2.2.0 dir" do
351
+
352
+ def _do_download_test1(cdn_code)
353
+ tmpdir = "tmpdir1"
354
+ Dir.mkdir(tmpdir)
355
+ begin
356
+ sout, serr = capture_io() do
357
+ actual = CDNGet::Main.new().run("cdnjs", "jquery", "2.2.0", tmpdir)
358
+ end
359
+ ok {"#{tmpdir}/jquery/2.2.0/jquery.js" }.file_exist?
360
+ ok {"#{tmpdir}/jquery/2.2.0/jquery.min.js" }.file_exist?
361
+ ok {"#{tmpdir}/jquery/2.2.0/jquery.min.map"}.file_exist?
362
+ ok {sout} == <<END
363
+ #{tmpdir}/jquery/2.2.0/jquery.js ... Done (258,388 byte)
364
+ #{tmpdir}/jquery/2.2.0/jquery.min.js ... Done (85,589 byte)
365
+ #{tmpdir}/jquery/2.2.0/jquery.min.map ... Done (129,544 byte)
366
+ END
367
+ ensure
368
+ FileUtils.rm_r(tmpdir)
369
+ end
370
+ end
371
+
372
+ def _do_download_test2(cdn_code)
373
+ tmpdir = "tmpdir1"
374
+ Dir.mkdir(tmpdir)
375
+ expected = <<END
376
+ #{tmpdir}/jqueryui/1.9.2/i18n/jquery-ui-i18n.js ... Done (70,146 byte)
377
+ #{tmpdir}/jqueryui/1.9.2/i18n/jquery-ui-i18n.min.js ... Done (54,926 byte)
378
+ #{tmpdir}/jqueryui/1.9.2/jquery-ui.min.js ... Done (237,802 byte)
379
+ END
380
+ begin
381
+ sout, serr = capture_io() do
382
+ actual = CDNGet::Main.new().run("cdnjs", "jqueryui", "1.9.2", tmpdir)
383
+ end
384
+ ok {"#{tmpdir}/jqueryui/1.9.2/i18n/jquery-ui-i18n.js" }.file_exist?
385
+ ok {"#{tmpdir}/jqueryui/1.9.2/i18n/jquery-ui-i18n.min.js"}.file_exist?
386
+ ok {"#{tmpdir}/jqueryui/1.9.2/jquery-ui.min.js" }.file_exist?
387
+ ok {sout} == expected
388
+ ensure
389
+ FileUtils.rm_r(tmpdir)
390
+ end
391
+ end
392
+
393
+ def _do_download_test3(cdn_code)
394
+ tmpdir = "tmpdir1"
395
+ Dir.mkdir(tmpdir)
396
+ expected = <<END
397
+ #{tmpdir}/jqueryui/1.9.2/i18n/jquery-ui-i18n.js ... Done (70,146 byte)
398
+ #{tmpdir}/jqueryui/1.9.2/i18n/jquery-ui-i18n.min.js ... Done (54,926 byte)
399
+ #{tmpdir}/jqueryui/1.9.2/jquery-ui.min.js ... Done (237,802 byte)
400
+ END
401
+ begin
402
+ path = "#{tmpdir}/jqueryui/1.9.2"
403
+ # 1st
404
+ sout, serr = capture_io() do
405
+ actual = CDNGet::Main.new().run("cdnjs", "jqueryui", "1.9.2", tmpdir)
406
+ end
407
+ ok {serr} == ""
408
+ ok {sout} == expected
409
+ # 2nd
410
+ sout, serr = capture_io() do
411
+ actual = CDNGet::Main.new().run("cdnjs", "jqueryui", "1.9.2", tmpdir)
412
+ end
413
+ ok {serr} == ""
414
+ ok {sout} == expected.gsub(/\n/, " (Unchanged)\n")
415
+ ensure
416
+ FileUtils.rm_r(tmpdir)
417
+ end
418
+ end
419
+
420
+
421
+ it "(cdnjs) downloads files into dir." do
422
+ _do_download_test1("cdnjs")
423
+ end
424
+
425
+ it "(google) downloads files into dir." do
426
+ _do_download_test1("google")
427
+ end
428
+
429
+ it "(jsdelivr) downloads files into dir." do
430
+ _do_download_test1("jsdelivr")
431
+ end
432
+
433
+ it "(cdnjs) downloads files (containing subdir) into dir." do
434
+ _do_download_test2("cdnjs")
435
+ end
436
+
437
+ it "(google) downloads files (containing subdir) into dir." do
438
+ skip("no libraries containing subdirectory")
439
+ end
440
+
441
+ it "(jsdelivr) downloads files (containing subdir) into dir." do
442
+ _do_download_test2("jsdelivr")
443
+ end
444
+
445
+ it "(cdnjs) doesn't override existing files when they are identical to downloaded files." do
446
+ _do_download_test3("cdnjs")
447
+ end
448
+
449
+ it "(google) doesn't override existing files when they are identical to downloaded files." do
450
+ _do_download_test3("google")
451
+ end
452
+
453
+ it "(jsdelivr) doesn't override existing files when they are identical to downloaded files." do
454
+ _do_download_test3("jsdelivr")
455
+ end
456
+
457
+ end
458
+
459
+
460
+ describe "cdnget CDN jquery 2.2.0 foo bar" do
461
+
462
+ it "results in argument error." do
463
+ args = ["cdnjs", "jquery", "2.2.0", "foo", "bar"]
464
+ pr = proc { CDNGet::Main.new("cdnget").run(*args) }
465
+ ok {pr}.raise?(CDNGet::CommandError, "'bar': Too many arguments.")
466
+ end
467
+
468
+ end
469
+
470
+
471
+ end