From 5853704babb64685e5f40badc313e692b56eaf0c Mon Sep 17 00:00:00 2001 From: Alan Kligman Date: Mon, 15 Jul 2013 11:52:41 -0400 Subject: [PATCH] Updated tests. --- dist/idbfs.js | 90 ++++++++++++++-------- examples/refactoring-test.html | 14 +--- src/file-system.js | 43 +++++++---- tests/spec/idbfs.spec.js | 136 ++++++++++++++++++++++++++++++++- 4 files changed, 223 insertions(+), 60 deletions(-) diff --git a/dist/idbfs.js b/dist/idbfs.js index 0cb85d5..21f3d75 100644 --- a/dist/idbfs.js +++ b/dist/idbfs.js @@ -5950,14 +5950,18 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p */ function read_object(objectStore, id, callback) { - var getRequest = objectStore.get(id); - getRequest.onsuccess = function onsuccess(event) { - var result = event.target.result; - callback(undefined, result); - }; - getRequest.onerror = function onerror(error) { - callback(error); - }; + try { + var getRequest = objectStore.get(id); + getRequest.onsuccess = function onsuccess(event) { + var result = event.target.result; + callback(undefined, result); + }; + getRequest.onerror = function onerror(error) { + callback(error); + }; + } catch(error) { + callback(new EIO(error.message)); + } }; /* @@ -5965,14 +5969,18 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p */ function write_object(objectStore, object, id, callback) { - var putRequest = objectStore.put(object, id); - putRequest.onsuccess = function onsuccess(event) { - var result = event.target.result; - callback(undefined, result); - }; - putRequest.onerror = function onerror(error) { - callback(error); - }; + try { + var putRequest = objectStore.put(object, id); + putRequest.onsuccess = function onsuccess(event) { + var result = event.target.result; + callback(undefined, result); + }; + putRequest.onerror = function onerror(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) { if(error) { callback(error); + } else if(ROOT_DIRECTORY_NAME == name) { + callback(new EBusy()); } else if(!result) { callback(new ENoEntry()); } else { @@ -6386,22 +6396,21 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p }; openRequest.onsuccess = function onsuccess(event) { var db = event.target.result; + var transaction = db.transaction([FILE_STORE_NAME], IDB_RW); + var files = transaction.objectStore(FILE_STORE_NAME); function complete(error) { + that.db = db; if(error) { that.readyState = FS_ERROR; deferred.reject(error); } else { that.readyState = FS_READY; - that.db = db; deferred.resolve(); } }; if(format) { - var transaction = db.transaction([FILE_STORE_NAME], IDB_RW); - var files = transaction.objectStore(FILE_STORE_NAME); - var clearRequest = files.clear(); clearRequest.onsuccess = function onsuccess(event) { make_root_directory(files, complete); @@ -6513,21 +6522,36 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p ); }; FileSystem.prototype.rmdir = function rmdir(path, callback) { - var deferred = when.defer(); - var transaction = this.db.transaction([FILE_STORE_NAME], IDB_RW); - var files = transaction.objectStore(FILE_STORE_NAME); + var that = this; + this.promise.then( + function() { + var deferred = when.defer(); + var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW); + var files = transaction.objectStore(FILE_STORE_NAME); - function check_result(error) { - if(error) { - // if(transaction.error) transaction.abort(); - deferred.reject(error); - } else { - deferred.resolve(); + function check_result(error) { + if(error) { + // if(transaction.error) transaction.abort(); + deferred.reject(error); + } else { + deferred.resolve(); + } + }; + + remove_directory(files, path, check_result); + deferred.promise.then( + function() { + callback(); + }, + function(error) { + callback(error); + } + ); + }, + function() { + callback(new EFileSystemError('unknown error')); } - }; - - remove_directory(files, path, check_result); - deferred.then(callback); + ); }; FileSystem.prototype.readdir = function readdir(path, callback) { diff --git a/examples/refactoring-test.html b/examples/refactoring-test.html index 7a8cff2..ee7a8e2 100644 --- a/examples/refactoring-test.html +++ b/examples/refactoring-test.html @@ -21,19 +21,11 @@ var flags = 'FORMAT'; //var 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) { - if(error) throw error; - fs.stat('/tmp', function(error, result) { - console.log('stat /tmp', error, result); - }); + /*fs.rmdir('/tmp', function(error) { + console.log('!'); + });*/ }); -*/ /* function make_tmp_directory() { diff --git a/src/file-system.js b/src/file-system.js index 50bd1f2..1ec9e51 100644 --- a/src/file-system.js +++ b/src/file-system.js @@ -313,6 +313,8 @@ define(function(require) { function check_if_directory_exists(error, result) { if(error) { callback(error); + } else if(ROOT_DIRECTORY_NAME == name) { + callback(new EBusy()); } else if(!result) { callback(new ENoEntry()); } else { @@ -720,21 +722,36 @@ define(function(require) { ); }; FileSystem.prototype.rmdir = function rmdir(path, callback) { - var deferred = when.defer(); - var transaction = this.db.transaction([FILE_STORE_NAME], IDB_RW); - var files = transaction.objectStore(FILE_STORE_NAME); + var that = this; + this.promise.then( + function() { + var deferred = when.defer(); + var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW); + var files = transaction.objectStore(FILE_STORE_NAME); - function check_result(error) { - if(error) { - // if(transaction.error) transaction.abort(); - deferred.reject(error); - } else { - deferred.resolve(); + function check_result(error) { + if(error) { + // if(transaction.error) transaction.abort(); + deferred.reject(error); + } else { + deferred.resolve(); + } + }; + + remove_directory(files, path, check_result); + deferred.promise.then( + function() { + callback(); + }, + function(error) { + callback(error); + } + ); + }, + function() { + callback(new EFileSystemError('unknown error')); } - }; - - remove_directory(files, path, check_result); - deferred.then(callback); + ); }; FileSystem.prototype.readdir = function readdir(path, callback) { diff --git a/tests/spec/idbfs.spec.js b/tests/spec/idbfs.spec.js index 84110a0..28ef2e1 100644 --- a/tests/spec/idbfs.spec.js +++ b/tests/spec/idbfs.spec.js @@ -69,7 +69,7 @@ describe("fs", function() { }); }); -describe('stat', function() { +describe('fs.stat', function() { beforeEach(function() { this.db_name = mk_db_name(); 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() { this.db_name = mk_db_name(); this.fs = new IDBFS.FileSystem(this.db_name, 'FORMAT'); @@ -144,6 +144,10 @@ describe('mkdir', function() { 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() { var complete = false; 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 _error, _result, _stat; 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(); + }); + }); +}); \ No newline at end of file