Skip to content

Commit

Permalink
Style - formatting, make jshint happy
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtajina committed Oct 1, 2012
1 parent f46e991 commit 9258eaa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
33 changes: 24 additions & 9 deletions lib/fs.js
Expand Up @@ -10,31 +10,39 @@ var predictableNextTick = require('./util.js').predictableNextTick;
*/ */
var Stats = function(isFile, mtime) { var Stats = function(isFile, mtime) {
this.mtime = mtime; this.mtime = mtime;

this.isDirectory = function() { this.isDirectory = function() {
return !isFile; return !isFile;
}; };

this.isFile = function() { this.isFile = function() {
return isFile; return isFile;
}; };
}; };



var File = function(mtime, content) { var File = function(mtime, content) {
this.mtime = mtime; this.mtime = mtime;
this.content = content || ''; this.content = content || '';

this.getStats = function() { this.getStats = function() {
return new Stats(true, new Date(this.mtime)); return new Stats(true, new Date(this.mtime));
}; };

this.getBuffer = function() { this.getBuffer = function() {
return new Buffer(this.content); return new Buffer(this.content);
}; };
}; };



var Directory = function(mtime) { var Directory = function(mtime) {
this.mtime = mtime; this.mtime = mtime;

this.getStats = function() { this.getStats = function() {
return new Stats(false, new Date(this.mtime)); return new Stats(false, new Date(this.mtime));
}; };
} };



/** /**
* @constructor * @constructor
Expand All @@ -56,12 +64,14 @@ var Mock = function(structure) {
return parts.length ? null : pointer; return parts.length ? null : pointer;
}; };



var validatePath = function(path) { var validatePath = function(path) {
if (path.charAt(0) !== '/') { if (path.charAt(0) !== '/') {
throw new Error('Relative path not supported !'); throw new Error('Relative path not supported !');
} }
}; };



// public API // public API
this.stat = function(path, callback) { this.stat = function(path, callback) {
var statSync = this.statSync; var statSync = this.statSync;
Expand All @@ -80,6 +90,7 @@ var Mock = function(structure) {
}); });
}; };



this.statSync = function(path) { this.statSync = function(path) {
validatePath(path); validatePath(path);


Expand All @@ -97,6 +108,7 @@ var Mock = function(structure) {
return pointer instanceof File ? pointer.getStats() : new Stats(typeof pointer !== 'object'); return pointer instanceof File ? pointer.getStats() : new Stats(typeof pointer !== 'object');
}; };



this.readdir = function(path, callback) { this.readdir = function(path, callback) {
validatePath(path); validatePath(path);
predictableNextTick(function() { predictableNextTick(function() {
Expand All @@ -106,34 +118,33 @@ var Mock = function(structure) {
}); });
}; };



this.readdirSync = function(path, callback) { this.readdirSync = function(path, callback) {
validatePath(path); validatePath(path);


var pointer = getPointer(path, structure); var pointer = getPointer(path, structure);
var error;


if (!pointer) { if (!pointer) {
var error = new Error('ENOENT, no such file or directory "' + path + '"'); error = new Error('ENOENT, no such file or directory "' + path + '"');

error.errno = 34; error.errno = 34;
error.code = 'ENOENT'; error.code = 'ENOENT';


throw error; throw error;
} }


if(pointer instanceof File) { if(pointer instanceof File) {
var error = new Error('ENOTDIR, not a directory "' + path + '"'); error = new Error('ENOTDIR, not a directory "' + path + '"');

error.errno = 27; error.errno = 27;
error.code = 'ENOTDIR'; error.code = 'ENOTDIR';


throw error; throw error;
} }


return Object.keys(pointer); return Object.keys(pointer);


}; };



this.mkdir = function(directory, callback) { this.mkdir = function(directory, callback) {
var mkdirSync = this.mkdirSync; var mkdirSync = this.mkdirSync;
var error = null; var error = null;
Expand All @@ -149,6 +160,7 @@ var Mock = function(structure) {
}); });
}; };



this.mkdirSync = function(directory) { this.mkdirSync = function(directory) {
var pointer = getPointer(path.dirname(directory), structure); var pointer = getPointer(path.dirname(directory), structure);
var baseName = path.basename(directory); var baseName = path.basename(directory);
Expand All @@ -165,6 +177,7 @@ var Mock = function(structure) {
throw error; throw error;
}; };



this.readFile = function(path, encoding, callback) { this.readFile = function(path, encoding, callback) {
var readFileSync = this.readFileSync; var readFileSync = this.readFileSync;
callback = callback || encoding; callback = callback || encoding;
Expand All @@ -183,6 +196,7 @@ var Mock = function(structure) {
}); });
}; };



this.readFileSync = function(path) { this.readFileSync = function(path) {
var pointer = getPointer(path, structure); var pointer = getPointer(path, structure);
var error; var error;
Expand All @@ -208,6 +222,7 @@ var Mock = function(structure) {
return new Buffer(''); return new Buffer('');
}; };



this.writeFile = function(filePath, content, callback) { this.writeFile = function(filePath, content, callback) {
predictableNextTick(function() { predictableNextTick(function() {
var pointer = getPointer(path.dirname(filePath), structure); var pointer = getPointer(path.dirname(filePath), structure);
Expand All @@ -224,12 +239,14 @@ var Mock = function(structure) {
}); });
}; };



this.watchFile = function(path, options, callback) { this.watchFile = function(path, options, callback) {
callback = callback || options; callback = callback || options;
watchers[path] = watchers[path] || []; watchers[path] = watchers[path] || [];
watchers[path].push(callback); watchers[path].push(callback);
}; };



// Mock API // Mock API
this._touchFile = function(path, mtime, content) { this._touchFile = function(path, mtime, content) {
var pointer = getPointer(path, structure); var pointer = getPointer(path, structure);
Expand All @@ -244,8 +261,6 @@ var Mock = function(structure) {
callback(current, previous); callback(current, previous);
}); });
}; };


}; };




Expand Down
2 changes: 1 addition & 1 deletion test/fs.spec.coffee
Expand Up @@ -135,6 +135,7 @@ describe 'fs', ->
fs.readdir '/home/not', callback fs.readdir '/home/not', callback
waitForFinished() waitForFinished()



# =========================================================================== # ===========================================================================
# fs.readdirSync # fs.readdirSync
# =========================================================================== # ===========================================================================
Expand All @@ -154,7 +155,6 @@ describe 'fs', ->
it 'should throw when reading a file', -> it 'should throw when reading a file', ->
expect(-> fs.readdirSync '/home/vojta/some.js'). expect(-> fs.readdirSync '/home/vojta/some.js').
toThrow 'ENOTDIR, not a directory "/home/vojta/some.js"' toThrow 'ENOTDIR, not a directory "/home/vojta/some.js"'





# =========================================================================== # ===========================================================================
Expand Down

0 comments on commit 9258eaa

Please sign in to comment.