test: use tmpDir in test-fs-utimes

test-fs-utimes was doing some tests against __filename. This made the
test unreliable when multiple copies were run simultaneously. In
general, tests should use files in either the tmp directory or else
fixtures, so change to using `common.tmpDir` instead. Each copy of the
test (if using `test.py` harness for parallel runs) will use its own
directory, making the test robust again.

PR-URL: https://github.com/nodejs/node/pull/16774
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
This commit is contained in:
Rich Trott 2017-11-04 20:33:31 -07:00
parent 71e7366c2a
commit da7b4a9d16

View File

@ -25,6 +25,8 @@ const assert = require('assert');
const util = require('util');
const fs = require('fs');
common.refreshTmpDir();
let tests_ok = 0;
let tests_run = 0;
@ -64,9 +66,6 @@ function expect_ok(syscall, resource, err, atime, mtime) {
}
}
// the tests assume that __filename belongs to the user running the tests
// this should be a fairly safe assumption; testing against a temp file
// would be even better though (node doesn't have such functionality yet)
function testIt(atime, mtime, callback) {
let fd;
@ -74,8 +73,8 @@ function testIt(atime, mtime, callback) {
// test synchronized code paths, these functions throw on failure
//
function syncTests() {
fs.utimesSync(__filename, atime, mtime);
expect_ok('utimesSync', __filename, undefined, atime, mtime);
fs.utimesSync(common.tmpDir, atime, mtime);
expect_ok('utimesSync', common.tmpDir, undefined, atime, mtime);
tests_run++;
// some systems don't have futimes
@ -110,17 +109,17 @@ function testIt(atime, mtime, callback) {
//
// test async code paths
//
fs.utimes(__filename, atime, mtime, common.mustCall(function(err) {
expect_ok('utimes', __filename, err, atime, mtime);
fs.utimes(common.tmpDir, atime, mtime, common.mustCall(function(err) {
expect_ok('utimes', common.tmpDir, err, atime, mtime);
fs.utimes('foobarbaz', atime, mtime, common.mustCall(function(err) {
expect_errno('utimes', 'foobarbaz', err, 'ENOENT');
// don't close this fd
if (common.isWindows) {
fd = fs.openSync(__filename, 'r+');
fd = fs.openSync(common.tmpDir, 'r+');
} else {
fd = fs.openSync(__filename, 'r');
fd = fs.openSync(common.tmpDir, 'r');
}
fs.futimes(fd, atime, mtime, common.mustCall(function(err) {
@ -140,7 +139,7 @@ function testIt(atime, mtime, callback) {
tests_run++;
}
const stats = fs.statSync(__filename);
const stats = fs.statSync(common.tmpDir);
// run tests
const runTest = common.mustCall(testIt, 6);
@ -169,7 +168,6 @@ process.on('exit', function() {
// Ref: https://github.com/nodejs/node/issues/13255
common.refreshTmpDir();
const path = `${common.tmpDir}/test-utimes-precision`;
fs.writeFileSync(path, '');