2014-01-21 21:25:09 +00:00
|
|
|
define(["Filer", "util"], function(Filer, util) {
|
2013-12-13 05:46:31 +00:00
|
|
|
|
|
|
|
describe('fs.utimes', function() {
|
2014-01-21 21:25:09 +00:00
|
|
|
beforeEach(util.setup);
|
|
|
|
afterEach(util.cleanup);
|
2013-12-13 05:46:31 +00:00
|
|
|
|
2013-12-16 02:22:36 +00:00
|
|
|
it('should be a function', function() {
|
2014-01-21 21:25:09 +00:00
|
|
|
var fs = util.fs();
|
|
|
|
expect(fs.utimes).to.be.a('function');
|
2013-12-16 02:22:36 +00:00
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should error when atime is negative', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-12-16 02:22:36 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.writeFile('/testfile', '', function(error) {
|
2013-12-16 02:22:36 +00:00
|
|
|
if (error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.utimes('/testfile', -1, Date.now(), function (error) {
|
|
|
|
expect(error).to.exist;
|
2014-03-05 19:28:31 +00:00
|
|
|
expect(error.code).to.equal('EINVAL');
|
2014-01-21 21:25:09 +00:00
|
|
|
done();
|
2013-12-16 02:22:36 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should error when mtime is negative', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-12-15 07:54:51 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.writeFile('/testfile', '', function(error) {
|
2013-12-16 02:22:36 +00:00
|
|
|
if (error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.utimes('/testfile', Date.now(), -1, function (error) {
|
|
|
|
expect(error).to.exist;
|
2014-03-05 19:28:31 +00:00
|
|
|
expect(error.code).to.equal('EINVAL');
|
2014-01-21 21:25:09 +00:00
|
|
|
done();
|
2013-12-16 02:22:36 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should error when atime is as invalid number', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-12-16 02:22:36 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.writeFile('/testfile', '', function (error) {
|
2013-12-16 02:22:36 +00:00
|
|
|
if (error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.utimes('/testfile', 'invalid datetime', Date.now(), function (error) {
|
|
|
|
expect(error).to.exist;
|
2014-03-05 19:28:31 +00:00
|
|
|
expect(error.code).to.equal('EINVAL');
|
2014-01-21 21:25:09 +00:00
|
|
|
done();
|
2013-12-16 02:22:36 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should error when path does not exist', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-12-17 16:28:22 +00:00
|
|
|
var atime = Date.parse('1 Oct 2000 15:33:22');
|
|
|
|
var mtime = Date.parse('30 Sep 2000 06:43:54');
|
2013-12-20 05:45:11 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.utimes('/pathdoesnotexist', atime, mtime, function (error) {
|
|
|
|
expect(error).to.exist;
|
2014-03-05 19:28:31 +00:00
|
|
|
expect(error.code).to.equal('ENOENT');
|
2014-01-21 21:25:09 +00:00
|
|
|
done();
|
2013-12-17 16:28:22 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should error when mtime is an invalid number', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-12-16 02:22:36 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.writeFile('/testfile', '', function (error) {
|
2013-12-16 02:22:36 +00:00
|
|
|
if (error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.utimes('/testfile', Date.now(), 'invalid datetime', function (error) {
|
|
|
|
expect(error).to.exist;
|
2014-03-05 19:28:31 +00:00
|
|
|
expect(error.code).to.equal('EINVAL');
|
2014-01-21 21:25:09 +00:00
|
|
|
done();
|
2013-12-16 02:22:36 +00:00
|
|
|
});
|
|
|
|
});
|
2013-12-17 16:28:22 +00:00
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should error when file descriptor is invalid', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-12-17 16:28:22 +00:00
|
|
|
var atime = Date.parse('1 Oct 2000 15:33:22');
|
|
|
|
var mtime = Date.parse('30 Sep 2000 06:43:54');
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.futimes(1, atime, mtime, function (error) {
|
|
|
|
expect(error).to.exist;
|
2014-03-05 19:28:31 +00:00
|
|
|
expect(error.code).to.equal('EBADF');
|
2014-01-21 21:25:09 +00:00
|
|
|
done();
|
2013-12-16 02:22:36 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should change atime and mtime of a file path', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-12-15 07:54:51 +00:00
|
|
|
var atime = Date.parse('1 Oct 2000 15:33:22');
|
|
|
|
var mtime = Date.parse('30 Sep 2000 06:43:54');
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.writeFile('/testfile', '', function (error) {
|
2013-12-15 07:54:51 +00:00
|
|
|
if (error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.utimes('/testfile', atime, mtime, function (error) {
|
|
|
|
expect(error).not.to.exist;
|
2013-12-15 07:54:51 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.stat('/testfile', function (error, stat) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(stat.mtime).to.equal(mtime);
|
|
|
|
done();
|
2013-12-15 07:54:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-12-16 02:22:36 +00:00
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it ('should change atime and mtime for a valid file descriptor', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-12-16 02:22:36 +00:00
|
|
|
var ofd;
|
|
|
|
var atime = Date.parse('1 Oct 2000 15:33:22');
|
|
|
|
var mtime = Date.parse('30 Sep 2000 06:43:54');
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.open('/testfile', 'w', function (error, result) {
|
2013-12-16 02:22:36 +00:00
|
|
|
if (error) throw error;
|
|
|
|
|
|
|
|
ofd = result;
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.futimes(ofd, atime, mtime, function (error) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
|
|
|
|
fs.fstat(ofd, function (error, stat) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(stat.mtime).to.equal(mtime);
|
|
|
|
done();
|
2013-12-20 05:45:11 +00:00
|
|
|
});
|
2013-12-16 02:22:36 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should update atime and mtime of directory path', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-12-16 02:22:36 +00:00
|
|
|
var atime = Date.parse('1 Oct 2000 15:33:22');
|
|
|
|
var mtime = Date.parse('30 Sep 2000 06:43:54');
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.mkdir('/testdir', function (error) {
|
2013-12-16 02:22:36 +00:00
|
|
|
if (error) throw error;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.utimes('/testdir', atime, mtime, function (error) {
|
|
|
|
expect(error).not.to.exist;
|
2013-12-16 02:22:36 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.stat('/testdir', function (error, stat) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
expect(stat.mtime).to.equal(mtime);
|
|
|
|
done();
|
2013-12-16 02:22:36 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-12-13 05:46:31 +00:00
|
|
|
});
|
2013-12-17 16:28:22 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
it('should update atime and mtime using current time if arguments are null', function(done) {
|
|
|
|
var fs = util.fs();
|
2013-12-17 16:28:22 +00:00
|
|
|
var atimeEst;
|
|
|
|
var mtimeEst;
|
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.writeFile('/myfile', '', function (error) {
|
2013-12-17 16:28:22 +00:00
|
|
|
if (error) throw error;
|
|
|
|
|
2014-03-04 20:30:46 +00:00
|
|
|
var then = Date.now();
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.utimes('/myfile', null, null, function (error) {
|
|
|
|
expect(error).not.to.exist;
|
2013-12-17 16:28:22 +00:00
|
|
|
|
2014-01-21 21:25:09 +00:00
|
|
|
fs.stat('/myfile', function (error, stat) {
|
|
|
|
expect(error).not.to.exist;
|
|
|
|
// Note: testing estimation as time may differ by a couple of milliseconds
|
|
|
|
// This number should be increased if tests are on slow systems
|
2014-03-04 20:30:46 +00:00
|
|
|
var delta = Date.now() - then;
|
2014-05-16 18:46:02 +00:00
|
|
|
expect(then - stat.atime).to.be.at.most(delta);
|
|
|
|
expect(then - stat.mtime).to.be.at.most(delta);
|
2014-01-21 21:25:09 +00:00
|
|
|
done();
|
2013-12-17 16:28:22 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-12-13 05:46:31 +00:00
|
|
|
});
|
2014-01-21 21:25:09 +00:00
|
|
|
});
|