Skip to content

Commit 7c76592

Browse files
committed
Retry long path tests on Linux under real tmp dir
Use os.tmpdir() on Linux if it is readable/writable but *only* when the original tests fail in test-fs-long-path.js and test-require-long-path.js to avoid failing tests on ecryptfs filesystems See issue #2255 and comments to PR #3925
1 parent ca2e8b2 commit 7c76592

File tree

2 files changed

+74
-35
lines changed

2 files changed

+74
-35
lines changed

test/parallel/test-fs-long-path.js

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,44 @@ var common = require('../common');
33
var fs = require('fs');
44
var path = require('path');
55
var assert = require('assert');
6+
var os = require('os');
67

7-
var successes = 0;
8+
// when it fails test again under real OS tmp dir on Linux when it is
9+
// readable/writable to avoid failing tests on ecryptfs filesystems:
10+
// https://github.com/nodejs/node/issues/2255
11+
// it follows advice in comments to:
12+
// https://github.com/nodejs/node/pull/3925
13+
try {
14+
common.refreshTmpDir();
15+
testFsLongPath(common.tmpDir);
16+
common.refreshTmpDir();
17+
} catch (e) {
18+
if (os.type() == 'Linux') {
19+
fs.accessSync(os.tmpdir(), fs.R_OK | fs.W_OK);
20+
var tmpDir = path.join(os.tmpdir(),
21+
'node-' + process.version + '-test-' + (Math.random() * 1e6 | 0));
22+
fs.mkdirSync(tmpDir);
23+
testFsLongPath(tmpDir);
24+
fs.rmdirSync(tmpDir);
25+
} else {
26+
throw e;
27+
}
28+
}
829

9-
// make a path that will be at least 260 chars long.
10-
var fileNameLen = Math.max(260 - common.tmpDir.length - 1, 1);
11-
var fileName = path.join(common.tmpDir, new Array(fileNameLen + 1).join('x'));
12-
var fullPath = path.resolve(fileName);
30+
function testFsLongPath(tmpDir) {
1331

14-
common.refreshTmpDir();
32+
// make a path that will be at least 260 chars long.
33+
var fileNameLen = Math.max(260 - tmpDir.length - 1, 1);
34+
var fileName = path.join(tmpDir, new Array(fileNameLen + 1).join('x'));
35+
var fullPath = path.resolve(fileName);
1536

16-
console.log({
17-
filenameLength: fileName.length,
18-
fullPathLength: fullPath.length
19-
});
20-
21-
fs.writeFile(fullPath, 'ok', function(err) {
22-
if (err) throw err;
23-
successes++;
24-
25-
fs.stat(fullPath, function(err, stats) {
26-
if (err) throw err;
27-
successes++;
37+
console.log({
38+
filenameLength: fileName.length,
39+
fullPathLength: fullPath.length
2840
});
29-
});
3041

31-
process.on('exit', function() {
42+
fs.writeFileSync(fullPath, 'ok');
43+
fs.statSync(fullPath);
3244
fs.unlinkSync(fullPath);
33-
assert.equal(2, successes);
34-
});
45+
46+
}

test/parallel/test-require-long-path.js

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,49 @@
22
const common = require('../common');
33
const fs = require('fs');
44
const path = require('path');
5+
const os = require('os');
56

6-
// make a path that is more than 260 chars long.
7-
const dirNameLen = Math.max(260 - common.tmpDir.length, 1);
8-
const dirName = path.join(common.tmpDir, 'x'.repeat(dirNameLen));
9-
const fullDirPath = path.resolve(dirName);
7+
// when it fails test again under real OS tmp dir on Linux when it is
8+
// readable/writable to avoid failing tests on ecryptfs filesystems:
9+
// https://github.com/nodejs/node/issues/2255
10+
// it takes into account comments in:
11+
// https://github.com/nodejs/node/pull/3925
12+
try {
13+
common.refreshTmpDir();
14+
testRequireLongPath(common.tmpDir);
15+
common.refreshTmpDir();
16+
} catch (e) {
17+
if (os.type() == 'Linux') {
18+
fs.accessSync(os.tmpdir(), fs.R_OK | fs.W_OK);
19+
var tmpDir = path.join(os.tmpdir(),
20+
'node-' + process.version + '-test-' + (Math.random() * 1e6 | 0));
21+
fs.mkdirSync(tmpDir);
22+
testRequireLongPath(tmpDir);
23+
fs.rmdirSync(tmpDir);
24+
} else {
25+
throw e;
26+
}
27+
}
1028

11-
const indexFile = path.join(fullDirPath, 'index.js');
12-
const otherFile = path.join(fullDirPath, 'other.js');
29+
function testRequireLongPath(tmpDir) {
1330

14-
common.refreshTmpDir();
31+
// make a path that is more than 260 chars long.
32+
const dirNameLen = Math.max(260 - tmpDir.length, 1);
33+
const dirName = path.join(tmpDir, 'x'.repeat(dirNameLen));
34+
const fullDirPath = path.resolve(dirName);
1535

16-
fs.mkdirSync(fullDirPath);
17-
fs.writeFileSync(indexFile, 'require("./other");');
18-
fs.writeFileSync(otherFile, '');
36+
const indexFile = path.join(fullDirPath, 'index.js');
37+
const otherFile = path.join(fullDirPath, 'other.js');
1938

20-
require(indexFile);
21-
require(otherFile);
39+
fs.mkdirSync(fullDirPath);
40+
fs.writeFileSync(indexFile, 'require("./other");');
41+
fs.writeFileSync(otherFile, '');
2242

23-
common.refreshTmpDir();
43+
require(indexFile);
44+
require(otherFile);
45+
46+
fs.unlinkSync(indexFile);
47+
fs.unlinkSync(otherFile);
48+
fs.rmdirSync(fullDirPath);
49+
50+
}

0 commit comments

Comments
 (0)