Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .to.be.a.symlink() support. Closes #3 #4

Merged
merged 2 commits into from Oct 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions lib/assertions/symlink.js
@@ -0,0 +1,49 @@
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');

// This doesn't work for broken symlinks (target doesn't exist), but
// technically it's still a symlink so should pass
// new chai.Assertion(obj, preMsg + 'value').to.be.a.path();

var pass = false;
try {
pass = fs.lstatSync(obj).isSymbolicLink();
} catch (err) {
if (err.code === 'ENOENT') {
// This will always fail
new chai.Assertion(obj, preMsg + 'value').to.be.a.path();
}
throw err;
}

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/broken-symlink.txt
1 change: 1 addition & 0 deletions test/fixtures/symlink.txt
74 changes: 74 additions & 0 deletions test/specs/symlink_be.js
@@ -0,0 +1,74 @@
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.valid({
value: 'test/fixtures/broken-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"
});
});