Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 77274d0

Browse filesBrowse 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 <ruben@bridgewater.de> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 1d6e18b commit 77274d0
Copy full SHA for 77274d0

File tree

Expand file treeCollapse file tree

1 file changed

+101
-106
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+101
-106
lines changed
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-fs-utimes.js
+101-106Lines changed: 101 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ const fs = require('fs');
2828
const tmpdir = require('../common/tmpdir');
2929
tmpdir.refresh();
3030

31-
let tests_ok = 0;
32-
let tests_run = 0;
33-
3431
function stat_resource(resource) {
3532
if (typeof resource === 'string') {
3633
return fs.statSync(resource);
@@ -49,71 +46,46 @@ function check_mtime(resource, mtime) {
4946
mtime = fs._toUnixTimestamp(mtime);
5047
const stats = stat_resource(resource);
5148
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;
5550
}
5651

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

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

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);
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+
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];
11687

88+
let fd;
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-
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);
158121

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+
}
179130

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');
183138

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

185143
// Ref: https://github.com/nodejs/node/issues/13255
186144
const path = `${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+
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) => {
216191
common.expectsError(
217192
() => fs.utimes(i, new Date(), new Date(), common.mustNotCall()),
218-
{
219-
code: 'ERR_INVALID_ARG_TYPE',
220-
type: TypeError
221-
}
193+
expectTypeError
222194
);
223195
common.expectsError(
224196
() => 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
229206
);
230207
});
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

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.