@@ -28,9 +28,6 @@ const fs = require('fs');
28
28
const tmpdir = require ( '../common/tmpdir' ) ;
29
29
tmpdir . refresh ( ) ;
30
30
31
- let tests_ok = 0 ;
32
- let tests_run = 0 ;
33
-
34
31
function stat_resource ( resource ) {
35
32
if ( typeof resource === 'string' ) {
36
33
return fs . statSync ( resource ) ;
@@ -49,71 +46,46 @@ function check_mtime(resource, mtime) {
49
46
mtime = fs . _toUnixTimestamp ( mtime ) ;
50
47
const stats = stat_resource ( resource ) ;
51
48
const real_mtime = fs . _toUnixTimestamp ( stats . mtime ) ;
52
- // check up to single-second precision
53
- // sub-second precision is OS and fs dependant
54
- return mtime - real_mtime < 2 ;
49
+ return mtime - real_mtime ;
55
50
}
56
51
57
52
function expect_errno ( syscall , resource , err , errno ) {
58
- if ( err && ( err . code === errno || err . code === 'ENOSYS' ) ) {
59
- tests_ok ++ ;
60
- } else {
61
- console . log ( 'FAILED:' , 'expect_errno' , util . inspect ( arguments ) ) ;
62
- }
53
+ assert (
54
+ err && ( err . code === errno || err . code === 'ENOSYS' ) ,
55
+ `FAILED: expect_errno ${ util . inspect ( arguments ) } `
56
+ ) ;
63
57
}
64
58
65
59
function expect_ok ( syscall , resource , err , atime , mtime ) {
66
- if ( ! err && check_mtime ( resource , mtime ) ||
67
- err && err . code === 'ENOSYS' ) {
68
- tests_ok ++ ;
69
- } else {
70
- console . log ( 'FAILED:' , 'expect_ok' , util . inspect ( arguments ) ) ;
71
- }
60
+ const mtime_diff = check_mtime ( resource , mtime ) ;
61
+ assert (
62
+ // check up to single-second precision
63
+ // sub-second precision is OS and fs dependant
64
+ ! err && ( mtime_diff < 2 ) || err && err . code === 'ENOSYS' ,
65
+ `FAILED: expect_ok ${ util . inspect ( arguments ) }
66
+ check_mtime: ${ mtime_diff } `
67
+ ) ;
72
68
}
73
69
74
- function testIt ( atime , mtime , callback ) {
75
-
76
- let fd ;
77
- //
78
- // test synchronized code paths, these functions throw on failure
79
- //
80
- function syncTests ( ) {
81
- fs . utimesSync ( tmpdir . path , atime , mtime ) ;
82
- expect_ok ( 'utimesSync' , tmpdir . path , undefined , atime , mtime ) ;
83
- tests_run ++ ;
84
-
85
- // some systems don't have futimes
86
- // if there's an error, it should be ENOSYS
87
- try {
88
- tests_run ++ ;
89
- fs . futimesSync ( fd , atime , mtime ) ;
90
- expect_ok ( 'futimesSync' , fd , undefined , atime , mtime ) ;
91
- } catch ( ex ) {
92
- expect_errno ( 'futimesSync' , fd , ex , 'ENOSYS' ) ;
93
- }
94
-
95
- let err ;
96
- try {
97
- fs . utimesSync ( 'foobarbaz' , atime , mtime ) ;
98
- } catch ( ex ) {
99
- err = ex ;
100
- }
101
- expect_errno ( 'utimesSync' , 'foobarbaz' , err , 'ENOENT' ) ;
102
- tests_run ++ ;
70
+ const stats = fs . statSync ( tmpdir . path ) ;
103
71
104
- err = undefined ;
105
- common . expectsError (
106
- ( ) => fs . futimesSync ( - 1 , atime , mtime ) ,
107
- {
108
- code : 'ERR_OUT_OF_RANGE' ,
109
- type : RangeError ,
110
- message : 'The value of "fd" is out of range. ' +
111
- 'It must be >= 0 && < 4294967296. Received -1'
112
- }
113
- ) ;
114
- tests_run ++ ;
115
- }
72
+ const cases = [
73
+ new Date ( '1982-09-10 13:37' ) ,
74
+ new Date ( ) ,
75
+ 123456.789 ,
76
+ stats . mtime ,
77
+ [ '123456' , - 1 ] ,
78
+ new Date ( '2017-04-08T17:59:38.008Z' )
79
+ ] ;
80
+ runTests ( cases . values ( ) ) ;
81
+
82
+ function runTests ( iter ) {
83
+ const { value, done } = iter . next ( ) ;
84
+ if ( done ) return ;
85
+ // Support easy setting same or different atime / mtime values
86
+ const [ atime , mtime ] = Array . isArray ( value ) ? value : [ value , value ] ;
116
87
88
+ let fd ;
117
89
//
118
90
// test async code paths
119
91
//
@@ -133,54 +105,40 @@ function testIt(atime, mtime, callback) {
133
105
fs . futimes ( fd , atime , mtime , common . mustCall ( ( err ) => {
134
106
expect_ok ( 'futimes' , fd , err , atime , mtime ) ;
135
107
136
- common . expectsError (
137
- ( ) => fs . futimes ( - 1 , atime , mtime , common . mustNotCall ( ) ) ,
138
- {
139
- code : 'ERR_OUT_OF_RANGE' ,
140
- type : RangeError ,
141
- message : 'The value of "fd" is out of range. ' +
142
- 'It must be >= 0 && < 4294967296. Received -1'
143
- }
144
- ) ;
145
-
146
108
syncTests ( ) ;
147
109
148
- tests_run ++ ;
110
+ setImmediate ( common . mustCall ( runTests ) , iter ) ;
149
111
} ) ) ;
150
- tests_run ++ ;
151
112
} ) ) ;
152
- tests_run ++ ;
153
113
} ) ) ;
154
- tests_run ++ ;
155
- }
156
114
157
- const stats = fs . statSync ( tmpdir . path ) ;
115
+ //
116
+ // test synchronized code paths, these functions throw on failure
117
+ //
118
+ function syncTests ( ) {
119
+ fs . utimesSync ( tmpdir . path , atime , mtime ) ;
120
+ expect_ok ( 'utimesSync' , tmpdir . path , undefined , atime , mtime ) ;
158
121
159
- // Run tests
160
- const runTest = common . mustCall ( testIt , 1 ) ;
161
-
162
- runTest ( new Date ( '1982-09-10 13:37' ) , new Date ( '1982-09-10 13:37' ) , ( ) => {
163
- runTest ( new Date ( ) , new Date ( ) , ( ) => {
164
- runTest ( 123456.789 , 123456.789 , ( ) => {
165
- runTest ( stats . mtime , stats . mtime , ( ) => {
166
- runTest ( '123456' , - 1 , ( ) => {
167
- runTest (
168
- new Date ( '2017-04-08T17:59:38.008Z' ) ,
169
- new Date ( '2017-04-08T17:59:38.008Z' ) ,
170
- common . mustCall ( ( ) => {
171
- // Done
172
- } )
173
- ) ;
174
- } ) ;
175
- } ) ;
176
- } ) ;
177
- } ) ;
178
- } ) ;
122
+ // some systems don't have futimes
123
+ // if there's an error, it should be ENOSYS
124
+ try {
125
+ fs . futimesSync ( fd , atime , mtime ) ;
126
+ expect_ok ( 'futimesSync' , fd , undefined , atime , mtime ) ;
127
+ } catch ( ex ) {
128
+ expect_errno ( 'futimesSync' , fd , ex , 'ENOSYS' ) ;
129
+ }
179
130
180
- process . on ( 'exit' , ( ) => {
181
- assert . strictEqual ( tests_ok , tests_run - 2 ) ;
182
- } ) ;
131
+ let err ;
132
+ try {
133
+ fs . utimesSync ( 'foobarbaz' , atime , mtime ) ;
134
+ } catch ( ex ) {
135
+ err = ex ;
136
+ }
137
+ expect_errno ( 'utimesSync' , 'foobarbaz' , err , 'ENOENT' ) ;
183
138
139
+ err = undefined ;
140
+ }
141
+ }
184
142
185
143
// Ref: https://github.com/nodejs/node/issues/13255
186
144
const path = `${ tmpdir . path } /test-utimes-precision` ;
@@ -212,19 +170,56 @@ if (common.isWindows) {
212
170
assert . strictEqual ( overflow_mtime , overflow_stats . mtime . getTime ( ) ) ;
213
171
}
214
172
215
- [ false , 0 , { } , [ ] , null , undefined ] . forEach ( ( i ) => {
173
+ const expectTypeError = {
174
+ code : 'ERR_INVALID_ARG_TYPE' ,
175
+ type : TypeError
176
+ } ;
177
+ // utimes-only error cases
178
+ {
179
+ common . expectsError (
180
+ ( ) => fs . utimes ( 0 , new Date ( ) , new Date ( ) , common . mustNotCall ( ) ) ,
181
+ expectTypeError
182
+ ) ;
183
+ common . expectsError (
184
+ ( ) => fs . utimesSync ( 0 , new Date ( ) , new Date ( ) ) ,
185
+ expectTypeError
186
+ ) ;
187
+ }
188
+
189
+ // shared error cases
190
+ [ false , { } , [ ] , null , undefined ] . forEach ( ( i ) => {
216
191
common . expectsError (
217
192
( ) => fs . utimes ( i , new Date ( ) , new Date ( ) , common . mustNotCall ( ) ) ,
218
- {
219
- code : 'ERR_INVALID_ARG_TYPE' ,
220
- type : TypeError
221
- }
193
+ expectTypeError
222
194
) ;
223
195
common . expectsError (
224
196
( ) => fs . utimesSync ( i , new Date ( ) , new Date ( ) ) ,
225
- {
226
- code : 'ERR_INVALID_ARG_TYPE' ,
227
- type : TypeError
228
- }
197
+ expectTypeError
198
+ ) ;
199
+ common . expectsError (
200
+ ( ) => fs . futimes ( i , new Date ( ) , new Date ( ) , common . mustNotCall ( ) ) ,
201
+ expectTypeError
202
+ ) ;
203
+ common . expectsError (
204
+ ( ) => fs . futimesSync ( i , new Date ( ) , new Date ( ) ) ,
205
+ expectTypeError
229
206
) ;
230
207
} ) ;
208
+
209
+ const expectRangeError = {
210
+ code : 'ERR_OUT_OF_RANGE' ,
211
+ type : RangeError ,
212
+ message : 'The value of "fd" is out of range. ' +
213
+ 'It must be >= 0 && < 4294967296. Received -1'
214
+ } ;
215
+ // futimes-only error cases
216
+ {
217
+ common . expectsError (
218
+ ( ) => fs . futimes ( - 1 , new Date ( ) , new Date ( ) , common . mustNotCall ( ) ) ,
219
+ expectRangeError
220
+ ) ;
221
+ common . expectsError (
222
+ ( ) => fs . futimesSync ( - 1 , new Date ( ) , new Date ( ) ) ,
223
+ expectRangeError
224
+ ) ;
225
+ }
0 commit comments