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

90
dist/idbfs.js vendored
View File

@ -5950,14 +5950,18 @@ 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) {
var getRequest = objectStore.get(id); try {
getRequest.onsuccess = function onsuccess(event) { var getRequest = objectStore.get(id);
var result = event.target.result; getRequest.onsuccess = function onsuccess(event) {
callback(undefined, result); var result = event.target.result;
}; callback(undefined, result);
getRequest.onerror = function onerror(error) { };
callback(error); 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) { function write_object(objectStore, object, id, callback) {
var putRequest = objectStore.put(object, id); try {
putRequest.onsuccess = function onsuccess(event) { var putRequest = objectStore.put(object, id);
var result = event.target.result; putRequest.onsuccess = function onsuccess(event) {
callback(undefined, result); var result = event.target.result;
}; callback(undefined, result);
putRequest.onerror = function onerror(error) { };
callback(error); 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) { 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,21 +6522,36 @@ 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 deferred = when.defer(); var that = this;
var transaction = this.db.transaction([FILE_STORE_NAME], IDB_RW); this.promise.then(
var files = transaction.objectStore(FILE_STORE_NAME); 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) { function check_result(error) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); deferred.reject(error);
} else { } else {
deferred.resolve(); 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) { 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,21 +722,36 @@ define(function(require) {
); );
}; };
FileSystem.prototype.rmdir = function rmdir(path, callback) { FileSystem.prototype.rmdir = function rmdir(path, callback) {
var deferred = when.defer(); var that = this;
var transaction = this.db.transaction([FILE_STORE_NAME], IDB_RW); this.promise.then(
var files = transaction.objectStore(FILE_STORE_NAME); 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) { function check_result(error) {
if(error) { if(error) {
// if(transaction.error) transaction.abort(); // if(transaction.error) transaction.abort();
deferred.reject(error); deferred.reject(error);
} else { } else {
deferred.resolve(); 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) { 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();
});
});
});