Browse Source

test-fs-realpath: lint

v0.7.4-release
isaacs 14 years ago
committed by Ryan Dahl
parent
commit
6525c04522
  1. 150
      test/simple/test-fs-realpath.js

150
test/simple/test-fs-realpath.js

@ -27,8 +27,7 @@ function asynctest(testBlock, args, callback, assertBlock) {
} }
function bashRealpath(path, callback) { function bashRealpath(path, callback) {
exec("cd '"+path.replace("'","\\'")+"' && pwd -P",function exec("cd '" + path.replace("'", "\\'") + "' && pwd -P", function(err, o) {
(err, o) {
callback(err, o.trim()); callback(err, o.trim());
}); });
} }
@ -36,11 +35,11 @@ function bashRealpath(path, callback) {
// sub-tests: // sub-tests:
function test_simple_relative_symlink(callback) { function test_simple_relative_symlink(callback) {
console.log("test_simple_relative_symlink"); console.log('test_simple_relative_symlink');
var entry = common.fixturesDir + '/cycles/symlink', var entry = common.fixturesDir + '/cycles/symlink',
expected = common.fixturesDir + '/cycles/root.js'; expected = common.fixturesDir + '/cycles/root.js';
[ [
[entry, 'root.js'], [entry, 'root.js']
].forEach(function(t) { ].forEach(function(t) {
try {fs.unlinkSync(t[0]);}catch (e) {} try {fs.unlinkSync(t[0]);}catch (e) {}
fs.symlinkSync(t[1], t[0]); fs.symlinkSync(t[1], t[0]);
@ -48,21 +47,25 @@ function test_simple_relative_symlink(callback) {
}); });
var result = fs.realpathSync(entry); var result = fs.realpathSync(entry);
assert.equal(result, expected, assert.equal(result, expected,
'got '+common.inspect(result)+' expected '+common.inspect(expected)); 'got ' + common.inspect(result) + ' expected ' +
common.inspect(expected));
asynctest(fs.realpath, [entry], callback, function(err, result) { asynctest(fs.realpath, [entry], callback, function(err, result) {
assert.equal(result, expected, assert.equal(result, expected,
'got '+common.inspect(result)+' expected '+common.inspect(expected)); 'got ' +
common.inspect(result) +
' expected ' +
common.inspect(expected));
}); });
} }
function test_simple_absolute_symlink(callback) { function test_simple_absolute_symlink(callback) {
console.log("test_simple_absolute_symlink"); console.log('test_simple_absolute_symlink');
bashRealpath(common.fixturesDir, function(err, fixturesAbsDir) { bashRealpath(common.fixturesDir, function(err, fixturesAbsDir) {
if (err) return callback(err); if (err) return callback(err);
var entry = fixturesAbsDir + '/cycles/symlink', var entry = fixturesAbsDir + '/cycles/symlink',
expected = fixturesAbsDir + '/nested-index/one/index.js'; expected = fixturesAbsDir + '/nested-index/one/index.js';
[ [
[entry, expected], [entry, expected]
].forEach(function(t) { ].forEach(function(t) {
try {fs.unlinkSync(t[0]);} catch (e) {} try {fs.unlinkSync(t[0]);} catch (e) {}
fs.symlinkSync(t[1], t[0]); fs.symlinkSync(t[1], t[0]);
@ -70,24 +73,32 @@ function test_simple_absolute_symlink(callback) {
}); });
var result = fs.realpathSync(entry); var result = fs.realpathSync(entry);
assert.equal(result, expected, assert.equal(result, expected,
'got '+common.inspect(result)+' expected '+common.inspect(expected)); 'got ' +
common.inspect(result) +
' expected ' +
common.inspect(expected));
asynctest(fs.realpath, [entry], callback, function(err, result) { asynctest(fs.realpath, [entry], callback, function(err, result) {
assert.equal(result, expected, assert.equal(result, expected,
'got '+common.inspect(result)+' expected '+common.inspect(expected)); 'got ' +
common.inspect(result) +
' expected ' +
common.inspect(expected));
}); });
}); });
} }
function test_deep_relative_file_symlink(callback) { function test_deep_relative_file_symlink(callback) {
console.log("test_deep_relative_file_symlink"); console.log('test_deep_relative_file_symlink');
var expected = path.join(common.fixturesDir, 'cycles', 'root.js'); var expected = path.join(common.fixturesDir, 'cycles', 'root.js');
var linkData1 = "../../cycles/root.js"; var linkData1 = '../../cycles/root.js';
var linkPath1 = path.join(common.fixturesDir, "nested-index", 'one', 'symlink1.js'); var linkPath1 = path.join(common.fixturesDir,
'nested-index', 'one', 'symlink1.js');
try {fs.unlinkSync(linkPath1);} catch (e) {} try {fs.unlinkSync(linkPath1);} catch (e) {}
fs.symlinkSync(linkData1, linkPath1); fs.symlinkSync(linkData1, linkPath1);
var linkData2 = "../one/symlink1.js"; var linkData2 = '../one/symlink1.js';
var entry = path.join(common.fixturesDir, "nested-index", 'two', 'symlink1-b.js'); var entry = path.join(common.fixturesDir,
'nested-index', 'two', 'symlink1-b.js');
try {fs.unlinkSync(entry);} catch (e) {} try {fs.unlinkSync(entry);} catch (e) {}
fs.symlinkSync(linkData2, entry); fs.symlinkSync(linkData2, entry);
unlink.push(linkPath1); unlink.push(linkPath1);
@ -96,20 +107,25 @@ function test_deep_relative_file_symlink(callback) {
assert.equal(fs.realpathSync(entry), expected); assert.equal(fs.realpathSync(entry), expected);
asynctest(fs.realpath, [entry], callback, function(err, result) { asynctest(fs.realpath, [entry], callback, function(err, result) {
assert.equal(result, expected, assert.equal(result, expected,
'got '+common.inspect(result)+' expected '+common.inspect(expected)); 'got ' +
common.inspect(result) +
' expected ' +
common.inspect(expected));
}); });
} }
function test_deep_relative_dir_symlink(callback) { function test_deep_relative_dir_symlink(callback) {
console.log("test_deep_relative_dir_symlink"); console.log('test_deep_relative_dir_symlink');
var expected = path.join(common.fixturesDir, 'cycles', 'folder'); var expected = path.join(common.fixturesDir, 'cycles', 'folder');
var linkData1b = "../../cycles/folder"; var linkData1b = '../../cycles/folder';
var linkPath1b = path.join(common.fixturesDir, "nested-index", 'one', 'symlink1-dir'); var linkPath1b = path.join(common.fixturesDir,
'nested-index', 'one', 'symlink1-dir');
try {fs.unlinkSync(linkPath1b);} catch (e) {} try {fs.unlinkSync(linkPath1b);} catch (e) {}
fs.symlinkSync(linkData1b, linkPath1b); fs.symlinkSync(linkData1b, linkPath1b);
var linkData2b = "../one/symlink1-dir"; var linkData2b = '../one/symlink1-dir';
var entry = path.join(common.fixturesDir, "nested-index", 'two', 'symlink12-dir'); var entry = path.join(common.fixturesDir,
'nested-index', 'two', 'symlink12-dir');
try {fs.unlinkSync(entry);} catch (e) {} try {fs.unlinkSync(entry);} catch (e) {}
fs.symlinkSync(linkData2b, entry); fs.symlinkSync(linkData2b, entry);
unlink.push(linkPath1b); unlink.push(linkPath1b);
@ -119,17 +135,20 @@ function test_deep_relative_dir_symlink(callback) {
asynctest(fs.realpath, [entry], callback, function(err, result) { asynctest(fs.realpath, [entry], callback, function(err, result) {
assert.equal(result, expected, assert.equal(result, expected,
'got '+common.inspect(result)+' expected '+common.inspect(expected)); 'got ' +
common.inspect(result) +
' expected ' +
common.inspect(expected));
}); });
} }
function test_cyclic_link_protection(callback) { function test_cyclic_link_protection(callback) {
console.log("test_cyclic_link_protection"); console.log('test_cyclic_link_protection');
var entry = common.fixturesDir + '/cycles/realpath-3a'; var entry = common.fixturesDir + '/cycles/realpath-3a';
[ [
[entry, '../cycles/realpath-3b'], [entry, '../cycles/realpath-3b'],
[common.fixturesDir + '/cycles/realpath-3b', '../cycles/realpath-3c'], [common.fixturesDir + '/cycles/realpath-3b', '../cycles/realpath-3c'],
[common.fixturesDir+'/cycles/realpath-3c', '../cycles/realpath-3a'], [common.fixturesDir + '/cycles/realpath-3c', '../cycles/realpath-3a']
].forEach(function(t) { ].forEach(function(t) {
try {fs.unlinkSync(t[0]);} catch (e) {} try {fs.unlinkSync(t[0]);} catch (e) {}
fs.symlinkSync(t[1], t[0]); fs.symlinkSync(t[1], t[0]);
@ -143,7 +162,7 @@ function test_cyclic_link_protection(callback) {
} }
function test_cyclic_link_overprotection(callback) { function test_cyclic_link_overprotection(callback) {
console.log("test_cyclic_link_overprotection"); console.log('test_cyclic_link_overprotection');
var cycles = common.fixturesDir + '/cycles'; var cycles = common.fixturesDir + '/cycles';
var expected = fs.realpathSync(cycles); var expected = fs.realpathSync(cycles);
var folder = cycles + '/folder'; var folder = cycles + '/folder';
@ -159,7 +178,7 @@ function test_cyclic_link_overprotection (callback) {
} }
function test_relative_input_cwd(callback) { function test_relative_input_cwd(callback) {
console.log("test_relative_input_cwd"); console.log('test_relative_input_cwd');
var p = common.fixturesDir.lastIndexOf('/'); var p = common.fixturesDir.lastIndexOf('/');
var entrydir = common.fixturesDir.substr(0, p); var entrydir = common.fixturesDir.substr(0, p);
var entry = common.fixturesDir.substr(p + 1) + '/cycles/realpath-3a'; var entry = common.fixturesDir.substr(p + 1) + '/cycles/realpath-3a';
@ -167,7 +186,7 @@ function test_relative_input_cwd(callback) {
[ [
[entry, '../cycles/realpath-3b'], [entry, '../cycles/realpath-3b'],
[common.fixturesDir + '/cycles/realpath-3b', '../cycles/realpath-3c'], [common.fixturesDir + '/cycles/realpath-3b', '../cycles/realpath-3c'],
[common.fixturesDir+'/cycles/realpath-3c', 'root.js'], [common.fixturesDir + '/cycles/realpath-3c', 'root.js']
].forEach(function(t) { ].forEach(function(t) {
var fn = t[0]; var fn = t[0];
if (fn.charAt(0) !== '/') fn = entrydir + '/' + fn; if (fn.charAt(0) !== '/') fn = entrydir + '/' + fn;
@ -181,13 +200,16 @@ function test_relative_input_cwd(callback) {
asynctest(fs.realpath, [entry], callback, function(err, result) { asynctest(fs.realpath, [entry], callback, function(err, result) {
process.chdir(origcwd); process.chdir(origcwd);
assert.equal(result, expected, assert.equal(result, expected,
'got '+common.inspect(result)+' expected '+common.inspect(expected)); 'got ' +
common.inspect(result) +
' expected ' +
common.inspect(expected));
return true; return true;
}); });
} }
function test_deep_symlink_mix(callback) { function test_deep_symlink_mix(callback) {
console.log("test_deep_symlink_mix"); console.log('test_deep_symlink_mix');
// todo: check to see that common.fixturesDir is not rooted in the // todo: check to see that common.fixturesDir is not rooted in the
// same directory as our test symlink. // same directory as our test symlink.
// obtain our current realpath using bash (so we can test ourselves) // obtain our current realpath using bash (so we can test ourselves)
@ -213,9 +235,12 @@ function test_deep_symlink_mix(callback) {
[entry, '../tmp/node-test-realpath-d1/foo'], [entry, '../tmp/node-test-realpath-d1/foo'],
[tmp('node-test-realpath-d1'), '../tmp/node-test-realpath-d2'], [tmp('node-test-realpath-d1'), '../tmp/node-test-realpath-d2'],
[tmp('node-test-realpath-d2/foo'), '../node-test-realpath-f2'], [tmp('node-test-realpath-d2/foo'), '../node-test-realpath-f2'],
[tmp('node-test-realpath-f2'), fixturesAbsDir+'/nested-index/one/realpath-c'], [tmp('node-test-realpath-f2'), fixturesAbsDir +
[fixturesAbsDir+'/nested-index/one/realpath-c', fixturesAbsDir+'/nested-index/two/realpath-c'], '/nested-index/one/realpath-c'],
[fixturesAbsDir+'/nested-index/two/realpath-c', '../../cycles/root.js'], [fixturesAbsDir + '/nested-index/one/realpath-c', fixturesAbsDir +
'/nested-index/two/realpath-c'],
[fixturesAbsDir + '/nested-index/two/realpath-c',
'../../cycles/root.js']
].forEach(function(t) { ].forEach(function(t) {
//common.debug('setting up '+t[0]+' -> '+t[1]); //common.debug('setting up '+t[0]+' -> '+t[1]);
try { fs.unlinkSync(t[0]); } catch (e) {} try { fs.unlinkSync(t[0]); } catch (e) {}
@ -229,14 +254,17 @@ function test_deep_symlink_mix(callback) {
assert.equal(fs.realpathSync(entry), expected); assert.equal(fs.realpathSync(entry), expected);
asynctest(fs.realpath, [entry], callback, function(err, result) { asynctest(fs.realpath, [entry], callback, function(err, result) {
assert.equal(result, expected, assert.equal(result, expected,
'got '+common.inspect(result)+' expected '+common.inspect(expected)); 'got ' +
common.inspect(result) +
' expected ' +
common.inspect(expected));
return true; return true;
}); });
}); });
} }
function test_non_symlinks(callback) { function test_non_symlinks(callback) {
console.log("test_non_symlinks"); console.log('test_non_symlinks');
bashRealpath(common.fixturesDir, function(err, fixturesAbsDir) { bashRealpath(common.fixturesDir, function(err, fixturesAbsDir) {
if (err) return callback(err); if (err) return callback(err);
var p = fixturesAbsDir.lastIndexOf('/'); var p = fixturesAbsDir.lastIndexOf('/');
@ -249,23 +277,26 @@ function test_non_symlinks(callback) {
asynctest(fs.realpath, [entry], callback, function(err, result) { asynctest(fs.realpath, [entry], callback, function(err, result) {
process.chdir(origcwd); process.chdir(origcwd);
assert.equal(result, expected, assert.equal(result, expected,
'got '+common.inspect(result)+' expected '+common.inspect(expected)); 'got ' +
common.inspect(result) +
' expected ' +
common.inspect(expected));
return true; return true;
}); });
}); });
} }
var upone = path.join(process.cwd(), ".."); var upone = path.join(process.cwd(), '..');
function test_escape_cwd(cb) { function test_escape_cwd(cb) {
console.log("test_escape_cwd"); console.log('test_escape_cwd');
asynctest(fs.realpath, [".."], cb, function(er, uponeActual){ asynctest(fs.realpath, ['..'], cb, function(er, uponeActual) {
assert.equal(upone, uponeActual, assert.equal(upone, uponeActual,
"realpath('..') expected: "+upone+" actual:"+uponeActual); 'realpath("..") expected: ' + upone + ' actual:' + uponeActual);
}) });
} }
var uponeActual = fs.realpathSync(".."); var uponeActual = fs.realpathSync('..');
assert.equal(upone, uponeActual, assert.equal(upone, uponeActual,
"realpathSync('..') expected: "+upone+" actual:"+uponeActual); 'realpathSync("..") expected: ' + upone + ' actual:' + uponeActual);
// absolute symlinks with children. // absolute symlinks with children.
// . // .
@ -276,31 +307,31 @@ assert.equal(upone, uponeActual,
// `-- link -> /tmp/node-test-realpath-abs-kids/a/b/ // `-- link -> /tmp/node-test-realpath-abs-kids/a/b/
// realpath(root+'/a/link/c/x.txt') ==> root+'/a/b/c/x.txt' // realpath(root+'/a/link/c/x.txt') ==> root+'/a/b/c/x.txt'
function test_abs_with_kids(cb) { function test_abs_with_kids(cb) {
console.log("test_abs_with_kids"); console.log('test_abs_with_kids');
bashRealpath(common.fixturesDir, function(err, fixturesAbsDir) { bashRealpath(common.fixturesDir, function(err, fixturesAbsDir) {
var root = fixturesAbsDir + '/node-test-realpath-abs-kids'; var root = fixturesAbsDir + '/node-test-realpath-abs-kids';
function cleanup() { function cleanup() {
;['/a/b/c/x.txt' ['/a/b/c/x.txt',
, '/a/link' '/a/link'
].forEach(function(file) { ].forEach(function(file) {
try {fs.unlinkSync(root + file)} catch (ex) {} try {fs.unlinkSync(root + file)} catch (ex) {}
}); });
;['/a/b/c' ['/a/b/c',
, '/a/b' '/a/b',
, '/a' '/a',
, '' ''
].forEach(function(folder) { ].forEach(function(folder) {
try {fs.rmdirSync(root + folder)} catch (ex) {} try {fs.rmdirSync(root + folder)} catch (ex) {}
}); });
} }
function setup() { function setup() {
cleanup() cleanup();
;['' ['',
, '/a' '/a',
, '/a/b' '/a/b',
, '/a/b/c' '/a/b/c'
].forEach(function(folder) { ].forEach(function(folder) {
console.log("mkdir "+root+folder) console.log('mkdir ' + root + folder);
fs.mkdirSync(root + folder, 0700); fs.mkdirSync(root + folder, 0700);
}); });
fs.writeFileSync(root + '/a/b/c/x.txt', 'foo'); fs.writeFileSync(root + '/a/b/c/x.txt', 'foo');
@ -317,7 +348,7 @@ function test_abs_with_kids (cb) {
assert.equal(actual, expectPath); assert.equal(actual, expectPath);
cleanup(); cleanup();
}); });
}) });
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -338,8 +369,9 @@ var tests = [
var numtests = tests.length; var numtests = tests.length;
function runNextTest(err) { function runNextTest(err) {
if (err) throw err; if (err) throw err;
var test = tests.shift() var test = tests.shift();
if (!test) console.log(numtests+' subtests completed OK for fs.realpath'); if (!test) console.log(numtests +
' subtests completed OK for fs.realpath');
else test(runNextTest); else test(runNextTest);
} }
runNextTest(); runNextTest();
@ -353,7 +385,7 @@ fs.realpath('/', function (err, result) {
process.addListener("exit", function () { process.addListener('exit', function() {
unlink.forEach(function(path) { try {fs.unlinkSync(path);} catch (e) {} }); unlink.forEach(function(path) { try {fs.unlinkSync(path);} catch (e) {} });
assert.equal(async_completed, async_expected); assert.equal(async_completed, async_expected);
}); });

Loading…
Cancel
Save