-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathspec.coffee
executable file
·451 lines (307 loc) · 13.5 KB
/
spec.coffee
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
{expect, should} = chai = require 'chai'
should = should()
chai.use require 'sinon-chai'
gulpsmith = require './'
{to_metal, to_vinyl} = gulpsmith
fs = require 'fs'
{resolve, sep:pathsep} = require('path')
File = require 'vinyl'
_ = require 'highland'
Metalsmith = require 'metalsmith'
clone_stats = require 'clone-stats'
expect_fn = (item) -> expect(item).to.exist.and.be.a('function')
{spy} = sinon = require 'sinon'
spy.named = (name, args...) ->
s = if this is spy then spy(args...) else this
s.displayName = name
return s
compare_gulp = (infiles, transform, outfiles, done) ->
_(file for own path, file of infiles)
.pipe(transform).toArray (files) ->
transformed = {}
for file in files
transformed[file.relative] = file
transformed.should.eql outfiles
done()
compare_metal = (infiles, smith, outfiles, done) ->
smith.run infiles, (err, transformed) ->
if err
done(err)
else
transformed.should.eql outfiles
done()
check_mode = (vinylmode, metalmode) ->
(vinylmode).should.equal parseInt(metalmode, 8)
describe "Metal -> Vinyl Conversion", ->
mf = mystat = null
beforeEach ->
mystat = fs.statSync('README.md')
mf = contents: Buffer(''), mode: (mystat.mode).toString(8)
it "assigns a correct relative path", ->
to_vinyl("path1", mf).relative.should.equal "path1"
to_vinyl("README.src", mf).relative.should.equal "README.src"
it "converts Metalsmith .mode to Gulp .stat", ->
check_mode to_vinyl("README.md", mf).stat.mode, mf.mode
mf.mode = (~parseInt(mf.mode, 8)).toString(8)
check_mode to_vinyl("README.md", mf).stat.mode, mf.mode
it "always ignores Metalsmith .stat", ->
delete mf.mode
mf.stat = mystat
expect(to_vinyl("README.md", mf).stat).to.not.exist
it "converts Metalsmith .stats to Gulp .stat", ->
delete mf.mode
mf.stats = mystat
to_vinyl("README.md", mf).stat.should.eql mystat
it "overrides the Gulp .stat.mode with the Metalsmith .mode", ->
cstat = clone_stats(mystat)
mf.stats = mystat
mf.mode = (cstat.mode = ~parseInt(mf.mode, 8)).toString(8)
to_vinyl("README.md", mf).stat.should.eql cstat
it "removes the Metalsmith.mode", ->
expect(to_vinyl("README.md", mf).mode).not.to.exist
it "assigns Metalsmith .contents to Gulp .contents", ->
to_vinyl("xyz", mf).contents.should.equal mf.contents
mf.contents = Buffer("blah blah blah")
to_vinyl("abc", mf).contents.should.eql Buffer("blah blah blah")
it "adds .cwd and .base to files (w/Metalsmith instance given)", ->
verify = (smith) ->
vf = to_vinyl("mnop", mf, smith)
vf.base.should.equal smith.source()
vf.cwd.should.equal (if smith.join then smith.join() else smith.path())
verify smith = Metalsmith "/foo/bar"
verify smith.source "spoon"
verify Metalsmith __dirname
it "copies arbitrary attributes (exactly)", ->
verify = new File(
base: __dirname, cwd: __dirname, stat: mystat, path:resolve "README.md"
)
mf.x = verify.x = 1
mf.y = verify.y = z: 2
mf.stats = mystat
res = to_vinyl("README.md", mf)
delete mf.contents
delete res._contents
delete verify._contents
res.should.eql verify
it "doesn't overwrite the .relative property on Vinyl files", ->
mf.relative = "ping!"
to_vinyl("pong/whiz", mf, Metalsmith __dirname)
.relative.should.equal "pong#{pathsep}whiz"
it "doesn't overwrite any ``vinyl`` methods or properties", ->
for own name of (File::)
mf[name] = "bad data for .#{name}"
for own name of new File()
mf[name] = "bad data for .#{name}"
vf = to_vinyl("what/ever", mf, Metalsmith __dirname)
for own name, prop of (File::)
expect(vf[name]).to.equal prop
for own name of vf
expect(vf[name]).to.not.equal "bad data for .#{name}"
describe "Vinyl -> Metal Conversion", ->
gf = null
beforeEach -> gf = new File(
path: "README.md", contents: Buffer(''), stat: fs.statSync('README.md')
)
it "throws an error for non-buffered (empty or stream) files", ->
expect(
-> to_metal new File(path:"foo.bar")
).to.throw /foo\.bar/
expect(
-> to_metal new File(path:"spam.baz",
contents: fs.createReadStream('README.md')
)).to.throw /spam\.baz/
it "converts Gulp .stat to Metalsmith .mode", ->
(parseInt(to_metal(gf).mode, 8)).should.equal gf.stat.mode & 4095
gf.stat.mode = ~gf.stat.mode
(parseInt(to_metal(gf).mode, 8)).should.equal gf.stat.mode & 4095
it "assigns Gulp .contents to Metalsmith .contents", ->
to_metal(gf).contents.should.equal gf.contents
gf.contents = Buffer("blah blah blah")
to_metal(gf).contents.should.eql Buffer("blah blah blah")
it "copies arbitrary attributes (exactly)", ->
verify =
x: 1, y: {z:2}, stats: gf.stat
gf.x = 1
gf.y = z: 2
res = to_metal(gf)
delete gf.contents
delete res.contents
delete res.mode
res.should.eql verify
it "doesn't keep any ``vinyl`` methods or properties", ->
to_metal(gf).should.not.have.property name for own name of (File::)
to_metal(gf).should.not.have.property name for own name of gf
to_metal(gf).should.not.have.property "relative"
describe "gulpsmith() streams", ->
s = testfiles = null
beforeEach -> s = gulpsmith()
null_plugin = (files, smith, done) -> done()
describe ".use() method", ->
plugin1 = spy.named "plugin1", null_plugin
plugin2 = spy.named "plugin2", null_plugin
it "returns self", -> expect(s.use(plugin1)).to.equal s
it "invokes passed plugins during build", (done) ->
s.use(plugin1).use(plugin2)
_([]).pipe(s).toArray ->
plugin1.should.be.calledOnce.and.calledBefore plugin2
plugin2.should.be.calledOnce.and.calledAfter plugin1
done()
describe ".metadata() method", ->
data = {a: 1, b:2}
it "returns self when setting", ->
expect(s.metadata(data)).to.equal s
it "returns matching metadata when getting", ->
s.metadata(data)
expect(s.metadata()).to.eql data
describe "streaming", ->
beforeEach ->
testfiles =
f1: new File(path:resolve("f1"), contents:Buffer('f1'))
f2: new File(path:resolve("f2"), contents:Buffer('f2'))
testfiles.f1.a = "b"
testfiles.f2.c = 3
it "should yield the same files (if no plugins)", (done) ->
compare_gulp testfiles, s, testfiles, done
it "should delete files deleted by a Metalsmith plugin", (done) ->
s.use (f,s,d) -> delete f.f1; d()
compare_gulp testfiles, s, {f2:testfiles.f2}, done
it "should add files added by a Metalsmith plugin", (done) ->
s.use (files, smith, done) ->
files.f3 = contents:Buffer "f3"
done()
compare_gulp(
{}, s, f3: new File(
path:resolve("f3"), base:__dirname, contents:Buffer "f3"
), done
)
it "yields errors for non-buffered files (and continues)", (done) ->
testfiles.f1.contents = null
done = should_error done, /buffered.*f1/
compare_gulp testfiles, s, {f2:testfiles.f2},
-> done new Error "No error caught"
it "yields errors for errors produced by Metalsmith plugins", (done) ->
error_message = "demo error!"
s.use (files, smith, d) -> d new Error(error_message)
done = should_error done, error_message
_([]).pipe(s).toArray -> done Error "Error wasn't caught"
it "excludes Gulp directories", (done) ->
testfiles.f2.isDirectory = -> true
compare_gulp testfiles, s, {f1:testfiles.f1}, done
it "converts Gulp files to Metalsmith and back", (done) ->
vinyl_spy = spy.named 'vinyl_spy', gulpsmith, 'to_vinyl'
metal_spy = spy.named 'metal_spy', gulpsmith, 'to_metal'
err = metal_files = null
# Capture files coming into Metalsmith
catch_metal = (files, smith, done) ->
metal_files = files
done()
_(file for own path, file of testfiles)
.pipe(gulpsmith().use(catch_metal)).toArray (files) ->
try
for file in files
vinyl_spy.should.have.returned file
metal_spy.should.have.been.calledWithExactly file
for own relative, file of metal_files
vinyl_spy.should.have.been
.calledWithExactly relative, file
metal_spy.should.have.returned file
catch err
return done(err)
finally
vinyl_spy.restore()
metal_spy.restore()
done()
should_error = (done, ematch, etype=Error) ->
s.on "error", (e) ->
try
e.should.be.instanceOf Error
if ematch?
if ematch instanceof RegExp
e.message.should.match ematch
else
e.message.should.equal ematch
cb()
catch err
cb(err)
return cb = ->
done arguments...
done = ->
describe "gulpsmith.pipe() plugins", ->
smith = testfiles = null
it "are functions", ->
expect(gulpsmith.pipe()).to.be.a('function')
describe ".pipe() method", ->
it "is a function", -> expect(gulpsmith.pipe().pipe).to.exist.and.be.a('function')
it "returns a function with another pipe() method", ->
expect(gulpsmith.pipe().pipe().pipe).to.exist.and.be.a('function')
describe "streaming", ->
beforeEach ->
smith = Metalsmith(process.cwd())
testfiles =
f1: contents:Buffer('f1')
f2: contents:Buffer('f2')
testfiles.f1.a = "b"
testfiles.f2.c = 3
it "should yield the same files (if no plugins)", (done) ->
compare_metal testfiles, smith.use(gulpsmith.pipe()), testfiles, done
it "should delete files deleted by a Gulp plugin", (done) ->
s = smith.use gulpsmith.pipe _.where relative: 'f2'
compare_metal testfiles, s, {f2:testfiles.f2}, done
it "should add files added by a Gulp plugin", (done) ->
f3 = new File path: "f3", contents:Buffer "f3"
f3.x = "y"; f3.z = 42
s = smith.use gulpsmith.pipe(_.append f3)
compare_metal {}, s, {f3: {
x: "y", z:42, contents:Buffer "f3"
}}, done
it "converts Metalsmith files to Gulp and back", (done) ->
vinyl_spy = spy.named 'vinyl_spy', gulpsmith, 'to_vinyl'
metal_spy = spy.named 'metal_spy', gulpsmith, 'to_metal'
vinyl_files = []
# Capture files coming into Gulp
catch_vinyl = (file) ->
vinyl_files.push file
return file
smith = Metalsmith(__dirname)
smith.use(gulpsmith.pipe((_.map catch_vinyl)))
smith.run testfiles, (err, files) ->
return done(err) if err?
try
for file in vinyl_files
vinyl_spy.should.have.returned file
metal_spy.should.have.been.calledWithExactly file
for own relative, file of files
vinyl_spy.should.have.been
.calledWithExactly relative, file, smith
metal_spy.should.have.returned file
catch err
return done(err)
finally
vinyl_spy.restore()
metal_spy.restore()
done()
it "exits with any error yielded by a Gulp plugin", (done) ->
message = "dummy error!"
smith.use gulpsmith.pipe _ (push) ->
push new Error message
push null, _.nil
done = should_error done, {}, message
it "exits with an error if a Gulp plugin yields an unbuffered file",
(done) ->
smith.use gulpsmith.pipe _.append new File(path: "README.md")
done = should_error done, {}, /buffered.*README.md/
should_error = (done, files, ematch, etype=Error) ->
smith.run files, (e, files) ->
try
e.should.be.instanceOf Error
if ematch?
if ematch instanceof RegExp
e.message.should.match ematch
else
e.message.should.equal ematch
cb()
catch err
cb(err)
return cb = ->
done arguments...
done = ->