diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js index b8dd308..aab3a1f 100644 --- a/src/filesystem/implementation.js +++ b/src/filesystem/implementation.js @@ -1085,6 +1085,8 @@ function read_directory(context, path, callback) { function read_directory_data(error, result) { if(error) { callback(error); + } else if(result.mode !== MODE_DIRECTORY) { + callback(new Errors.ENOTDIR(null, path)); } else { directoryNode = result; context.getObject(directoryNode.data, handle_directory_data); diff --git a/tests/bugs/issue267.js b/tests/bugs/issue267.js new file mode 100644 index 0000000..8e828b2 --- /dev/null +++ b/tests/bugs/issue267.js @@ -0,0 +1,22 @@ +var Filer = require('../..'); +var util = require('../lib/test-utils.js'); +var expect = require('chai').expect; + +describe('fs.readdir on non-dir paths, issue 267', function() { + beforeEach(util.setup); + afterEach(util.cleanup); + + it('should fail with ENOTDIR when called on filepath', function(done) { + var fs = util.fs(); + + fs.writeFile('/myfile.txt', 'data', function(err) { + if(err) throw err; + + fs.readdir('/myfile.txt', function(err, contents) { + expect(err).to.exist; + expect(err.code).to.equal('ENOTDIR'); + done(); + }); + }); + }); +}); diff --git a/tests/index.js b/tests/index.js index 823f698..a2f83ef 100644 --- a/tests/index.js +++ b/tests/index.js @@ -73,3 +73,4 @@ require("./bugs/ls-depth-bug"); require("./bugs/issue247.js"); require("./bugs/issue254.js"); require("./bugs/issue258.js"); +require("./bugs/issue267.js");