diff --git a/README.md b/README.md index fc130e2..06374a9 100644 --- a/README.md +++ b/README.md @@ -328,7 +328,7 @@ fs.open('/myfile', 'w', function(err, fd) { #### fs.stat(path, callback) 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. Examples: diff --git a/src/fs.js b/src/fs.js index c950f36..48893d9 100644 --- a/src/fs.js +++ b/src/fs.js @@ -129,11 +129,11 @@ define(function(require) { } Stats.prototype.isFile = function() { - return this.type === constants.MODE_FILE; + return this.type === MODE_FILE; }; Stats.prototype.isDirectory = function() { - return this.type === constants.MODE_DIRECTORY; + return this.type === MODE_DIRECTORY; }; Stats.prototype.isBlockDevice = function() { @@ -145,7 +145,7 @@ define(function(require) { }; Stats.prototype.isSymbolicLink = function() { - return this.type === constants.MODE_SYMBOLIC_LINK; + return this.type === MODE_SYMBOLIC_LINK; }; Stats.prototype.isFIFO = function() { diff --git a/tests/spec/fs.Stats.spec.js b/tests/spec/fs.Stats.spec.js index a8074cb..57e8201 100644 --- a/tests/spec/fs.Stats.spec.js +++ b/tests/spec/fs.Stats.spec.js @@ -6,53 +6,103 @@ define(["Filer", "util"], function(Filer, util) { afterEach(util.cleanup); it('should be a function', function() { - var fs = util.fs(); - expect(fs.stat.isFile).to.be.a('function'); + var fs = util.fs(); + 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) { 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; - fs.stat('/myFile').isFile(function (error, data) { - expect(error).not.to.exist; - expect(data).toEqual(true); - done() + fs.fstat(fd, function(error, stats) { + expect(stats.isFile()).to.equal(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.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); afterEach(util.cleanup); 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; + 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() { - var complete = false; - var _result; - var that = this; + it('should return false if stats are for file', function(done) { + var fs = util.fs(); - _result = that.fs.stat('/myFile', function(error) { + fs.open('/myfile', 'w+', function(error, fd) { if(error) throw error; - }).isFile(); - complete = true; + fs.fstat(fd, function(error, stats) { + expect(stats.isDirectory()).to.equal(false); + done(); + }); + }); + }); - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); + it('should return true if stats are for directory', function(done) { + var fs = util.fs(); - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(true); + fs.stat('/', function(error, stats) { + if(error) throw error; + 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); 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; + expect(typeof stats.isBlockDevice).to.equal('function'); }); - expect(typeof this.testStat.isBlockDevice).toEqual('function'); }); it('should return false', function() { - var complete = false; - var _result; - var that = this; - - _result = that.fs.stat('/', function(error) { + var fs = util.fs(); + fs.stat('/', function(error, stats) { if(error) throw error; - }).isBlockDevice(); - complete = true; - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(false); + expect(stats.isBlockDevice()).to.equal(false); }); }); }); @@ -94,29 +133,18 @@ define(["Filer", "util"], function(Filer, util) { afterEach(util.cleanup); 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; + expect(typeof stats.isCharacterDevice).to.equal('function'); }); - expect(typeof this.testStat.isCharacterDevice).toEqual('function'); }); it('should return false', function() { - var complete = false; - var _result; - var that = this; - - _result = that.fs.stat('/', function(error) { + var fs = util.fs(); + fs.stat('/', function(error, stats) { if(error) throw error; - }).isCharacterDevice(); - complete = true; - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(false); + expect(stats.isCharacterDevice()).to.equal(false); }); }); }); @@ -126,35 +154,50 @@ define(["Filer", "util"], function(Filer, util) { afterEach(util.cleanup); 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; + 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() { - var complete = false; - var _error, _result; - var that = this; + it('should return false if stats are for file', function(done) { + var fs = util.fs(); - that.fs.writeFile('/myfile', '', { encoding: 'utf8' }, function(error) { + fs.open('/myfile', 'w+', function(error, fd) { if(error) throw error; - that.fs.symlink('/myfile', '/myFileLink', function (error) { - if (error) throw error; - _result = that.fs.lstat('/myfile', function(error) { - if(error) throw error; - }).isSymbolicLink(); - complete = true; + fs.fstat(fd, function(error, stats) { + expect(stats.isSymbolicLink()).to.equal(false); + done(); }); }); - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); + }); - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(true); + 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.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); 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; + expect(typeof stats.isFIFO).to.equal('function'); }); - expect(typeof this.testStat.isFIFO).toEqual('function'); }); it('should return false', function() { - var complete = false; - var _result; - var that = this; - - _result = that.fs.stat('/', function(error) { + var fs = util.fs(); + fs.stat('/', function(error, stats) { if(error) throw error; - }).isFIFO(); - complete = true; - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(false); + expect(stats.isFIFO()).to.equal(false); }); }); }); @@ -196,31 +228,20 @@ define(["Filer", "util"], function(Filer, util) { afterEach(util.cleanup); 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; + expect(typeof stats.isSocket).to.equal('function'); }); - expect(typeof this.testStat.isSocket).toEqual('function'); }); it('should return false', function() { - var complete = false; - var _result; - var that = this; - - _result = that.fs.stat('/', function(error) { + var fs = util.fs(); + fs.stat('/', function(error, stats) { if(error) throw error; - }).isSocket(); - complete = true; - - waitsFor(function() { - return complete; - }, 'test to complete', DEFAULT_TIMEOUT); - - runs(function() { - expect(_error).toEqual(null); - expect(_result).toEqual(false); + expect(stats.isSocket()).to.equal(false); }); }); - });*/ + }); }); -) \ No newline at end of file +}); \ No newline at end of file