Merge pull request #141 from kwkofler/issue#112

Issue #112 - Stats methods
This commit is contained in:
Alan K 2014-03-08 15:52:33 -05:00
commit f412c65846
6 changed files with 1102 additions and 124 deletions

View File

@ -346,7 +346,19 @@ 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. The following convenience methods are also present on the callback's `stats`:
```
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:

912
dist/filer.js vendored

File diff suppressed because it is too large Load Diff

10
dist/filer.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -131,6 +131,34 @@ define(function(require) {
this.type = fileNode.mode; this.type = fileNode.mode;
} }
Stats.prototype.isFile = function() {
return this.type === MODE_FILE;
};
Stats.prototype.isDirectory = function() {
return this.type === MODE_DIRECTORY;
};
Stats.prototype.isBlockDevice = function() {
return false;
};
Stats.prototype.isCharacterDevice = function() {
return false;
};
Stats.prototype.isSymbolicLink = function() {
return this.type === MODE_SYMBOLIC_LINK;
};
Stats.prototype.isFIFO = function() {
return false;
};
Stats.prototype.isSocket = function() {
return false;
};
/* /*
* Update node times. Only passed times are modified (undefined times are ignored) * Update node times. Only passed times are modified (undefined times are ignored)
* and filesystem flags are examined in order to override update logic. * and filesystem flags are examined in order to override update logic.

247
tests/spec/fs.stats.spec.js Normal file
View File

@ -0,0 +1,247 @@
define(["Filer", "util"], function(Filer, util) {
describe('fs.stats', function() {
describe('#isFile()', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error;
expect(stats.isFile).to.be.a('function');
});
});
it('should return true if stats are for file', function(done) {
var fs = util.fs();
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
fs.fstat(fd, function(error, stats) {
expect(stats.isFile()).to.be.true;
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.be.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.be.false;
done();
});
});
});
});
});
});
describe('#isDirectory()', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error;
expect(stats.isDirectory).to.be.a('function');
});
});
it('should return false if stats are for file', function(done) {
var fs = util.fs();
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
fs.fstat(fd, function(error, stats) {
expect(stats.isDirectory()).to.be.false;
done();
});
});
});
it('should return true if stats are for directory', function(done) {
var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error;
expect(stats.isDirectory()).to.be.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.be.false;
done();
});
});
});
});
});
});
describe('#isBlockDevice()', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error;
expect(stats.isBlockDevice).to.be.a('function');
});
});
it('should return false', function() {
var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error;
expect(stats.isBlockDevice()).to.be.false;
});
});
});
describe('#isCharacterDevice()', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error;
expect(stats.isCharacterDevice).to.be.a('function');
});
});
it('should return false', function() {
var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error;
expect(stats.isCharacterDevice()).to.be.false;
});
});
});
describe('#isSymbolicLink()', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error;
expect(stats.isSymbolicLink).to.be.a('function');
});
});
it('should return false if stats are for file', function(done) {
var fs = util.fs();
fs.open('/myfile', 'w+', function(error, fd) {
if(error) throw error;
fs.fstat(fd, function(error, stats) {
expect(stats.isSymbolicLink()).to.be.false;
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.isSymbolicLink()).to.be.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.be.true;
done();
});
});
});
});
});
});
describe('#isFIFO()', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error;
expect(stats.isFIFO).to.be.a('function');
});
});
it('should return false', function() {
var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error;
expect(stats.isFIFO()).to.be.false;
});
});
});
describe('#isSocket()', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error;
expect(stats.isSocket).to.be.a('function');
});
});
it('should return false', function() {
var fs = util.fs();
fs.stat('/', function(error, stats) {
if(error) throw error;
expect(stats.isSocket()).to.be.false;
});
});
});
});
});

View File

@ -31,6 +31,7 @@ define([
"spec/fs.truncate.spec", "spec/fs.truncate.spec",
"spec/fs.utimes.spec", "spec/fs.utimes.spec",
"spec/fs.xattr.spec", "spec/fs.xattr.spec",
"spec/fs.stats.spec",
"spec/path-resolution.spec", "spec/path-resolution.spec",
"spec/times.spec", "spec/times.spec",
"spec/time-flags.spec", "spec/time-flags.spec",