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.stat', 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(typeof fs.stat).to.equal('function');
|
|
|
|
});
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should return an error if path does not exist', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.stat('/tmp', function(error, result) {
|
|
|
|
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(result).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 stat object if path exists', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.stat('/', function(error, result) {
|
|
|
|
expect(error).not.to.exist;
|
2018-06-28 17:35:20 +00:00
|
|
|
expect(result).to.exist;
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(result['node']).to.be.a('string');
|
|
|
|
expect(result['dev']).to.equal(fs.name);
|
|
|
|
expect(result['size']).to.be.a('number');
|
|
|
|
expect(result['nlinks']).to.be.a('number');
|
2014-03-29 19:21:53 +00:00
|
|
|
expect(result['atime']).to.be.a('date');
|
|
|
|
expect(result['mtime']).to.be.a('date');
|
|
|
|
expect(result['ctime']).to.be.a('date');
|
|
|
|
expect(result['atimeMs']).to.be.a('number');
|
|
|
|
expect(result['mtimeMs']).to.be.a('number');
|
|
|
|
expect(result['ctimeMs']).to.be.a('number');
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(result['type']).to.equal('DIRECTORY');
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +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-03-29 19:21:53 +00:00
|
|
|
it('should return a stat object with equal Date and Unix Timestamps', function(done) {
|
|
|
|
var fs = util.fs();
|
|
|
|
|
|
|
|
fs.stat('/', function(error, result) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(result).to.exist;
|
|
|
|
|
|
|
|
expect(result['atime']).to.be.a('date');
|
|
|
|
expect(result['mtime']).to.be.a('date');
|
|
|
|
expect(result['ctime']).to.be.a('date');
|
|
|
|
|
|
|
|
expect(result.atime.getTime()/1000).to.equal(result.atimeMs);
|
|
|
|
expect(result.mtime.getTime()/1000).to.equal(result.mtimeMs);
|
|
|
|
expect(result.ctime.getTime()/1000).to.equal(result.ctimeMs);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
it('should follow symbolic links and return a stat object for the resulting path', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.open('/myfile', 'w', function(error, result) {
|
|
|
|
if(error) throw error;
|
|
|
|
var fd = result;
|
|
|
|
fs.close(fd, function(error) {
|
2013-11-27 17:18:09 +00:00
|
|
|
if(error) throw error;
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.stat('/myfile', function(error, result) {
|
2013-11-27 17:18:09 +00:00
|
|
|
if(error) throw error;
|
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(result['node']).to.exist;
|
|
|
|
fs.symlink('/myfile', '/myfilelink', function(error) {
|
|
|
|
if(error) throw error;
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.stat('/myfilelink', function(error, result) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(result).to.exist;
|
|
|
|
expect(result['node']).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 stat object for a valid descriptor', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.open('/myfile', 'w+', function(error, fd) {
|
|
|
|
if(error) throw error;
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
fs.fstat(fd, function(error, result) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(result).to.exist;
|
2013-11-27 17:18:09 +00:00
|
|
|
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(result['node']).to.exist;
|
|
|
|
expect(result['dev']).to.equal(fs.name);
|
|
|
|
expect(result['size']).to.be.a('number');
|
|
|
|
expect(result['nlinks']).to.be.a('number');
|
2014-03-29 19:21:53 +00:00
|
|
|
expect(result['atime']).to.be.a('date');
|
|
|
|
expect(result['mtime']).to.be.a('date');
|
|
|
|
expect(result['ctime']).to.be.a('date');
|
|
|
|
expect(result['atimeMs']).to.be.a('number');
|
|
|
|
expect(result['mtimeMs']).to.be.a('number');
|
|
|
|
expect(result['ctimeMs']).to.be.a('number');
|
2014-05-23 20:53:50 +00:00
|
|
|
expect(result['type']).to.equal('FILE');
|
|
|
|
|
|
|
|
done();
|
2013-11-27 17:18:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-10-09 16:54:40 +00:00
|
|
|
|
|
|
|
it('(promise) should be a function', function() {
|
|
|
|
var fs = util.fs();
|
|
|
|
expect(fs.promises.stat).to.be.a('function');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a promise', function() {
|
|
|
|
var fs = util.fs();
|
2018-11-28 01:53:35 +00:00
|
|
|
|
|
|
|
var p = fs.promises.stat('/');
|
|
|
|
expect(p).to.be.a('Promise');
|
|
|
|
return p;
|
2018-10-09 16:54:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('(promise) should return a stat object if file exists', function() {
|
|
|
|
var fs = util.fs();
|
|
|
|
|
|
|
|
return fs.promises
|
|
|
|
.stat('/')
|
|
|
|
.then(result => {
|
|
|
|
expect(result).to.exist;
|
|
|
|
|
|
|
|
expect(result.node).to.be.a('string');
|
|
|
|
expect(result.dev).to.equal(fs.name);
|
|
|
|
expect(result.size).to.be.a('number');
|
|
|
|
expect(result.nlinks).to.be.a('number');
|
2014-03-29 19:21:53 +00:00
|
|
|
expect(result['atime']).to.be.a('date');
|
|
|
|
expect(result['mtime']).to.be.a('date');
|
|
|
|
expect(result['ctime']).to.be.a('date');
|
|
|
|
expect(result['atimeMs']).to.be.a('number');
|
|
|
|
expect(result['mtimeMs']).to.be.a('number');
|
|
|
|
expect(result['ctimeMs']).to.be.a('number');
|
2018-11-28 18:54:20 +00:00
|
|
|
expect(result.isDirectory()).to.be.true;
|
2018-10-09 16:54:40 +00:00
|
|
|
});
|
|
|
|
});
|
2014-05-23 20:53:50 +00:00
|
|
|
});
|
2018-10-09 21:40:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* fsPromises tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
describe('fsPromises.stat', function() {
|
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
|
|
|
|
|
|
|
it('should return an error if path does not exist', function() {
|
|
|
|
var fsPromises = util.fs().promises;
|
|
|
|
|
|
|
|
return fsPromises.stat('/tmp')
|
|
|
|
.catch(error => {
|
|
|
|
expect(error).to.exist;
|
|
|
|
expect(error.code).to.equal('ENOENT');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|