mirror of https://github.com/lukechilds/node.git
Ryan Dahl
15 years ago
30 changed files with 1120 additions and 324 deletions
@ -0,0 +1,26 @@ |
|||
exports.parse = function(d) { |
|||
var trim = function(str) { return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); } |
|||
var ini = {'-':{}}; |
|||
|
|||
var section = '-'; |
|||
|
|||
var lines = d.split('\n'); |
|||
for (var i=0; i<lines.length; i++) { |
|||
|
|||
var re = /(.*)=(.*)|\[([a-z:\.0-9_\s]+)\]/i; |
|||
|
|||
var match = lines[i].match(re); |
|||
if (match != null) { |
|||
if (match[3] != undefined) { |
|||
section = match[3]; |
|||
ini[section] = {}; |
|||
} else { |
|||
var key = trim(match[1]); |
|||
var value = trim(match[2]); |
|||
ini[section][key] = value; |
|||
} |
|||
} |
|||
} |
|||
|
|||
return ini; |
|||
} |
@ -0,0 +1,11 @@ |
|||
root=something |
|||
|
|||
[section] |
|||
one=two |
|||
Foo=Bar |
|||
this=Your Mother! |
|||
blank= |
|||
|
|||
[Section Two] |
|||
something else=blah |
|||
remove = whitespace |
@ -0,0 +1,67 @@ |
|||
process.mixin(require("../common")); |
|||
tcp = require("tcp"); |
|||
N = 200; |
|||
|
|||
server = tcp.createServer(function (connection) { |
|||
function write (j) { |
|||
if (j >= N) { |
|||
connection.close(); |
|||
return; |
|||
} |
|||
setTimeout(function () { |
|||
connection.write("C"); |
|||
write(j+1); |
|||
}, 10); |
|||
} |
|||
write(0); |
|||
}); |
|||
server.listen(PORT); |
|||
|
|||
|
|||
recv = ""; |
|||
chars_recved = 0; |
|||
|
|||
client = tcp.createConnection(PORT); |
|||
client.setEncoding("ascii"); |
|||
client.addListener("data", function (d) { |
|||
print(d); |
|||
recv += d; |
|||
}); |
|||
|
|||
setTimeout(function () { |
|||
chars_recved = recv.length; |
|||
puts("pause at: " + chars_recved); |
|||
assert.equal(true, chars_recved > 1); |
|||
client.pause(); |
|||
setTimeout(function () { |
|||
puts("resume at: " + chars_recved); |
|||
assert.equal(chars_recved, recv.length); |
|||
client.resume(); |
|||
|
|||
setTimeout(function () { |
|||
chars_recved = recv.length; |
|||
puts("pause at: " + chars_recved); |
|||
client.pause(); |
|||
|
|||
setTimeout(function () { |
|||
puts("resume at: " + chars_recved); |
|||
assert.equal(chars_recved, recv.length); |
|||
client.resume(); |
|||
|
|||
}, 500); |
|||
|
|||
}, 500); |
|||
|
|||
}, 500); |
|||
|
|||
}, 500); |
|||
|
|||
client.addListener("end", function () { |
|||
server.close(); |
|||
client.close(); |
|||
}); |
|||
|
|||
process.addListener("exit", function () { |
|||
assert.equal(N, recv.length); |
|||
debug("Exit"); |
|||
}); |
@ -1,55 +0,0 @@ |
|||
process.mixin(require("../common")); |
|||
tcp = require("tcp"); |
|||
N = 30*1024; // 500kb
|
|||
|
|||
puts("build big string"); |
|||
var body = ""; |
|||
for (var i = 0; i < N; i++) { |
|||
body += "C"; |
|||
} |
|||
|
|||
puts("start server on port " + PORT); |
|||
|
|||
server = tcp.createServer(function (connection) { |
|||
connection.addListener("connect", function () { |
|||
connection.write(body); |
|||
connection.close(); |
|||
}); |
|||
}); |
|||
server.listen(PORT); |
|||
|
|||
|
|||
chars_recved = 0; |
|||
npauses = 0; |
|||
|
|||
|
|||
var paused = false; |
|||
client = tcp.createConnection(PORT); |
|||
client.setEncoding("ascii"); |
|||
client.addListener("data", function (d) { |
|||
chars_recved += d.length; |
|||
puts("got " + chars_recved); |
|||
if (!paused) { |
|||
client.pause(); |
|||
npauses += 1; |
|||
paused = true; |
|||
puts("pause"); |
|||
x = chars_recved; |
|||
setTimeout(function () { |
|||
assert.equal(chars_recved, x); |
|||
client.resume(); |
|||
puts("resume"); |
|||
paused = false; |
|||
}, 100); |
|||
} |
|||
}); |
|||
|
|||
client.addListener("end", function () { |
|||
server.close(); |
|||
client.close(); |
|||
}); |
|||
|
|||
process.addListener("exit", function () { |
|||
assert.equal(N, chars_recved); |
|||
assert.equal(true, npauses > 2); |
|||
}); |
@ -0,0 +1,12 @@ |
|||
process.mixin(require("../common")); |
|||
child = process.createChildProcess('/usr/bin/env', [], {'HELLO' : 'WORLD'}); |
|||
response = ""; |
|||
|
|||
child.addListener("output", function (chunk) { |
|||
puts("stdout: " + JSON.stringify(chunk)); |
|||
if (chunk) response += chunk; |
|||
}); |
|||
|
|||
process.addListener('exit', function () { |
|||
assert.ok(response.indexOf('HELLO=WORLD') >= 0); |
|||
}); |
@ -0,0 +1,54 @@ |
|||
process.mixin(require('../common')); |
|||
|
|||
var |
|||
fn = path.join(fixturesDir, 'multipart.js'), |
|||
file = fs.createReadStream(fn), |
|||
|
|||
callbacks = { |
|||
open: -1, |
|||
end: -1, |
|||
close: -1 |
|||
}, |
|||
|
|||
paused = false, |
|||
|
|||
fileContent = ''; |
|||
|
|||
file |
|||
.addListener('open', function(fd) { |
|||
callbacks.open++; |
|||
assert.equal('number', typeof fd); |
|||
assert.ok(file.readable); |
|||
}) |
|||
.addListener('error', function(err) { |
|||
throw err; |
|||
}) |
|||
.addListener('data', function(data) { |
|||
assert.ok(!paused); |
|||
fileContent += data; |
|||
|
|||
paused = true; |
|||
file.pause(); |
|||
assert.ok(file.paused); |
|||
|
|||
setTimeout(function() { |
|||
paused = false; |
|||
file.resume(); |
|||
assert.ok(!file.paused); |
|||
}, 10); |
|||
}) |
|||
.addListener('end', function(chunk) { |
|||
callbacks.end++; |
|||
}) |
|||
.addListener('close', function() { |
|||
callbacks.close++; |
|||
assert.ok(!file.readable); |
|||
|
|||
assert.equal(fs.readFileSync(fn), fileContent); |
|||
}); |
|||
|
|||
process.addListener('exit', function() { |
|||
for (var k in callbacks) { |
|||
assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]); |
|||
} |
|||
}); |
@ -0,0 +1,50 @@ |
|||
process.mixin(require('../common')); |
|||
|
|||
var |
|||
fn = path.join(fixturesDir, "write.txt"), |
|||
file = fs.createWriteStream(fn), |
|||
|
|||
EXPECTED = '0123456789', |
|||
|
|||
callbacks = { |
|||
open: -1, |
|||
drain: -2, |
|||
close: -1 |
|||
}; |
|||
|
|||
file |
|||
.addListener('open', function(fd) { |
|||
callbacks.open++; |
|||
assert.equal('number', typeof fd); |
|||
}) |
|||
.addListener('error', function(err) { |
|||
throw err; |
|||
}) |
|||
.addListener('drain', function() { |
|||
callbacks.drain++; |
|||
if (callbacks.drain == -1) { |
|||
assert.equal(EXPECTED, fs.readFileSync(fn)); |
|||
file.write(EXPECTED); |
|||
} else if (callbacks.drain == 0) { |
|||
assert.equal(EXPECTED+EXPECTED, fs.readFileSync(fn)); |
|||
file.close(); |
|||
} |
|||
}) |
|||
.addListener('close', function() { |
|||
callbacks.close++; |
|||
assert.throws(function() { |
|||
file.write('should not work anymore'); |
|||
}); |
|||
|
|||
fs.unlinkSync(fn); |
|||
}); |
|||
|
|||
for (var i = 0; i < 10; i++) { |
|||
assert.strictEqual(false, file.write(i)); |
|||
} |
|||
|
|||
process.addListener('exit', function() { |
|||
for (var k in callbacks) { |
|||
assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]); |
|||
} |
|||
}); |
@ -1,56 +1,251 @@ |
|||
process.mixin(require("../common")); |
|||
|
|||
var async_completed = 0, async_expected = 0; |
|||
|
|||
// a. deep relative file symlink
|
|||
var dstPath = path.join(fixturesDir, 'cycles', 'root.js'); |
|||
var linkData1 = "../../cycles/root.js"; |
|||
var linkPath1 = path.join(fixturesDir, "nested-index", 'one', 'symlink1.js'); |
|||
try {fs.unlinkSync(linkPath1);}catch(e){} |
|||
fs.symlinkSync(linkData1, linkPath1); |
|||
|
|||
var linkData2 = "../one/symlink1.js"; |
|||
var linkPath2 = path.join(fixturesDir, "nested-index", 'two', 'symlink1-b.js'); |
|||
try {fs.unlinkSync(linkPath2);}catch(e){} |
|||
fs.symlinkSync(linkData2, linkPath2); |
|||
|
|||
// b. deep relative directory symlink
|
|||
var dstPath_b = path.join(fixturesDir, 'cycles', 'folder'); |
|||
var linkData1b = "../../cycles/folder"; |
|||
var linkPath1b = path.join(fixturesDir, "nested-index", 'one', 'symlink1-dir'); |
|||
try {fs.unlinkSync(linkPath1b);}catch(e){} |
|||
fs.symlinkSync(linkData1b, linkPath1b); |
|||
|
|||
var linkData2b = "../one/symlink1-dir"; |
|||
var linkPath2b = path.join(fixturesDir, "nested-index", 'two', 'symlink12-dir'); |
|||
try {fs.unlinkSync(linkPath2b);}catch(e){} |
|||
fs.symlinkSync(linkData2b, linkPath2b); |
|||
|
|||
assert.equal(fs.realpathSync(linkPath2), dstPath); |
|||
assert.equal(fs.realpathSync(linkPath2b), dstPath_b); |
|||
|
|||
async_expected++; |
|||
fs.realpath(linkPath2, function(err, rpath) { |
|||
if (err) throw err; |
|||
assert.equal(rpath, dstPath); |
|||
async_completed++; |
|||
}); |
|||
var async_completed = 0, async_expected = 0, unlink = []; |
|||
|
|||
async_expected++; |
|||
fs.realpath(linkPath2b, function(err, rpath) { |
|||
if (err) throw err; |
|||
assert.equal(rpath, dstPath_b); |
|||
function asynctest(testBlock, args, callback, assertBlock) { |
|||
async_expected++; |
|||
testBlock.apply(testBlock, args.concat([function(err){ |
|||
var ignoreError = false; |
|||
if (assertBlock) { |
|||
try { |
|||
ignoreError = assertBlock.apply(assertBlock, |
|||
Array.prototype.slice.call(arguments)); |
|||
} |
|||
catch (e) { |
|||
err = e; |
|||
} |
|||
} |
|||
async_completed++; |
|||
}); |
|||
callback(ignoreError ? null : err); |
|||
}])); |
|||
} |
|||
|
|||
// todo: test shallow symlinks (file & dir)
|
|||
// todo: test non-symlinks (file & dir)
|
|||
// todo: test error on cyclic symlinks
|
|||
function bashRealpath(path, callback) { |
|||
exec("cd '"+path.replace("'","\\'")+"' && pwd -P",function (err, o) { |
|||
callback(err, o.trim()); |
|||
}); |
|||
} |
|||
|
|||
process.addListener("exit", function () { |
|||
// sub-tests:
|
|||
|
|||
function test_simple_relative_symlink(callback) { |
|||
var entry = fixturesDir+'/cycles/symlink', |
|||
expected = fixturesDir+'/cycles/root.js'; |
|||
[ |
|||
[entry, 'root.js'], |
|||
].forEach(function(t) { |
|||
try {fs.unlinkSync(t[0]);}catch(e){} |
|||
fs.symlinkSync(t[1], t[0]); |
|||
unlink.push(t[0]); |
|||
}); |
|||
var result = fs.realpathSync(entry); |
|||
assert.equal(result, expected, |
|||
'got '+inspect(result)+' expected '+inspect(expected)); |
|||
asynctest(fs.realpath, [entry], callback, function(err, result){ |
|||
assert.equal(result, expected, |
|||
'got '+inspect(result)+' expected '+inspect(expected)); |
|||
}); |
|||
} |
|||
|
|||
function test_simple_absolute_symlink(callback) { |
|||
bashRealpath(fixturesDir, function(err, fixturesAbsDir) { |
|||
if (err) return callback(err); |
|||
var entry = fixturesAbsDir+'/cycles/symlink', |
|||
expected = fixturesAbsDir+'/nested-index/one/index.js'; |
|||
[ |
|||
[entry, expected], |
|||
].forEach(function(t) { |
|||
try {fs.unlinkSync(t[0]);}catch(e){} |
|||
fs.symlinkSync(t[1], t[0]); |
|||
unlink.push(t[0]); |
|||
}); |
|||
var result = fs.realpathSync(entry); |
|||
assert.equal(result, expected, |
|||
'got '+inspect(result)+' expected '+inspect(expected)); |
|||
asynctest(fs.realpath, [entry], callback, function(err, result){ |
|||
assert.equal(result, expected, |
|||
'got '+inspect(result)+' expected '+inspect(expected)); |
|||
}); |
|||
}); |
|||
} |
|||
|
|||
function test_deep_relative_file_symlink(callback) { |
|||
var expected = path.join(fixturesDir, 'cycles', 'root.js'); |
|||
var linkData1 = "../../cycles/root.js"; |
|||
var linkPath1 = path.join(fixturesDir, "nested-index", 'one', 'symlink1.js'); |
|||
try {fs.unlinkSync(linkPath1);}catch(e){} |
|||
try {fs.unlinkSync(linkPath2);}catch(e){} |
|||
fs.symlinkSync(linkData1, linkPath1); |
|||
|
|||
var linkData2 = "../one/symlink1.js"; |
|||
var entry = path.join(fixturesDir, "nested-index", 'two', 'symlink1-b.js'); |
|||
try {fs.unlinkSync(entry);}catch(e){} |
|||
fs.symlinkSync(linkData2, entry); |
|||
unlink.push(linkPath1); |
|||
unlink.push(entry); |
|||
|
|||
assert.equal(fs.realpathSync(entry), expected); |
|||
asynctest(fs.realpath, [entry], callback, function(err, result){ |
|||
assert.equal(result, expected, |
|||
'got '+inspect(result)+' expected '+inspect(expected)); |
|||
}); |
|||
} |
|||
|
|||
function test_deep_relative_dir_symlink(callback) { |
|||
var expected = path.join(fixturesDir, 'cycles', 'folder'); |
|||
var linkData1b = "../../cycles/folder"; |
|||
var linkPath1b = path.join(fixturesDir, "nested-index", 'one', 'symlink1-dir'); |
|||
try {fs.unlinkSync(linkPath1b);}catch(e){} |
|||
try {fs.unlinkSync(linkPath2b);}catch(e){} |
|||
fs.symlinkSync(linkData1b, linkPath1b); |
|||
|
|||
var linkData2b = "../one/symlink1-dir"; |
|||
var entry = path.join(fixturesDir, "nested-index", 'two', 'symlink12-dir'); |
|||
try {fs.unlinkSync(entry);}catch(e){} |
|||
fs.symlinkSync(linkData2b, entry); |
|||
unlink.push(linkPath1b); |
|||
unlink.push(entry); |
|||
|
|||
assert.equal(fs.realpathSync(entry), expected); |
|||
|
|||
asynctest(fs.realpath, [entry], callback, function(err, result){ |
|||
assert.equal(result, expected, |
|||
'got '+inspect(result)+' expected '+inspect(expected)); |
|||
}); |
|||
} |
|||
|
|||
function test_cyclic_link_protection(callback) { |
|||
var entry = fixturesDir+'/cycles/realpath-3a'; |
|||
[ |
|||
[entry, '../cycles/realpath-3b'], |
|||
[fixturesDir+'/cycles/realpath-3b', '../cycles/realpath-3c'], |
|||
[fixturesDir+'/cycles/realpath-3c', '../cycles/realpath-3a'], |
|||
].forEach(function(t) { |
|||
try {fs.unlinkSync(t[0]);}catch(e){} |
|||
fs.symlinkSync(t[1], t[0]); |
|||
unlink.push(t[0]); |
|||
}); |
|||
assert.throws(function(){ fs.realpathSync(entry); }); |
|||
asynctest(fs.realpath, [entry], callback, function(err, result){ |
|||
assert.ok(err && true); |
|||
return true; |
|||
}); |
|||
} |
|||
|
|||
function test_relative_input_cwd(callback) { |
|||
var p = fixturesDir.lastIndexOf('/'); |
|||
var entrydir = fixturesDir.substr(0, p); |
|||
var entry = fixturesDir.substr(p+1)+'/cycles/realpath-3a'; |
|||
var expected = fixturesDir+'/cycles/root.js'; |
|||
[ |
|||
[entry, '../cycles/realpath-3b'], |
|||
[fixturesDir+'/cycles/realpath-3b', '../cycles/realpath-3c'], |
|||
[fixturesDir+'/cycles/realpath-3c', 'root.js'], |
|||
].forEach(function(t) { |
|||
var fn = t[0]; |
|||
if (fn.charAt(0) !== '/') fn = entrydir + '/' + fn; |
|||
try {fs.unlinkSync(fn);}catch(e){} |
|||
fs.symlinkSync(t[1], fn); |
|||
unlink.push(fn); |
|||
}); |
|||
var origcwd = process.cwd(); |
|||
process.chdir(entrydir); |
|||
assert.equal(fs.realpathSync(entry), expected); |
|||
asynctest(fs.realpath, [entry], callback, function(err, result){ |
|||
process.chdir(origcwd); |
|||
assert.equal(result, expected, |
|||
'got '+inspect(result)+' expected '+inspect(expected)); |
|||
return true; |
|||
}); |
|||
} |
|||
|
|||
function test_deep_symlink_mix(callback) { |
|||
// todo: check to see that fixturesDir is not rooted in the
|
|||
// same directory as our test symlink.
|
|||
// obtain our current realpath using bash (so we can test ourselves)
|
|||
bashRealpath(fixturesDir, function(err, fixturesAbsDir) { |
|||
if (err) return callback(err); |
|||
/* |
|||
/tmp/node-test-realpath-f1 -> ../tmp/node-test-realpath-d1/foo |
|||
/tmp/node-test-realpath-d1 -> ../node-test-realpath-d2 |
|||
/tmp/node-test-realpath-d2/foo -> ../node-test-realpath-f2 |
|||
/tmp/node-test-realpath-f2 |
|||
-> /node/test/fixtures/nested-index/one/realpath-c |
|||
/node/test/fixtures/nested-index/one/realpath-c |
|||
-> /node/test/fixtures/nested-index/two/realpath-c |
|||
/node/test/fixtures/nested-index/two/realpath-c -> ../../cycles/root.js |
|||
/node/test/fixtures/cycles/root.js (hard) |
|||
*/ |
|||
var entry = '/tmp/node-test-realpath-f1'; |
|||
try {fs.unlinkSync('/tmp/node-test-realpath-d2/foo');}catch(e){} |
|||
try {fs.rmdirSync('/tmp/node-test-realpath-d2');}catch(e){} |
|||
fs.mkdirSync('/tmp/node-test-realpath-d2', 0700); |
|||
try { |
|||
[ |
|||
[entry, '../tmp/node-test-realpath-d1/foo'], |
|||
['/tmp/node-test-realpath-d1', '../tmp/node-test-realpath-d2'], |
|||
['/tmp/node-test-realpath-d2/foo', '../node-test-realpath-f2'], |
|||
['/tmp/node-test-realpath-f2', fixturesAbsDir+'/nested-index/one/realpath-c'], |
|||
[fixturesAbsDir+'/nested-index/one/realpath-c', fixturesAbsDir+'/nested-index/two/realpath-c'], |
|||
[fixturesAbsDir+'/nested-index/two/realpath-c', '../../cycles/root.js'], |
|||
].forEach(function(t) { |
|||
//debug('setting up '+t[0]+' -> '+t[1]);
|
|||
try {fs.unlinkSync(t[0]);}catch(e){} |
|||
fs.symlinkSync(t[1], t[0]); |
|||
unlink.push(t[0]); |
|||
}); |
|||
} finally { |
|||
unlink.push('/tmp/node-test-realpath-d2'); |
|||
} |
|||
var expected = fixturesAbsDir+'/cycles/root.js'; |
|||
assert.equal(fs.realpathSync(entry), expected); |
|||
asynctest(fs.realpath, [entry], callback, function(err, result){ |
|||
assert.equal(result, expected, |
|||
'got '+inspect(result)+' expected '+inspect(expected)); |
|||
return true; |
|||
}); |
|||
}); |
|||
} |
|||
|
|||
function test_non_symlinks(callback) { |
|||
bashRealpath(fixturesDir, function(err, fixturesAbsDir) { |
|||
if (err) return callback(err); |
|||
var p = fixturesAbsDir.lastIndexOf('/'); |
|||
var entrydir = fixturesAbsDir.substr(0, p); |
|||
var entry = fixturesAbsDir.substr(p+1)+'/cycles/root.js'; |
|||
var expected = fixturesAbsDir+'/cycles/root.js'; |
|||
var origcwd = process.cwd(); |
|||
process.chdir(entrydir); |
|||
assert.equal(fs.realpathSync(entry), expected); |
|||
asynctest(fs.realpath, [entry], callback, function(err, result){ |
|||
process.chdir(origcwd); |
|||
assert.equal(result, expected, |
|||
'got '+inspect(result)+' expected '+inspect(expected)); |
|||
return true; |
|||
}); |
|||
}); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------
|
|||
|
|||
var tests = [ |
|||
test_simple_relative_symlink, |
|||
test_simple_absolute_symlink, |
|||
test_deep_relative_file_symlink, |
|||
test_deep_relative_dir_symlink, |
|||
test_cyclic_link_protection, |
|||
test_relative_input_cwd, |
|||
test_deep_symlink_mix, |
|||
test_non_symlinks, |
|||
]; |
|||
var numtests = tests.length; |
|||
function runNextTest(err) { |
|||
if (err) throw err; |
|||
var test = tests.shift() |
|||
if (!test) puts(numtests+' subtests completed OK for fs.realpath'); |
|||
else test(runNextTest); |
|||
} |
|||
runNextTest(); |
|||
|
|||
process.addListener("exit", function () { |
|||
unlink.forEach(function(path){ try {fs.unlinkSync(path);}catch(e){} }); |
|||
assert.equal(async_completed, async_expected); |
|||
}); |
|||
|
@ -0,0 +1,22 @@ |
|||
process.mixin(require("../common")); |
|||
require("fs"); |
|||
parse = require("ini").parse; |
|||
|
|||
debug("load fixtures/fixture.ini"); |
|||
|
|||
p = path.join(fixturesDir, "fixture.ini"); |
|||
|
|||
fs.readFile(p,function(err, data) { |
|||
if (err) throw err; |
|||
|
|||
assert.equal(typeof parse, 'function'); |
|||
|
|||
var iniContents = parse(data); |
|||
assert.equal(typeof iniContents, 'object'); |
|||
assert.deepEqual(iniContents,{"-":{"root":"something"},"section":{"one":"two","Foo":"Bar","this":"Your Mother!","blank":""},"Section Two":{"something else":"blah","remove":"whitespace"}}) |
|||
|
|||
assert.equal(iniContents['-']['root'],'something'); |
|||
assert.equal(iniContents['section']['blank'],''); |
|||
assert.equal(iniContents['Section Two']['remove'],'whitespace'); |
|||
|
|||
}); |
Loading…
Reference in new issue