Skip to content

Commit

Permalink
Add .to.be.a.symlink() support. Closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
d13r committed Oct 9, 2014
1 parent d624013 commit 748074a
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/assertions/symlink.js
@@ -0,0 +1,37 @@
module.exports = function (chai, utils) {

var Assertion = chai.Assertion;
var flag = utils.flag;
var assert = chai.assert;

var fs = require('fs');

//-------------------------------------------------------------------------------------------------------------

Assertion.addMethod('symlink', function (msg) {
var preMsg = '';
if (msg) {
flag(this, 'message', msg);
preMsg = msg + ': ';
}

var obj = this._obj;

new chai.Assertion(obj, preMsg + 'value').is.a('string');
new chai.Assertion(obj, preMsg + 'value').to.be.a.path();

var pass = fs.lstatSync(obj).isSymbolicLink();

this.assert(
pass
, "expected #{this} to be a symlink"
, "expected #{this} not to be a symlink"
);
});
assert.isSymlink = function (val, msg) {
new chai.Assertion(val).to.be.a.symlink(msg);
};
assert.notIsSymlink = function (val, msg) {
new chai.Assertion(val).to.not.be.a.symlink(msg);
};
};
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -10,5 +10,6 @@ module.exports = function (chai, utils) {
require('./assertions/file')(chai, utils);
require('./assertions/directory')(chai, utils);
require('./assertions/mode')(chai, utils);
require('./assertions/symlink')(chai, utils);

};
1 change: 1 addition & 0 deletions test/fixtures/symlink.txt
70 changes: 70 additions & 0 deletions test/specs/symlink_be.js
@@ -0,0 +1,70 @@
describe(require('path').basename(__filename), function () {

var chai = require('chai');
var expect = chai.expect;
var assert = chai.assert;

var styles = {
"expect/should": {
base: {
"basic": function (params) {
expect(params.value).to.be.a.symlink();
params.value.should.be.a.symlink();
},
"with message": {msg: true, call: function (params) {
expect(params.value).to.be.a.symlink(params.msg);
params.value.should.be.a.symlink(params.msg);
}}
},
negate: function (params) {
expect(params.value).to.not.be.a.symlink();
params.value.should.not.be.a.symlink();

}
},
assert: {
base: {
"basic": function (params) {
assert.isSymlink(params.value);
},
"with message": {msg: true, call: function (params) {
assert.isSymlink(params.value, params.msg);
}}
},
negate: {
"basic": function (params) {
assert.notIsSymlink(params.value);
},
"with message": {msg: true, call: function (params) {
assert.notIsSymlink(params.value, params.msg);
}}
}
}
};

var defaults = {
msg: 'My Message'
};

var test = chai.getStyleTest(styles, defaults);

test.valid({
value: 'test/fixtures/symlink.txt',
report: "expected '<%= value %>' not to be a symlink"
});
test.invalid({
label: 'regular file',
value: 'test/fixtures/alpha.txt',
report: "expected '<%= value %>' to be a symlink"
});
test.error({
label: 'non-existing path',
value: 'test/fixtures/non-existing.txt',
report: "value: expected '<%= value %>' to exist"
});
test.error({
label: 'bad value type',
value: 123,
report: "value: expected <%= value %> to be a string"
});
});

0 comments on commit 748074a

Please sign in to comment.