tools: enable no-redeclare rule for linter
[platform/upstream/nodejs.git] / test / parallel / test-fs-utimes.js
1 'use strict';
2 var common = require('../common');
3 var assert = require('assert');
4 var util = require('util');
5 var fs = require('fs');
6
7 var tests_ok = 0;
8 var tests_run = 0;
9
10 function stat_resource(resource) {
11   if (typeof resource == 'string') {
12     return fs.statSync(resource);
13   } else {
14     // ensure mtime has been written to disk
15     fs.fsyncSync(resource);
16     return fs.fstatSync(resource);
17   }
18 }
19
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;
27 }
28
29 function expect_errno(syscall, resource, err, errno) {
30   if (err && (err.code === errno || err.code === 'ENOSYS')) {
31     tests_ok++;
32   } else {
33     console.log('FAILED:', 'expect_errno', util.inspect(arguments));
34   }
35 }
36
37 function expect_ok(syscall, resource, err, atime, mtime) {
38   if (!err && check_mtime(resource, mtime) ||
39       err && err.code === 'ENOSYS') {
40     tests_ok++;
41   } else {
42     console.log('FAILED:', 'expect_ok', util.inspect(arguments));
43   }
44 }
45
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) {
50
51   var fd;
52   //
53   // test synchronized code paths, these functions throw on failure
54   //
55   function syncTests() {
56     fs.utimesSync(__filename, atime, mtime);
57     expect_ok('utimesSync', __filename, undefined, atime, mtime);
58     tests_run++;
59
60     // some systems don't have futimes
61     // if there's an error, it should be ENOSYS
62     try {
63       tests_run++;
64       fs.futimesSync(fd, atime, mtime);
65       expect_ok('futimesSync', fd, undefined, atime, mtime);
66     } catch (ex) {
67       expect_errno('futimesSync', fd, ex, 'ENOSYS');
68     }
69
70     var err;
71     err = undefined;
72     try {
73       fs.utimesSync('foobarbaz', atime, mtime);
74     } catch (ex) {
75       err = ex;
76     }
77     expect_errno('utimesSync', 'foobarbaz', err, 'ENOENT');
78     tests_run++;
79
80     err = undefined;
81     try {
82       fs.futimesSync(-1, atime, mtime);
83     } catch (ex) {
84       err = ex;
85     }
86     expect_errno('futimesSync', -1, err, 'EBADF');
87     tests_run++;
88   }
89
90   //
91   // test async code paths
92   //
93   fs.utimes(__filename, atime, mtime, function(err) {
94     expect_ok('utimes', __filename, err, atime, mtime);
95
96     fs.utimes('foobarbaz', atime, mtime, function(err) {
97       expect_errno('utimes', 'foobarbaz', err, 'ENOENT');
98
99       // don't close this fd
100       if (common.isWindows) {
101         fd = fs.openSync(__filename, 'r+');
102       } else {
103         fd = fs.openSync(__filename, 'r');
104       }
105
106       fs.futimes(fd, atime, mtime, function(err) {
107         expect_ok('futimes', fd, err, atime, mtime);
108
109         fs.futimes(-1, atime, mtime, function(err) {
110           expect_errno('futimes', -1, err, 'EBADF');
111           syncTests();
112           callback();
113         });
114         tests_run++;
115       });
116       tests_run++;
117     });
118     tests_run++;
119   });
120   tests_run++;
121 }
122
123 var stats = fs.statSync(__filename);
124
125 // run tests
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() {
132             // done
133           });
134         });
135       });
136     });
137   });
138 });
139
140
141 process.on('exit', function() {
142   console.log('Tests run / ok:', tests_run, '/', tests_ok);
143   assert.equal(tests_ok, tests_run);
144 });