diff --git a/src/NativeFileSystem.js b/src/NativeFileSystem.js index 4cdfa63ba4c..3c4c88caf4a 100644 --- a/src/NativeFileSystem.js +++ b/src/NativeFileSystem.js @@ -123,7 +123,8 @@ NativeFileSystem.Entry = function( fullPath, isDirectory) { -/** class: FileEntry +/** + * This interface represents a file on a file system. * * @param {string} name * @constructor @@ -136,8 +137,7 @@ NativeFileSystem.FileEntry = function( name ) { }; -/** createWriter - * +/** * Creates a new FileWriter associated with the file that this FileEntry represents. * * @param {function(_FileWriter)} successCallback @@ -157,9 +157,13 @@ NativeFileSystem.FileEntry.prototype.createWriter = function( successCallback, e // initialize file length // TODO (jasonsj): handle async + var self = this; + brackets.fs.readFile( fileEntry.fullPath, "utf8", function(err, contents) { + self._err = err; + if ( contents ) - this._length = contents.length; + self._length = contents.length; }); }; @@ -231,10 +235,26 @@ NativeFileSystem.FileEntry.prototype.createWriter = function( successCallback, e _FileWriter.prototype.truncate = function( size ) { }; - successCallback( new _FileWriter() ); -}; + var fileWriter = new _FileWriter(); + if ( fileWriter._err && ( errorCallback !== undefined ) ) { + errorCallback( NativeFileSystem._nativeToFileError( fileWriter._err ) ); + } + else if ( successCallback !== undefined ) { + successCallback( fileWriter ); + } +}; +/** + * This interface extends the FileException interface described in to add + * several new error codes. Any errors that need to be reported synchronously, + * including all that occur during use of the synchronous filesystem methods, + * are reported using the FileException exception. + * + * @param {number} code The code attribute, on getting, must return one of the + * constants of the FileException exception, which must be the most appropriate + * code from the table below. + */ NativeFileSystem.FileException = function ( code ){ this.code = code || 0; }; @@ -252,12 +272,12 @@ Object.defineProperties(NativeFileSystem.FileException, , QUOTA_EXCEEDED_ERR: { value: 10, writable: false } }); -/** class: FileSaver - * This interface provides methods to monitor the asynchronous writing of - * blobs to disk using progress events and event handler attributes. +/** + * This interface provides methods to monitor the asynchronous writing of blobs + * to disk using progress events and event handler attributes. * * This interface is specified to be used within the context of the global - * object and within Web Workers. + * object (Window) and within Web Workers. * * @param {Blob} data * @constructor @@ -307,8 +327,7 @@ NativeFileSystem.FileSaver.prototype.abort = function() { return err; }; -/** file - * +/** * Obtains the File objecte for a FileEntry object * * @param {function} successCallback @@ -328,7 +347,8 @@ NativeFileSystem.FileEntry.prototype.createfileerror = function( successCallback }; */ -/** class: DirectoryEntry +/** + * This interface represents a directory on a file system. * * @constructor * @param {string} name diff --git a/test/spec/FileCommandHandlers-test.js b/test/spec/FileCommandHandlers-test.js index 9f6cd438b65..d7137eda306 100644 --- a/test/spec/FileCommandHandlers-test.js +++ b/test/spec/FileCommandHandlers-test.js @@ -1,4 +1,5 @@ -if (window.opener) { // (function(){ +// FIXME (jasonsj): these tests are ommitted when launching in the main app window +if (window.opener) { describe("FileCommandHandlers", function() { @@ -207,4 +208,4 @@ describe("FileCommandHandlers", function() { // TODO (jasonsj): experiment with mocks instead of real UI }); -} // })(); \ No newline at end of file +} \ No newline at end of file diff --git a/test/spec/NativeFileSystem-test-files/cant_read_here.txt b/test/spec/NativeFileSystem-test-files/cant_read_here.txt new file mode 100755 index 00000000000..3bb817bffca --- /dev/null +++ b/test/spec/NativeFileSystem-test-files/cant_read_here.txt @@ -0,0 +1 @@ +cant_read_here \ No newline at end of file diff --git a/test/spec/NativeFileSystem-test.js b/test/spec/NativeFileSystem-test.js index f43f242bcc2..4a729f5db1a 100644 --- a/test/spec/NativeFileSystem-test.js +++ b/test/spec/NativeFileSystem-test.js @@ -222,20 +222,41 @@ describe("NativeFileSystem", function(){ describe("Writing", function() { beforeEach( function() { - }); + var nfs = null; - afterEach( function() { - }); + runs(function() { + NativeFileSystem.requestNativeFileSystem( this.path, function( fs ) { + nfs = fs; + }); + }); + waitsFor( function() { return nfs }, 1000); - it("should create new, zero-length files", function() { - var nfs = null; + runs(function() { + this.nfs = nfs; + }); - NativeFileSystem.requestNativeFileSystem( this.path, function( fs ) { - nfs = fs; + // set read-only permissions + runs(function() { + brackets.fs.chmod(this.path + "/cant_read_here.txt", 0222, function(err) { + _err = err; + chmodDone = true; + }); }); + waitsFor( function() { return chmodDone && ( _err === brackets.fs.NO_ERROR ) }, 1000); + }); - waitsFor( function() { return nfs }, 1000); + afterEach( function() { + // restore permissions for git + runs(function() { + brackets.fs.chmod(this.path + "/cant_read_here.txt", 0777, function(err) { + _err = err; + chmodDone = true; + }); + }); + waitsFor( function() { return chmodDone && ( _err === brackets.fs.NO_ERROR ) }, 1000); + }); + it("should create new, zero-length files", function() { var fileEntry = null; var writeComplete = false; @@ -250,7 +271,7 @@ describe("NativeFileSystem", function(){ }; // FIXME (jasonsj): NativeFileSystem.root is missing - nfs.getFile("new-zero-length-file.txt", { create: true, exclusive: true }, successCallback, errorCallback ); + this.nfs.getFile("new-zero-length-file.txt", { create: true, exclusive: true }, successCallback, errorCallback ); }); waitsFor( function() { return writeComplete; }, 1000 ); @@ -286,14 +307,6 @@ describe("NativeFileSystem", function(){ }); it("should report an error when a file does not exist and create = false", function() { - var nfs = null; - - NativeFileSystem.requestNativeFileSystem( this.path, function( fs ) { - nfs = fs; - }); - - waitsFor( function() { return nfs }, 1000); - var fileEntry = null; var writeComplete = false; var error = null; @@ -310,7 +323,7 @@ describe("NativeFileSystem", function(){ }; // FIXME (jasonsj): NativeFileSystem.root is missing - nfs.getFile("does-not-exist.txt", { create: false }, successCallback, errorCallback ); + this.nfs.getFile("does-not-exist.txt", { create: false }, successCallback, errorCallback ); }); waitsFor( function() { return writeComplete; }, 1000 ); @@ -323,14 +336,6 @@ describe("NativeFileSystem", function(){ }); it("should return an error if file exists and exclusive is true", function() { - var nfs = null; - - NativeFileSystem.requestNativeFileSystem( this.path, function( fs ) { - nfs = fs; - }); - - waitsFor( function() { return nfs }, 1000); - var fileEntry = null; var writeComplete = false; var error = null; @@ -347,7 +352,7 @@ describe("NativeFileSystem", function(){ }; // FIXME (jasonsj): NativeFileSystem.root is missing - nfs.getFile("file1", { create: true, exclusive: true }, successCallback, errorCallback ); + this.nfs.getFile("file1", { create: true, exclusive: true }, successCallback, errorCallback ); }); // wait for success or error to return @@ -363,14 +368,6 @@ describe("NativeFileSystem", function(){ }); it("should return an error if the path is a directory", function() { - var nfs = null; - - NativeFileSystem.requestNativeFileSystem( this.path, function( fs ) { - nfs = fs; - }); - - waitsFor( function() { return nfs }, 1000); - var fileEntry = null; var writeComplete = false; var error = null; @@ -387,7 +384,7 @@ describe("NativeFileSystem", function(){ }; // FIXME (jasonsj): NativeFileSystem.root is missing - nfs.getFile("dir1", { create: false }, successCallback, errorCallback ); + this.nfs.getFile("dir1", { create: false }, successCallback, errorCallback ); }); // wait for success or error to return @@ -403,14 +400,6 @@ describe("NativeFileSystem", function(){ }); it("should create overwrite files with new content", function() { - var nfs = null; - - NativeFileSystem.requestNativeFileSystem( this.path, function( fs ) { - nfs = fs; - }); - - waitsFor( function() { return nfs }, 1000); - var fileEntry = null; var writeComplete = false; var error = null; @@ -435,7 +424,7 @@ describe("NativeFileSystem", function(){ writeComplete = true; }; - nfs.getFile( "file1", { create: false }, successCallback, errorCallback ); + this.nfs.getFile( "file1", { create: false }, successCallback, errorCallback ); }); waitsFor( function() { return writeComplete && fileEntry; }, 1000 ); @@ -458,6 +447,30 @@ describe("NativeFileSystem", function(){ }); }); + it("should report an error when writing to a file that cannot be read", function() { + var chmodDone = false; + var complete = false; + var error = null; + + // createWriter() should return an error for files it can't read + runs(function() { + this.nfs.getFile( "cant_read_here.txt", { create: false }, function( entry ) { + entry.createWriter(function() { + complete = true; + } + , function(err) { + error = err; + }); + }); + }); + waitsFor( function() { return complete || error; }, 1000 ); + + runs(function() { + expect(complete).toBeFalsy(); + expect(error.code).toBe(FileError.NOT_READABLE_ERR); + }); + }); + xit("should append to existing files", function() { this.fail("TODO (jasonsj): not supported for sprint 1"); }); diff --git a/test/spec/ProjectManager-test.js b/test/spec/ProjectManager-test.js index 5211c062946..950d0417696 100644 --- a/test/spec/ProjectManager-test.js +++ b/test/spec/ProjectManager-test.js @@ -1,3 +1,6 @@ +// FIXME (jasonsj): these tests are ommitted when launching in the main app window +if (window.opener) { // (function(){ + describe("ProjectManager", function() { beforeEach(function() { @@ -137,4 +140,6 @@ describe("ProjectManager", function() { }); }); -}); \ No newline at end of file +}); + +} \ No newline at end of file