Skip to content

Commit

Permalink
Finish mock filesystem
Browse files Browse the repository at this point in the history
Also, ported all filesystem tests to Jasmine.
  • Loading branch information
kriskowal committed Dec 22, 2012
1 parent 8c4a800 commit fbb2177
Show file tree
Hide file tree
Showing 44 changed files with 1,246 additions and 758 deletions.
59 changes: 59 additions & 0 deletions buffer-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

var Q = require("q");
var Reader = require("./reader");

module.exports = BufferStream;
function BufferStream(chunks, charset) {
if (!(this instanceof BufferStream)) {
return new BufferStream(chunks, charset);
}
if (!chunks) {
chunks = [];
} else if (!Array.isArray(chunks)) {
chunks = [chunks];
}
this._charset = charset;
this._chunks = chunks;
this._close = Q.defer();
this.closed = this._close.promise;
}

BufferStream.prototype.forEach = function (write, thisp) {
var self = this;
var chunks = self._chunks;
return Q.fcall(function () {
chunks.splice(0, chunks.length).forEach(write, thisp);

This comment has been minimized.

Copy link
@hthetiot

hthetiot Apr 13, 2017

Collaborator

This is causing File.chunk passed to BufferStream to be cleared by reference.
Resulting in serving empty response the second time if using joey+MockFs.

I dont think "chunks.splice" is needed.
@kriskowal

});
};

BufferStream.prototype.read = function () {
var result;
result = Reader.join(this._chunks);
if (this._charset) {
result = result.toString(this._charset);
}
return Q.resolve(result);
};

BufferStream.prototype.write = function (chunk) {
if (this._charset) {
chunk = new Buffer(String(chunk), this._charset);
} else {
if (!(chunk instanceof Buffer)) {
throw new Error("Can't write strings to buffer stream without a charset: " + chunk);
}
}
this._chunks.push(chunk);
return Q.resolve();
};

BufferStream.prototype.close = function () {
this._close.resolve();
return Q.resolve();
};

BufferStream.prototype.destroy = function () {
this._close.resolve();
return Q.resolve();
};

2 changes: 1 addition & 1 deletion fs-boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ exports.split = function (path) {
* @returns {String} path
*/
exports.join = function () {
if (arguments.length === 1 && typeof arguments[0] === "object")
if (arguments.length === 1 && Array.isArray(arguments[0]))
return exports.normal.apply(exports, arguments[0]);
return exports.normal.apply(exports, arguments);
};
Expand Down
76 changes: 35 additions & 41 deletions fs-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var MockFs = require("./fs-mock");
// TODO patternToRegExp
// TODO glob
// TODO match
// TODO copyTree

var concat = function (arrays) {
return Array.prototype.concat.apply([], arrays);
Expand Down Expand Up @@ -35,12 +34,11 @@ exports.update = function (exports, workingDirectory) {
options.flags = "r" + (options.flags || "").replace(/r/g, "");
return Q.when(this.open(path, options), function (stream) {
return stream.read();
}, function (reason) {
var error = new Error("Can't read " + path + " because " + reason.message);
}, function (error) {
error.message = "Can't read " + path + " because " + error.message;
error.path = path;
error.flags = flags;
error.charset = charset;
error.cause = reason;
throw error;
});
};
Expand Down Expand Up @@ -105,22 +103,22 @@ exports.update = function (exports, workingDirectory) {

exports.copyTree = function (source, target) {
var self = this;
return Q.when(exports.stat(source), function (stat) {
return Q.when(self.stat(source), function (stat) {
if (stat.isFile()) {
return exports.copy(source, target);
return self.copy(source, target);
} else if (stat.isDirectory()) {
return Q.when(exports.makeDirectory(target), function () {
return Q.when(exports.list(source), function (list) {
return Q.when(self.makeDirectory(target), function () {
return Q.when(self.list(source), function (list) {
return Q.all(list.map(function (child) {
return exports.copyTree(
exports.join(source, child),
exports.join(target, child)
return self.copyTree(
self.join(source, child),
self.join(target, child)
);
}));
});
});
} else if (stat.isSymbolicLink()) {
return exports.symbolicCopy(source, target);
return self.symbolicCopy(source, target);
}
});
};
Expand Down Expand Up @@ -214,6 +212,13 @@ exports.update = function (exports, workingDirectory) {
});
};

exports.symbolicCopy = function (source, target) {
var self = this;
return Q.when(self.relative(target, source), function (relative) {
return self.symbolicLink(target, relative, "file");
});
};

exports.exists = function (path) {
return Q.when(this.stat(path), function () {
return true;
Expand All @@ -238,6 +243,24 @@ exports.update = function (exports, workingDirectory) {
});
};

exports.isSymbolicLink = function (path) {
return Q.when(this.statLink(path), function (stat) {
return stat.isSymbolicLink();
}, function (reason) {
return false;
});
};

exports.lastModified = function (path) {
var self = this;
return self.stat(path).invoke('lastModified');
};

exports.lastAccessed = function (path) {
var self = this;
return self.stat(path).invoke('lastAccessed');
};

exports.absolute = function (path) {
if (this.isAbsolute(path))
return path;
Expand Down Expand Up @@ -377,34 +400,5 @@ exports.update = function (exports, workingDirectory) {
});
};

var Stats = exports.Stats = function (nodeStat) {
this.node = nodeStat;
this.size = nodeStat.size;
};

var stats = [
"isDirectory",
"isFile",
"isBlockDevice",
"isCharacterDevice",
"isSymbolicLink",
"isFIFO",
"isSocket"
];

stats.forEach(function (name) {
Stats.prototype[name] = function () {
return this.node[name]();
};
});

Stats.prototype.lastModified = function () {
return new Date(this.node.mtime);
};

Stats.prototype.lastAccessed = function () {
return new Date(this.node.atime);
};

}

Loading

0 comments on commit fbb2177

Please sign in to comment.