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 )
0 commit comments