2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2010-10-29 12:38:13 +02:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
|
|
|
var util = require('util');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
var tests_ok = 0;
|
|
|
|
var tests_run = 0;
|
|
|
|
|
|
|
|
function stat_resource(resource) {
|
|
|
|
if (typeof resource == 'string') {
|
|
|
|
return fs.statSync(resource);
|
|
|
|
} else {
|
|
|
|
// ensure mtime has been written to disk
|
|
|
|
fs.fsyncSync(resource);
|
|
|
|
return fs.fstatSync(resource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_mtime(resource, mtime) {
|
2016-01-31 05:32:27 +01:00
|
|
|
mtime = fs._toUnixTimestamp(mtime);
|
2010-10-29 12:38:13 +02:00
|
|
|
var stats = stat_resource(resource);
|
|
|
|
var real_mtime = fs._toUnixTimestamp(stats.mtime);
|
|
|
|
// check up to single-second precision
|
|
|
|
// sub-second precision is OS and fs dependant
|
2015-11-23 10:28:13 +01:00
|
|
|
return mtime - real_mtime < 2;
|
2010-10-29 12:38:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function expect_errno(syscall, resource, err, errno) {
|
2011-04-01 21:25:21 +02:00
|
|
|
if (err && (err.code === errno || err.code === 'ENOSYS')) {
|
2010-10-29 12:38:13 +02:00
|
|
|
tests_ok++;
|
|
|
|
} else {
|
2015-10-03 13:44:58 +02:00
|
|
|
console.log('FAILED:', 'expect_errno', util.inspect(arguments));
|
2010-10-29 12:38:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function expect_ok(syscall, resource, err, atime, mtime) {
|
2011-04-01 21:25:21 +02:00
|
|
|
if (!err && check_mtime(resource, mtime) ||
|
|
|
|
err && err.code === 'ENOSYS') {
|
2010-10-29 12:38:13 +02:00
|
|
|
tests_ok++;
|
|
|
|
} else {
|
2015-10-03 13:44:58 +02:00
|
|
|
console.log('FAILED:', 'expect_ok', util.inspect(arguments));
|
2010-10-29 12:38:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the tests assume that __filename belongs to the user running the tests
|
|
|
|
// this should be a fairly safe assumption; testing against a temp file
|
|
|
|
// would be even better though (node doesn't have such functionality yet)
|
2012-01-17 19:43:34 +01:00
|
|
|
function runTest(atime, mtime, callback) {
|
2010-10-29 12:38:13 +02:00
|
|
|
|
2016-01-02 02:37:10 +01:00
|
|
|
var fd;
|
2010-10-29 12:38:13 +02:00
|
|
|
//
|
|
|
|
// test synchronized code paths, these functions throw on failure
|
|
|
|
//
|
|
|
|
function syncTests() {
|
|
|
|
fs.utimesSync(__filename, atime, mtime);
|
|
|
|
expect_ok('utimesSync', __filename, undefined, atime, mtime);
|
|
|
|
tests_run++;
|
|
|
|
|
2011-04-01 21:25:21 +02:00
|
|
|
// some systems don't have futimes
|
|
|
|
// if there's an error, it should be ENOSYS
|
|
|
|
try {
|
|
|
|
tests_run++;
|
|
|
|
fs.futimesSync(fd, atime, mtime);
|
|
|
|
expect_ok('futimesSync', fd, undefined, atime, mtime);
|
|
|
|
} catch (ex) {
|
|
|
|
expect_errno('futimesSync', fd, ex, 'ENOSYS');
|
|
|
|
}
|
2010-10-29 12:38:13 +02:00
|
|
|
|
2011-04-01 21:25:21 +02:00
|
|
|
var err;
|
2010-10-29 12:38:13 +02:00
|
|
|
err = undefined;
|
|
|
|
try {
|
|
|
|
fs.utimesSync('foobarbaz', atime, mtime);
|
|
|
|
} catch (ex) {
|
|
|
|
err = ex;
|
|
|
|
}
|
|
|
|
expect_errno('utimesSync', 'foobarbaz', err, 'ENOENT');
|
|
|
|
tests_run++;
|
2016-07-23 01:37:54 +02:00
|
|
|
|
|
|
|
err = undefined;
|
|
|
|
try {
|
|
|
|
fs.futimesSync(-1, atime, mtime);
|
|
|
|
} catch (ex) {
|
|
|
|
err = ex;
|
|
|
|
}
|
|
|
|
expect_errno('futimesSync', -1, err, 'EBADF');
|
|
|
|
tests_run++;
|
2010-10-29 12:38:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// test async code paths
|
|
|
|
//
|
|
|
|
fs.utimes(__filename, atime, mtime, function(err) {
|
|
|
|
expect_ok('utimes', __filename, err, atime, mtime);
|
|
|
|
|
|
|
|
fs.utimes('foobarbaz', atime, mtime, function(err) {
|
|
|
|
expect_errno('utimes', 'foobarbaz', err, 'ENOENT');
|
|
|
|
|
|
|
|
// don't close this fd
|
2015-07-29 13:48:04 +02:00
|
|
|
if (common.isWindows) {
|
2011-10-12 07:34:51 +02:00
|
|
|
fd = fs.openSync(__filename, 'r+');
|
|
|
|
} else {
|
|
|
|
fd = fs.openSync(__filename, 'r');
|
|
|
|
}
|
2010-10-29 12:38:13 +02:00
|
|
|
|
|
|
|
fs.futimes(fd, atime, mtime, function(err) {
|
|
|
|
expect_ok('futimes', fd, err, atime, mtime);
|
2016-07-23 01:37:54 +02:00
|
|
|
|
|
|
|
fs.futimes(-1, atime, mtime, function(err) {
|
|
|
|
expect_errno('futimes', -1, err, 'EBADF');
|
|
|
|
syncTests();
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
tests_run++;
|
2010-10-29 12:38:13 +02:00
|
|
|
});
|
|
|
|
tests_run++;
|
|
|
|
});
|
|
|
|
tests_run++;
|
|
|
|
});
|
|
|
|
tests_run++;
|
|
|
|
}
|
|
|
|
|
|
|
|
var stats = fs.statSync(__filename);
|
|
|
|
|
2015-08-15 18:30:31 +02:00
|
|
|
// run tests
|
2012-01-17 19:43:34 +01:00
|
|
|
runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
|
|
|
|
runTest(new Date(), new Date(), function() {
|
|
|
|
runTest(123456.789, 123456.789, function() {
|
|
|
|
runTest(stats.mtime, stats.mtime, function() {
|
2015-08-15 18:30:31 +02:00
|
|
|
runTest(NaN, Infinity, function() {
|
|
|
|
runTest('123456', -1, function() {
|
|
|
|
// done
|
|
|
|
});
|
|
|
|
});
|
2010-10-29 12:38:13 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-08-15 18:30:31 +02:00
|
|
|
|
2010-10-29 12:38:13 +02:00
|
|
|
process.on('exit', function() {
|
|
|
|
console.log('Tests run / ok:', tests_run, '/', tests_ok);
|
|
|
|
assert.equal(tests_ok, tests_run);
|
|
|
|
});
|