1- import * as path from "path" ;
2- import fs from "fs" ;
3- import { minimatch } from "minimatch" ;
4- import yaml from "js-yaml" ;
1+ import { spawnSync } from "node:child_process" ;
2+ import fs from "node:fs" ;
3+ import * as path from "node:path" ;
4+ import Ajv2020 from "ajv/dist/2020" ;
5+ import draft7MetaSchema from "ajv/dist/refs/json-schema-draft-07.json" with {
6+ type : "json" ,
7+ } ;
58import { assert } from "chai" ;
9+ import yaml from "js-yaml" ;
10+ import { minimatch } from "minimatch" ;
611import stringify from "safe-stable-stringify" ;
7- import { spawnSync } from "child_process" ;
8- import Ajv2020 from "ajv/dist/2020" ;
9- import draft7MetaSchema from "ajv/dist/refs/json-schema-draft-07.json" with { type : "json" } ;
1012
1113function ansiRegex ( { onlyFirst = false } = { } ) {
1214 const pattern = [
@@ -20,17 +22,17 @@ function ansiRegex({ onlyFirst = false } = {}) {
2022function stripAnsi ( data : string ) {
2123 if ( typeof data !== "string" ) {
2224 throw new TypeError (
23- `Expected a \`string\`, got \`${ typeof data } \ = ${ data } ` ,
25+ `Expected a \`string\`, got \`${ typeof data } = ${ data } ` ,
2426 ) ;
2527 }
2628 return data . replace ( ansiRegex ( ) , "" ) ;
2729}
2830
2931const ajv = new Ajv2020 ( {
30- strictTypes : false ,
31- strict : false ,
32- inlineRefs : true , // https://github.com/ajv-validator/ajv/issues/1581#issuecomment-832211568
3332 allErrors : true , // https://github.com/ajv-validator/ajv/issues/1581#issuecomment-832211568
33+ inlineRefs : true , // https://github.com/ajv-validator/ajv/issues/1581#issuecomment-832211568
34+ strict : false ,
35+ strictTypes : false ,
3436} ) ;
3537ajv . addMetaSchema ( draft7MetaSchema ) ;
3638
@@ -44,7 +46,7 @@ const schema_files = fs
4446 . filter ( ( el ) => path . extname ( el ) === ".json" ) ;
4547console . log ( `Schemas: ${ schema_files } ` ) ;
4648
47- describe ( "schemas under f/" , function ( ) {
49+ describe ( "schemas under f/" , ( ) => {
4850 schema_files . forEach ( ( schema_file ) => {
4951 if (
5052 schema_file . startsWith ( "_" ) ||
@@ -56,20 +58,21 @@ describe("schemas under f/", function () {
5658 ajv . addSchema ( schema_json ) ;
5759 const validator = ajv . compile ( schema_json ) ;
5860 if (
59- schema_json [ " examples" ] == undefined &&
60- schema_json [ "x-ansible-lint" ] == undefined
61+ schema_json . examples = == undefined &&
62+ schema_json [ "x-ansible-lint" ] === undefined
6163 ) {
6264 console . error (
6365 `Schema file ${ schema_file } is missing an 'examples' or 'x-ansible-lint' key that we need for documenting file matching patterns.` ,
6466 ) ;
65- return process . exit ( 1 ) ;
67+ process . exit ( 1 ) ;
68+ return ;
6669 }
67- describe ( schema_file , function ( ) {
70+ describe ( schema_file , ( ) => {
6871 const file_path_key =
69- schema_json [ "x-ansible-lint" ] || schema_json [ " examples" ] ;
72+ schema_json [ "x-ansible-lint" ] || schema_json . examples ;
7073 getTestFiles ( file_path_key ) . forEach (
7174 ( { file : test_file , expect_fail } ) => {
72- it ( `linting ${ test_file } using ${ schema_file } ` , function ( ) {
75+ it ( `linting ${ test_file } using ${ schema_file } ` , ( ) => {
7376 var errors_md = "" ;
7477 const result = validator (
7578 yaml . load ( fs . readFileSync ( test_file , "utf8" ) ) ,
@@ -86,9 +89,9 @@ describe("schemas under f/", function () {
8689 // and breaks usage from inside virtualenvs.
8790 const proc = spawnSync (
8891 `${ process . env . VIRTUAL_ENV } /bin/check-jsonschema -v -o json --schemafile f/${ schema_file } ${ test_file } ` ,
89- { shell : true , encoding : "utf-8" , stdio : "pipe" } ,
92+ { encoding : "utf-8" , shell : true , stdio : "pipe" } ,
9093 ) ;
91- if ( proc . status != 0 ) {
94+ if ( proc . status !== 0 ) {
9295 // real errors are sent to stderr due to https://github.com/python-jsonschema/check-jsonschema/issues/88
9396 errors_md += "# check-jsonschema\n\nstdout:\n\n```json\n" ;
9497 errors_md += stripAnsi ( proc . output [ 1 ] || "" ) ;
@@ -106,8 +109,8 @@ describe("schemas under f/", function () {
106109 fs . writeFileSync ( md_filename , errors_md ) ;
107110 } else {
108111 // if no error occurs, we should ensure there is no md file present
109- fs . unlink ( md_filename , function ( err ) {
110- if ( err && err . code != "ENOENT" ) {
112+ fs . unlink ( md_filename , ( err ) => {
113+ if ( err && err . code !== "ENOENT" ) {
111114 console . error ( `Failed to remove ${ md_filename } .` ) ;
112115 }
113116 } ) ;
@@ -123,17 +126,18 @@ describe("schemas under f/", function () {
123126 // All /$defs/ that have examples property are assumed to be
124127 // subschemas, "tasks" being the primary such case, which is also used
125128 // for validating separated files.
126- for ( var definition in schema_json [ " $defs" ] ) {
127- if ( schema_json [ " $defs" ] [ definition ] . examples ) {
128- const subschema_uri = `${ schema_json [ " $id" ] } #/$defs/${ definition } ` ;
129+ for ( var definition in schema_json . $defs ) {
130+ if ( schema_json . $defs [ definition ] . examples ) {
131+ const subschema_uri = `${ schema_json . $id } #/$defs/${ definition } ` ;
129132 const subschema_validator = ajv . getSchema ( subschema_uri ) ;
130133 if ( ! subschema_validator ) {
131134 console . error ( `Failed to load subschema ${ subschema_uri } ` ) ;
132- return process . exit ( 1 ) ;
135+ process . exit ( 1 ) ;
136+ return ;
133137 }
134- getTestFiles ( schema_json [ " $defs" ] [ definition ] . examples ) . forEach (
138+ getTestFiles ( schema_json . $defs [ definition ] . examples ) . forEach (
135139 ( { file : test_file , expect_fail } ) => {
136- it ( `linting ${ test_file } using ${ subschema_uri } ` , function ( ) {
140+ it ( `linting ${ test_file } using ${ subschema_uri } ` , ( ) => {
137141 const result = subschema_validator (
138142 yaml . load ( fs . readFileSync ( test_file , "utf8" ) ) ,
139143 ) ;
@@ -157,25 +161,23 @@ function getTestFiles(
157161) : { file : string ; expect_fail : boolean } [ ] {
158162 const files = Array . from (
159163 new Set (
160- globs
161- . map ( ( glob : any ) => minimatch . match ( test_files , path . join ( "**" , glob ) ) )
162- . flat ( ) ,
164+ globs . flatMap ( ( glob : any ) =>
165+ minimatch . match ( test_files , path . join ( "**" , glob ) ) ,
166+ ) ,
163167 ) ,
164168 ) ;
165169 const negative_files = Array . from (
166170 new Set (
167- globs
168- . map ( ( glob : any ) =>
169- minimatch . match ( negative_test_files , path . join ( "**" , glob ) ) ,
170- )
171- . flat ( ) ,
171+ globs . flatMap ( ( glob : any ) =>
172+ minimatch . match ( negative_test_files , path . join ( "**" , glob ) ) ,
173+ ) ,
172174 ) ,
173175 ) ;
174176
175177 // All fails ending with fail, like `foo.fail.yml` are expected to fail validation
176- let result = files . map ( ( f ) => ( { file : f , expect_fail : false } ) ) ;
178+ let result = files . map ( ( f ) => ( { expect_fail : false , file : f } ) ) ;
177179 result = result . concat (
178- negative_files . map ( ( f ) => ( { file : f , expect_fail : true } ) ) ,
180+ negative_files . map ( ( f ) => ( { expect_fail : true , file : f } ) ) ,
179181 ) ;
180182 return result ;
181183}
0 commit comments