Skip to content

Commit

Permalink
Merge pull request #4 from izaakschroeder/exists
Browse files Browse the repository at this point in the history
Add an `exists` and `existsSync` method.
  • Loading branch information
sokra committed Jun 29, 2015
2 parents 2d307df + 71d9401 commit a4d5b02
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
21 changes: 17 additions & 4 deletions lib/MemoryFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,24 @@ function pathToArray(path) {
function trueFn() { return true; }
function falseFn() { return false; }

MemoryFileSystem.prototype.statSync = function(_path) {
MemoryFileSystem.prototype.meta = function(_path) {
var path = pathToArray(_path);
var current = this.data;
for(var i = 0; i < path.length - 1; i++) {
if(!isDir(current[path[i]]))
throw new Error("Path doesn't exist '" + _path + "'");
return null;
current = current[path[i]];
}
if(_path === "/" || isDir(current[path[i]])) {
return current[path[i]];
}

MemoryFileSystem.prototype.existsSync = function(_path) {
return !!this.meta(_path);
}

MemoryFileSystem.prototype.statSync = function(_path) {
var current = this.meta(_path);
if(_path === "/" || isDir(current)) {
return {
isFile: falseFn,
isDirectory: trueFn,
Expand All @@ -57,7 +66,7 @@ MemoryFileSystem.prototype.statSync = function(_path) {
isFIFO: falseFn,
isSocket: falseFn
};
} else if(isFile(current[path[i]])) {
} else if(isFile(current)) {
return {
isFile: trueFn,
isDirectory: falseFn,
Expand Down Expand Up @@ -198,6 +207,10 @@ MemoryFileSystem.prototype.normalize = normalize;
};
});

MemoryFileSystem.prototype.exists = function(path, callback) {
return callback(this.existsSync(path));
}

MemoryFileSystem.prototype.readFile = function(path, optArg, callback) {
if(!callback) {
callback = optArg;
Expand Down
11 changes: 9 additions & 2 deletions test/MemoryFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ describe("directory", function() {
fs.readdirSync("//test").should.be.eql(["sub2"]);
fs.rmdirSync("/test/sub2");
fs.rmdirSync("/test");
fs.existsSync("/test").should.be.eql(false);
(function() {
fs.readdirSync("/test");
}).should.throw();
fs.readdirSync("/").should.be.eql(["root\\dir"]);
fs.mkdirpSync("/a/depth/sub/dir");
fs.existsSync("/a/depth/sub").should.be.eql(true);
var stat = fs.statSync("/a/depth/sub");
stat.isFile().should.be.eql(false);
stat.isDirectory().should.be.eql(true);
Expand All @@ -50,11 +52,13 @@ describe("directory", function() {
fs.readdirSync("C:\\\\test").should.be.eql(["sub2"]);
fs.rmdirSync("C:\\test\\sub2");
fs.rmdirSync("C:\\test");
fs.existsSync("C:\\test").should.be.eql(false);
(function() {
fs.readdirSync("C:\\test");
}).should.throw();
fs.readdirSync("C:").should.be.eql(["root-dir"]);
fs.mkdirpSync("D:\\a\\depth\\sub\\dir");
fs.existsSync("D:\\a\\depth\\sub").should.be.eql(true);
var stat = fs.statSync("D:\\a\\depth\\sub");
stat.isFile().should.be.eql(false);
stat.isDirectory().should.be.eql(true);
Expand Down Expand Up @@ -206,7 +210,10 @@ describe("async", function() {
fs.readFile("/test/dir/b", function(err, content) {
if(err) throw err;
content.should.be.eql(new Buffer("World"));
done();
fs.exists("/test/dir/b", function(exists) {
exists.should.be.eql(true);
done();
});
});
});
});
Expand Down Expand Up @@ -359,4 +366,4 @@ describe("os", function() {
fileSystem.statSync("C:\\a/dir\\index").isFile().should.be.eql(true);
});
});
});
});

0 comments on commit a4d5b02

Please sign in to comment.