From 2357391faabf65bf89d0a63f2c04786f49138ef2 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 31 Aug 2016 11:34:06 -0700 Subject: [PATCH] New: Add isSymbolic method and symlink property (closes #79) (#98) --- index.js | 41 ++++++++++++++++++++++++-- test/File.js | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 5a1fe55..0cdbdb3 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,7 @@ var Stream = require('stream'); var replaceExt = require('replace-ext'); var builtInFields = [ - '_contents', 'contents', 'stat', 'history', 'path', 'base', 'cwd', + '_contents', '_symlink', 'contents', 'stat', 'history', 'path', 'base', 'cwd', ]; function File(file) { @@ -35,6 +35,8 @@ function File(file) { this._isVinyl = true; + this._symlink = null; + // Set custom properties Object.keys(file).forEach(function(key) { if (self.constructor.isCustomProp(key)) { @@ -55,9 +57,28 @@ File.prototype.isNull = function() { return isNull(this.contents); }; -// TODO: Should this be moved to vinyl-fs? File.prototype.isDirectory = function() { - return this.isNull() && this.stat && this.stat.isDirectory(); + if (!this.isNull()) { + return false; + } + + if (this.stat && typeof this.stat.isDirectory === 'function') { + return this.stat.isDirectory(); + } + + return false; +}; + +File.prototype.isSymbolic = function() { + if (!this.isNull()) { + return false; + } + + if (this.stat && typeof this.stat.isSymbolicLink === 'function') { + return this.stat.isSymbolicLink(); + } + + return false; }; File.prototype.clone = function(opt) { @@ -267,4 +288,18 @@ Object.defineProperty(File.prototype, 'path', { }, }); +Object.defineProperty(File.prototype, 'symlink', { + get: function() { + return this._symlink; + }, + set: function(symlink) { + // TODO: should this set the mode to symbolic if set? + if (typeof symlink !== 'string') { + throw new Error('symlink should be a string'); + } + + this._symlink = symlink; + }, +}); + module.exports = File; diff --git a/test/File.js b/test/File.js index 81a22ae..1aaebc7 100644 --- a/test/File.js +++ b/test/File.js @@ -211,6 +211,46 @@ describe('File', function() { file.isDirectory().should.equal(true); done(); }); + + it('returns false when the stats exist but do not contain isDirectory method', function(done) { + var file = new File({ contents: null, stat: {} }); + file.isDirectory().should.equal(false); + done(); + }); + }); + + describe('isSymbolic()', function() { + var fakeStat = { + isSymbolicLink: function() { + return true; + }, + }; + + it('should return false when the contents are a Buffer', function(done) { + var val = new Buffer('test'); + var file = new File({ contents: val, stat: fakeStat }); + file.isSymbolic().should.equal(false); + done(); + }); + + it('should return false when the contents are a Stream', function(done) { + var val = new Stream(); + var file = new File({ contents: val, stat: fakeStat }); + file.isSymbolic().should.equal(false); + done(); + }); + + it('should return true when the contents are a null', function(done) { + var file = new File({ contents: null, stat: fakeStat }); + file.isSymbolic().should.equal(true); + done(); + }); + + it('returns false when the stats exist but do not contain isSymbolicLink method', function(done) { + var file = new File({ contents: null, stat: {} }); + file.isSymbolic().should.equal(false); + done(); + }); }); describe('clone()', function() { @@ -972,4 +1012,45 @@ describe('File', function() { }).should.throw('path should be string'); }); }); + + describe('symlink get/set', function() { + it('should return null on get when no symlink', function(done) { + var file = new File(); + var a = file.symlink; + should.not.exist(a); + done(); + }); + + it('should return the symlink if set', function(done) { + var file = new File({ + symlink: '/test/test.coffee', + }); + file.symlink.should.equal('/test/test.coffee'); + done(); + }); + + it('should error on set with non-string symlink', function(done) { + var file = new File(); + try { + file.symlink = null; + } catch (err) { + should.exist(err); + done(); + } + }); + + it('should set the symlink', function(done) { + var file = new File(); + file.symlink = '/test/test.coffee'; + file.symlink.should.equal('/test/test.coffee'); + done(); + }); + + it('should set the relative symlink', function(done) { + var file = new File(); + file.symlink = './test.coffee'; + file.symlink.should.equal('./test.coffee'); + done(); + }); + }); });