From 748074a98f387c5e2807ee1d7ad8b8473282372f Mon Sep 17 00:00:00 2001 From: Dave James Miller Date: Thu, 9 Oct 2014 08:51:17 +0100 Subject: [PATCH] Add .to.be.a.symlink() support. Closes #3 --- lib/assertions/symlink.js | 37 +++++++++++++++++++++ lib/index.js | 1 + test/fixtures/symlink.txt | 1 + test/specs/symlink_be.js | 70 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 lib/assertions/symlink.js create mode 120000 test/fixtures/symlink.txt create mode 100644 test/specs/symlink_be.js diff --git a/lib/assertions/symlink.js b/lib/assertions/symlink.js new file mode 100644 index 0000000..284ebb2 --- /dev/null +++ b/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); + }; +}; diff --git a/lib/index.js b/lib/index.js index 42e516b..8410537 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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); }; diff --git a/test/fixtures/symlink.txt b/test/fixtures/symlink.txt new file mode 120000 index 0000000..93ae521 --- /dev/null +++ b/test/fixtures/symlink.txt @@ -0,0 +1 @@ +alpha.txt \ No newline at end of file diff --git a/test/specs/symlink_be.js b/test/specs/symlink_be.js new file mode 100644 index 0000000..75bd216 --- /dev/null +++ b/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" + }); +});