Skip to content

Commit

Permalink
New: Add isSymbolic method and symlink property (closes #79) (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Sep 27, 2016
1 parent a95f650 commit 1fb1631
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 3 deletions.
41 changes: 38 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
81 changes: 81 additions & 0 deletions test/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
});
});
});

0 comments on commit 1fb1631

Please sign in to comment.