aboutsummaryrefslogtreecommitdiffstats
path: root/vnfmarket/src/main/webapp/vnfmarket/node_modules/readdirp/test/readdirp-stream.js
blob: c43d132d34355f9b4bafe4941e4047cb76eaa60d (plain)
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
/*jshint asi:true */

var debug           //= true;
var test            = debug  ? function () {} : require('tap').test
var test_           = !debug ? function () {} : require('tap').test
  , path            = require('path')
  , fs              = require('fs')
  , util            = require('util')
  , TransformStream = require('readable-stream').Transform
  , through         = require('through2')
  , proxyquire      = require('proxyquire')
  , streamapi       = require('../stream-api')
  , readdirp        = require('..')
  , root            = path.join(__dirname, 'bed')
  , totalDirs       = 6
  , totalFiles      = 12
  , ext1Files       = 4
  , ext2Files       = 3
  , ext3Files       = 2
  ;
  
// see test/readdirp.js for test bed layout

function opts (extend) {
  var o = { root: root };

  if (extend) {
    for (var prop in extend) {
      o[prop] = extend[prop];
    }
  }
  return o;
}

function capture () {
  var result = { entries: [], errors: [], ended: false }
    , dst = new TransformStream({ objectMode: true });

  dst._transform = function (entry, _, cb) {
    result.entries.push(entry);
    cb();
  }

  dst._flush = function (cb) {
    result.ended = true;
    this.push(result); 
    cb();
  }

  return dst;
}

test('\nintegrated', function (t) {
  t.test('\n# reading root without filter', function (t) {
    t.plan(2);

    readdirp(opts())
      .on('error', function (err) {
        t.fail('should not throw error', err);
      })
      .pipe(capture())
      .pipe(through.obj(
        function (result, _ , cb) { 
          t.equals(result.entries.length, totalFiles, 'emits all files');
          t.ok(result.ended, 'ends stream');
          t.end();
          cb();
        }
      ));
  })

  t.test('\n# normal: ["*.ext1", "*.ext3"]', function (t) {
    t.plan(2);

    readdirp(opts( { fileFilter: [ '*.ext1', '*.ext3' ] } ))
      .on('error', function (err) {
        t.fail('should not throw error', err);
      })
      .pipe(capture())
      .pipe(through.obj(
        function (result, _ , cb) { 
          t.equals(result.entries.length, ext1Files + ext3Files, 'all ext1 and ext3 files');
          t.ok(result.ended, 'ends stream');
          t.end();
          cb();
        }
      ))
  })

  t.test('\n# files only', function (t) {
    t.plan(2);

    readdirp(opts( { entryType: 'files' } ))
      .on('error', function (err) {
        t.fail('should not throw error', err);
      })
      .pipe(capture())
      .pipe(through.obj(
        function (result, _ , cb) { 
          t.equals(result.entries.length, totalFiles, 'returned files');
          t.ok(result.ended, 'ends stream');
          t.end();
          cb();
        }
      ))
  })

  t.test('\n# directories only', function (t) {
    t.plan(2);

    readdirp(opts( { entryType: 'directories' } ))
      .on('error', function (err) {
        t.fail('should not throw error', err);
      })
      .pipe(capture())
      .pipe(through.obj(
        function (result, _ , cb) { 
          t.equals(result.entries.length, totalDirs, 'returned directories');
          t.ok(result.ended, 'ends stream');
          t.end();
          cb();
        }
      ))
  })

  t.test('\n# both directories + files', function (t) {
    t.plan(2);

    readdirp(opts( { entryType: 'both' } ))
      .on('error', function (err) {
        t.fail('should not throw error', err);
      })
      .pipe(capture())
      .pipe(through.obj(
        function (result, _ , cb) { 
          t.equals(result.entries.length, totalDirs + totalFiles, 'returned everything');
          t.ok(result.ended, 'ends stream');
          t.end();
          cb();
        }
      ))
  })

  t.test('\n# directory filter with directories only', function (t) {
    t.plan(2);

    readdirp(opts( { entryType: 'directories', directoryFilter: [ 'root_dir1', '*dir1_subdir1' ] } ))
      .on('error', function (err) {
        t.fail('should not throw error', err);
      })
      .pipe(capture())
      .pipe(through.obj(
        function (result, _ , cb) {
          t.equals(result.entries.length, 2, 'two directories');
          t.ok(result.ended, 'ends stream');
          t.end();
          cb();
        }
      ))
  })

  t.test('\n# directory and file filters with both entries', function (t) {
    t.plan(2);

    readdirp(opts( { entryType: 'both', directoryFilter: [ 'root_dir1', '*dir1_subdir1' ], fileFilter: [ '!*.ext1' ] } ))
      .on('error', function (err) {
        t.fail('should not throw error', err);
      })
      .pipe(capture())
      .pipe(through.obj(
        function (result, _ , cb) {
          t.equals(result.entries.length, 6, '2 directories and 4 files');
          t.ok(result.ended, 'ends stream');
          t.end();
          cb();
        }
      ))
  })

  t.test('\n# negated: ["!*.ext1", "!*.ext3"]', function (t) {
    t.plan(2);

    readdirp(opts( { fileFilter: [ '!*.ext1', '!*.ext3' ] } ))
      .on('error', function (err) {
        t.fail('should not throw error', err);
      })
      .pipe(capture())
      .pipe(through.obj(
        function (result, _ , cb) { 
          t.equals(result.entries.length, totalFiles - ext1Files - ext3Files, 'all but ext1 and ext3 files');
          t.ok(result.ended, 'ends stream');
          t.end();
        }
      ))
  })

  t.test('\n# no options given', function (t) {
    t.plan(1);
    readdirp()
      .on('error', function (err) {
        t.similar(err.toString() , /Need to pass at least one argument/ , 'emits meaningful error');
        t.end();
      })
  })

  t.test('\n# mixed: ["*.ext1", "!*.ext3"]', function (t) {
    t.plan(1);

    readdirp(opts( { fileFilter: [ '*.ext1', '!*.ext3' ] } ))
      .on('error', function (err) {
        t.similar(err.toString() , /Cannot mix negated with non negated glob filters/ , 'emits meaningful error');
        t.end();
      })
  })
})


test('\napi separately', function (t) {

  t.test('\n# handleError', function (t) {
    t.plan(1);

    var api = streamapi()
      , warning = new Error('some file caused problems');

    api.stream
      .on('warn', function (err) {
        t.equals(err, warning, 'warns with the handled error');
      })
    api.handleError(warning);
  })

  t.test('\n# when stream is paused and then resumed', function (t) {
    t.plan(6);
    var api = streamapi()
      , resumed = false
      , fatalError = new Error('fatal!')
      , nonfatalError = new Error('nonfatal!')
      , processedData = 'some data'
      ;

    api.stream
      .on('warn', function (err) {
        t.equals(err, nonfatalError, 'emits the buffered warning');
        t.ok(resumed, 'emits warning only after it was resumed');
      })
      .on('error', function (err) {
        t.equals(err, fatalError, 'emits the buffered fatal error');
        t.ok(resumed, 'emits errors only after it was resumed');
      })
      .on('data', function (data) {
        t.equals(data, processedData, 'emits the buffered data');
        t.ok(resumed, 'emits data only after it was resumed');
      })
      .pause()
    
    api.processEntry(processedData);
    api.handleError(nonfatalError);
    api.handleFatalError(fatalError);
  
    setTimeout(function () {
      resumed = true;
      api.stream.resume();
    }, 1)
  })

  t.test('\n# when a stream is paused it stops walking the fs', function (t) {
    var resumed = false,
      mockedAPI = streamapi();

    mockedAPI.processEntry = function (entry) {
      if (!resumed) t.notOk(true, 'should not emit while paused')
      t.ok(entry, 'emitted while resumed')
    }.bind(mockedAPI.stream)

    function wrapper () {
      return mockedAPI
    }

    var readdirp = proxyquire('../readdirp', {'./stream-api': wrapper})
      , stream = readdirp(opts())
      .on('error', function (err) {
        t.fail('should not throw error', err);
      })
      .on('end', function () {
        t.end()
      })
      .pause();

    setTimeout(function () {
      resumed = true;
      stream.resume();
    }, 5)
  })

  t.test('\n# when a stream is destroyed, it emits "closed", but no longer emits "data", "warn" and "error"', function (t) {
    var api = streamapi()
      , fatalError = new Error('fatal!')
      , nonfatalError = new Error('nonfatal!')
      , processedData = 'some data'
      , plan = 0;

    t.plan(6)
    var stream = api.stream
      .on('warn', function (err) {
        t.ok(!stream._destroyed, 'emits warning until destroyed');
      })
      .on('error', function (err) {
        t.ok(!stream._destroyed, 'emits errors until destroyed');
      })
      .on('data', function (data) {
        t.ok(!stream._destroyed, 'emits data until destroyed');
      })
      .on('close', function () {
        t.ok(stream._destroyed, 'emits close when stream is destroyed');
      })
    

    api.processEntry(processedData);
    api.handleError(nonfatalError);
    api.handleFatalError(fatalError);

    setTimeout(function () {
      stream.destroy()

      t.notOk(stream.readable, 'stream is no longer readable after it is destroyed')

      api.processEntry(processedData);
      api.handleError(nonfatalError);
      api.handleFatalError(fatalError);

      process.nextTick(function () {
        t.pass('emits no more data, warn or error events after it was destroyed')  
        t.end();
      })
    }, 10)
  })
})