Skip to content

Commit

Permalink
Add some simple tests for the remaining rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jhinch committed Dec 24, 2018
1 parent 66e4fcf commit d6dfd61
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/rules/rule-indentation.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
let assert = require('assert');
let parser = require('../../lib/parser');
let {runRules} = require('../../lib/rules/runner');
let indentationRule = require('../../lib/rules/rule-indentation');

function testConfig(name, setting, contents, expectedErrors) {
return { name, setting, contents, expectedErrors };
}

const TEST_CONFIGS = [
testConfig('simple indentation', 4, `
location / {
error_page 418 = @other;
recursive_error_pages on;
if ($something) {
return 418;
}
}
location @other {
# some other configuration
}
`, []),
];

describe('rules/indentation', () => {
describe('#invoke()', () => {
TEST_CONFIGS.forEach(({name, setting, contents, expectedErrors}) => {
it(`should have ${expectedErrors.length ? 'errors' : 'no errors'} with ${name}`, () => {
let parseTree = parser.parse(contents);
let actualErrors = runRules(parseTree, [indentationRule], {'indentation': setting});
assert.deepStrictEqual(actualErrors, expectedErrors);
});
});
});
});
86 changes: 86 additions & 0 deletions test/rules/rule-line-ending.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
let assert = require('assert');
let parser = require('../../lib/parser');
let {runRules} = require('../../lib/rules/runner');
let lineEndingRule = require('../../lib/rules/rule-line-ending');
let fs = require('fs');

function testConfig(name, lineEnding, contents, expectedErrors) {
return { name, lineEnding, contents, expectedErrors };
}

const TEST_CONFIGS = [
testConfig('simple lf', 'lf', 'location /no-content {\n return 204;\n}\n', []),
testConfig('simple lf with error', 'lf', 'location /no-content {\r\n return 204;\n}\r\n', [
{
rule: 'line-ending',
text: 'Expected lf, found crlf',
type: 'error',
pos: {
start: { column: 23, line: 1, offset: 22 },
end: { column: 1, line: 2, offset: 24 }
},
},
{
rule: 'line-ending',
text: 'Expected lf, found crlf',
type: 'error',
pos: {
start: { column: 2, line: 3, offset: 41 },
end: { column: 1, line: 4, offset: 43 }
}
}
]),
testConfig('simple crlf', 'crlf', 'location /no-content {\r\n return 204;\r\n}\r\n', []),
testConfig('simple crlf with error', 'crlf', 'location /no-content {\r\n return 204;\n}\r\n', [
{
rule: 'line-ending',
text: 'Expected crlf, found lf',
type: 'error',
pos: {
start: { column: 16, line: 2, offset: 39 },
end: { column: 1, line: 3, offset: 40 }
}
}
]),
testConfig('newlines.conf', 'lf', fs.readFileSync(__dirname + '/../examples/newlines.conf', 'utf8'), [
{
rule: 'line-ending',
text: 'Expected lf, found crlf',
type: 'error',
pos: {
start: { column: 2, line: 2, offset: 10 },
end: { column: 1, line: 3, offset: 12 }
}
},
{
rule: 'line-ending',
text: 'Expected lf, found crlf',
type: 'error',
pos: {
start: { column: 13, line: 4, offset: 31 },
end: { column: 1, line: 5, offset: 33 }
}
},
{
rule: 'line-ending',
text: 'Expected lf, found crlf',
type: 'error',
pos: {
start: { column: 24, line: 7, offset: 100 },
end: { column: 1, line: 8, offset: 102 }
}
}
])
];

describe('rules/line-ending', () => {
describe('#invoke()', () => {
TEST_CONFIGS.forEach(({name, lineEnding, contents, expectedErrors}) => {
it(`should have ${expectedErrors.length ? 'errors' : 'no errors'} with ${name}`, () => {
let parseTree = parser.parse(contents);
let actualErrors = runRules(parseTree, [lineEndingRule], {'line-ending': lineEnding});
assert.deepStrictEqual(actualErrors, expectedErrors);
});
});
});
});
49 changes: 49 additions & 0 deletions test/rules/rule-strict-location.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
let assert = require('assert');
let parser = require('../../lib/parser');
let {runRules} = require('../../lib/rules/runner');
let strictLocationRule = require('../../lib/rules/rule-strict-location');
let fs = require('fs');

function testConfig(name, contents, expectedErrors) {
return { name, contents, expectedErrors };
}

const TEST_CONFIGS = [
testConfig('prefix location', `
location / {
return 204;
}
`, []),
testConfig('strict-location.conf', fs.readFileSync(__dirname + '/../examples/strict-location.conf', 'utf8'), [
{
rule: 'strict-location',
type: 'error',
text: 'Expected string when using \'location\', got regular expression',
pos: {
start: { column: 44, line: 7, offset: 144 },
end: { column: 45, line: 7, offset: 145 }
}
},
{
rule: 'strict-location',
type: 'error',
text: 'Expected string when using \'=\' modifier in \'location\', got regular expression',
pos: {
start: { column: 20, line: 11, offset: 249 },
end: { column: 36, line: 11, offset: 265 }
}
}
])
];

describe('rules/if-is-evil', () => {
describe('#invoke()', () => {
TEST_CONFIGS.forEach(({name, contents, expectedErrors}) => {
it(`should have ${expectedErrors.length ? 'errors' : 'no errors'} with ${name}`, () => {
let parseTree = parser.parse(contents);
let actualErrors = runRules(parseTree, [strictLocationRule]);
assert.deepStrictEqual(actualErrors, expectedErrors);
});
});
});
});

0 comments on commit d6dfd61

Please sign in to comment.