Skip to content

Commit

Permalink
issue-813 -- Adds unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danwaz committed Aug 25, 2016
1 parent d506b26 commit 75ac1f0
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/rules/no-domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
var stripped = helpers.stripQuotes(item.content),
parsedUrl = url.parse(stripped, false, true);

if (parsedUrl.host) {
if (parsedUrl.host && parsedUrl.protocol !== 'data:') {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'severity': parser.severity,
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/no-url-protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var isUrlRegex = /^(https?:)?\/\//,
module.exports = {
'name': 'no-url-protocols',
'defaults': {
'enforce-domains' : true
'enforce-domains': true
},
'detect': function (ast, parser) {
var result = [];
Expand All @@ -20,8 +20,8 @@ module.exports = {
regexSelector = parser.options['enforce-domains'] ?
isUrlRegex : noProtocolRegex,
message = parser.options['enforce-domains'] ?
"Protocols and domains in URLs are disallowed" :
"Protocols in URLS are disallowed";
'Protocols and domains in URLs are disallowed' :
'Protocols in URLS are disallowed';

if (stripped.match(regexSelector)) {
result = helpers.addUnique(result, {
Expand Down
35 changes: 35 additions & 0 deletions tests/rules/no-domains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var lint = require('./_lint');

//////////////////////////////
// SCSS syntax tests
//////////////////////////////
describe('no domains - scss', function () {
var file = lint.file('no-domains.scss');

it('enforce', function (done) {
lint.test(file, {
'no-domains': 1
}, function (data) {
lint.assert.equal(3, data.warningCount);
done();
});
});
});

//////////////////////////////
// Sass syntax tests
//////////////////////////////
describe('no domains - sass', function () {
var file = lint.file('no-domains.sass');

it('enforce', function (done) {
lint.test(file, {
'no-domains': 1
}, function (data) {
lint.assert.equal(3, data.warningCount);
done();
});
});
});
29 changes: 29 additions & 0 deletions tests/rules/no-url-protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,23 @@ describe('no url protocols - scss', function () {
done();
});
});

it('allow protocol-relative urls [enforce-domains: false]', function (done) {
lint.test(file, {
'no-url-protocols': [
1,
{
'enforce-domains': false
}
]
}, function (data) {
lint.assert.equal(2, data.warningCount);
done();
});
});
});


//////////////////////////////
// Sass syntax tests
//////////////////////////////
Expand All @@ -32,4 +47,18 @@ describe('no url protocols - sass', function () {
done();
});
});

it('allow protocol-relative urls [enforce-domains: false]', function (done) {
lint.test(file, {
'no-url-protocols': [
1,
{
'enforce-domains': false
}
]
}, function (data) {
lint.assert.equal(2, data.warningCount);
done();
});
});
});
25 changes: 25 additions & 0 deletions tests/sass/no-domains.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.foo
background-image: url('https://foo.com/img/bar.png')


.foo
background-image: url('http://foo.com/img/bar.png')


.foo
background-image: url('//foo.com/img/bar.png')


.foo
background-image: url('/img/bar.png')


.foo
background-image: url('img/bar.png')


.foo
background-image: url('bar.png')

.foo
background-image: url('data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7')
27 changes: 27 additions & 0 deletions tests/sass/no-domains.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.foo {
background-image: url('https://foo.com/img/bar.png');
}

.foo {
background-image: url('http://foo.com/img/bar.png');
}

.foo {
background-image: url('//foo.com/img/bar.png');
}

.foo {
background-image: url('/img/bar.png');
}

.foo {
background-image: url('img/bar.png');
}

.foo {
background-image: url('bar.png');
}

.foo {
background-image: url('data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');
}

0 comments on commit 75ac1f0

Please sign in to comment.