Skip to content

Commit bc19a93

Browse files
Trottevanlucas
authored andcommitted
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: #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>
1 parent 4c11801 commit bc19a93

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const assert = require('assert');
2525
constutil=require('util');
2626
constfs=require('fs');
2727

28+
common.refreshTmpDir();
29+
2830
lettests_ok=0;
2931
lettests_run=0;
3032

@@ -64,18 +66,15 @@ function expect_ok(syscall, resource, err, atime, mtime) {
6466
}
6567
}
6668

67-
// the tests assume that __filename belongs to the user running the tests
68-
// this should be a fairly safe assumption; testing against a temp file
69-
// would be even better though (node doesn't have such functionality yet)
7069
functiontestIt(atime,mtime,callback){
7170

7271
letfd;
7372
//
7473
// test synchronized code paths, these functions throw on failure
7574
//
7675
functionsyncTests(){
77-
fs.utimesSync(__filename,atime,mtime);
78-
expect_ok('utimesSync',__filename,undefined,atime,mtime);
76+
fs.utimesSync(common.tmpDir,atime,mtime);
77+
expect_ok('utimesSync',common.tmpDir,undefined,atime,mtime);
7978
tests_run++;
8079

8180
// some systems don't have futimes
@@ -110,17 +109,17 @@ function testIt(atime, mtime, callback) {
110109
//
111110
// test async code paths
112111
//
113-
fs.utimes(__filename,atime,mtime,common.mustCall(function(err){
114-
expect_ok('utimes',__filename,err,atime,mtime);
112+
fs.utimes(common.tmpDir,atime,mtime,common.mustCall(function(err){
113+
expect_ok('utimes',common.tmpDir,err,atime,mtime);
115114

116115
fs.utimes('foobarbaz',atime,mtime,common.mustCall(function(err){
117116
expect_errno('utimes','foobarbaz',err,'ENOENT');
118117

119118
// don't close this fd
120119
if(common.isWindows){
121-
fd=fs.openSync(__filename,'r+');
120+
fd=fs.openSync(common.tmpDir,'r+');
122121
}else{
123-
fd=fs.openSync(__filename,'r');
122+
fd=fs.openSync(common.tmpDir,'r');
124123
}
125124

126125
fs.futimes(fd,atime,mtime,common.mustCall(function(err){
@@ -140,7 +139,7 @@ function testIt(atime, mtime, callback) {
140139
tests_run++;
141140
}
142141

143-
conststats=fs.statSync(__filename);
142+
conststats=fs.statSync(common.tmpDir);
144143

145144
// run tests
146145
construnTest=common.mustCall(testIt,6);
@@ -169,7 +168,6 @@ process.on('exit', function() {
169168

170169

171170
// Ref: https://github.com/nodejs/node/issues/13255
172-
common.refreshTmpDir();
173171
constpath=`${common.tmpDir}/test-utimes-precision`;
174172
fs.writeFileSync(path,'');
175173

0 commit comments

Comments
 (0)