Initial tests from node repo

This commit is contained in:
David Humphrey 2013-11-26 15:17:30 -05:00 committed by David Humphrey (:humph) david.humphrey@senecacollege.ca
parent b0dbbf932a
commit 77f32c4c50
4 changed files with 231 additions and 2 deletions

50
dist/idbfs.js vendored
View File

@ -8499,6 +8499,16 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
return O_FLAGS[flags];
}
// nullCheck from https://github.com/joyent/node/blob/master/lib/fs.js
function nullCheck(path, callback) {
if (('' + path).indexOf('\u0000') !== -1) {
var er = new Error('Path must be a string without null bytes.');
callback(er);
return false;
}
return true;
}
/*
* FileSystem
*
@ -8613,6 +8623,8 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
FileSystem.providers = providers;
function _open(fs, context, path, flags, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error, fileNode) {
if(error) {
callback(error);
@ -8647,6 +8659,8 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
}
function _mkdir(context, path, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error) {
if(error) {
callback(error);
@ -8659,6 +8673,8 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
}
function _rmdir(context, path, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error) {
if(error) {
callback(error);
@ -8671,6 +8687,8 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
}
function _stat(context, name, path, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error, result) {
if(error) {
callback(error);
@ -8703,6 +8721,9 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
}
function _link(context, oldpath, newpath, callback) {
if(!nullCheck(oldpath, callback)) return;
if(!nullCheck(newpath, callback)) return;
function check_result(error) {
if(error) {
callback(error);
@ -8715,6 +8736,8 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
}
function _unlink(context, path, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error) {
if(error) {
callback(error);
@ -8759,6 +8782,8 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
options = { encoding: options, flag: 'r' };
}
if(!nullCheck(path, callback)) return;
var flags = validate_flags(options.flag || 'r');
if(!flags) {
callback(new EInvalid('flags is not valid'));
@ -8834,6 +8859,8 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
options = { encoding: options, flag: 'w' };
}
if(!nullCheck(path, callback)) return;
var flags = validate_flags(options.flag || 'w');
if(!flags) {
callback(new EInvalid('flags is not valid'));
@ -8862,10 +8889,12 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
function _getxattr(path, name, callback) {
// TODO
// if(!nullCheck(path, callback)) return;
}
function _setxattr(path, name, value, callback) {
// TODO
// if(!nullCheck(path, callback)) return;
}
function _lseek(fs, context, fd, offset, whence, callback) {
@ -8918,6 +8947,8 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
}
function _readdir(context, path, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error, files) {
if(error) {
callback(error);
@ -8931,9 +8962,13 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
function _utimes(path, atime, mtime, callback) {
// TODO
// if(!nullCheck(path, callback)) return;
}
function _rename(context, oldpath, newpath, callback) {
if(!nullCheck(oldpath, callback)) return;
if(!nullCheck(newpath, callback)) return;
function check_result(error) {
if(error) {
callback(error);
@ -8954,6 +8989,9 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
}
function _symlink(context, srcpath, dstpath, callback) {
if(!nullCheck(srcpath, callback)) return;
if(!nullCheck(dstpath, callback)) return;
function check_result(error) {
if(error) {
callback(error);
@ -8966,6 +9004,8 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
}
function _readlink(context, path, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error, result) {
if(error) {
callback(error);
@ -8982,6 +9022,8 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
}
function _lstat(fs, context, path, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error, result) {
if(error) {
callback(error);
@ -8996,10 +9038,12 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
function _truncate(path, length, callback) {
// TODO
// if(!nullCheck(path, callback)) return;
}
function _ftruncate(fd, length, callback) {
// TODO
// if(!nullCheck(path, callback)) return;
}
@ -9020,7 +9064,11 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
FileSystem.prototype.close = function(fd, callback) {
_close(this, fd, callback);
};
FileSystem.prototype.mkdir = function(path, callback) {
FileSystem.prototype.mkdir = function(path, mode, callback) {
// Support passing a mode arg, but we ignore it internally for now.
if(typeof mode === 'function') {
callback = mode;
}
var fs = this;
var error = fs.queueOrRun(
function() {

View File

@ -950,6 +950,16 @@ define(function(require) {
return O_FLAGS[flags];
}
// nullCheck from https://github.com/joyent/node/blob/master/lib/fs.js
function nullCheck(path, callback) {
if (('' + path).indexOf('\u0000') !== -1) {
var er = new Error('Path must be a string without null bytes.');
callback(er);
return false;
}
return true;
}
/*
* FileSystem
*
@ -1064,6 +1074,8 @@ define(function(require) {
FileSystem.providers = providers;
function _open(fs, context, path, flags, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error, fileNode) {
if(error) {
callback(error);
@ -1098,6 +1110,8 @@ define(function(require) {
}
function _mkdir(context, path, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error) {
if(error) {
callback(error);
@ -1110,6 +1124,8 @@ define(function(require) {
}
function _rmdir(context, path, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error) {
if(error) {
callback(error);
@ -1122,6 +1138,8 @@ define(function(require) {
}
function _stat(context, name, path, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error, result) {
if(error) {
callback(error);
@ -1154,6 +1172,9 @@ define(function(require) {
}
function _link(context, oldpath, newpath, callback) {
if(!nullCheck(oldpath, callback)) return;
if(!nullCheck(newpath, callback)) return;
function check_result(error) {
if(error) {
callback(error);
@ -1166,6 +1187,8 @@ define(function(require) {
}
function _unlink(context, path, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error) {
if(error) {
callback(error);
@ -1210,6 +1233,8 @@ define(function(require) {
options = { encoding: options, flag: 'r' };
}
if(!nullCheck(path, callback)) return;
var flags = validate_flags(options.flag || 'r');
if(!flags) {
callback(new EInvalid('flags is not valid'));
@ -1285,6 +1310,8 @@ define(function(require) {
options = { encoding: options, flag: 'w' };
}
if(!nullCheck(path, callback)) return;
var flags = validate_flags(options.flag || 'w');
if(!flags) {
callback(new EInvalid('flags is not valid'));
@ -1313,10 +1340,12 @@ define(function(require) {
function _getxattr(path, name, callback) {
// TODO
// if(!nullCheck(path, callback)) return;
}
function _setxattr(path, name, value, callback) {
// TODO
// if(!nullCheck(path, callback)) return;
}
function _lseek(fs, context, fd, offset, whence, callback) {
@ -1369,6 +1398,8 @@ define(function(require) {
}
function _readdir(context, path, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error, files) {
if(error) {
callback(error);
@ -1382,9 +1413,13 @@ define(function(require) {
function _utimes(path, atime, mtime, callback) {
// TODO
// if(!nullCheck(path, callback)) return;
}
function _rename(context, oldpath, newpath, callback) {
if(!nullCheck(oldpath, callback)) return;
if(!nullCheck(newpath, callback)) return;
function check_result(error) {
if(error) {
callback(error);
@ -1405,6 +1440,9 @@ define(function(require) {
}
function _symlink(context, srcpath, dstpath, callback) {
if(!nullCheck(srcpath, callback)) return;
if(!nullCheck(dstpath, callback)) return;
function check_result(error) {
if(error) {
callback(error);
@ -1417,6 +1455,8 @@ define(function(require) {
}
function _readlink(context, path, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error, result) {
if(error) {
callback(error);
@ -1433,6 +1473,8 @@ define(function(require) {
}
function _lstat(fs, context, path, callback) {
if(!nullCheck(path, callback)) return;
function check_result(error, result) {
if(error) {
callback(error);
@ -1447,10 +1489,12 @@ define(function(require) {
function _truncate(path, length, callback) {
// TODO
// if(!nullCheck(path, callback)) return;
}
function _ftruncate(fd, length, callback) {
// TODO
// if(!nullCheck(path, callback)) return;
}
@ -1471,7 +1515,11 @@ define(function(require) {
FileSystem.prototype.close = function(fd, callback) {
_close(this, fd, callback);
};
FileSystem.prototype.mkdir = function(path, callback) {
FileSystem.prototype.mkdir = function(path, mode, callback) {
// Support passing a mode arg, but we ignore it internally for now.
if(typeof mode === 'function') {
callback = mode;
}
var fs = this;
var error = fs.queueOrRun(
function() {

View File

@ -19,6 +19,7 @@
<script type="text/javascript" src="spec/providers.indexeddb.spec.js"></script>
<script type="text/javascript" src="spec/providers.memory.spec.js"></script>
<script type="text/javascript" src="spec/idbfs.spec.js"></script>
<script type="text/javascript" src="spec/node.spec.js"></script>
<script type="text/javascript">
(function() {

132
tests/spec/node.spec.js Normal file
View File

@ -0,0 +1,132 @@
describe("node.js tests from https://github.com/joyent/node/blob/master/test", function() {
beforeEach(function() {
this.db_name = mk_db_name();
this.fs = new IDBFS.FileSystem({
name: this.db_name,
flags: 'FORMAT',
// provider: new IDBFS.Proviers.Memory()
});
});
afterEach(function() {
indexedDB.deleteDatabase(this.db_name);
delete this.fs;
});
// Based on test1 from https://github.com/joyent/node/blob/master/test/simple/test-fs-mkdir.js
it('should create a dir without a mode arg', function() {
var _error, _result;
var complete = false;
var pathname = '/test1';
var fs = this.fs;
fs.mkdir(pathname, function(err) {
_error = err;
fs.stat(pathname, function(err, result) {
_error = _error || err;
_result = result;
complete = true;
});
});
waitsFor(function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_result).toBeDefined();
expect(_error).toEqual(null);
});
});
// Based on test2 https://github.com/joyent/node/blob/master/test/simple/test-fs-mkdir.js
it('should create a dir with a mode arg', function() {
var _error, _result;
var complete = false;
var pathname = '/test2';
var fs = this.fs;
fs.mkdir(pathname, 511 /*=0777*/, function(err) {
_error = err;
fs.stat(pathname, function(err, result) {
_error = _error || err;
_result = result;
complete = true;
});
});
waitsFor(function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_result).toBeDefined();
expect(_error).toEqual(null);
});
});
// Based on https://github.com/joyent/node/blob/master/test/simple/test-fs-null-bytes.js
it('should reject paths with null bytes in them', function() {
var complete = false;
var checks = [];
var fnCount = 0;
var fnTotal = 16;
var expected = "Path must be a string without null bytes.";
var fs = this.fs;
// Make sure function fails with null path error in callback.
function check(fn) {
var args = Array.prototype.slice.call(arguments, 1);
args = args.concat(function(err) {
checks.push(function(){
expect(err).toBeDefined();
expect(err.message).toEqual(expected);
});
fnCount++;
complete = fnCount === fnTotal;
});
fn.apply(fs, args);
}
check(fs.link, 'foo\u0000bar', 'foobar');
check(fs.link, 'foobar', 'foo\u0000bar');
check(fs.lstat, 'foo\u0000bar');
check(fs.mkdir, 'foo\u0000bar', '0755');
check(fs.open, 'foo\u0000bar', 'r');
check(fs.readFile, 'foo\u0000bar');
check(fs.readdir, 'foo\u0000bar');
check(fs.readlink, 'foo\u0000bar');
check(fs.rename, 'foo\u0000bar', 'foobar');
check(fs.rename, 'foobar', 'foo\u0000bar');
check(fs.rmdir, 'foo\u0000bar');
check(fs.stat, 'foo\u0000bar');
check(fs.symlink, 'foo\u0000bar', 'foobar');
check(fs.symlink, 'foobar', 'foo\u0000bar');
check(fs.unlink, 'foo\u0000bar');
check(fs.writeFile, 'foo\u0000bar');
// TODO - need to be implemented still...
// check(fs.appendFile, 'foo\u0000bar');
// check(fs.realpath, 'foo\u0000bar');
// check(fs.chmod, 'foo\u0000bar', '0644');
// check(fs.chown, 'foo\u0000bar', 12, 34);
// check(fs.realpath, 'foo\u0000bar');
// check(fs.truncate, 'foo\u0000bar');
// check(fs.utimes, 'foo\u0000bar', 0, 0);
waitsFor(function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
checks.forEach(function(fn){
fn();
});
});
});
});