Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass optional options argument to readdir
Close #84
- Loading branch information
Showing
2 changed files
with
84 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var t = require("tap") | ||
var fs = require("fs") | ||
|
||
var currentTest | ||
|
||
var strings = ['b', 'z', 'a'] | ||
var buffs = strings.map(function (s) { return new Buffer(s) }) | ||
var hexes = buffs.map(function (b) { return b.toString('hex') }) | ||
|
||
function getRet (encoding) { | ||
switch (encoding) { | ||
case 'hex': | ||
return hexes | ||
case 'buffer': | ||
return buffs | ||
default: | ||
return strings | ||
} | ||
} | ||
|
||
var readdir = fs.readdir | ||
var failed = false | ||
fs.readdir = function(path, options, cb) { | ||
if (!failed) { | ||
// simulate an EMFILE and then open and close a thing to retry | ||
failed = true | ||
process.nextTick(function () { | ||
var er = new Error('synthetic emfile') | ||
er.code = 'EMFILE' | ||
cb(er) | ||
process.nextTick(function () { | ||
fs.closeSync(fs.openSync(__filename, 'r')) | ||
}) | ||
}) | ||
return | ||
} | ||
|
||
failed = false | ||
currentTest.isa(cb, 'function') | ||
currentTest.isa(options, 'object') | ||
currentTest.ok(options) | ||
process.nextTick(function() { | ||
var ret = getRet(options.encoding) | ||
cb(null, ret) | ||
}) | ||
} | ||
|
||
var g = require("../") | ||
|
||
var encodings = ['buffer', 'hex', 'utf8', null] | ||
encodings.forEach(function (enc) { | ||
t.test('encoding=' + enc, function (t) { | ||
currentTest = t | ||
g.readdir("whatevers", { encoding: enc }, function (er, files) { | ||
if (er) | ||
throw er | ||
t.same(files, getRet(enc).sort()) | ||
t.end() | ||
}) | ||
}) | ||
}) |