Fixed Stats prototypes, created full Mocha test suite

This commit is contained in:
kwkofler 2014-03-07 18:10:02 -05:00
parent 0fb099d6c8
commit 7c14af9668
3 changed files with 150 additions and 119 deletions

View File

@ -328,7 +328,7 @@ fs.open('/myfile', 'w', function(err, fd) {
#### fs.stat(path, callback)<a name="stat"></a> #### fs.stat(path, callback)<a name="stat"></a>
Obtain file status about the file at `path`. Asynchronous [stat(2)](http://pubs.opengroup.org/onlinepubs/009695399/functions/stat.html). Obtain file status about the file at `path`. Asynchronous [stat(2)](http://pubs.opengroup.org/onlinepubs/009695399/functions/stat.html).
Callback gets `(error, stats)`, where `stats` is an object with the following properties: Callback gets `(error, stats)`, where `stats` is an object with the following properties...
``` ```
{ {
@ -343,7 +343,17 @@ Callback gets `(error, stats)`, where `stats` is an object with the following pr
} }
``` ```
If the file at `path` is a symbolik link, the file to which it links will be used instead. ...and the following methods:
isFile(): Returns true if the node is a file.
isDirectory(): Returns true if the node is a directory.
isBlockDevice(): Not implemented, returns false.
isCharacterDevice(): Not implemented, returns false.
isSymbolicLink(): Returns true if the node is a symbolic link.
isFIFO(): Not implemented, returns false.
isSocket(): Not implemented, returns false.
If the file at `path` is a symbolic link, the file to which it links will be used instead.
To get the status of a symbolic link file, use [fs.lstat()](#lstat) instead. To get the status of a symbolic link file, use [fs.lstat()](#lstat) instead.
Examples: Examples:

View File

@ -129,11 +129,11 @@ define(function(require) {
} }
Stats.prototype.isFile = function() { Stats.prototype.isFile = function() {
return this.type === constants.MODE_FILE; return this.type === MODE_FILE;
}; };
Stats.prototype.isDirectory = function() { Stats.prototype.isDirectory = function() {
return this.type === constants.MODE_DIRECTORY; return this.type === MODE_DIRECTORY;
}; };
Stats.prototype.isBlockDevice = function() { Stats.prototype.isBlockDevice = function() {
@ -145,7 +145,7 @@ define(function(require) {
}; };
Stats.prototype.isSymbolicLink = function() { Stats.prototype.isSymbolicLink = function() {
return this.type === constants.MODE_SYMBOLIC_LINK; return this.type === MODE_SYMBOLIC_LINK;
}; };
Stats.prototype.isFIFO = function() { Stats.prototype.isFIFO = function() {

View File

@ -7,52 +7,102 @@ define(["Filer", "util"], function(Filer, util) {
it('should be a function', function() { it('should be a function', function() {
var fs = util.fs(); var fs = util.fs();
expect(fs.stat.isFile).to.be.a('function'); fs.stat('/', function(error, stats) {
if(error) throw error;
expect(typeof stats.isFile).to.equal('function');
});
}); });
it('should return true if stats are for file', function(done) { it('should return true if stats are for file', function(done) {
var fs = util.fs(); var fs = util.fs();
var contents = "This is a file.";
fs.writeFile('/myFile', contents, function(error) { fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error; if(error) throw error;
fs.stat('/myFile').isFile(function (error, data) { fs.fstat(fd, function(error, stats) {
expect(error).not.to.exist; expect(stats.isFile()).to.equal(true);
expect(data).toEqual(true); done();
done() });
});
});
it('should return false if stats are for directory', function(done) {
var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error;
expect(stats.isFile()).to.equal(false);
done();
});
});
it('should return false if stats are for symbolic link', function(done) {
var fs = util.fs();
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
fs.close(fd, function(error, stats) {
if(error) throw error;
fs.symlink('/myfile', '/myfilelink', function(error) {
if(error) throw error;
fs.lstat('/myfilelink', function(error, stats) {
expect(stats.isFile()).to.equal(false);
done();
});
});
}); });
}); });
}); });
}); });
/* describe('#isDirectory()', function() { describe('#isDirectory()', function() {
beforeEach(util.setup); beforeEach(util.setup);
afterEach(util.cleanup); afterEach(util.cleanup);
it('should be a function', function() { it('should be a function', function() {
var testStat = this.fs.stat('/', function(error) { var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error; if(error) throw error;
expect(typeof stats.isDirectory).to.equal('function');
}); });
expect(typeof this.testStat.isDirectory).toEqual('function');
}); });
it('should only return true if stats are for directory', function() { it('should return false if stats are for file', function(done) {
var complete = false; var fs = util.fs();
var _result;
var that = this;
_result = that.fs.stat('/myFile', function(error) { fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error; if(error) throw error;
}).isFile(); fs.fstat(fd, function(error, stats) {
complete = true; expect(stats.isDirectory()).to.equal(false);
done();
});
});
});
waitsFor(function() { it('should return true if stats are for directory', function(done) {
return complete; var fs = util.fs();
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() { fs.stat('/', function(error, stats) {
expect(_error).toEqual(null); if(error) throw error;
expect(_result).toEqual(true); expect(stats.isDirectory()).to.equal(true);
done();
});
});
it('should return false if stats are for symbolic link', function(done) {
var fs = util.fs();
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
fs.close(fd, function(error, stats) {
if(error) throw error;
fs.symlink('/myfile', '/myfilelink', function(error) {
if(error) throw error;
fs.lstat('/myfilelink', function(error, stats) {
expect(stats.isDirectory()).to.equal(false);
done();
});
});
});
}); });
}); });
}); });
@ -62,29 +112,18 @@ define(["Filer", "util"], function(Filer, util) {
afterEach(util.cleanup); afterEach(util.cleanup);
it('should be a function', function() { it('should be a function', function() {
var testStat = this.fs.stat('/', function(error) { var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error; if(error) throw error;
expect(typeof stats.isBlockDevice).to.equal('function');
}); });
expect(typeof this.testStat.isBlockDevice).toEqual('function');
}); });
it('should return false', function() { it('should return false', function() {
var complete = false; var fs = util.fs();
var _result; fs.stat('/', function(error, stats) {
var that = this;
_result = that.fs.stat('/', function(error) {
if(error) throw error; if(error) throw error;
}).isBlockDevice(); expect(stats.isBlockDevice()).to.equal(false);
complete = true;
waitsFor(function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).toEqual(null);
expect(_result).toEqual(false);
}); });
}); });
}); });
@ -94,29 +133,18 @@ define(["Filer", "util"], function(Filer, util) {
afterEach(util.cleanup); afterEach(util.cleanup);
it('should be a function', function() { it('should be a function', function() {
var testStat = this.fs.stat('/', function(error) { var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error; if(error) throw error;
expect(typeof stats.isCharacterDevice).to.equal('function');
}); });
expect(typeof this.testStat.isCharacterDevice).toEqual('function');
}); });
it('should return false', function() { it('should return false', function() {
var complete = false; var fs = util.fs();
var _result; fs.stat('/', function(error, stats) {
var that = this;
_result = that.fs.stat('/', function(error) {
if(error) throw error; if(error) throw error;
}).isCharacterDevice(); expect(stats.isCharacterDevice()).to.equal(false);
complete = true;
waitsFor(function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).toEqual(null);
expect(_result).toEqual(false);
}); });
}); });
}); });
@ -126,35 +154,50 @@ define(["Filer", "util"], function(Filer, util) {
afterEach(util.cleanup); afterEach(util.cleanup);
it('should be a function', function() { it('should be a function', function() {
var testStat = this.fs.stat('/', function(error) { var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error; if(error) throw error;
expect(typeof stats.isSymbolicLink).to.equal('function');
}); });
expect(typeof this.testStat.isSymbolicLink).toEqual('function');
}); });
it('should return true if stats are for symbolic link', function() { it('should return false if stats are for file', function(done) {
var complete = false; var fs = util.fs();
var _error, _result;
var that = this;
that.fs.writeFile('/myfile', '', { encoding: 'utf8' }, function(error) { fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error; if(error) throw error;
that.fs.symlink('/myfile', '/myFileLink', function (error) { fs.fstat(fd, function(error, stats) {
if (error) throw error; expect(stats.isSymbolicLink()).to.equal(false);
_result = that.fs.lstat('/myfile', function(error) { done();
if(error) throw error;
}).isSymbolicLink();
complete = true;
}); });
}); });
});
waitsFor(function() { it('should return false if stats are for directory', function(done) {
return complete; var fs = util.fs();
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() { fs.stat('/', function(error, stats) {
expect(_error).toEqual(null); if(error) throw error;
expect(_result).toEqual(true); expect(stats.isSymbolicLink()).to.equal(false);
done();
});
});
it('should return true if stats are for symbolic link', function(done) {
var fs = util.fs();
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
fs.close(fd, function(error, stats) {
if(error) throw error;
fs.symlink('/myfile', '/myfilelink', function(error) {
if(error) throw error;
fs.lstat('/myfilelink', function(error, stats) {
expect(stats.isSymbolicLink()).to.equal(true);
done();
});
});
});
}); });
}); });
}); });
@ -164,29 +207,18 @@ define(["Filer", "util"], function(Filer, util) {
afterEach(util.cleanup); afterEach(util.cleanup);
it('should be a function', function() { it('should be a function', function() {
var testStat = this.fs.stat('/', function(error) { var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error; if(error) throw error;
expect(typeof stats.isFIFO).to.equal('function');
}); });
expect(typeof this.testStat.isFIFO).toEqual('function');
}); });
it('should return false', function() { it('should return false', function() {
var complete = false; var fs = util.fs();
var _result; fs.stat('/', function(error, stats) {
var that = this;
_result = that.fs.stat('/', function(error) {
if(error) throw error; if(error) throw error;
}).isFIFO(); expect(stats.isFIFO()).to.equal(false);
complete = true;
waitsFor(function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).toEqual(null);
expect(_result).toEqual(false);
}); });
}); });
}); });
@ -196,31 +228,20 @@ define(["Filer", "util"], function(Filer, util) {
afterEach(util.cleanup); afterEach(util.cleanup);
it('should be a function', function() { it('should be a function', function() {
var testStat = this.fs.stat('/', function(error) { var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error; if(error) throw error;
expect(typeof stats.isSocket).to.equal('function');
}); });
expect(typeof this.testStat.isSocket).toEqual('function');
}); });
it('should return false', function() { it('should return false', function() {
var complete = false; var fs = util.fs();
var _result; fs.stat('/', function(error, stats) {
var that = this;
_result = that.fs.stat('/', function(error) {
if(error) throw error; if(error) throw error;
}).isSocket(); expect(stats.isSocket()).to.equal(false);
complete = true;
waitsFor(function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).toEqual(null);
expect(_result).toEqual(false);
}); });
}); });
});*/ });
}); });
) });