Skip to content

Commit a35309b

Browse files
committed
Switch integration test script to be in JS
This allows us to do more clear checks on the errors returned from eslint. The sample projects eslintrc is also updated to "allowComments"
1 parent 7054733 commit a35309b

File tree

5 files changed

+86
-62
lines changed

5 files changed

+86
-62
lines changed

examples/.eslintrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
},
88
"overrides": [
99
{
10-
"files": ["samples/jsonc.json"],
10+
"files": ["samples/json-with-comments.json"],
1111
"rules": {
12-
"json/json": ["warn", {"allowComments": true}]
12+
"json/*": ["warn", {"allowComments": true}]
1313
}
1414
}
1515
]

examples/integration-test.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env node
2+
3+
const { execFileSync }= require('child_process');
4+
const assert = require('assert');
5+
6+
function get_lint_results(filename) {
7+
let cmd_args = ['-f', 'json', filename];
8+
let lint_results = [];
9+
try {
10+
let results = execFileSync('eslint', cmd_args, {encoding: 'utf8', stdio: 'pipe'})
11+
lint_results = JSON.parse(results)
12+
}
13+
catch (e) {
14+
if (e.status !== 1 && e.status !== 0) {
15+
console.error("The lint command itself failed", e.status);
16+
throw e;
17+
}
18+
lint_results = JSON.parse(e.stdout)
19+
}
20+
return lint_results
21+
}
22+
23+
function sum_by_rule_id(lint_results) {
24+
error_count_by_rule_id = {}
25+
warning_count_by_rule_id = {}
26+
for (file_result of lint_results) {
27+
for (message of file_result.messages) {
28+
let counter = error_count_by_rule_id;
29+
if (message.severity === 1) {
30+
counter = warning_count_by_rule_id;
31+
}
32+
if (counter[message.ruleId]) {
33+
counter[message.ruleId]++;
34+
}
35+
else {
36+
counter[message.ruleId] = 1;
37+
}
38+
}
39+
}
40+
return {"errors": error_count_by_rule_id, "warnings": warning_count_by_rule_id};
41+
}
42+
43+
// counts is an object mapping ruleIds to counts
44+
// expected_array is al ist of expected ruleIds (can have repeated ruleIds)
45+
function match_count_by_expectation(counts, expected_array) {
46+
for (rule_id of expected_array) {
47+
assert(counts[rule_id], `${rule_id} violation not found`);
48+
counts[rule_id]--;
49+
}
50+
for (rule_id in counts) {
51+
assert(counts[rule_id] == 0, `${rule_id} had an unexpected occurence`);
52+
}
53+
}
54+
55+
function validate_file_by_rules(filename, expected_errors=[], expected_warnings=[]) {
56+
lint_results = get_lint_results(filename);
57+
counts_by_rule_id = sum_by_rule_id(lint_results)
58+
59+
error_counts = counts_by_rule_id.errors
60+
match_count_by_expectation(error_counts, expected_errors)
61+
warning_counts = counts_by_rule_id.warnings
62+
match_count_by_expectation(warning_counts, expected_warnings)
63+
}
64+
65+
function validate_files_by_totals(filename, error_totals, warning_totals) {
66+
lint_results = get_lint_results(filename);
67+
lint_result = lint_results[0]
68+
assert(lint_result.errorCount === error_totals,
69+
`${filename} has unexpected number of errors`);
70+
assert(lint_result.warningCount === warning_totals,
71+
`${filename} has unexpected number of warnings`);
72+
}
73+
74+
validate_file_by_rules('samples/good-json.json', [], []);
75+
validate_file_by_rules('samples/duplicate-keys.json',
76+
['json/duplicate-key', 'json/duplicate-key'], [])
77+
validate_file_by_rules('samples/whole-mess.json',
78+
['json/duplicate-key', 'json/duplicate-key', 'json/trailing-comma'],
79+
['json/*']);
80+
validate_file_by_rules('samples/json-with-comments.json', [], [])
81+
82+
validate_files_by_totals('samples/wrong-syntax.json', 0, 1)

examples/integration-test.sh

Lines changed: 0 additions & 58 deletions
This file was deleted.

examples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"lint": "eslint",
8-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"test": "node integration-test.js"
99
},
1010
"keywords": [],
1111
"license": "ISC",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"scripts": {
1818
"test": "mocha",
1919
"lint": "eslint lib test",
20-
"integration-tests": "(cd examples && ./integration-test.sh)"
20+
"integration-tests": "(cd examples && npm run test)"
2121
},
2222
"repository": {
2323
"type": "git",

0 commit comments

Comments
 (0)