Skip to content

Commit

Permalink
switching to streams2, through2 in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thlorenz committed Mar 30, 2014
1 parent 333dc21 commit 719fdd5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -31,11 +31,12 @@
},
"dependencies": {
"graceful-fs": "~2.0.0",
"minimatch": "~0.2.12"
"minimatch": "~0.2.12",
"readable-stream": "~1.0.26-2"
},
"devDependencies": {
"tap": "~0.4.3",
"through": "~2.3.4"
"through2": "~0.4.1"
},
"optionalDependencies": {},
"license": "MIT"
Expand Down
59 changes: 31 additions & 28 deletions test/readdirp-stream.js
@@ -1,19 +1,21 @@
/*jshint asi:true */

var test = require('tap').test
, path = require('path')
, fs = require('fs')
, util = require('util')
, Stream = require('stream')
, through = require('through')
, streamapi = require('../stream-api')
, readdirp = require('..')
, root = path.join(__dirname, 'bed')
, totalDirs = 6
, totalFiles = 12
, ext1Files = 4
, ext2Files = 3
, ext3Files = 2
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')
, 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
Expand All @@ -31,19 +33,17 @@ function opts (extend) {

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

dst.writable = true;
dst.readable = true;

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

dst.end = function () {
dst._flush = function (cb) {
result.ended = true;
dst.emit('data', result);
dst.emit('end');
this.push(result);
cb();
}

return dst;
Expand All @@ -52,16 +52,18 @@ function capture () {
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(
function (result) {
.pipe(through.obj(
function (result, _ , cb) {
t.equals(result.entries.length, totalFiles, 'emits all files');
t.ok(result.ended, 'ends stream');
t.end();
cb();
}
));
})
Expand All @@ -74,11 +76,12 @@ test('\nintegrated', function (t) {
t.fail('should not throw error', err);
})
.pipe(capture())
.pipe(through(
function (result) {
.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();
}
))
})
Expand All @@ -91,8 +94,8 @@ test('\nintegrated', function (t) {
t.fail('should not throw error', err);
})
.pipe(capture())
.pipe(through(
function (result) {
.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();
Expand Down

0 comments on commit 719fdd5

Please sign in to comment.