Skip to content

Commit f78c8e7

Browse files
Trottrvagg
authored andcommitted
test: fix flaky test for symlinks
If the symlink portion of the test was being skipped due to a combination of OS support and user privileges, then an assertion would always fail. This fixes that problem, improves assertion error reporting and splits the test to make it clear that it is a test for links and symlinks. Fixes: #3311 PR-URL: #3418 Reviewed-By: Johan Bergström <[email protected]>
1 parent b36b4f3 commit f78c8e7

File tree

2 files changed

+55
-62
lines changed

2 files changed

+55
-62
lines changed

test/parallel/test-fs-link.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const path = require('path');
5+
const fs = require('fs');
6+
7+
common.refreshTmpDir();
8+
9+
// test creating and reading hard link
10+
const srcPath = path.join(common.fixturesDir, 'cycles', 'root.js');
11+
const dstPath = path.join(common.tmpDir, 'link1.js');
12+
13+
const callback = function(err) {
14+
if (err) throw err;
15+
const srcContent = fs.readFileSync(srcPath, 'utf8');
16+
const dstContent = fs.readFileSync(dstPath, 'utf8');
17+
assert.strictEqual(srcContent, dstContent);
18+
};
19+
20+
fs.link(srcPath, dstPath, common.mustCall(callback));

test/parallel/test-fs-symlink.js

+35-62
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,50 @@
11
'use strict';
2-
var common = require('../common');
3-
var assert = require('assert');
4-
var path = require('path');
5-
var fs = require('fs');
6-
var exec = require('child_process').exec;
7-
var completed = 0;
8-
var expected_async = 4;
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const path = require('path');
5+
const fs = require('fs');
6+
const exec = require('child_process').exec;
7+
98
var linkTime;
109
var fileTime;
1110

12-
common.refreshTmpDir();
13-
14-
var runtest = function(skip_symlinks) {
15-
if (!skip_symlinks) {
16-
// test creating and reading symbolic link
17-
var linkData = path.join(common.fixturesDir, '/cycles/root.js');
18-
var linkPath = path.join(common.tmpDir, 'symlink1.js');
19-
20-
fs.symlink(linkData, linkPath, function(err) {
21-
if (err) throw err;
22-
console.log('symlink done');
23-
24-
fs.lstat(linkPath, function(err, stats) {
25-
if (err) throw err;
26-
linkTime = stats.mtime.getTime();
27-
completed++;
28-
});
29-
30-
fs.stat(linkPath, function(err, stats) {
31-
if (err) throw err;
32-
fileTime = stats.mtime.getTime();
33-
completed++;
34-
});
35-
36-
fs.readlink(linkPath, function(err, destination) {
37-
if (err) throw err;
38-
assert.equal(destination, linkData);
39-
completed++;
40-
});
41-
});
42-
}
43-
44-
// test creating and reading hard link
45-
var srcPath = path.join(common.fixturesDir, 'cycles', 'root.js');
46-
var dstPath = path.join(common.tmpDir, 'link1.js');
47-
48-
fs.link(srcPath, dstPath, function(err) {
49-
if (err) throw err;
50-
console.log('hard link done');
51-
var srcContent = fs.readFileSync(srcPath, 'utf8');
52-
var dstContent = fs.readFileSync(dstPath, 'utf8');
53-
assert.equal(srcContent, dstContent);
54-
completed++;
55-
});
56-
};
57-
5811
if (common.isWindows) {
5912
// On Windows, creating symlinks requires admin privileges.
6013
// We'll only try to run symlink test if we have enough privileges.
6114
exec('whoami /priv', function(err, o) {
6215
if (err || o.indexOf('SeCreateSymbolicLinkPrivilege') == -1) {
63-
expected_async = 1;
64-
runtest(true);
65-
} else {
66-
runtest(false);
16+
console.log('1..0 # Skipped: insufficient privileges');
17+
return;
6718
}
6819
});
69-
} else {
70-
runtest(false);
7120
}
7221

73-
process.on('exit', function() {
74-
assert.equal(completed, expected_async);
75-
assert(linkTime !== fileTime);
22+
common.refreshTmpDir();
23+
24+
// test creating and reading symbolic link
25+
const linkData = path.join(common.fixturesDir, '/cycles/root.js');
26+
const linkPath = path.join(common.tmpDir, 'symlink1.js');
27+
28+
fs.symlink(linkData, linkPath, function(err) {
29+
if (err) throw err;
30+
31+
fs.lstat(linkPath, common.mustCall(function(err, stats) {
32+
if (err) throw err;
33+
linkTime = stats.mtime.getTime();
34+
}));
35+
36+
fs.stat(linkPath, common.mustCall(function(err, stats) {
37+
if (err) throw err;
38+
fileTime = stats.mtime.getTime();
39+
}));
40+
41+
fs.readlink(linkPath, common.mustCall(function(err, destination) {
42+
if (err) throw err;
43+
assert.equal(destination, linkData);
44+
}));
7645
});
7746

47+
48+
process.on('exit', function() {
49+
assert.notStrictEqual(linkTime, fileTime);
50+
});

0 commit comments

Comments
 (0)