Skip to content

Commit

Permalink
test: use tmpDir in test-fs-utimes
Browse files Browse the repository at this point in the history
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: #16774
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
  • Loading branch information
Trott authored and MylesBorins committed Nov 17, 2017
1 parent 360f403 commit c1309d6
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions test/parallel/test-fs-utimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -64,18 +66,15 @@ 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;
//
// 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
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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, '');

Expand Down

0 comments on commit c1309d6

Please sign in to comment.