Permalink
Browse files

Finish mock filesystem

Also, ported all filesystem tests to Jasmine.
  • Loading branch information...
1 parent 8c4a800 commit fbb217765182b99180909923c197ded50ba424a6 @kriskowal committed Dec 22, 2012
Showing with 1,246 additions and 758 deletions.
  1. +59 −0 buffer-stream.js
  2. +1 −1 fs-boot.js
  3. +35 −41 fs-common.js
  4. +425 −139 fs-mock.js
  5. +1 −1 fs-root.js
  6. +32 −15 fs.js
  7. +0 −5 reader.js
  8. +9 −8 test/fs/boot-directory.js → spec/fs/boot-directory-spec.js
  9. +33 −0 spec/fs/issues/1-spec.js
  10. +56 −0 spec/fs/mock/copy-tree-spec.js
  11. 0 {test → spec}/fs/mock/dummy/hello.txt
  12. 0 {test/fs → spec/fs/mock}/fixtures/1234.txt
  13. +69 −0 spec/fs/mock/link-spec.js
  14. +48 −0 spec/fs/mock/make-tree-spec.js
  15. +66 −0 spec/fs/mock/merge-spec.js
  16. +59 −0 spec/fs/mock/move-spec.js
  17. +20 −0 spec/fs/mock/object-spec.js
  18. +26 −0 spec/fs/mock/range-spec.js
  19. +23 −0 spec/fs/mock/read-spec.js
  20. +37 −0 spec/fs/mock/remove-directory-spec.js
  21. +38 −0 spec/fs/mock/remove-tree-spec.js
  22. +32 −0 spec/fs/mock/root-spec.js
  23. +85 −0 spec/fs/mock/symbolic-link-spec.js
  24. +26 −0 spec/fs/mock/write-spec.js
  25. +23 −0 spec/fs/range-spec.js
  26. +1 −0 spec/fs/range-spec.txt
  27. +42 −0 spec/lib/jasmine-promise.js
  28. +0 −2 test/all.js
  29. +0 −17 test/fs/all.js
  30. +0 −25 test/fs/issues/1.js
  31. +0 −32 test/fs/lazy.js
  32. +0 −35 test/fs/mock/copy-tree.js
  33. +0 −34 test/fs/mock/make-tree.js
  34. +0 −51 test/fs/mock/merge.js
  35. +0 −37 test/fs/mock/move.js
  36. +0 −28 test/fs/mock/object.js
  37. +0 −35 test/fs/mock/read.js
  38. +0 −34 test/fs/mock/remove-directory.js
  39. +0 −34 test/fs/mock/remove-tree.js
  40. +0 −47 test/fs/mock/subtree.js
  41. +0 −35 test/fs/mock/write.js
  42. +0 −25 test/fs/partial.js
  43. +0 −52 test/fs/root.js
  44. +0 −25 test/issues/1.js
View
@@ -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);
@hthetiot

hthetiot Apr 13, 2017 edited

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();
+};
+
View
@@ -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);
};
View
@@ -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);
@@ -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;
});
};
@@ -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);
}
});
};
@@ -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;
@@ -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;
@@ -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);
- };
-
}
Oops, something went wrong.

0 comments on commit fbb2177

Please sign in to comment.