|
| 1 | +var common = require('../common'); |
| 2 | +var assert = require('assert'); |
| 3 | +var util = require('util'); |
| 4 | +var fs = require('fs'); |
| 5 | + |
| 6 | +var tests_ok = 0; |
| 7 | +var tests_run = 0; |
| 8 | + |
| 9 | +function stat_resource(resource) { |
| 10 | + if (typeof resource == 'string') { |
| 11 | + return fs.statSync(resource); |
| 12 | + } else { |
| 13 | + // ensure mtime has been written to disk |
| 14 | + fs.fsyncSync(resource); |
| 15 | + return fs.fstatSync(resource); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +function check_mtime(resource, mtime) { |
| 20 | + var mtime = fs._toUnixTimestamp(mtime); |
| 21 | + var stats = stat_resource(resource); |
| 22 | + var real_mtime = fs._toUnixTimestamp(stats.mtime); |
| 23 | + // check up to single-second precision |
| 24 | + // sub-second precision is OS and fs dependant |
| 25 | + return Math.floor(mtime) == Math.floor(real_mtime); |
| 26 | +} |
| 27 | + |
| 28 | +function expect_errno(syscall, resource, err, errno) { |
| 29 | + if (err && err.code == errno) { |
| 30 | + tests_ok++; |
| 31 | + } else { |
| 32 | + console.log('FAILED:', arguments.callee.name, util.inspect(arguments)); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function expect_ok(syscall, resource, err, atime, mtime) { |
| 37 | + if (!err && check_mtime(resource, mtime)) { |
| 38 | + tests_ok++; |
| 39 | + } else { |
| 40 | + console.log('FAILED:', arguments.callee.name, util.inspect(arguments)); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// the tests assume that __filename belongs to the user running the tests |
| 45 | +// this should be a fairly safe assumption; testing against a temp file |
| 46 | +// would be even better though (node doesn't have such functionality yet) |
| 47 | +function runTests(atime, mtime, callback) { |
| 48 | + |
| 49 | + var fd, err; |
| 50 | + // |
| 51 | + // test synchronized code paths, these functions throw on failure |
| 52 | + // |
| 53 | + function syncTests() { |
| 54 | + fs.utimesSync(__filename, atime, mtime); |
| 55 | + expect_ok('utimesSync', __filename, undefined, atime, mtime); |
| 56 | + tests_run++; |
| 57 | + |
| 58 | + fs.futimesSync(fd, atime, mtime); |
| 59 | + expect_ok('futimesSync', fd, undefined, atime, mtime); |
| 60 | + tests_run++; |
| 61 | + |
| 62 | + err = undefined; |
| 63 | + try { |
| 64 | + fs.utimesSync('foobarbaz', atime, mtime); |
| 65 | + } catch (ex) { |
| 66 | + err = ex; |
| 67 | + } |
| 68 | + expect_errno('utimesSync', 'foobarbaz', err, 'ENOENT'); |
| 69 | + tests_run++; |
| 70 | + |
| 71 | + err = undefined; |
| 72 | + try { |
| 73 | + fs.futimesSync(-1, atime, mtime); |
| 74 | + } catch (ex) { |
| 75 | + err = ex; |
| 76 | + } |
| 77 | + expect_errno('futimesSync', -1, err, 'EBADF'); |
| 78 | + tests_run++; |
| 79 | + } |
| 80 | + |
| 81 | + // |
| 82 | + // test async code paths |
| 83 | + // |
| 84 | + fs.utimes(__filename, atime, mtime, function(err) { |
| 85 | + expect_ok('utimes', __filename, err, atime, mtime); |
| 86 | + |
| 87 | + fs.utimes('foobarbaz', atime, mtime, function(err) { |
| 88 | + expect_errno('utimes', 'foobarbaz', err, 'ENOENT'); |
| 89 | + |
| 90 | + // don't close this fd |
| 91 | + fd = fs.openSync(__filename, 'r'); |
| 92 | + |
| 93 | + fs.futimes(fd, atime, mtime, function(err) { |
| 94 | + expect_ok('futimes', fd, err, atime, mtime); |
| 95 | + |
| 96 | + fs.futimes(-1, atime, mtime, function(err) { |
| 97 | + expect_errno('futimes', -1, err, 'EBADF'); |
| 98 | + syncTests(); |
| 99 | + callback(); |
| 100 | + }); |
| 101 | + tests_run++; |
| 102 | + }); |
| 103 | + tests_run++; |
| 104 | + }); |
| 105 | + tests_run++; |
| 106 | + }); |
| 107 | + tests_run++; |
| 108 | +} |
| 109 | + |
| 110 | +var stats = fs.statSync(__filename); |
| 111 | + |
| 112 | +runTests(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() { |
| 113 | + runTests(new Date(), new Date(), function() { |
| 114 | + runTests(1234.5678, 1234.5678, function() { |
| 115 | + runTests(stats.mtime, stats.mtime, function() { |
| 116 | + // done |
| 117 | + }); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +process.on('exit', function() { |
| 123 | + console.log('Tests run / ok:', tests_run, '/', tests_ok); |
| 124 | + assert.equal(tests_ok, tests_run); |
| 125 | +}); |
0 commit comments