File tree Expand file tree Collapse file tree 3 files changed +35
-2
lines changed
Expand file tree Collapse file tree 3 files changed +35
-2
lines changed Original file line number Diff line number Diff line change 4040 "author" : " Codacy <support@codacy.com> (https://www.codacy.com)" ,
4141 "license" : " ISC" ,
4242 "engines" : {
43- "node" : " >=18 "
43+ "node" : " >=20 "
4444 },
4545 "dependencies" : {
4646 "@codacy/tooling" : " 0.1.0" ,
Original file line number Diff line number Diff line change @@ -103,6 +103,27 @@ describe("readConfigFile", () => {
103103 expect ( ( ) => readConfigFile ( tmpPath ) ) . toThrow ( "missing" ) ;
104104 fs . unlinkSync ( tmpPath ) ;
105105 } ) ;
106+
107+ it ( "should throw when a tool entry is missing toolId" , ( ) => {
108+ const tmpPath = "/tmp/test-no-toolid.json" ;
109+ fs . writeFileSync ( tmpPath , JSON . stringify ( {
110+ version : 1 ,
111+ tools : [ { patterns : [ ] } ] ,
112+ } ) ) ;
113+ expect ( ( ) => readConfigFile ( tmpPath ) ) . toThrow ( "tools[0] is missing a valid 'toolId'" ) ;
114+ fs . unlinkSync ( tmpPath ) ;
115+ } ) ;
116+
117+ it ( "should default patterns to empty array when missing" , ( ) => {
118+ const tmpPath = "/tmp/test-no-patterns.json" ;
119+ fs . writeFileSync ( tmpPath , JSON . stringify ( {
120+ version : 1 ,
121+ tools : [ { toolId : "eslint" } ] ,
122+ } ) ) ;
123+ const result = readConfigFile ( tmpPath ) ;
124+ expect ( result . tools [ 0 ] . patterns ) . toEqual ( [ ] ) ;
125+ fs . unlinkSync ( tmpPath ) ;
126+ } ) ;
106127} ) ;
107128
108129// ─── resolveToolId ────────────────────────────────────────────────────
Original file line number Diff line number Diff line change @@ -37,6 +37,18 @@ export function readConfigFile(filePath: string): CodacyConfig {
3737 if ( ! config . version || ! Array . isArray ( config . tools ) ) {
3838 throw new Error ( "Invalid configuration file: missing 'version' or 'tools' fields." ) ;
3939 }
40+ for ( let i = 0 ; i < config . tools . length ; i ++ ) {
41+ const tool = config . tools [ i ] ;
42+ if ( ! tool || typeof tool !== "object" ) {
43+ throw new Error ( `Invalid configuration file: tools[${ i } ] must be an object.` ) ;
44+ }
45+ if ( typeof tool . toolId !== "string" || tool . toolId . trim ( ) === "" ) {
46+ throw new Error ( `Invalid configuration file: tools[${ i } ] is missing a valid 'toolId'.` ) ;
47+ }
48+ if ( ! Array . isArray ( tool . patterns ) ) {
49+ tool . patterns = [ ] ;
50+ }
51+ }
4052 return config ;
4153 } catch ( err ) {
4254 if ( err instanceof SyntaxError ) {
@@ -110,7 +122,7 @@ export function buildImportPreview(
110122 ) ;
111123
112124 const totalPatterns = config . tools . reduce (
113- ( sum , t ) => sum + t . patterns . length ,
125+ ( sum , t ) => sum + ( Array . isArray ( t . patterns ) ? t . patterns . length : 0 ) ,
114126 0 ,
115127 ) ;
116128
You can’t perform that action at this time.
0 commit comments