From 415ad414063aef6ea545b9713b92471899ad2f05 Mon Sep 17 00:00:00 2001 From: "David Humphrey (:humph) david.humphrey@senecacollege.ca" Date: Wed, 27 Nov 2013 12:18:09 -0500 Subject: [PATCH] Break test files up using require --- dist/idbfs.js | 3 +- tests/index.html | 43 +- tests/require-config.js | 41 + tests/spec/fs.close.spec.js | 50 + tests/spec/fs.link.spec.js | 103 + tests/spec/fs.lseek.spec.js | 231 ++ tests/spec/fs.lstat.spec.js | 92 + tests/spec/fs.mkdir.spec.js | 89 + tests/spec/fs.open.spec.js | 175 ++ tests/spec/fs.read.spec.js | 98 + tests/spec/fs.readdir.spec.js | 96 + tests/spec/fs.readlink.spec.js | 87 + tests/spec/fs.rename.spec.js | 63 + tests/spec/fs.rmdir.spec.js | 162 ++ tests/spec/fs.spec.js | 41 + tests/spec/fs.stat.spec.js | 147 ++ tests/spec/fs.symlink.spec.js | 83 + tests/spec/fs.unlink.spec.js | 110 + tests/spec/fs.write.spec.js | 100 + tests/spec/fs.writeFile-readFile.spec.js | 183 ++ tests/spec/idbfs.spec.js | 2255 +---------------- tests/spec/node-js/simple/test-fs-mkdir.js | 74 + .../spec/node-js/simple/test-fs-null-bytes.js | 77 + tests/spec/node.spec.js | 132 - tests/spec/path-resolution.spec.js | 351 +++ tests/spec/providers.indexeddb.spec.js | 188 -- tests/spec/providers.memory.spec.js | 172 -- tests/spec/providers.spec.js | 17 - .../providers/providers.indexeddb.spec.js | 192 ++ tests/spec/providers/providers.memory.spec.js | 175 ++ tests/spec/providers/providers.spec.js | 19 + tests/test-manifest.js | 40 + 32 files changed, 2889 insertions(+), 2800 deletions(-) create mode 100644 tests/require-config.js create mode 100644 tests/spec/fs.close.spec.js create mode 100644 tests/spec/fs.link.spec.js create mode 100644 tests/spec/fs.lseek.spec.js create mode 100644 tests/spec/fs.lstat.spec.js create mode 100644 tests/spec/fs.mkdir.spec.js create mode 100644 tests/spec/fs.open.spec.js create mode 100644 tests/spec/fs.read.spec.js create mode 100644 tests/spec/fs.readdir.spec.js create mode 100644 tests/spec/fs.readlink.spec.js create mode 100644 tests/spec/fs.rename.spec.js create mode 100644 tests/spec/fs.rmdir.spec.js create mode 100644 tests/spec/fs.spec.js create mode 100644 tests/spec/fs.stat.spec.js create mode 100644 tests/spec/fs.symlink.spec.js create mode 100644 tests/spec/fs.unlink.spec.js create mode 100644 tests/spec/fs.write.spec.js create mode 100644 tests/spec/fs.writeFile-readFile.spec.js create mode 100644 tests/spec/node-js/simple/test-fs-mkdir.js create mode 100644 tests/spec/node-js/simple/test-fs-null-bytes.js delete mode 100644 tests/spec/node.spec.js create mode 100644 tests/spec/path-resolution.spec.js delete mode 100644 tests/spec/providers.indexeddb.spec.js delete mode 100644 tests/spec/providers.memory.spec.js delete mode 100644 tests/spec/providers.spec.js create mode 100644 tests/spec/providers/providers.indexeddb.spec.js create mode 100644 tests/spec/providers/providers.memory.spec.js create mode 100644 tests/spec/providers/providers.spec.js create mode 100644 tests/test-manifest.js diff --git a/dist/idbfs.js b/dist/idbfs.js index 989c944..3471ff6 100644 --- a/dist/idbfs.js +++ b/dist/idbfs.js @@ -9154,9 +9154,10 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr }; FileSystem.prototype.read = function(fd, buffer, offset, length, position, callback) { // Follow how node.js does this + callback = maybeCallback(callback); function wrapper(err, bytesRead) { // Retain a reference to buffer so that it can't be GC'ed too soon. - callback && callback(err, bytesRead || 0, buffer); + callback(err, bytesRead || 0, buffer); } var fs = this; var error = fs.queueOrRun( diff --git a/tests/index.html b/tests/index.html index a23297a..d1c6329 100644 --- a/tests/index.html +++ b/tests/index.html @@ -8,50 +8,9 @@ - - - - - - - - - - - - - - + - diff --git a/tests/require-config.js b/tests/require-config.js new file mode 100644 index 0000000..d6b1eb8 --- /dev/null +++ b/tests/require-config.js @@ -0,0 +1,41 @@ +/** + * Assembles fs.js at runtime in the browswer, as well as all test + * spec files. Add spec files to the list in test-manifest.js + */ + +require.config({ + paths: { + "tests": "../tests", + "src": "../src", + "spec": "../tests/spec", + "IDBFS": "../src/fs" + }, + baseUrl: "../lib", + optimize: "none" +}); + +require(["tests/test-manifest"], function() { + var jasmineEnv = jasmine.getEnv(); + jasmineEnv.updateInterval = 1000; + + var htmlReporter = new jasmine.HtmlReporter(); + + jasmineEnv.addReporter(htmlReporter); + + jasmineEnv.specFilter = function(spec) { + return htmlReporter.specFilter(spec); + }; + + var currentWindowOnload = window.onload; + + window.onload = function() { + if (currentWindowOnload) { + currentWindowOnload(); + } + execJasmine(); + }; + + function execJasmine() { + jasmineEnv.execute(); + } +}); diff --git a/tests/spec/fs.close.spec.js b/tests/spec/fs.close.spec.js new file mode 100644 index 0000000..ba3ad35 --- /dev/null +++ b/tests/spec/fs.close.spec.js @@ -0,0 +1,50 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.close', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.close).toEqual('function'); + }); + + it('should release the file descriptor', function() { + var complete = false; + var _error; + var that = this; + + var buffer = new Uint8Array(0); + + that.fs.open('/myfile', 'w+', function(error, result) { + if(error) throw error; + + var fd = result; + that.fs.close(fd, function(error) { + that.fs.read(fd, buffer, 0, buffer.length, undefined, function(error, result) { + _error = error; + complete = true; + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + }); + }); + }); + +}); \ No newline at end of file diff --git a/tests/spec/fs.link.spec.js b/tests/spec/fs.link.spec.js new file mode 100644 index 0000000..9ed0bcf --- /dev/null +++ b/tests/spec/fs.link.spec.js @@ -0,0 +1,103 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.link', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.link).toEqual('function'); + }); + + it('should create a link to an existing file', function() { + var complete = false; + var _error, _oldstats, _newstats; + var that = this; + + that.fs.open('/myfile', 'w+', function(error, result) { + if(error) throw error; + + var fd = result; + that.fs.close(fd, function(error) { + if(error) throw error; + + that.fs.link('/myfile', '/myotherfile', function(error) { + if(error) throw error; + + that.fs.stat('/myfile', function(error, result) { + if(error) throw error; + + _oldstats = result; + that.fs.stat('/myotherfile', function(error, result) { + if(error) throw error; + + _newstats = result; + + complete = true; + }); + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_newstats.node).toEqual(_oldstats.node); + expect(_newstats.nlinks).toEqual(2); + expect(_newstats).toEqual(_oldstats); + }); + }); + + it('should not follow symbolic links', function () { + var complete = false; + var _error, _oldstats, _linkstats, _newstats; + var that = this; + + that.fs.stat('/', function (error, result) { + if (error) throw error; + _oldstats = result; + that.fs.symlink('/', '/myfileLink', function (error) { + if (error) throw error; + that.fs.link('/myfileLink', '/myotherfile', function (error) { + if (error) throw error; + that.fs.lstat('/myfileLink', function (error, result) { + if (error) throw error; + _linkstats = result; + that.fs.lstat('/myotherfile', function (error, result) { + if (error) throw error; + _newstats = result; + complete = true; + }); + }); + }); + }); + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toEqual(null); + expect(_newstats.node).toEqual(_linkstats.node); + expect(_newstats.node).toNotEqual(_oldstats.node); + expect(_newstats.nlinks).toEqual(2); + expect(_newstats).toEqual(_linkstats); + }); + }); + }); + +}); \ No newline at end of file diff --git a/tests/spec/fs.lseek.spec.js b/tests/spec/fs.lseek.spec.js new file mode 100644 index 0000000..d460ae7 --- /dev/null +++ b/tests/spec/fs.lseek.spec.js @@ -0,0 +1,231 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.lseek', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.lseek).toEqual('function'); + }); + + it('should not follow symbolic links', function () { + var complete = false; + var _error, _stats; + var that = this; + + that.fs.open('/myfile', 'w', function (error, result) { + if (error) throw error; + + var fd = result; + that.fs.close(fd, function (error) { + if (error) throw error; + + that.fs.symlink('/myfile', '/myFileLink', function (error) { + if (error) throw error; + + that.fs.rename('/myFileLink', '/myOtherFileLink', function (error) { + if (error) throw error; + + that.fs.stat('/myfile', function (error, result) { + _error1 = error; + + that.fs.lstat('/myFileLink', function (error, result) { + _error2 = error; + + that.fs.stat('/myOtherFileLink', function (error, result) { + if (error) throw error; + + _stats = result; + complete = true; + }); + }); + }); + }); + }); + }); + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error1).toEqual(null); + expect(_error2).toBeDefined(); + expect(_stats.nlinks).toEqual(1); + }); + }); + + it('should set the current position if whence is SET', function() { + var complete = false; + var _error, _result, _stats; + var that = this; + + var offset = 3; + var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); + var result_buffer = new Uint8Array(buffer.length + offset); + + that.fs.open('/myfile', 'w+', function(error, result) { + if(error) throw error; + + var fd = result; + that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) { + if(error) throw error; + + that.fs.lseek(fd, offset, 'SET', function(error, result) { + _error = error; + _result = result; + + that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) { + if(error) throw error; + + that.fs.read(fd, result_buffer, 0, result_buffer.length, 0, function(error, result) { + if(error) throw error; + + that.fs.stat('/myfile', function(error, result) { + if(error) throw error; + + _stats = result; + + complete = true; + }); + }); + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(offset); + expect(_stats.size).toEqual(offset + buffer.length); + var expected = new Uint8Array([1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8]); + expect(typed_array_equal(result_buffer, expected)).toEqual(true); + }); + }); + + it('should update the current position if whence is CUR', function() { + var complete = false; + var _error, _result, _stats; + var that = this; + + var offset = -2; + var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); + var result_buffer = new Uint8Array(2 * buffer.length + offset); + + that.fs.open('/myfile', 'w+', function(error, result) { + if(error) throw error; + + var fd = result; + that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) { + if(error) throw error; + + that.fs.lseek(fd, offset, 'CUR', function(error, result) { + _error = error; + _result = result; + + that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) { + if(error) throw error; + + that.fs.read(fd, result_buffer, 0, result_buffer.length, 0, function(error, result) { + if(error) throw error; + + that.fs.stat('/myfile', function(error, result) { + if(error) throw error; + + _stats = result; + + complete = true; + }); + }); + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(offset + buffer.length); + expect(_stats.size).toEqual(offset + 2 * buffer.length); + var expected = new Uint8Array([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8]); + expect(typed_array_equal(result_buffer, expected)).toEqual(true); + }); + }); + + it('should update the current position if whence is END', function() { + var complete = false; + var _error, _result, _stats; + var that = this; + + var offset = 5; + var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); + var result_buffer; + + that.fs.open('/myfile', 'w+', function(error, result) { + if(error) throw error; + + var fd1 = result; + that.fs.write(fd1, buffer, 0, buffer.length, undefined, function(error, result) { + if(error) throw error; + + that.fs.open('/myfile', 'w+', function(error, result) { + if(error) throw error; + + var fd2 = result; + that.fs.lseek(fd2, offset, 'END', function(error, result) { + _error = error; + _result = result; + + that.fs.write(fd2, buffer, 0, buffer.length, undefined, function(error, result) { + if(error) throw error; + + that.fs.stat('/myfile', function(error, result) { + if(error) throw error; + + _stats = result; + result_buffer = new Uint8Array(_stats.size); + that.fs.read(fd2, result_buffer, 0, result_buffer.length, 0, function(error, result) { + if(error) throw error; + + complete = true; + }); + }); + }); + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(offset + buffer.length); + expect(_stats.size).toEqual(offset + 2 * buffer.length); + var expected = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8]); + expect(typed_array_equal(result_buffer, expected)).toEqual(true); + }); + }); + }); + +}); \ No newline at end of file diff --git a/tests/spec/fs.lstat.spec.js b/tests/spec/fs.lstat.spec.js new file mode 100644 index 0000000..641be69 --- /dev/null +++ b/tests/spec/fs.lstat.spec.js @@ -0,0 +1,92 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.lstat', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.lstat).toEqual('function'); + }); + + it('should return an error if path does not exist', function() { + var complete = false; + var _error, _result; + + this.fs.lstat('/tmp', function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_result).not.toBeDefined(); + }); + }); + + it('should return a stat object if path is not a symbolic link', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.lstat('/', function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toBeDefined(); + }); + }); + + + it('should return a stat object if path is a symbolic link', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.symlink('/', '/mylink', function(error) { + if(error) throw error; + + that.fs.lstat('/mylink', function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toBeDefined(); + }); + }); + }); + +}); \ No newline at end of file diff --git a/tests/spec/fs.mkdir.spec.js b/tests/spec/fs.mkdir.spec.js new file mode 100644 index 0000000..93d4d12 --- /dev/null +++ b/tests/spec/fs.mkdir.spec.js @@ -0,0 +1,89 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.mkdir', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + 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; + var that = this; + + that.fs.mkdir('/tmp/mydir', function(error) { + _error = error; + + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'stat to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + }); + }); + + 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; + }, 'test 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; + + that.fs.mkdir('/tmp', function(error, result) { + _error = error; + _result = result; + + that.fs.stat('/tmp', function(error, result) { + _stat = result; + + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).not.toBeDefined(); + expect(_stat).toBeDefined(); + }); + }); + }); + +}); \ No newline at end of file diff --git a/tests/spec/fs.open.spec.js b/tests/spec/fs.open.spec.js new file mode 100644 index 0000000..c944232 --- /dev/null +++ b/tests/spec/fs.open.spec.js @@ -0,0 +1,175 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.open', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.open).toEqual('function'); + }); + + it('should return an error if the parent path does not exist', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.open('/tmp/myfile', 'w+', function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_result).not.toBeDefined(); + }); + }); + + it('should return an error when flagged for read and the path does not exist', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.open('/myfile', 'r+', function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_result).not.toBeDefined(); + }); + }); + + + it('should return an error when flagged for write and the path is a directory', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.mkdir('/tmp', function(error) { + if(error) throw error; + that.fs.open('/tmp', 'w', function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_result).not.toBeDefined(); + }); + }); + + it('should return an error when flagged for append and the path is a directory', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.mkdir('/tmp', function(error) { + if(error) throw error; + that.fs.open('/tmp', 'a', function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_result).not.toBeDefined(); + }); + }); + + it('should return a unique file descriptor', function() { + var complete1 = false; + var complete2 = false; + var _error, _result1, _result2; + var that = this; + + that.fs.open('/file1', 'w+', function(error, fd) { + if(error) throw error; + _error = error; + _result1 = fd; + + complete1 = true; + }); + that.fs.open('/file2', 'w+', function(error, fd) { + if(error) throw error; + _error = error; + _result2 = fd; + + complete2 = true; + }); + + waitsFor(function() { + return complete1 && complete2; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result1).toBeDefined(); + expect(_result2).toBeDefined(); + expect(_result1).not.toEqual(_result2); + }); + }); + + it('should create a new file when flagged for write', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.open('/myfile', 'w', function(error, result) { + if(error) throw error; + that.fs.stat('/myfile', function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toBeDefined(); + }); + }); + }); + +}); \ No newline at end of file diff --git a/tests/spec/fs.read.spec.js b/tests/spec/fs.read.spec.js new file mode 100644 index 0000000..70c05f2 --- /dev/null +++ b/tests/spec/fs.read.spec.js @@ -0,0 +1,98 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.read', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.read).toEqual('function'); + }); + + it('should read data from a file', function() { + var complete = false; + var _error, _result; + var that = this; + + var wbuffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); + var rbuffer = new Uint8Array(wbuffer.length); + + that.fs.open('/myfile', 'w+', function(error, result) { + if(error) throw error; + + var fd = result; + that.fs.write(fd, wbuffer, 0, wbuffer.length, 0, function(error, result) { + if(error) throw error; + + that.fs.read(fd, rbuffer, 0, rbuffer.length, 0, function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(rbuffer.length); + expect(typed_array_equal(wbuffer, rbuffer)).toEqual(true); + }); + }); + + it('should update the current file position', function() { + var complete = false; + var _error, _result; + var that = this; + + var wbuffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); + var rbuffer = new Uint8Array(wbuffer.length); + _result = 0; + + that.fs.open('/myfile', 'w+', function(error, result) { + if(error) throw error; + + var fd = result; + that.fs.write(fd, wbuffer, 0, wbuffer.length, 0, function(error, result) { + if(error) throw error; + + that.fs.read(fd, rbuffer, 0, rbuffer.length / 2, undefined, function(error, result) { + if(error) throw error; + + _result += result; + that.fs.read(fd, rbuffer, rbuffer.length / 2, rbuffer.length, undefined, function(error, result) { + if(error) throw error; + + _result += result; + complete = true; + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(rbuffer.length); + expect(typed_array_equal(wbuffer.buffer, rbuffer.buffer)).toEqual(true); + }); + }); + }); + +}); \ No newline at end of file diff --git a/tests/spec/fs.readdir.spec.js b/tests/spec/fs.readdir.spec.js new file mode 100644 index 0000000..9cc2b19 --- /dev/null +++ b/tests/spec/fs.readdir.spec.js @@ -0,0 +1,96 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.readdir', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.readdir).toEqual('function'); + }); + + it('should return an error if the path does not exist', function() { + var complete = false; + var _error; + var that = this; + + that.fs.readdir('/tmp/mydir', function(error) { + _error = error; + + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + }); + }); + + it('should return a list of files from an existing directory', function() { + var complete = false; + var _error, _files; + var that = this; + + that.fs.mkdir('/tmp', function(error) { + that.fs.readdir('/', function(error, result) { + _error = error; + _files = result; + + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_files.length).toEqual(1); + expect(_files[0]).toEqual('tmp'); + }); + }); + + it('should follow symbolic links', function() { + var complete = false; + var _error, _files; + var that = this; + + that.fs.mkdir('/tmp', function(error) { + if(error) throw error; + that.fs.symlink('/', '/tmp/dirLink', function(error) { + if(error) throw error; + that.fs.readdir('/tmp/dirLink', function(error, result) { + _error = error; + _files = result; + + complete = true; + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_files.length).toEqual(1); + expect(_files[0]).toEqual('tmp'); + }); + }); + }); + +}); \ No newline at end of file diff --git a/tests/spec/fs.readlink.spec.js b/tests/spec/fs.readlink.spec.js new file mode 100644 index 0000000..e89955b --- /dev/null +++ b/tests/spec/fs.readlink.spec.js @@ -0,0 +1,87 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.readlink', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.readlink).toEqual('function'); + }); + + it('should return an error if part of the parent destination path does not exist', function() { + var complete = false; + var _error; + var that = this; + + that.fs.readlink('/tmp/mydir', function(error) { + _error = error; + + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + }); + }); + + it('should return an error if the path is not a symbolic link', function() { + var complete = false; + var _error; + var that = this; + + that.fs.readlink('/', function(error) { + _error = error; + + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + }); + }); + + it('should return the contents of a symbolic link', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.symlink('/', '/myfile', function(error) { + if(error) throw error; + + that.fs.readlink('/myfile', function(error, result) { + _error = error; + _result = result; + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual('/'); + }); + }); + }); + +}); \ No newline at end of file diff --git a/tests/spec/fs.rename.spec.js b/tests/spec/fs.rename.spec.js new file mode 100644 index 0000000..c258095 --- /dev/null +++ b/tests/spec/fs.rename.spec.js @@ -0,0 +1,63 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.rename', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.rename).toEqual('function'); + }); + + it('should rename an existing file', function() { + var complete1 = false; + var complete2 = false; + var _error, _stats; + var that = this; + + that.fs.open('/myfile', 'w+', function(error, result) { + if(error) throw error; + + var fd = result; + that.fs.close(fd, function(error) { + if(error) throw error; + + that.fs.rename('/myfile', '/myotherfile', function(error) { + if(error) throw error; + + that.fs.stat('/myfile', function(error, result) { + _error = error; + complete1 = true; + }); + + that.fs.stat('/myotherfile', function(error, result) { + if(error) throw error; + + _stats = result; + complete2 = true; + }); + }); + }); + }); + + waitsFor(function() { + return complete1 && complete2; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_stats.nlinks).toEqual(1); + }); + }); + }); + +}); diff --git a/tests/spec/fs.rmdir.spec.js b/tests/spec/fs.rmdir.spec.js new file mode 100644 index 0000000..f76be35 --- /dev/null +++ b/tests/spec/fs.rmdir.spec.js @@ -0,0 +1,162 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.rmdir', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: '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 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; + }, 'test 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; + }, 'test 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; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + }); + }); + + it('should return an error if the path is not a directory', function() { + var complete = false; + var _error; + var that = this; + + that.fs.mkdir('/tmp', function(error) { + that.fs.open('/tmp/myfile', 'w', function(error, fd) { + that.fs.close(fd, function(error) { + that.fs.rmdir('/tmp/myfile', function(error) { + _error = error; + + complete = true; + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + }); + }); + + it('should return an error if the path is a symbolic link', function () { + var complete = false; + var _error; + var that = this; + + that.fs.mkdir('/tmp', function (error) { + that.fs.symlink('/tmp', '/tmp/myfile', function (error) { + that.fs.rmdir('/tmp/myfile', function (error) { + _error = error; + + complete = true; + }); + }); + }); + + waitsFor(function () { + return complete; + }, 'test 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; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_stat).not.toBeDefined(); + }); + }); + }); + +}); diff --git a/tests/spec/fs.spec.js b/tests/spec/fs.spec.js new file mode 100644 index 0000000..9cc22d8 --- /dev/null +++ b/tests/spec/fs.spec.js @@ -0,0 +1,41 @@ +define(["IDBFS"], function(IDBFS) { + + describe("fs", function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it("is an object", function() { + expect(typeof this.fs).toEqual('object'); + }); + + it('should have a root directory', function() { + var complete = false; + var _result; + + this.fs.stat('/', function(error, result) { + _result = result; + + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_result).toBeDefined(); + }); + }); + }); + +}); diff --git a/tests/spec/fs.stat.spec.js b/tests/spec/fs.stat.spec.js new file mode 100644 index 0000000..4e6d831 --- /dev/null +++ b/tests/spec/fs.stat.spec.js @@ -0,0 +1,147 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.stat', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.stat).toEqual('function'); + }); + + it('should return an error if path does not exist', function() { + var complete = false; + var _error, _result; + + this.fs.stat('/tmp', function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_result).not.toBeDefined(); + }); + }); + + it('should return a stat object if path exists', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.stat('/', function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_result).toBeDefined(); + expect(_error).toEqual(null); + expect(_result['node']).toBeDefined(); + expect(_result['dev']).toEqual(that.db_name); + expect(_result['size']).toBeDefined(); + expect(_result['nlinks']).toEqual(jasmine.any(Number)); + expect(_result['atime']).toEqual(jasmine.any(Number)); + expect(_result['mtime']).toEqual(jasmine.any(Number)); + expect(_result['ctime']).toEqual(jasmine.any(Number)); + expect(_result['type']).toBeDefined(); + }); + }); + + it('should follow symbolic links and return a stat object for the resulting path', function() { + var complete = false; + var _error, _node, _result; + var that = this; + + that.fs.open('/myfile', 'w', function(error, result) { + if(error) throw error; + var fd = result; + that.fs.close(fd, function(error) { + if(error) throw error; + that.fs.stat('/myfile', function(error, result) { + if(error) throw error; + + _node = result['node']; + that.fs.symlink('/myfile', '/myfilelink', function(error) { + if(error) throw error; + + that.fs.stat('/myfilelink', function(error, result) { + _error = error; + _result = result; + complete = true; + }); + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_result).toBeDefined(); + expect(_node).toBeDefined(); + expect(_error).toEqual(null); + expect(_result['node']).toEqual(_node); + }); + }); + + it('should return a stat object for a valid descriptor', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.open('/myfile', 'w+', function(error, result) { + if(error) throw error; + + var fd = result; + that.fs.fstat(fd, function(error, result) { + _error = error; + _result = result; + + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_result).toBeDefined(); + expect(_error).toEqual(null); + expect(_result['node']).toBeDefined(); + expect(_result['dev']).toEqual(that.db_name); + expect(_result['size']).toBeDefined(); + expect(_result['nlinks']).toEqual(jasmine.any(Number)); + expect(_result['atime']).toEqual(jasmine.any(Number)); + expect(_result['mtime']).toEqual(jasmine.any(Number)); + expect(_result['ctime']).toEqual(jasmine.any(Number)); + expect(_result['type']).toBeDefined(); + }); + }); + }); + +}); \ No newline at end of file diff --git a/tests/spec/fs.symlink.spec.js b/tests/spec/fs.symlink.spec.js new file mode 100644 index 0000000..15554c5 --- /dev/null +++ b/tests/spec/fs.symlink.spec.js @@ -0,0 +1,83 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.symlink', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.symlink).toEqual('function'); + }); + + it('should return an error if part of the parent destination path does not exist', function() { + var complete = false; + var _error; + var that = this; + + that.fs.symlink('/', '/tmp/mydir', function(error) { + _error = error; + + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + }); + }); + + it('should return an error if the destination path already exists', function() { + var complete = false; + var _error; + var that = this; + + that.fs.symlink('/tmp', '/', function(error) { + _error = error; + + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + }); + }); + + it('should create a symlink', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.symlink('/', '/myfile', function(error, result) { + _error = error; + _result = result; + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).not.toBeDefined(); + }); + }); + }); + +}); \ No newline at end of file diff --git a/tests/spec/fs.unlink.spec.js b/tests/spec/fs.unlink.spec.js new file mode 100644 index 0000000..0f0255c --- /dev/null +++ b/tests/spec/fs.unlink.spec.js @@ -0,0 +1,110 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.unlink', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.unlink).toEqual('function'); + }); + + it('should remove a link to an existing file', function() { + var complete1 = false; + var complete2 = false; + var _error, _stats; + var that = this; + + that.fs.open('/myfile', 'w+', function(error, result) { + if(error) throw error; + + var fd = result; + that.fs.close(fd, function(error) { + if(error) throw error; + + that.fs.link('/myfile', '/myotherfile', function(error) { + if(error) throw error; + + that.fs.unlink('/myfile', function(error) { + if(error) throw error; + + that.fs.stat('/myfile', function(error, result) { + _error = error; + complete1 = true; + }); + + that.fs.stat('/myotherfile', function(error, result) { + if(error) throw error; + + _stats = result; + complete2 = true; + }); + }); + }); + }); + }); + + waitsFor(function() { + return complete1 && complete2; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_stats.nlinks).toEqual(1); + }); + }); + + it('should not follow symbolic links', function () { + var complete = false; + var _error, _stats1, _stats2; + var that = this; + + that.fs.symlink('/', '/myFileLink', function (error) { + if (error) throw error; + + that.fs.link('/myFileLink', '/myotherfile', function (error) { + if (error) throw error; + + that.fs.unlink('/myFileLink', function (error) { + if (error) throw error; + + that.fs.lstat('/myFileLink', function (error, result) { + _error = error; + + that.fs.lstat('/myotherfile', function (error, result) { + if (error) throw error; + _stats1 = result; + + that.fs.stat('/', function (error, result) { + if (error) throw error; + _stats2 = result; + complete = true; + }); + }); + }); + }); + }); + }); + + waitsFor(function () { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function () { + expect(_error).toBeDefined(); + expect(_stats1.nlinks).toEqual(1); + expect(_stats2.nlinks).toEqual(1); + }); + }); + }); + +}); diff --git a/tests/spec/fs.write.spec.js b/tests/spec/fs.write.spec.js new file mode 100644 index 0000000..e1714b8 --- /dev/null +++ b/tests/spec/fs.write.spec.js @@ -0,0 +1,100 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.write', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.write).toEqual('function'); + }); + + it('should write data to a file', function() { + var complete = false; + var _error, _result, _stats; + var that = this; + + var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); + + that.fs.open('/myfile', 'w', function(error, result) { + if(error) throw error; + + var fd = result; + that.fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) { + _error = error; + _result = result; + + that.fs.stat('/myfile', function(error, result) { + if(error) throw error; + + _stats = result; + + complete = true; + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(buffer.length); + expect(_stats.size).toEqual(buffer.length); + }); + }); + + it('should update the current file position', function() { + var complete = false; + var _error, _result, _stats; + var that = this; + + var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); + _result = 0; + + that.fs.open('/myfile', 'w', function(error, result) { + if(error) throw error; + + var fd = result; + that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) { + if(error) throw error; + _result += result; + + that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) { + if(error) throw error; + _result += result; + + that.fs.stat('/myfile', function(error, result) { + if(error) throw error; + + _stats = result; + + complete = true; + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(2 * buffer.length); + expect(_stats.size).toEqual(_result); + }); + }); + }); + +}); diff --git a/tests/spec/fs.writeFile-readFile.spec.js b/tests/spec/fs.writeFile-readFile.spec.js new file mode 100644 index 0000000..9e23c6e --- /dev/null +++ b/tests/spec/fs.writeFile-readFile.spec.js @@ -0,0 +1,183 @@ +define(["IDBFS"], function(IDBFS) { + + describe('fs.writeFile, fs.readFile', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should be a function', function() { + expect(typeof this.fs.writeFile).toEqual('function'); + expect(typeof this.fs.readFile).toEqual('function'); + }); + + it('should error when path is wrong to readFile', function() { + var complete = false; + var _error, _result; + var that = this; + + var contents = "This is a file."; + + that.fs.readFile('/no-such-file', 'utf8', function(error, data) { + _error = error; + _result = data; + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_result).not.toBeDefined(); + }); + }); + + it('should write, read a utf8 file without specifying utf8 in writeFile', function() { + var complete = false; + var _error, _result; + var that = this; + + var contents = "This is a file."; + + that.fs.writeFile('/myfile', contents, function(error) { + if(error) throw error; + that.fs.readFile('/myfile', 'utf8', function(error2, data) { + if(error2) throw error2; + _result = data; + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(contents); + }); + }); + + it('should write, read a utf8 file with "utf8" option to writeFile', function() { + var complete = false; + var _error, _result; + var that = this; + + var contents = "This is a file."; + + that.fs.writeFile('/myfile', contents, 'utf8', function(error) { + if(error) throw error; + that.fs.readFile('/myfile', 'utf8', function(error2, data) { + if(error2) throw error2; + _result = data; + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(contents); + }); + }); + + it('should write, read a utf8 file with {encoding: "utf8"} option to writeFile', function() { + var complete = false; + var _error, _result; + var that = this; + + var contents = "This is a file."; + + that.fs.writeFile('/myfile', contents, { encoding: 'utf8' }, function(error) { + if(error) throw error; + that.fs.readFile('/myfile', 'utf8', function(error2, data) { + if(error2) throw error2; + _result = data; + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(contents); + }); + }); + + it('should write, read a binary file', function() { + var complete = false; + var _error, _result; + var that = this; + + // String and utf8 binary encoded versions of the same thing: + var contents = "This is a file."; + var binary = new Uint8Array([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]); + + that.fs.writeFile('/myfile', binary, function(error) { + if(error) throw error; + that.fs.readFile('/myfile', function(error2, data) { + if(error2) throw error2; + _result = data; + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(binary); + }); + }); + + it('should follow symbolic links', function () { + var complete = false; + var _result; + var that = this; + + var contents = "This is a file."; + + that.fs.writeFile('/myfile', '', { encoding: 'utf8' }, function(error) { + if(error) throw error; + that.fs.symlink('/myfile', '/myFileLink', function (error) { + if (error) throw error; + that.fs.writeFile('/myFileLink', contents, 'utf8', function (error) { + if (error) throw error; + that.fs.readFile('/myFileLink', 'utf8', function(error, data) { + if(error) throw error; + _result = data; + complete = true; + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_result).toEqual(contents); + }); + }); + }); + +}); \ No newline at end of file diff --git a/tests/spec/idbfs.spec.js b/tests/spec/idbfs.spec.js index 9911dc3..a56864d 100644 --- a/tests/spec/idbfs.spec.js +++ b/tests/spec/idbfs.spec.js @@ -1,2254 +1,13 @@ -describe("IDBFS", function() { - it("is defined", function() { - expect(typeof IDBFS).not.toEqual(undefined); - }); - - it("has FileSystem constructor", function() { - expect(typeof IDBFS.FileSystem).toEqual('function'); - }); -}); - -describe("fs", function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it("is an object", function() { - expect(typeof this.fs).toEqual('object'); - }); - - it('should have a root directory', function() { - var complete = false; - var _result; - - this.fs.stat('/', function(error, result) { - _result = result; - - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_result).toBeDefined(); - }); - }); -}); - -describe('fs.stat', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.stat).toEqual('function'); - }); - - it('should return an error if path does not exist', function() { - var complete = false; - var _error, _result; - - this.fs.stat('/tmp', function(error, result) { - _error = error; - _result = result; - - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - expect(_result).not.toBeDefined(); - }); - }); - - it('should return a stat object if path exists', function() { - var complete = false; - var _error, _result; - var that = this; - - that.fs.stat('/', function(error, result) { - _error = error; - _result = result; - - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_result).toBeDefined(); - expect(_error).toEqual(null); - expect(_result['node']).toBeDefined(); - expect(_result['dev']).toEqual(that.db_name); - expect(_result['size']).toBeDefined(); - expect(_result['nlinks']).toEqual(jasmine.any(Number)); - expect(_result['atime']).toEqual(jasmine.any(Number)); - expect(_result['mtime']).toEqual(jasmine.any(Number)); - expect(_result['ctime']).toEqual(jasmine.any(Number)); - expect(_result['type']).toBeDefined(); - }); - }); - - it('should follow symbolic links and return a stat object for the resulting path', function() { - var complete = false; - var _error, _node, _result; - var that = this; - - that.fs.open('/myfile', 'w', function(error, result) { - if(error) throw error; - var fd = result; - that.fs.close(fd, function(error) { - if(error) throw error; - that.fs.stat('/myfile', function(error, result) { - if(error) throw error; - - _node = result['node']; - that.fs.symlink('/myfile', '/myfilelink', function(error) { - if(error) throw error; - - that.fs.stat('/myfilelink', function(error, result) { - _error = error; - _result = result; - complete = true; - }); - }); - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_result).toBeDefined(); - expect(_node).toBeDefined(); - expect(_error).toEqual(null); - expect(_result['node']).toEqual(_node); - }); - }); -}); - -describe('fs.fstat', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.fstat).toEqual('function'); - }); - - it('should return a stat object for a valid descriptor', function() { - var complete = false; - var _error, _result; - var that = this; - - that.fs.open('/myfile', 'w+', function(error, result) { - if(error) throw error; - - var fd = result; - that.fs.fstat(fd, function(error, result) { - _error = error; - _result = result; - - complete = true; - }) - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_result).toBeDefined(); - expect(_error).toEqual(null); - expect(_result['node']).toBeDefined(); - expect(_result['dev']).toEqual(that.db_name); - expect(_result['size']).toBeDefined(); - expect(_result['nlinks']).toEqual(jasmine.any(Number)); - expect(_result['atime']).toEqual(jasmine.any(Number)); - expect(_result['mtime']).toEqual(jasmine.any(Number)); - expect(_result['ctime']).toEqual(jasmine.any(Number)); - expect(_result['type']).toBeDefined(); - }); - }); -}); - -describe('fs.lstat', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.lstat).toEqual('function'); - }); - - it('should return an error if path does not exist', function() { - var complete = false; - var _error, _result; - - this.fs.lstat('/tmp', function(error, result) { - _error = error; - _result = result; - - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - expect(_result).not.toBeDefined(); - }); - }); - - it('should return a stat object if path is not a symbolic link', function() { - var complete = false; - var _error, _result; - var that = this; - - that.fs.lstat('/', function(error, result) { - _error = error; - _result = result; - - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toBeDefined(); - }); - }); - - - it('should return a stat object if path is a symbolic link', function() { - var complete = false; - var _error, _result; - var that = this; - - that.fs.symlink('/', '/mylink', function(error) { - if(error) throw error; - - that.fs.lstat('/mylink', function(error, result) { - _error = error; - _result = result; - - complete = true; - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toBeDefined(); - }); - }); -}); - -describe('fs.mkdir', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - 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; - var that = this; - - that.fs.mkdir('/tmp/mydir', function(error) { - _error = error; - - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'stat to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - }); - }); - - 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; - }, 'test 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; - - that.fs.mkdir('/tmp', function(error, result) { - _error = error; - _result = result; - - that.fs.stat('/tmp', function(error, result) { - _stat = result; - - complete = true; - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).not.toBeDefined(); - expect(_stat).toBeDefined(); - }); - }); -}); - -describe('fs.readdir', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.readdir).toEqual('function'); - }); - - it('should return an error if the path does not exist', function() { - var complete = false; - var _error; - var that = this; - - that.fs.readdir('/tmp/mydir', function(error) { - _error = error; - - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - }); - }); - - it('should return a list of files from an existing directory', function() { - var complete = false; - var _error, _files; - var that = this; - - that.fs.mkdir('/tmp', function(error) { - that.fs.readdir('/', function(error, result) { - _error = error; - _files = result; - - complete = true; - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_files.length).toEqual(1); - expect(_files[0]).toEqual('tmp'); - }); - }); - - it('should follow symbolic links', function() { - var complete = false; - var _error, _files; - var that = this; - - that.fs.mkdir('/tmp', function(error) { - if(error) throw error; - that.fs.symlink('/', '/tmp/dirLink', function(error) { - if(error) throw error; - that.fs.readdir('/tmp/dirLink', function(error, result) { - _error = error; - _files = result; - - complete = true; - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_files.length).toEqual(1); - expect(_files[0]).toEqual('tmp'); - }); - }); -}); - -describe('fs.rmdir', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: '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 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; - }, 'test 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; - }, 'test 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; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - }); - }); - - it('should return an error if the path is not a directory', function() { - var complete = false; - var _error; - var that = this; - - that.fs.mkdir('/tmp', function(error) { - that.fs.open('/tmp/myfile', 'w', function(error, fd) { - that.fs.close(fd, function(error) { - that.fs.rmdir('/tmp/myfile', function(error) { - _error = error; - - complete = true; - }); - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - }); - }); - - it('should return an error if the path is a symbolic link', function () { - var complete = false; - var _error; - var that = this; - - that.fs.mkdir('/tmp', function (error) { - that.fs.symlink('/tmp', '/tmp/myfile', function (error) { - that.fs.rmdir('/tmp/myfile', function (error) { - _error = error; - - complete = true; - }); - }); - }); - - waitsFor(function () { - return complete; - }, 'test 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; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_stat).not.toBeDefined(); - }); - }); -}); - -describe('fs.open', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.open).toEqual('function'); - }); - - it('should return an error if the parent path does not exist', function() { - var complete = false; - var _error, _result; - var that = this; - - that.fs.open('/tmp/myfile', 'w+', function(error, result) { - _error = error; - _result = result; - - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - expect(_result).not.toBeDefined(); - }); - }); - - it('should return an error when flagged for read and the path does not exist', function() { - var complete = false; - var _error, _result; - var that = this; - - that.fs.open('/myfile', 'r+', function(error, result) { - _error = error; - _result = result; - - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - expect(_result).not.toBeDefined(); - }); - }); - - - it('should return an error when flagged for write and the path is a directory', function() { - var complete = false; - var _error, _result; - var that = this; - - that.fs.mkdir('/tmp', function(error) { - if(error) throw error; - that.fs.open('/tmp', 'w', function(error, result) { - _error = error; - _result = result; - - complete = true; - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - expect(_result).not.toBeDefined(); - }); - }); - - it('should return an error when flagged for append and the path is a directory', function() { - var complete = false; - var _error, _result; - var that = this; - - that.fs.mkdir('/tmp', function(error) { - if(error) throw error; - that.fs.open('/tmp', 'a', function(error, result) { - _error = error; - _result = result; - - complete = true; - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - expect(_result).not.toBeDefined(); - }); - }); - - it('should return a unique file descriptor', function() { - var complete1 = false; - var complete2 = false; - var _error, _result1, _result2; - var that = this; - - that.fs.open('/file1', 'w+', function(error, fd) { - if(error) throw error; - _error = error; - _result1 = fd; - - complete1 = true; - }); - that.fs.open('/file2', 'w+', function(error, fd) { - if(error) throw error; - _error = error; - _result2 = fd; - - complete2 = true; - }); - - waitsFor(function() { - return complete1 && complete2; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result1).toBeDefined(); - expect(_result2).toBeDefined(); - expect(_result1).not.toEqual(_result2); - }); - }); - - it('should create a new file when flagged for write', function() { - var complete = false; - var _error, _result; - var that = this; - - that.fs.open('/myfile', 'w', function(error, result) { - if(error) throw error; - that.fs.stat('/myfile', function(error, result) { - _error = error; - _result = result; - - complete = true; - }) - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toBeDefined(); - }); - }); -}); - -describe('fs.write', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.write).toEqual('function'); - }); - - it('should write data to a file', function() { - var complete = false; - var _error, _result, _stats; - var that = this; - - var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); - - that.fs.open('/myfile', 'w', function(error, result) { - if(error) throw error; - - var fd = result; - that.fs.write(fd, buffer, 0, buffer.length, 0, function(error, result) { - _error = error; - _result = result; - - that.fs.stat('/myfile', function(error, result) { - if(error) throw error; - - _stats = result; - - complete = true; - }); - }) - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(buffer.length); - expect(_stats.size).toEqual(buffer.length); - }); - }); - - it('should update the current file position', function() { - var complete = false; - var _error, _result, _stats; - var that = this; - - var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); - _result = 0; - - that.fs.open('/myfile', 'w', function(error, result) { - if(error) throw error; - - var fd = result; - that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) { - if(error) throw error; - _result += result; - - that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) { - if(error) throw error; - _result += result; - - that.fs.stat('/myfile', function(error, result) { - if(error) throw error; - - _stats = result; - - complete = true; - }); - }); - }) - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(2 * buffer.length); - expect(_stats.size).toEqual(_result); - }); - }); -}); - -describe('fs.writeFile, fs.readFile', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.writeFile).toEqual('function'); - expect(typeof this.fs.readFile).toEqual('function'); - }); - - it('should error when path is wrong to readFile', function() { - var complete = false; - var _error, _result; - var that = this; - - var contents = "This is a file."; - - that.fs.readFile('/no-such-file', 'utf8', function(error, data) { - _error = error; - _result = data; - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - expect(_result).not.toBeDefined(); - }); - }); - - it('should write, read a utf8 file without specifying utf8 in writeFile', function() { - var complete = false; - var _error, _result; - var that = this; - - var contents = "This is a file."; - - that.fs.writeFile('/myfile', contents, function(error) { - if(error) throw error; - that.fs.readFile('/myfile', 'utf8', function(error2, data) { - if(error2) throw error2; - _result = data; - complete = true; - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(contents); - }); - }); - - it('should write, read a utf8 file with "utf8" option to writeFile', function() { - var complete = false; - var _error, _result; - var that = this; - - var contents = "This is a file."; - - that.fs.writeFile('/myfile', contents, 'utf8', function(error) { - if(error) throw error; - that.fs.readFile('/myfile', 'utf8', function(error2, data) { - if(error2) throw error2; - _result = data; - complete = true; - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(contents); - }); - }); - - it('should write, read a utf8 file with {encoding: "utf8"} option to writeFile', function() { - var complete = false; - var _error, _result; - var that = this; - - var contents = "This is a file."; - - that.fs.writeFile('/myfile', contents, { encoding: 'utf8' }, function(error) { - if(error) throw error; - that.fs.readFile('/myfile', 'utf8', function(error2, data) { - if(error2) throw error2; - _result = data; - complete = true; - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(contents); - }); - }); - - it('should write, read a binary file', function() { - var complete = false; - var _error, _result; - var that = this; - - // String and utf8 binary encoded versions of the same thing: - var contents = "This is a file."; - var binary = new Uint8Array([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]); - - that.fs.writeFile('/myfile', binary, function(error) { - if(error) throw error; - that.fs.readFile('/myfile', function(error2, data) { - if(error2) throw error2; - _result = data; - complete = true; - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(binary); - }); - }); - - it('should follow symbolic links', function () { - var complete = false; - var _result; - var that = this; - - var contents = "This is a file."; - - that.fs.writeFile('/myfile', '', { encoding: 'utf8' }, function(error) { - if(error) throw error; - that.fs.symlink('/myfile', '/myFileLink', function (error) { - if (error) throw error; - that.fs.writeFile('/myFileLink', contents, 'utf8', function (error) { - if (error) throw error; - that.fs.readFile('/myFileLink', 'utf8', function(error, data) { - if(error) throw error; - _result = data; - complete = true; - }); - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_result).toEqual(contents); - }); - }); -}); - -describe('fs.read', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.read).toEqual('function'); - }); - - it('should read data from a file', function() { - var complete = false; - var _error, _result; - var that = this; - - var wbuffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); - var rbuffer = new Uint8Array(wbuffer.length); - - that.fs.open('/myfile', 'w+', function(error, result) { - if(error) throw error; - - var fd = result; - that.fs.write(fd, wbuffer, 0, wbuffer.length, 0, function(error, result) { - if(error) throw error; - - that.fs.read(fd, rbuffer, 0, rbuffer.length, 0, function(error, result) { - _error = error; - _result = result; - - complete = true; - }); - }) - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(rbuffer.length); - expect(typed_array_equal(wbuffer, rbuffer)).toEqual(true); - }); - }); - - it('should update the current file position', function() { - var complete = false; - var _error, _result; - var that = this; - - var wbuffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); - var rbuffer = new Uint8Array(wbuffer.length); - _result = 0; - - that.fs.open('/myfile', 'w+', function(error, result) { - if(error) throw error; - - var fd = result; - that.fs.write(fd, wbuffer, 0, wbuffer.length, 0, function(error, result) { - if(error) throw error; - - that.fs.read(fd, rbuffer, 0, rbuffer.length / 2, undefined, function(error, result) { - if(error) throw error; - - _result += result; - that.fs.read(fd, rbuffer, rbuffer.length / 2, rbuffer.length, undefined, function(error, result) { - if(error) throw error; - - _result += result; - complete = true; - }); - }); - }) - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(rbuffer.length); - expect(typed_array_equal(wbuffer.buffer, rbuffer.buffer)).toEqual(true); - }); - }); -}); - -describe('fs.close', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.close).toEqual('function'); - }); - - it('should release the file descriptor', function() { - var complete = false; - var _error; - var that = this; - - var buffer = new Uint8Array(0); - - that.fs.open('/myfile', 'w+', function(error, result) { - if(error) throw error; - - var fd = result; - that.fs.close(fd, function(error) { - that.fs.read(fd, buffer, 0, buffer.length, undefined, function(error, result) { - _error = error; - complete = true; - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - }); - }); -}); - -describe('fs.link', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.link).toEqual('function'); - }); - - it('should create a link to an existing file', function() { - var complete = false; - var _error, _oldstats, _newstats; - var that = this; - - that.fs.open('/myfile', 'w+', function(error, result) { - if(error) throw error; - - var fd = result; - that.fs.close(fd, function(error) { - if(error) throw error; - - that.fs.link('/myfile', '/myotherfile', function(error) { - if(error) throw error; - - that.fs.stat('/myfile', function(error, result) { - if(error) throw error; - - _oldstats = result; - that.fs.stat('/myotherfile', function(error, result) { - if(error) throw error; - - _newstats = result; - - complete = true; - }); - }); - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_newstats.node).toEqual(_oldstats.node); - expect(_newstats.nlinks).toEqual(2); - expect(_newstats).toEqual(_oldstats); - }); - }); - - it('should not follow symbolic links', function () { - var complete = false; - var _error, _oldstats, _linkstats, _newstats; - var that = this; - - that.fs.stat('/', function (error, result) { - if (error) throw error; - _oldstats = result; - that.fs.symlink('/', '/myfileLink', function (error) { - if (error) throw error; - that.fs.link('/myfileLink', '/myotherfile', function (error) { - if (error) throw error; - that.fs.lstat('/myfileLink', function (error, result) { - if (error) throw error; - _linkstats = result; - that.fs.lstat('/myotherfile', function (error, result) { - if (error) throw error; - _newstats = result; - complete = true; - }); - }); - }); - }); - }); - - waitsFor(function () { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function () { - expect(_error).toEqual(null); - expect(_newstats.node).toEqual(_linkstats.node); - expect(_newstats.node).toNotEqual(_oldstats.node); - expect(_newstats.nlinks).toEqual(2); - expect(_newstats).toEqual(_linkstats); - }); -}); - }); - -describe('fs.unlink', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.unlink).toEqual('function'); - }); +define(["IDBFS"], function(IDBFS) { - it('should remove a link to an existing file', function() { - var complete1 = false; - var complete2 = false; - var _error, _stats; - var that = this; - - that.fs.open('/myfile', 'w+', function(error, result) { - if(error) throw error; - - var fd = result; - that.fs.close(fd, function(error) { - if(error) throw error; - - that.fs.link('/myfile', '/myotherfile', function(error) { - if(error) throw error; - - that.fs.unlink('/myfile', function(error) { - if(error) throw error; - - that.fs.stat('/myfile', function(error, result) { - _error = error; - complete1 = true; - }); - - that.fs.stat('/myotherfile', function(error, result) { - if(error) throw error; - - _stats = result; - complete2 = true; - }); - }); - }); - }); - }); - - waitsFor(function() { - return complete1 && complete2; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - expect(_stats.nlinks).toEqual(1); - }); - }); - - it('should not follow symbolic links', function () { - var complete = false; - var _error, _stats1, _stats2; - var that = this; - - that.fs.symlink('/', '/myFileLink', function (error) { - if (error) throw error; - - that.fs.link('/myFileLink', '/myotherfile', function (error) { - if (error) throw error; - - that.fs.unlink('/myFileLink', function (error) { - if (error) throw error; - - that.fs.lstat('/myFileLink', function (error, result) { - _error = error; - - that.fs.lstat('/myotherfile', function (error, result) { - if (error) throw error; - _stats1 = result; - - that.fs.stat('/', function (error, result) { - if (error) throw error; - _stats2 = result; - complete = true; - }); - }); - }); - }); - }); - }); - - waitsFor(function () { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function () { - expect(_error).toBeDefined(); - expect(_stats1.nlinks).toEqual(1); - expect(_stats2.nlinks).toEqual(1); - }); - }); -}); - -describe('fs.rename', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.rename).toEqual('function'); - }); - - it('should rename an existing file', function() { - var complete1 = false; - var complete2 = false; - var _error, _stats; - var that = this; - - that.fs.open('/myfile', 'w+', function(error, result) { - if(error) throw error; - - var fd = result; - that.fs.close(fd, function(error) { - if(error) throw error; - - that.fs.rename('/myfile', '/myotherfile', function(error) { - if(error) throw error; - - that.fs.stat('/myfile', function(error, result) { - _error = error; - complete1 = true; - }); - - that.fs.stat('/myotherfile', function(error, result) { - if(error) throw error; - - _stats = result; - complete2 = true; - }); - }); - }); - }); - - waitsFor(function() { - return complete1 && complete2; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - expect(_stats.nlinks).toEqual(1); - }); - }); -}); - -describe('fs.lseek', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.lseek).toEqual('function'); - }); - - it('should not follow symbolic links', function () { - var complete = false; - var _error, _stats; - var that = this; - - that.fs.open('/myfile', 'w', function (error, result) { - if (error) throw error; - - var fd = result; - that.fs.close(fd, function (error) { - if (error) throw error; - - that.fs.symlink('/myfile', '/myFileLink', function (error) { - if (error) throw error; - - that.fs.rename('/myFileLink', '/myOtherFileLink', function (error) { - if (error) throw error; - - that.fs.stat('/myfile', function (error, result) { - _error1 = error; - - that.fs.lstat('/myFileLink', function (error, result) { - _error2 = error; - - that.fs.stat('/myOtherFileLink', function (error, result) { - if (error) throw error; - - _stats = result; - complete = true; - }); - }); - }); - }); - }); - }); - }); - - waitsFor(function () { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function () { - expect(_error1).toEqual(null); - expect(_error2).toBeDefined(); - expect(_stats.nlinks).toEqual(1); - }); - }); -}); - -describe('fs.lseek', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.lseek).toEqual('function'); - }); - - it('should set the current position if whence is SET', function() { - var complete = false; - var _error, _result, _stats; - var that = this; - - var offset = 3; - var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); - var result_buffer = new Uint8Array(buffer.length + offset); - - that.fs.open('/myfile', 'w+', function(error, result) { - if(error) throw error; - - var fd = result; - that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) { - if(error) throw error; - - that.fs.lseek(fd, offset, 'SET', function(error, result) { - _error = error; - _result = result; - - that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) { - if(error) throw error; - - that.fs.read(fd, result_buffer, 0, result_buffer.length, 0, function(error, result) { - if(error) throw error; - - that.fs.stat('/myfile', function(error, result) { - if(error) throw error; - - _stats = result; - - complete = true; - }); - }); - }); - }); - }) - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(offset); - expect(_stats.size).toEqual(offset + buffer.length); - var expected = new Uint8Array([1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8]); - expect(typed_array_equal(result_buffer, expected)).toEqual(true); - }); - }); - - it('should update the current position if whence is CUR', function() { - var complete = false; - var _error, _result, _stats; - var that = this; - - var offset = -2; - var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); - var result_buffer = new Uint8Array(2 * buffer.length + offset); - - that.fs.open('/myfile', 'w+', function(error, result) { - if(error) throw error; - - var fd = result; - that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) { - if(error) throw error; - - that.fs.lseek(fd, offset, 'CUR', function(error, result) { - _error = error; - _result = result; - - that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) { - if(error) throw error; - - that.fs.read(fd, result_buffer, 0, result_buffer.length, 0, function(error, result) { - if(error) throw error; - - that.fs.stat('/myfile', function(error, result) { - if(error) throw error; - - _stats = result; - - complete = true; - }); - }); - }); - }); - }) - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(offset + buffer.length); - expect(_stats.size).toEqual(offset + 2 * buffer.length); - var expected = new Uint8Array([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8]); - expect(typed_array_equal(result_buffer, expected)).toEqual(true); - }); - }); - - it('should update the current position if whence is END', function() { - var complete = false; - var _error, _result, _stats; - var that = this; - - var offset = 5; - var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); - var result_buffer; - - that.fs.open('/myfile', 'w+', function(error, result) { - if(error) throw error; - - var fd1 = result; - that.fs.write(fd1, buffer, 0, buffer.length, undefined, function(error, result) { - if(error) throw error; - - that.fs.open('/myfile', 'w+', function(error, result) { - if(error) throw error; - - var fd2 = result; - that.fs.lseek(fd2, offset, 'END', function(error, result) { - _error = error; - _result = result; - - that.fs.write(fd2, buffer, 0, buffer.length, undefined, function(error, result) { - if(error) throw error; - - that.fs.stat('/myfile', function(error, result) { - if(error) throw error; - - _stats = result; - result_buffer = new Uint8Array(_stats.size); - that.fs.read(fd2, result_buffer, 0, result_buffer.length, 0, function(error, result) { - if(error) throw error; - - complete = true; - }); - }); - }); - }); - }); - }) - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(offset + buffer.length); - expect(_stats.size).toEqual(offset + 2 * buffer.length); - var expected = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8]); - expect(typed_array_equal(result_buffer, expected)).toEqual(true); - }); - }); -}); - -describe('fs.symlink', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.symlink).toEqual('function'); - }); - - it('should return an error if part of the parent destination path does not exist', function() { - var complete = false; - var _error; - var that = this; - - that.fs.symlink('/', '/tmp/mydir', function(error) { - _error = error; - - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - }); - }); - - it('should return an error if the destination path already exists', function() { - var complete = false; - var _error; - var that = this; - - that.fs.symlink('/tmp', '/', function(error) { - _error = error; - - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - }); - }); - - it('should create a symlink', function() { - var complete = false; - var _error, _result; - var that = this; - - that.fs.symlink('/', '/myfile', function(error, result) { - _error = error; - _result = result; - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).not.toBeDefined(); - }); - }); -}); - -describe('fs.readlink', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should be a function', function() { - expect(typeof this.fs.readlink).toEqual('function'); - }); - - it('should return an error if part of the parent destination path does not exist', function() { - var complete = false; - var _error; - var that = this; - - that.fs.readlink('/tmp/mydir', function(error) { - _error = error; - - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - }); - }); - - it('should return an error if the path is not a symbolic link', function() { - var complete = false; - var _error; - var that = this; - - that.fs.readlink('/', function(error) { - _error = error; - - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - }); - }); - - it('should return the contents of a symbolic link', function() { - var complete = false; - var _error, _result; - var that = this; - - that.fs.symlink('/', '/myfile', function(error) { - if(error) throw error; - - that.fs.readlink('/myfile', function(error, result) { - _error = error; - _result = result; - complete = true; - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual('/'); - }); - }); -}); - -describe('path resolution', function() { - beforeEach(function() { - this.db_name = mk_db_name(); - this.fs = new IDBFS.FileSystem({ - name: this.db_name, - flags: 'FORMAT' - }); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - delete this.fs; - }); - - it('should follow a symbolic link to the root directory', function() { - var complete = false; - var _error, _node, _result; - var that = this; - - that.fs.symlink('/', '/mydirectorylink', function(error) { - if(error) throw error; - - that.fs.stat('/', function(error, result) { - if(error) throw error; - - _node = result['node']; - that.fs.stat('/mydirectorylink', function(error, result) { - _error = error; - _result = result; - complete = true; - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_result).toBeDefined(); - expect(_node).toBeDefined(); - expect(_error).toEqual(null); - expect(_result['node']).toEqual(_node); - }); - }); - - it('should follow a symbolic link to a directory', function() { - var complete = false; - var _error, _node, _result; - var that = this; - - that.fs.mkdir('/mydir', function(error) { - that.fs.symlink('/mydir', '/mydirectorylink', function(error) { - if(error) throw error; - - that.fs.stat('/mydir', function(error, result) { - if(error) throw error; - - _node = result['node']; - that.fs.stat('/mydirectorylink', function(error, result) { - _error = error; - _result = result; - complete = true; - }); - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_result).toBeDefined(); - expect(_node).toBeDefined(); - expect(_error).toEqual(null); - expect(_result['node']).toEqual(_node); - }); - }); - - it('should follow a symbolic link to a file', function() { - var complete = false; - var _error, _node, _result; - var that = this; - - that.fs.open('/myfile', 'w', function(error, result) { - if(error) throw error; - var fd = result; - that.fs.close(fd, function(error) { - if(error) throw error; - that.fs.stat('/myfile', function(error, result) { - if(error) throw error; - - _node = result['node']; - that.fs.symlink('/myfile', '/myfilelink', function(error) { - if(error) throw error; - - that.fs.stat('/myfilelink', function(error, result) { - _error = error; - _result = result; - complete = true; - }); - }); - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_result).toBeDefined(); - expect(_node).toBeDefined(); - expect(_error).toEqual(null); - expect(_result['node']).toEqual(_node); - }); - }); - - it('should follow multiple symbolic links to a file', function() { - var complete = false; - var _error, _node, _result; - var that = this; - - that.fs.open('/myfile', 'w', function(error, result) { - if(error) throw error; - var fd = result; - that.fs.close(fd, function(error) { - if(error) throw error; - that.fs.stat('/myfile', function(error, result) { - if(error) throw error; - - _node = result['node']; - that.fs.symlink('/myfile', '/myfilelink1', function(error) { - if(error) throw error; - that.fs.symlink('/myfilelink1', '/myfilelink2', function(error) { - if(error) throw error; - - that.fs.stat('/myfilelink2', function(error, result) { - _error = error; - _result = result; - complete = true; - }); - }); - }); - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_result).toBeDefined(); - expect(_node).toBeDefined(); - expect(_error).toEqual(null); - expect(_result['node']).toEqual(_node); - }); - }); - - it('should error if symbolic link leads to itself', function() { - var complete = false; - var _error, _node, _result; - var that = this; - - that.fs.symlink('/mylink1', '/mylink2', function(error) { - if(error) throw error; - - that.fs.symlink('/mylink2', '/mylink1', function(error) { - if(error) throw error; - - that.fs.stat('/myfilelink1', function(error, result) { - _error = error; - _result = result; - complete = true; - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - expect(_result).not.toBeDefined(); - }); - }); - - it('should error if it follows more than 10 symbolic links', function() { - var complete = false; - var _error, _result; - var that = this; - - that.fs.open('/myfile', 'w', function(error, result) { - if(error) throw error; - var fd = result; - that.fs.close(fd, function(error) { - if(error) throw error; - that.fs.stat('/myfile', function(error, result) { - if(error) throw error; - - that.fs.symlink('/myfile', '/myfilelink1', function(error) { - if(error) throw error; - that.fs.symlink('/myfilelink1', '/myfilelink2', function(error) { - if(error) throw error; - - that.fs.symlink('/myfilelink2', '/myfilelink3', function(error) { - if(error) throw error; - - that.fs.symlink('/myfilelink3', '/myfilelink4', function(error) { - if(error) throw error; - - that.fs.symlink('/myfilelink4', '/myfilelink5', function(error) { - if(error) throw error; - - that.fs.symlink('/myfilelink5', '/myfilelink6', function(error) { - if(error) throw error; - - that.fs.symlink('/myfilelink6', '/myfilelink7', function(error) { - if(error) throw error; - - that.fs.symlink('/myfilelink7', '/myfilelink8', function(error) { - if(error) throw error; - - that.fs.symlink('/myfilelink8', '/myfilelink9', function(error) { - if(error) throw error; - - that.fs.symlink('/myfilelink9', '/myfilelink10', function(error) { - if(error) throw error; - - that.fs.symlink('/myfilelink10', '/myfilelink11', function(error) { - if(error) throw error; - - that.fs.stat('/myfilelink11', function(error, result) { - _error = error; - _result = result; - complete = true; - }); - }); - }); - }); - }); - }); - }); - }); - }); - }); - }); - }); - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_result).not.toBeDefined(); - expect(_error).toBeDefined(); - }); - }); - - it('should follow a symbolic link in the path to a file', function() { - var complete = false; - var _error, _node, _result; - var that = this; - - that.fs.open('/myfile', 'w', function(error, result) { - if(error) throw error; - var fd = result; - that.fs.close(fd, function(error) { - if(error) throw error; - that.fs.stat('/myfile', function(error, result) { - if(error) throw error; - - _node = result['node']; - that.fs.symlink('/', '/mydirlink', function(error) { - if(error) throw error; - - that.fs.stat('/mydirlink/myfile', function(error, result) { - _error = error; - _result = result; - complete = true; - }); - }); - }); - }); + describe("IDBFS", function() { + it("is defined", function() { + expect(typeof IDBFS).not.toEqual(undefined); }); - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_result).toBeDefined(); - expect(_node).toBeDefined(); - expect(_error).toEqual(null); - expect(_result['node']).toEqual(_node); + it("has FileSystem constructor", function() { + expect(typeof IDBFS.FileSystem).toEqual('function'); }); }); - it('should error if a symbolic link in the path to a file is itself a file', function() { - var complete = false; - var _error, _result; - var that = this; - - that.fs.open('/myfile', 'w', function(error, result) { - if(error) throw error; - var fd = result; - that.fs.close(fd, function(error) { - if(error) throw error; - that.fs.stat('/myfile', function(error, result) { - if(error) throw error; - - that.fs.open('/myfile2', 'w', function(error, result) { - if(error) throw error; - var fd = result; - that.fs.close(fd, function(error) { - if(error) throw error; - that.fs.symlink('/myfile2', '/mynotdirlink', function(error) { - if(error) throw error; - - that.fs.stat('/mynotdirlink/myfile', function(error, result) { - _error = error; - _result = result; - complete = true; - }); - }); - }); - }); - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - expect(_result).not.toBeDefined(); - }); - }); -}); +}); \ No newline at end of file diff --git a/tests/spec/node-js/simple/test-fs-mkdir.js b/tests/spec/node-js/simple/test-fs-mkdir.js new file mode 100644 index 0000000..9393a1e --- /dev/null +++ b/tests/spec/node-js/simple/test-fs-mkdir.js @@ -0,0 +1,74 @@ +define(["IDBFS"], function(IDBFS) { + + describe("node.js tests: https://github.com/joyent/node/blob/master/test/simple/test-fs-mkdir.js", function() { + + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + 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); + }); + }); + + }); + +}); diff --git a/tests/spec/node-js/simple/test-fs-null-bytes.js b/tests/spec/node-js/simple/test-fs-null-bytes.js new file mode 100644 index 0000000..b441d34 --- /dev/null +++ b/tests/spec/node-js/simple/test-fs-null-bytes.js @@ -0,0 +1,77 @@ +define(["IDBFS"], function(IDBFS) { + + describe("node.js tests: https://github.com/joyent/node/blob/master/test/simple/test-fs-null-bytes.js", function() { + + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + 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(); + }); + }); + }); + }); +}); \ No newline at end of file diff --git a/tests/spec/node.spec.js b/tests/spec/node.spec.js deleted file mode 100644 index c370a0f..0000000 --- a/tests/spec/node.spec.js +++ /dev/null @@ -1,132 +0,0 @@ -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(); - }); - }); - }); - -}); diff --git a/tests/spec/path-resolution.spec.js b/tests/spec/path-resolution.spec.js new file mode 100644 index 0000000..07d5b26 --- /dev/null +++ b/tests/spec/path-resolution.spec.js @@ -0,0 +1,351 @@ +define(["IDBFS"], function(IDBFS) { + + describe('path resolution', function() { + beforeEach(function() { + this.db_name = mk_db_name(); + this.fs = new IDBFS.FileSystem({ + name: this.db_name, + flags: 'FORMAT' + }); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + delete this.fs; + }); + + it('should follow a symbolic link to the root directory', function() { + var complete = false; + var _error, _node, _result; + var that = this; + + that.fs.symlink('/', '/mydirectorylink', function(error) { + if(error) throw error; + + that.fs.stat('/', function(error, result) { + if(error) throw error; + + _node = result['node']; + that.fs.stat('/mydirectorylink', function(error, result) { + _error = error; + _result = result; + complete = true; + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_result).toBeDefined(); + expect(_node).toBeDefined(); + expect(_error).toEqual(null); + expect(_result['node']).toEqual(_node); + }); + }); + + it('should follow a symbolic link to a directory', function() { + var complete = false; + var _error, _node, _result; + var that = this; + + that.fs.mkdir('/mydir', function(error) { + that.fs.symlink('/mydir', '/mydirectorylink', function(error) { + if(error) throw error; + + that.fs.stat('/mydir', function(error, result) { + if(error) throw error; + + _node = result['node']; + that.fs.stat('/mydirectorylink', function(error, result) { + _error = error; + _result = result; + complete = true; + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_result).toBeDefined(); + expect(_node).toBeDefined(); + expect(_error).toEqual(null); + expect(_result['node']).toEqual(_node); + }); + }); + + it('should follow a symbolic link to a file', function() { + var complete = false; + var _error, _node, _result; + var that = this; + + that.fs.open('/myfile', 'w', function(error, result) { + if(error) throw error; + var fd = result; + that.fs.close(fd, function(error) { + if(error) throw error; + that.fs.stat('/myfile', function(error, result) { + if(error) throw error; + + _node = result['node']; + that.fs.symlink('/myfile', '/myfilelink', function(error) { + if(error) throw error; + + that.fs.stat('/myfilelink', function(error, result) { + _error = error; + _result = result; + complete = true; + }); + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_result).toBeDefined(); + expect(_node).toBeDefined(); + expect(_error).toEqual(null); + expect(_result['node']).toEqual(_node); + }); + }); + + it('should follow multiple symbolic links to a file', function() { + var complete = false; + var _error, _node, _result; + var that = this; + + that.fs.open('/myfile', 'w', function(error, result) { + if(error) throw error; + var fd = result; + that.fs.close(fd, function(error) { + if(error) throw error; + that.fs.stat('/myfile', function(error, result) { + if(error) throw error; + + _node = result['node']; + that.fs.symlink('/myfile', '/myfilelink1', function(error) { + if(error) throw error; + that.fs.symlink('/myfilelink1', '/myfilelink2', function(error) { + if(error) throw error; + + that.fs.stat('/myfilelink2', function(error, result) { + _error = error; + _result = result; + complete = true; + }); + }); + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_result).toBeDefined(); + expect(_node).toBeDefined(); + expect(_error).toEqual(null); + expect(_result['node']).toEqual(_node); + }); + }); + + it('should error if symbolic link leads to itself', function() { + var complete = false; + var _error, _node, _result; + var that = this; + + that.fs.symlink('/mylink1', '/mylink2', function(error) { + if(error) throw error; + + that.fs.symlink('/mylink2', '/mylink1', function(error) { + if(error) throw error; + + that.fs.stat('/myfilelink1', function(error, result) { + _error = error; + _result = result; + complete = true; + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_result).not.toBeDefined(); + }); + }); + + it('should error if it follows more than 10 symbolic links', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.open('/myfile', 'w', function(error, result) { + if(error) throw error; + var fd = result; + that.fs.close(fd, function(error) { + if(error) throw error; + that.fs.stat('/myfile', function(error, result) { + if(error) throw error; + + that.fs.symlink('/myfile', '/myfilelink1', function(error) { + if(error) throw error; + that.fs.symlink('/myfilelink1', '/myfilelink2', function(error) { + if(error) throw error; + + that.fs.symlink('/myfilelink2', '/myfilelink3', function(error) { + if(error) throw error; + + that.fs.symlink('/myfilelink3', '/myfilelink4', function(error) { + if(error) throw error; + + that.fs.symlink('/myfilelink4', '/myfilelink5', function(error) { + if(error) throw error; + + that.fs.symlink('/myfilelink5', '/myfilelink6', function(error) { + if(error) throw error; + + that.fs.symlink('/myfilelink6', '/myfilelink7', function(error) { + if(error) throw error; + + that.fs.symlink('/myfilelink7', '/myfilelink8', function(error) { + if(error) throw error; + + that.fs.symlink('/myfilelink8', '/myfilelink9', function(error) { + if(error) throw error; + + that.fs.symlink('/myfilelink9', '/myfilelink10', function(error) { + if(error) throw error; + + that.fs.symlink('/myfilelink10', '/myfilelink11', function(error) { + if(error) throw error; + + that.fs.stat('/myfilelink11', function(error, result) { + _error = error; + _result = result; + complete = true; + }); + }); + }); + }); + }); + }); + }); + }); + }); + }); + }); + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_result).not.toBeDefined(); + expect(_error).toBeDefined(); + }); + }); + + it('should follow a symbolic link in the path to a file', function() { + var complete = false; + var _error, _node, _result; + var that = this; + + that.fs.open('/myfile', 'w', function(error, result) { + if(error) throw error; + var fd = result; + that.fs.close(fd, function(error) { + if(error) throw error; + that.fs.stat('/myfile', function(error, result) { + if(error) throw error; + + _node = result['node']; + that.fs.symlink('/', '/mydirlink', function(error) { + if(error) throw error; + + that.fs.stat('/mydirlink/myfile', function(error, result) { + _error = error; + _result = result; + complete = true; + }); + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_result).toBeDefined(); + expect(_node).toBeDefined(); + expect(_error).toEqual(null); + expect(_result['node']).toEqual(_node); + }); + }); + + it('should error if a symbolic link in the path to a file is itself a file', function() { + var complete = false; + var _error, _result; + var that = this; + + that.fs.open('/myfile', 'w', function(error, result) { + if(error) throw error; + var fd = result; + that.fs.close(fd, function(error) { + if(error) throw error; + that.fs.stat('/myfile', function(error, result) { + if(error) throw error; + + that.fs.open('/myfile2', 'w', function(error, result) { + if(error) throw error; + var fd = result; + that.fs.close(fd, function(error) { + if(error) throw error; + that.fs.symlink('/myfile2', '/mynotdirlink', function(error) { + if(error) throw error; + + that.fs.stat('/mynotdirlink/myfile', function(error, result) { + _error = error; + _result = result; + complete = true; + }); + }); + }); + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_result).not.toBeDefined(); + }); + }); + }); + +}); diff --git a/tests/spec/providers.indexeddb.spec.js b/tests/spec/providers.indexeddb.spec.js deleted file mode 100644 index 8f904d5..0000000 --- a/tests/spec/providers.indexeddb.spec.js +++ /dev/null @@ -1,188 +0,0 @@ -describe("IDBFS.FileSystem.providers.IndexedDB", function() { - it("is supported -- if it isn't, none of these tests can run.", function() { - expect(IDBFS.FileSystem.providers.IndexedDB.isSupported()).toEqual(true); - }); - - it("has open, getReadOnlyContext, and getReadWriteContext instance methods", function() { - var indexedDBProvider = new IDBFS.FileSystem.providers.IndexedDB(); - expect(typeof indexedDBProvider.open).toEqual('function'); - expect(typeof indexedDBProvider.getReadOnlyContext).toEqual('function'); - expect(typeof indexedDBProvider.getReadWriteContext).toEqual('function'); - }); - - describe("open an IndexedDB provider", function() { - beforeEach(function() { - this.db_name = mk_db_name(); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - }); - - it("should open a new IndexedDB database", function() { - var complete = false; - var _error, _result; - - var provider = new IDBFS.FileSystem.providers.IndexedDB(this.db_name); - provider.open(function(err, firstAccess) { - _error = err; - _result = firstAccess; - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(true); - }); - }); - }); - - describe("Read/Write operations on an IndexedDB provider", function() { - beforeEach(function() { - this.db_name = mk_db_name(); - }); - - afterEach(function() { - indexedDB.deleteDatabase(this.db_name); - }); - - it("should allow put() and get()", function() { - var complete = false; - var _error, _result; - - var provider = new IDBFS.FileSystem.providers.IndexedDB(this.db_name); - provider.open(function(err, firstAccess) { - _error = err; - - var context = provider.getReadWriteContext(); - context.put("key", "value", function(err, result) { - _error = _error || err; - context.get("key", function(err, result) { - _error = _error || err; - _result = result; - - complete = true; - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual("value"); - }); - }); - - it("should allow delete()", function() { - var complete = false; - var _error, _result; - - var provider = new IDBFS.FileSystem.providers.IndexedDB(this.db_name); - provider.open(function(err, firstAccess) { - _error = err; - - var context = provider.getReadWriteContext(); - context.put("key", "value", function(err, result) { - _error = _error || err; - context.delete("key", function(err, result) { - _error = _error || err; - context.get("key", function(err, result) { - _error = _error || err; - _result = result; - - complete = true; - }); - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(null); - }); - }); - - it("should allow clear()", function() { - var complete = false; - var _error, _result1, _result2; - - var provider = new IDBFS.FileSystem.providers.IndexedDB(this.db_name); - provider.open(function(err, firstAccess) { - _error = err; - - var context = provider.getReadWriteContext(); - context.put("key1", "value1", function(err, result) { - _error = _error || err; - context.put("key2", "value2", function(err, result) { - _error = _error || err; - - context.clear(function(err) { - _error = _error || err; - - context.get("key1", function(err, result) { - _error = _error || err; - _result1 = result; - - context.get("key2", function(err, result) { - _error = _error || err; - _result2 = result; - - complete = true; - }); - }); - }); - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result1).toEqual(null); - expect(_result2).toEqual(null); - }); - }); - - it("should fail when trying to write on ReadOnlyContext", function() { - var complete = false; - var _error, _result; - - var provider = new IDBFS.FileSystem.providers.IndexedDB(this.db_name); - provider.open(function(err, firstAccess) { - _error = err; - - var context = provider.getReadOnlyContext(); - context.put("key1", "value1", function(err, result) { - _error = _error || err; - _result = result; - - complete = true; - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - expect(_result).toEqual(null); - }); - }); - }); - -}); diff --git a/tests/spec/providers.memory.spec.js b/tests/spec/providers.memory.spec.js deleted file mode 100644 index abfd8bc..0000000 --- a/tests/spec/providers.memory.spec.js +++ /dev/null @@ -1,172 +0,0 @@ -describe("IDBFS.FileSystem.providers.Memory", function() { - it("is supported -- if it isn't, none of these tests can run.", function() { - expect(IDBFS.FileSystem.providers.Memory.isSupported()).toEqual(true); - }); - - it("has open, getReadOnlyContext, and getReadWriteContext instance methods", function() { - var indexedDBProvider = new IDBFS.FileSystem.providers.Memory(); - expect(typeof indexedDBProvider.open).toEqual('function'); - expect(typeof indexedDBProvider.getReadOnlyContext).toEqual('function'); - expect(typeof indexedDBProvider.getReadWriteContext).toEqual('function'); - }); - - describe("open an Memory provider", function() { - it("should open a new Memory database", function() { - var complete = false; - var _error, _result; - - var provider = new IDBFS.FileSystem.providers.Memory(this.db_name); - provider.open(function(err, firstAccess) { - _error = err; - _result = firstAccess; - complete = true; - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(true); - }); - }); - }); - - describe("Read/Write operations on an Memory provider", function() { - it("should allow put() and get()", function() { - var complete = false; - var _error, _result; - - var provider = new IDBFS.FileSystem.providers.Memory(this.db_name); - provider.open(function(err, firstAccess) { - _error = err; - - var context = provider.getReadWriteContext(); - context.put("key", "value", function(err, result) { - _error = _error || err; - context.get("key", function(err, result) { - _error = _error || err; - _result = result; - - complete = true; - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual("value"); - }); - }); - - it("should allow delete()", function() { - var complete = false; - var _error, _result; - - var provider = new IDBFS.FileSystem.providers.Memory(this.db_name); - provider.open(function(err, firstAccess) { - _error = err; - - var context = provider.getReadWriteContext(); - context.put("key", "value", function(err, result) { - _error = _error || err; - context.delete("key", function(err, result) { - _error = _error || err; - context.get("key", function(err, result) { - _error = _error || err; - _result = result; - - complete = true; - }); - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(null); - }); - }); - - it("should allow clear()", function() { - var complete = false; - var _error, _result1, _result2; - - var provider = new IDBFS.FileSystem.providers.Memory(this.db_name); - provider.open(function(err, firstAccess) { - _error = err; - - var context = provider.getReadWriteContext(); - context.put("key1", "value1", function(err, result) { - _error = _error || err; - context.put("key2", "value2", function(err, result) { - _error = _error || err; - - context.clear(function(err) { - _error = _error || err; - - context.get("key1", function(err, result) { - _error = _error || err; - _result1 = result; - - context.get("key2", function(err, result) { - _error = _error || err; - _result2 = result; - - complete = true; - }); - }); - }); - }); - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result1).toEqual(null); - expect(_result2).toEqual(null); - }); - }); - - it("should fail when trying to write on ReadOnlyContext", function() { - var complete = false; - var _error, _result; - - var provider = new IDBFS.FileSystem.providers.Memory(this.db_name); - provider.open(function(err, firstAccess) { - _error = err; - - var context = provider.getReadOnlyContext(); - context.put("key1", "value1", function(err, result) { - _error = _error || err; - _result = result; - - complete = true; - }); - }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toBeDefined(); - expect(_result).toEqual(null); - }); - }); - }); - -}); diff --git a/tests/spec/providers.spec.js b/tests/spec/providers.spec.js deleted file mode 100644 index 759d5db..0000000 --- a/tests/spec/providers.spec.js +++ /dev/null @@ -1,17 +0,0 @@ -describe("IDBFS.Providers", function() { - it("is defined", function() { - expect(typeof IDBFS.FileSystem.providers).not.toEqual(undefined); - }); - - it("has IndexedDB constructor", function() { - expect(typeof IDBFS.FileSystem.providers.IndexedDB).toEqual('function'); - }); - - it("has Memory constructor", function() { - expect(typeof IDBFS.FileSystem.providers.Memory).toEqual('function'); - }); - - it("has a Default constructor", function() { - expect(typeof IDBFS.FileSystem.providers.Default).toEqual('function'); - }); -}); diff --git a/tests/spec/providers/providers.indexeddb.spec.js b/tests/spec/providers/providers.indexeddb.spec.js new file mode 100644 index 0000000..2f02d69 --- /dev/null +++ b/tests/spec/providers/providers.indexeddb.spec.js @@ -0,0 +1,192 @@ +define(["IDBFS"], function(IDBFS) { + + describe("IDBFS.FileSystem.providers.IndexedDB", function() { + it("is supported -- if it isn't, none of these tests can run.", function() { + expect(IDBFS.FileSystem.providers.IndexedDB.isSupported()).toEqual(true); + }); + + it("has open, getReadOnlyContext, and getReadWriteContext instance methods", function() { + var indexedDBProvider = new IDBFS.FileSystem.providers.IndexedDB(); + expect(typeof indexedDBProvider.open).toEqual('function'); + expect(typeof indexedDBProvider.getReadOnlyContext).toEqual('function'); + expect(typeof indexedDBProvider.getReadWriteContext).toEqual('function'); + }); + + describe("open an IndexedDB provider", function() { + beforeEach(function() { + this.db_name = mk_db_name(); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + }); + + it("should open a new IndexedDB database", function() { + var complete = false; + var _error, _result; + + var provider = new IDBFS.FileSystem.providers.IndexedDB(this.db_name); + provider.open(function(err, firstAccess) { + _error = err; + _result = firstAccess; + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(true); + }); + }); + }); + + describe("Read/Write operations on an IndexedDB provider", function() { + beforeEach(function() { + this.db_name = mk_db_name(); + }); + + afterEach(function() { + indexedDB.deleteDatabase(this.db_name); + }); + + it("should allow put() and get()", function() { + var complete = false; + var _error, _result; + + var provider = new IDBFS.FileSystem.providers.IndexedDB(this.db_name); + provider.open(function(err, firstAccess) { + _error = err; + + var context = provider.getReadWriteContext(); + context.put("key", "value", function(err, result) { + _error = _error || err; + context.get("key", function(err, result) { + _error = _error || err; + _result = result; + + complete = true; + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual("value"); + }); + }); + + it("should allow delete()", function() { + var complete = false; + var _error, _result; + + var provider = new IDBFS.FileSystem.providers.IndexedDB(this.db_name); + provider.open(function(err, firstAccess) { + _error = err; + + var context = provider.getReadWriteContext(); + context.put("key", "value", function(err, result) { + _error = _error || err; + context.delete("key", function(err, result) { + _error = _error || err; + context.get("key", function(err, result) { + _error = _error || err; + _result = result; + + complete = true; + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(null); + }); + }); + + it("should allow clear()", function() { + var complete = false; + var _error, _result1, _result2; + + var provider = new IDBFS.FileSystem.providers.IndexedDB(this.db_name); + provider.open(function(err, firstAccess) { + _error = err; + + var context = provider.getReadWriteContext(); + context.put("key1", "value1", function(err, result) { + _error = _error || err; + context.put("key2", "value2", function(err, result) { + _error = _error || err; + + context.clear(function(err) { + _error = _error || err; + + context.get("key1", function(err, result) { + _error = _error || err; + _result1 = result; + + context.get("key2", function(err, result) { + _error = _error || err; + _result2 = result; + + complete = true; + }); + }); + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result1).toEqual(null); + expect(_result2).toEqual(null); + }); + }); + + it("should fail when trying to write on ReadOnlyContext", function() { + var complete = false; + var _error, _result; + + var provider = new IDBFS.FileSystem.providers.IndexedDB(this.db_name); + provider.open(function(err, firstAccess) { + _error = err; + + var context = provider.getReadOnlyContext(); + context.put("key1", "value1", function(err, result) { + _error = _error || err; + _result = result; + + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_result).toEqual(null); + }); + }); + }); + + }); + +}); diff --git a/tests/spec/providers/providers.memory.spec.js b/tests/spec/providers/providers.memory.spec.js new file mode 100644 index 0000000..31a4d68 --- /dev/null +++ b/tests/spec/providers/providers.memory.spec.js @@ -0,0 +1,175 @@ +define(["IDBFS"], function(IDBFS) { + + describe("IDBFS.FileSystem.providers.Memory", function() { + it("is supported -- if it isn't, none of these tests can run.", function() { + expect(IDBFS.FileSystem.providers.Memory.isSupported()).toEqual(true); + }); + + it("has open, getReadOnlyContext, and getReadWriteContext instance methods", function() { + var indexedDBProvider = new IDBFS.FileSystem.providers.Memory(); + expect(typeof indexedDBProvider.open).toEqual('function'); + expect(typeof indexedDBProvider.getReadOnlyContext).toEqual('function'); + expect(typeof indexedDBProvider.getReadWriteContext).toEqual('function'); + }); + + describe("open an Memory provider", function() { + it("should open a new Memory database", function() { + var complete = false; + var _error, _result; + + var provider = new IDBFS.FileSystem.providers.Memory(this.db_name); + provider.open(function(err, firstAccess) { + _error = err; + _result = firstAccess; + complete = true; + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(true); + }); + }); + }); + + describe("Read/Write operations on an Memory provider", function() { + it("should allow put() and get()", function() { + var complete = false; + var _error, _result; + + var provider = new IDBFS.FileSystem.providers.Memory(this.db_name); + provider.open(function(err, firstAccess) { + _error = err; + + var context = provider.getReadWriteContext(); + context.put("key", "value", function(err, result) { + _error = _error || err; + context.get("key", function(err, result) { + _error = _error || err; + _result = result; + + complete = true; + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual("value"); + }); + }); + + it("should allow delete()", function() { + var complete = false; + var _error, _result; + + var provider = new IDBFS.FileSystem.providers.Memory(this.db_name); + provider.open(function(err, firstAccess) { + _error = err; + + var context = provider.getReadWriteContext(); + context.put("key", "value", function(err, result) { + _error = _error || err; + context.delete("key", function(err, result) { + _error = _error || err; + context.get("key", function(err, result) { + _error = _error || err; + _result = result; + + complete = true; + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result).toEqual(null); + }); + }); + + it("should allow clear()", function() { + var complete = false; + var _error, _result1, _result2; + + var provider = new IDBFS.FileSystem.providers.Memory(this.db_name); + provider.open(function(err, firstAccess) { + _error = err; + + var context = provider.getReadWriteContext(); + context.put("key1", "value1", function(err, result) { + _error = _error || err; + context.put("key2", "value2", function(err, result) { + _error = _error || err; + + context.clear(function(err) { + _error = _error || err; + + context.get("key1", function(err, result) { + _error = _error || err; + _result1 = result; + + context.get("key2", function(err, result) { + _error = _error || err; + _result2 = result; + + complete = true; + }); + }); + }); + }); + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toEqual(null); + expect(_result1).toEqual(null); + expect(_result2).toEqual(null); + }); + }); + + it("should fail when trying to write on ReadOnlyContext", function() { + var complete = false; + var _error, _result; + + var provider = new IDBFS.FileSystem.providers.Memory(this.db_name); + provider.open(function(err, firstAccess) { + _error = err; + + var context = provider.getReadOnlyContext(); + context.put("key1", "value1", function(err, result) { + _error = _error || err; + _result = result; + + complete = true; + }); + }); + + waitsFor(function() { + return complete; + }, 'test to complete', DEFAULT_TIMEOUT); + + runs(function() { + expect(_error).toBeDefined(); + expect(_result).toEqual(null); + }); + }); + }); + }); + +}); diff --git a/tests/spec/providers/providers.spec.js b/tests/spec/providers/providers.spec.js new file mode 100644 index 0000000..d2dcc50 --- /dev/null +++ b/tests/spec/providers/providers.spec.js @@ -0,0 +1,19 @@ +define(["IDBFS"], function(IDBFS) { + describe("IDBFS.Providers", function() { + it("is defined", function() { + expect(typeof IDBFS.FileSystem.providers).not.toEqual(undefined); + }); + + it("has IndexedDB constructor", function() { + expect(typeof IDBFS.FileSystem.providers.IndexedDB).toEqual('function'); + }); + + it("has Memory constructor", function() { + expect(typeof IDBFS.FileSystem.providers.Memory).toEqual('function'); + }); + + it("has a Default constructor", function() { + expect(typeof IDBFS.FileSystem.providers.Default).toEqual('function'); + }); + }); +}); diff --git a/tests/test-manifest.js b/tests/test-manifest.js new file mode 100644 index 0000000..880bf9a --- /dev/null +++ b/tests/test-manifest.js @@ -0,0 +1,40 @@ +define([ + + /** + * Add your test spec files to the list in order to + * get them running by default. + */ + + // IDBFS + "spec/idbfs.spec", + + // IDBFS.FileSystem.* + "spec/fs.spec", + "spec/fs.stat.spec", + "spec/fs.lstat.spec", + "spec/fs.mkdir.spec", + "spec/fs.readdir.spec", + "spec/fs.rmdir.spec", + "spec/fs.open.spec", + "spec/fs.write.spec", + "spec/fs.writeFile-readFile.spec", + "spec/fs.read.spec", + "spec/fs.close.spec", + "spec/fs.link.spec", + "spec/fs.unlink.spec", + "spec/fs.rename.spec", + "spec/fs.lseek.spec", + "spec/fs.symlink.spec", + "spec/fs.readlink.spec", + "spec/path-resolution.spec", + + // IDBFS.FileSystem.providers.* + "spec/providers/providers.spec", + "spec/providers/providers.memory.spec", + "spec/providers/providers.indexeddb.spec", + + // Ported node.js tests (filenames match names in https://github.com/joyent/node/tree/master/test) + "spec/node-js/simple/test-fs-mkdir", + "spec/node-js/simple/test-fs-null-bytes" + +]);