Skip to content

Commit

Permalink
test: fix flaky test for symlinks
Browse files Browse the repository at this point in the history
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 renames the test to make it clear that it is a test for links and
not just symlinks.

Fixes: #3311
  • Loading branch information
Trott committed Oct 18, 2015
1 parent d8db757 commit 193c6b2
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions test/parallel/test-fs-symlink.js → test/parallel/test-fs-link.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
'use strict';
var common = require('../common');
var assert = require('assert');
var path = require('path');
var fs = require('fs');
var exec = require('child_process').exec;
const common = require('../common');
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const exec = require('child_process').exec;
var completed = 0;
var expected_async = 4;
var expected_async;
var linkTime;
var fileTime;

common.refreshTmpDir();

var runtest = function(skip_symlinks) {
const runTest = function() {
if (!skip_symlinks) {
// test creating and reading symbolic link
var linkData = path.join(common.fixturesDir, '/cycles/root.js');
var linkPath = path.join(common.tmpDir, 'symlink1.js');
const linkData = path.join(common.fixturesDir, '/cycles/root.js');
const linkPath = path.join(common.tmpDir, 'symlink1.js');

fs.symlink(linkData, linkPath, function(err) {
if (err) throw err;
Expand All @@ -42,36 +42,36 @@ var runtest = function(skip_symlinks) {
}

// test creating and reading hard link
var srcPath = path.join(common.fixturesDir, 'cycles', 'root.js');
var dstPath = path.join(common.tmpDir, 'link1.js');
const srcPath = path.join(common.fixturesDir, 'cycles', 'root.js');
const dstPath = path.join(common.tmpDir, 'link1.js');

fs.link(srcPath, dstPath, function(err) {
if (err) throw err;
console.log('hard link done');
var srcContent = fs.readFileSync(srcPath, 'utf8');
var dstContent = fs.readFileSync(dstPath, 'utf8');
const srcContent = fs.readFileSync(srcPath, 'utf8');
const dstContent = fs.readFileSync(dstPath, 'utf8');
assert.equal(srcContent, dstContent);
completed++;
});
};

var skip_symlinks = false;
var expected_async = 4;
if (common.isWindows) {
// On Windows, creating symlinks requires admin privileges.
// We'll only try to run symlink test if we have enough privileges.
exec('whoami /priv', function(err, o) {
if (err || o.indexOf('SeCreateSymbolicLinkPrivilege') == -1) {
expected_async = 1;
runtest(true);
} else {
runtest(false);
skip_symlinks = true;
}
});
} else {
runtest(false);
}
runTest();

process.on('exit', function() {
assert.equal(completed, expected_async);
assert(linkTime !== fileTime);
if (! skip_symlinks) {
assert.notStrictEqual(linkTime, fileTime);
}
});

0 comments on commit 193c6b2

Please sign in to comment.