2 var common = require('../common');
3 var assert = require('assert');
4 var util = require('util');
5 var fs = require('fs');
10 function stat_resource(resource) {
11 if (typeof resource == 'string') {
12 return fs.statSync(resource);
14 // ensure mtime has been written to disk
15 fs.fsyncSync(resource);
16 return fs.fstatSync(resource);
20 function check_mtime(resource, mtime) {
21 mtime = fs._toUnixTimestamp(mtime);
22 var stats = stat_resource(resource);
23 var real_mtime = fs._toUnixTimestamp(stats.mtime);
24 // check up to single-second precision
25 // sub-second precision is OS and fs dependant
26 return mtime - real_mtime < 2;
29 function expect_errno(syscall, resource, err, errno) {
30 if (err && (err.code === errno || err.code === 'ENOSYS')) {
33 console.log('FAILED:', 'expect_errno', util.inspect(arguments));
37 function expect_ok(syscall, resource, err, atime, mtime) {
38 if (!err && check_mtime(resource, mtime) ||
39 err && err.code === 'ENOSYS') {
42 console.log('FAILED:', 'expect_ok', util.inspect(arguments));
46 // the tests assume that __filename belongs to the user running the tests
47 // this should be a fairly safe assumption; testing against a temp file
48 // would be even better though (node doesn't have such functionality yet)
49 function runTest(atime, mtime, callback) {
53 // test synchronized code paths, these functions throw on failure
55 function syncTests() {
56 fs.utimesSync(__filename, atime, mtime);
57 expect_ok('utimesSync', __filename, undefined, atime, mtime);
60 // some systems don't have futimes
61 // if there's an error, it should be ENOSYS
64 fs.futimesSync(fd, atime, mtime);
65 expect_ok('futimesSync', fd, undefined, atime, mtime);
67 expect_errno('futimesSync', fd, ex, 'ENOSYS');
73 fs.utimesSync('foobarbaz', atime, mtime);
77 expect_errno('utimesSync', 'foobarbaz', err, 'ENOENT');
82 fs.futimesSync(-1, atime, mtime);
86 expect_errno('futimesSync', -1, err, 'EBADF');
91 // test async code paths
93 fs.utimes(__filename, atime, mtime, function(err) {
94 expect_ok('utimes', __filename, err, atime, mtime);
96 fs.utimes('foobarbaz', atime, mtime, function(err) {
97 expect_errno('utimes', 'foobarbaz', err, 'ENOENT');
99 // don't close this fd
100 if (common.isWindows) {
101 fd = fs.openSync(__filename, 'r+');
103 fd = fs.openSync(__filename, 'r');
106 fs.futimes(fd, atime, mtime, function(err) {
107 expect_ok('futimes', fd, err, atime, mtime);
109 fs.futimes(-1, atime, mtime, function(err) {
110 expect_errno('futimes', -1, err, 'EBADF');
123 var stats = fs.statSync(__filename);
126 runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
127 runTest(new Date(), new Date(), function() {
128 runTest(123456.789, 123456.789, function() {
129 runTest(stats.mtime, stats.mtime, function() {
130 runTest(NaN, Infinity, function() {
131 runTest('123456', -1, function() {
141 process.on('exit', function() {
142 console.log('Tests run / ok:', tests_run, '/', tests_ok);
143 assert.equal(tests_ok, tests_run);