2014-05-23 20:53:50 +00:00
|
|
|
var util = require('../lib/test-utils.js');
|
|
|
|
var expect = require('chai').expect;
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
describe('fs.readdir', function() {
|
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should be a function', function() {
|
|
|
|
var fs = util.fs();
|
|
|
|
expect(fs.readdir).to.be.a('function');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return an error if the path does not exist', function(done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
|
|
|
|
fs.readdir('/tmp/mydir', function(error, files) {
|
|
|
|
expect(error).to.exist;
|
2018-07-15 17:25:35 +00:00
|
|
|
expect(error.code).to.equal('ENOENT');
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(files).not.to.exist;
|
|
|
|
done();
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should return a list of files from an existing directory', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.mkdir('/tmp', function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
|
|
|
|
fs.readdir('/', function(error, files) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(files).to.exist;
|
|
|
|
expect(files.length).to.equal(1);
|
|
|
|
expect(files[0]).to.equal('tmp');
|
2014-01-21 21:25:09 +00:00
|
|
|
done();
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should follow symbolic links', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.mkdir('/tmp', function(error) {
|
|
|
|
if(error) throw error;
|
|
|
|
fs.symlink('/', '/tmp/dirLink', function(error) {
|
2014-01-21 21:25:09 +00:00
|
|
|
if(error) throw error;
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.readdir('/tmp/dirLink', function(error, files) {
|
2014-01-21 21:25:09 +00:00
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(files).to.exist;
|
|
|
|
expect(files.length).to.equal(1);
|
|
|
|
expect(files[0]).to.equal('tmp');
|
|
|
|
done();
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-10-09 17:32:09 +00:00
|
|
|
|
|
|
|
it('(promise) should be a function', function() {
|
|
|
|
var fsPromises = util.fs().promises;
|
|
|
|
expect(fsPromises.readdir).to.be.a('function');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('(promise) should return a list of files from an existing directory', function() {
|
|
|
|
var fsPromises = util.fs().promises;
|
|
|
|
|
|
|
|
return fsPromises.mkdir('/tmp')
|
|
|
|
.then(() => {
|
|
|
|
return fsPromises.stat('/');
|
|
|
|
})
|
|
|
|
.then(stats => {
|
|
|
|
expect(stats).to.exist;
|
|
|
|
expect(stats.isDirectory()).to.be.true;
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return fsPromises.readdir('/');
|
|
|
|
})
|
|
|
|
.then(files => {
|
|
|
|
expect(files).to.exist;
|
|
|
|
expect(files.length).to.equal(1);
|
|
|
|
expect(files[0]).to.equal('tmp');
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
});
|
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2018-10-09 17:04:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* fsPromises tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
describe('fsPromises.readdir', function() {
|
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
|
|
|
|
|
|
|
it('should return an error if the path is a file', function() {
|
|
|
|
var fsPromises = util.fs().promises;
|
|
|
|
|
|
|
|
return fsPromises.open('/myfile', 'w')
|
|
|
|
.then(fd => fsPromises.close(fd))
|
|
|
|
.then(() => fsPromises.readdir('/myfile'))
|
|
|
|
.catch(error => {
|
|
|
|
expect(error).to.exist;
|
|
|
|
expect(error.code).to.equal('ENOTDIR');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|