Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions src/NativeFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ NativeFileSystem.Entry = function( fullPath, isDirectory) {



/** class: FileEntry
/**
* This interface represents a file on a file system.
*
* @param {string} name
* @constructor
Expand All @@ -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
Expand All @@ -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;
});
};

Expand Down Expand Up @@ -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;
};
Expand All @@ -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
Expand Down Expand Up @@ -307,8 +327,7 @@ NativeFileSystem.FileSaver.prototype.abort = function() {
return err;
};

/** file
*
/**
* Obtains the File objecte for a FileEntry object
*
* @param {function} successCallback
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions test/spec/FileCommandHandlers-test.js
Original file line number Diff line number Diff line change
@@ -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() {

Expand Down Expand Up @@ -207,4 +208,4 @@ describe("FileCommandHandlers", function() {

// TODO (jasonsj): experiment with mocks instead of real UI
});
} // })();
}
1 change: 1 addition & 0 deletions test/spec/NativeFileSystem-test-files/cant_read_here.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cant_read_here
103 changes: 58 additions & 45 deletions test/spec/NativeFileSystem-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 );
Expand Down Expand Up @@ -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;
Expand All @@ -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 );
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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 );
Expand All @@ -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");
});
Expand Down
7 changes: 6 additions & 1 deletion test/spec/ProjectManager-test.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -137,4 +140,6 @@ describe("ProjectManager", function() {
});
});

});
});

}