Skip to content

Commit 77274d0

Browse files
Fishrock123addaleax
authored andcommitted
test: rewrite fs{f}utimes test file
Previously this test silently swallowed some errors. Refactored to use `common.mustCall()` & `assert()`s. Also, this adds a couple of extra error-checking cases. PR-URL: #25656 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 1d6e18b commit 77274d0

File tree

1 file changed

+101
-106
lines changed

1 file changed

+101
-106
lines changed

‎test/parallel/test-fs-utimes.js‎

Lines changed: 101 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ const fs = require('fs');
2828
consttmpdir=require('../common/tmpdir');
2929
tmpdir.refresh();
3030

31-
lettests_ok=0;
32-
lettests_run=0;
33-
3431
functionstat_resource(resource){
3532
if(typeofresource==='string'){
3633
returnfs.statSync(resource);
@@ -49,71 +46,46 @@ function check_mtime(resource, mtime){
4946
mtime=fs._toUnixTimestamp(mtime);
5047
conststats=stat_resource(resource);
5148
constreal_mtime=fs._toUnixTimestamp(stats.mtime);
52-
// check up to single-second precision
53-
// sub-second precision is OS and fs dependant
54-
returnmtime-real_mtime<2;
49+
returnmtime-real_mtime;
5550
}
5651

5752
functionexpect_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+
);
6357
}
6458

6559
functionexpect_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+
constmtime_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+
);
7268
}
7369

74-
functiontestIt(atime,mtime,callback){
75-
76-
letfd;
77-
//
78-
// test synchronized code paths, these functions throw on failure
79-
//
80-
functionsyncTests(){
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-
leterr;
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+
conststats=fs.statSync(tmpdir.path);
10371

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+
constcases=[
73+
newDate('1982-09-10 13:37'),
74+
newDate(),
75+
123456.789,
76+
stats.mtime,
77+
['123456',-1],
78+
newDate('2017-04-08T17:59:38.008Z')
79+
];
80+
runTests(cases.values());
81+
82+
functionrunTests(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];
11687

88+
letfd;
11789
//
11890
// test async code paths
11991
//
@@ -133,54 +105,40 @@ function testIt(atime, mtime, callback){
133105
fs.futimes(fd,atime,mtime,common.mustCall((err)=>{
134106
expect_ok('futimes',fd,err,atime,mtime);
135107

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-
146108
syncTests();
147109

148-
tests_run++;
110+
setImmediate(common.mustCall(runTests),iter);
149111
}));
150-
tests_run++;
151112
}));
152-
tests_run++;
153113
}));
154-
tests_run++;
155-
}
156114

157-
conststats=fs.statSync(tmpdir.path);
115+
//
116+
// test synchronized code paths, these functions throw on failure
117+
//
118+
functionsyncTests(){
119+
fs.utimesSync(tmpdir.path,atime,mtime);
120+
expect_ok('utimesSync',tmpdir.path,undefined,atime,mtime);
158121

159-
// Run tests
160-
construnTest=common.mustCall(testIt,1);
161-
162-
runTest(newDate('1982-09-10 13:37'),newDate('1982-09-10 13:37'),()=>{
163-
runTest(newDate(),newDate(),()=>{
164-
runTest(123456.789,123456.789,()=>{
165-
runTest(stats.mtime,stats.mtime,()=>{
166-
runTest('123456',-1,()=>{
167-
runTest(
168-
newDate('2017-04-08T17:59:38.008Z'),
169-
newDate('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+
}
179130

180-
process.on('exit',()=>{
181-
assert.strictEqual(tests_ok,tests_run-2);
182-
});
131+
leterr;
132+
try{
133+
fs.utimesSync('foobarbaz',atime,mtime);
134+
}catch(ex){
135+
err=ex;
136+
}
137+
expect_errno('utimesSync','foobarbaz',err,'ENOENT');
183138

139+
err=undefined;
140+
}
141+
}
184142

185143
// Ref: https://github.com/nodejs/node/issues/13255
186144
constpath=`${tmpdir.path}/test-utimes-precision`;
@@ -212,19 +170,56 @@ if (common.isWindows){
212170
assert.strictEqual(overflow_mtime,overflow_stats.mtime.getTime());
213171
}
214172

215-
[false,0,{},[],null,undefined].forEach((i)=>{
173+
constexpectTypeError={
174+
code: 'ERR_INVALID_ARG_TYPE',
175+
type: TypeError
176+
};
177+
// utimes-only error cases
178+
{
179+
common.expectsError(
180+
()=>fs.utimes(0,newDate(),newDate(),common.mustNotCall()),
181+
expectTypeError
182+
);
183+
common.expectsError(
184+
()=>fs.utimesSync(0,newDate(),newDate()),
185+
expectTypeError
186+
);
187+
}
188+
189+
// shared error cases
190+
[false,{},[],null,undefined].forEach((i)=>{
216191
common.expectsError(
217192
()=>fs.utimes(i,newDate(),newDate(),common.mustNotCall()),
218-
{
219-
code: 'ERR_INVALID_ARG_TYPE',
220-
type: TypeError
221-
}
193+
expectTypeError
222194
);
223195
common.expectsError(
224196
()=>fs.utimesSync(i,newDate(),newDate()),
225-
{
226-
code: 'ERR_INVALID_ARG_TYPE',
227-
type: TypeError
228-
}
197+
expectTypeError
198+
);
199+
common.expectsError(
200+
()=>fs.futimes(i,newDate(),newDate(),common.mustNotCall()),
201+
expectTypeError
202+
);
203+
common.expectsError(
204+
()=>fs.futimesSync(i,newDate(),newDate()),
205+
expectTypeError
229206
);
230207
});
208+
209+
constexpectRangeError={
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,newDate(),newDate(),common.mustNotCall()),
219+
expectRangeError
220+
);
221+
common.expectsError(
222+
()=>fs.futimesSync(-1,newDate(),newDate()),
223+
expectRangeError
224+
);
225+
}

0 commit comments

Comments
(0)