Updated tests.

This commit is contained in:
Alan Kligman 2013-07-15 11:52:41 -04:00
parent cfd0932736
commit 5853704bab
4 changed files with 223 additions and 60 deletions

36
dist/idbfs.js vendored
View File

@ -5950,6 +5950,7 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
*/ */
function read_object(objectStore, id, callback) { function read_object(objectStore, id, callback) {
try {
var getRequest = objectStore.get(id); var getRequest = objectStore.get(id);
getRequest.onsuccess = function onsuccess(event) { getRequest.onsuccess = function onsuccess(event) {
var result = event.target.result; var result = event.target.result;
@ -5958,6 +5959,9 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
getRequest.onerror = function onerror(error) { getRequest.onerror = function onerror(error) {
callback(error); callback(error);
}; };
} catch(error) {
callback(new EIO(error.message));
}
}; };
/* /*
@ -5965,6 +5969,7 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
*/ */
function write_object(objectStore, object, id, callback) { function write_object(objectStore, object, id, callback) {
try {
var putRequest = objectStore.put(object, id); var putRequest = objectStore.put(object, id);
putRequest.onsuccess = function onsuccess(event) { putRequest.onsuccess = function onsuccess(event) {
var result = event.target.result; var result = event.target.result;
@ -5973,6 +5978,9 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
putRequest.onerror = function onerror(error) { putRequest.onerror = function onerror(error) {
callback(error); callback(error);
}; };
} catch(error) {
callback(new EIO(error.message));
}
}; };
/* /*
@ -6105,6 +6113,8 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
function check_if_directory_exists(error, result) { function check_if_directory_exists(error, result) {
if(error) { if(error) {
callback(error); callback(error);
} else if(ROOT_DIRECTORY_NAME == name) {
callback(new EBusy());
} else if(!result) { } else if(!result) {
callback(new ENoEntry()); callback(new ENoEntry());
} else { } else {
@ -6386,22 +6396,21 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
}; };
openRequest.onsuccess = function onsuccess(event) { openRequest.onsuccess = function onsuccess(event) {
var db = event.target.result; var db = event.target.result;
var transaction = db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
function complete(error) { function complete(error) {
that.db = db;
if(error) { if(error) {
that.readyState = FS_ERROR; that.readyState = FS_ERROR;
deferred.reject(error); deferred.reject(error);
} else { } else {
that.readyState = FS_READY; that.readyState = FS_READY;
that.db = db;
deferred.resolve(); deferred.resolve();
} }
}; };
if(format) { if(format) {
var transaction = db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME);
var clearRequest = files.clear(); var clearRequest = files.clear();
clearRequest.onsuccess = function onsuccess(event) { clearRequest.onsuccess = function onsuccess(event) {
make_root_directory(files, complete); make_root_directory(files, complete);
@ -6513,8 +6522,11 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
); );
}; };
FileSystem.prototype.rmdir = function rmdir(path, callback) { FileSystem.prototype.rmdir = function rmdir(path, callback) {
var that = this;
this.promise.then(
function() {
var deferred = when.defer(); var deferred = when.defer();
var transaction = this.db.transaction([FILE_STORE_NAME], IDB_RW); var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME); var files = transaction.objectStore(FILE_STORE_NAME);
function check_result(error) { function check_result(error) {
@ -6527,7 +6539,19 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
}; };
remove_directory(files, path, check_result); remove_directory(files, path, check_result);
deferred.then(callback); deferred.promise.then(
function() {
callback();
},
function(error) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}; };
FileSystem.prototype.readdir = function readdir(path, callback) { FileSystem.prototype.readdir = function readdir(path, callback) {

View File

@ -21,19 +21,11 @@ var flags = 'FORMAT';
//var flags; //var flags;
var fs = new IDBFS.FileSystem('local', flags); var fs = new IDBFS.FileSystem('local', flags);
fs.stat('/', function(error, result) {
console.log('stat /', error, result);
// indexedDB.deleteDatabase('local');
});
/*
fs.mkdir('/tmp', function(error) { fs.mkdir('/tmp', function(error) {
if(error) throw error; /*fs.rmdir('/tmp', function(error) {
fs.stat('/tmp', function(error, result) { console.log('!');
console.log('stat /tmp', error, result); });*/
}); });
});
*/
/* /*
function make_tmp_directory() { function make_tmp_directory() {

View File

@ -313,6 +313,8 @@ define(function(require) {
function check_if_directory_exists(error, result) { function check_if_directory_exists(error, result) {
if(error) { if(error) {
callback(error); callback(error);
} else if(ROOT_DIRECTORY_NAME == name) {
callback(new EBusy());
} else if(!result) { } else if(!result) {
callback(new ENoEntry()); callback(new ENoEntry());
} else { } else {
@ -720,8 +722,11 @@ define(function(require) {
); );
}; };
FileSystem.prototype.rmdir = function rmdir(path, callback) { FileSystem.prototype.rmdir = function rmdir(path, callback) {
var that = this;
this.promise.then(
function() {
var deferred = when.defer(); var deferred = when.defer();
var transaction = this.db.transaction([FILE_STORE_NAME], IDB_RW); var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
var files = transaction.objectStore(FILE_STORE_NAME); var files = transaction.objectStore(FILE_STORE_NAME);
function check_result(error) { function check_result(error) {
@ -734,7 +739,19 @@ define(function(require) {
}; };
remove_directory(files, path, check_result); remove_directory(files, path, check_result);
deferred.then(callback); deferred.promise.then(
function() {
callback();
},
function(error) {
callback(error);
}
);
},
function() {
callback(new EFileSystemError('unknown error'));
}
);
}; };
FileSystem.prototype.readdir = function readdir(path, callback) { FileSystem.prototype.readdir = function readdir(path, callback) {

View File

@ -69,7 +69,7 @@ describe("fs", function() {
}); });
}); });
describe('stat', function() { describe('fs.stat', function() {
beforeEach(function() { beforeEach(function() {
this.db_name = mk_db_name(); this.db_name = mk_db_name();
this.fs = new IDBFS.FileSystem(this.db_name, 'FORMAT'); this.fs = new IDBFS.FileSystem(this.db_name, 'FORMAT');
@ -133,7 +133,7 @@ describe('stat', function() {
}); });
}); });
describe('mkdir', function() { describe('fs.mkdir', function() {
beforeEach(function() { beforeEach(function() {
this.db_name = mk_db_name(); this.db_name = mk_db_name();
this.fs = new IDBFS.FileSystem(this.db_name, 'FORMAT'); this.fs = new IDBFS.FileSystem(this.db_name, 'FORMAT');
@ -144,6 +144,10 @@ describe('mkdir', function() {
delete this.fs; delete this.fs;
}); });
it('should be a function', function() {
expect(typeof this.fs.mkdir).toEqual('function');
});
it('should return an error if part of the parent path does not exist', function() { it('should return an error if part of the parent path does not exist', function() {
var complete = false; var complete = false;
var _error; var _error;
@ -164,7 +168,27 @@ describe('mkdir', function() {
}); });
}); });
it('should return with no result if successful', function() { it('should return an error if the path already exists', function() {
var complete = false;
var _error;
var that = this;
that.fs.mkdir('/', function(error) {
_error = error;
complete = true;
});
waitsFor(function() {
return complete;
}, 'stat to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).toBeDefined();
});
});
it('should make a new directory', function() {
var complete = false; var complete = false;
var _error, _result, _stat; var _error, _result, _stat;
var that = this; var that = this;
@ -191,3 +215,109 @@ describe('mkdir', function() {
}); });
}); });
}); });
describe('fs.rmdir', function() {
beforeEach(function() {
this.db_name = mk_db_name();
this.fs = new IDBFS.FileSystem(this.db_name, 'FORMAT');
});
afterEach(function() {
indexedDB.deleteDatabase(this.db_name);
delete this.fs;
});
it('should be a function', function() {
expect(typeof this.fs.rmdir).toEqual('function');
});
it('should return an error if part of the path does not exist', function() {
var complete = false;
var _error;
var that = this;
that.fs.rmdir('/tmp/mydir', function(error) {
_error = error;
complete = true;
});
waitsFor(function() {
return complete;
}, 'rmdir to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).toBeDefined();
});
});
it('should return an error if attempting to remove the root directory', function() {
var complete = false;
var _error;
var that = this;
that.fs.rmdir('/', function(error) {
_error = error;
complete = true;
});
waitsFor(function() {
return complete;
}, 'rmdir to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).toBeDefined();
});
});
it('should return an error if the directory is not empty', function() {
var complete = false;
var _error;
var that = this;
that.fs.mkdir('/tmp', function(error) {
that.fs.mkdir('/tmp/mydir', function(error) {
that.fs.rmdir('/', function(error) {
_error = error;
complete = true;
});
});
});
waitsFor(function() {
return complete;
}, 'rmdir to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).toBeDefined();
});
});
it('should remove an existing directory', function() {
var complete = false;
var _error, _stat;
var that = this;
that.fs.mkdir('/tmp', function(error) {
that.fs.rmdir('/tmp', function(error) {
_error = error;
that.fs.stat('/tmp', function(error, result) {
_stat = result;
complete = true;
});
});
});
waitsFor(function() {
return complete;
}, 'rmdir to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).not.toBeDefined();
expect(_stat).not.toBeDefined();
});
});
});