Skip to content

Commit

Permalink
Merge pull request #193 from vsiakka/noRestrictedTagsWithExamples
Browse files Browse the repository at this point in the history
Fix a bug where the no-restricted-tags rule wouldn't detect violation…
  • Loading branch information
vsiakka committed May 25, 2019
2 parents b6bc6aa + 4bf43eb commit 464958d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/rules/no-restricted-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,37 @@ var availableConfigs = {

function noRestrictedTags(feature, fileName, configuration) {
var forbiddenTags = configuration.tags;

var featureErrors = checkTags(feature, forbiddenTags);

var childrenErrors = _(feature.children).map(function(child) {
return checkTags(child, forbiddenTags);
}).flatten().value();

return featureErrors.concat(childrenErrors);
var errors = [];

checkTags(feature, forbiddenTags, errors);
if (feature.children) {
feature.children.forEach(function(child) {
checkTags(child, forbiddenTags, errors);

if (child.examples) {
child.examples.forEach(function(example) {
checkTags(example, forbiddenTags, errors);
});
}
});
}
return errors;
}

function checkTags(node, forbiddenTags) {
return (node.tags || []).filter(function(tag) {
return isForbidden(tag, forbiddenTags);
}).map(function(tag) {
return createError(node, tag);
function checkTags(node, forbiddenTags, errors) {
var nodeTags = node.tags || [];

nodeTags.forEach(function(tag) {
if (_.includes(forbiddenTags, tag.name)) {
errors.push({
message: 'Forbidden tag ' + tag.name + ' on ' + node.type,
rule : rule,
line : tag.location.line
});
}
});
}

function isForbidden(tag, forbiddenTags) {
return _.includes(forbiddenTags, tag.name);
}

function createError(node, tag) {
return {message: 'Forbidden tag ' + tag.name + ' on ' + node.type,
rule : rule,
line : tag.location.line};
}

module.exports = {
name: rule,
run: noRestrictedTags,
Expand Down
6 changes: 6 additions & 0 deletions test/rules/no-restricted-tags/Violations.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Scenario: This is a Scenario with three duplicate tags
@scenariotag @badTag @anotherBadTag
Scenario Outline: This is a Scenario Outline with two duplicate tags
Then this is a then step <foo>
@examplestag @badTag @anotherBadTag
Examples:
| foo |
| bar |

@examplestag @badTag @anotherBadTag
Examples:
| foo |
| bar |
16 changes: 16 additions & 0 deletions test/rules/no-restricted-tags/no-restricted-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ describe('No Restricted Tags Rule', function() {
{
messageElements: {tag: '@anotherBadTag', nodeType:'ScenarioOutline'},
line: 11
},
{
messageElements: {tag: '@badTag', nodeType:'Examples'},
line: 14
},
{
messageElements: {tag: '@anotherBadTag', nodeType:'Examples'},
line: 14
},
{
messageElements: {tag: '@badTag', nodeType:'Examples'},
line: 19
},
{
messageElements: {tag: '@anotherBadTag', nodeType:'Examples'},
line: 19
}]);
});
});

0 comments on commit 464958d

Please sign in to comment.