@@ -2,10 +2,11 @@ import * as path from 'path';
22import * as fs from 'fs' ;
33import { NeedleResponse } from 'needle' ;
44
5- import { getPathToFixture } from '../common' ;
5+ import { getPathToFixture , jdkVersionAtLeast } from '../common' ;
66import { inspect } from '../../lib' ;
77import * as search from '../../lib/search' ;
88import type { PomCoords , SnykHttpClient } from '../../lib/types' ;
9+ import { DepGraphData } from '@snyk/dep-graph' ;
910
1011// specify fixtures to test, or leave empty to test all fixtures
1112let fixtures : string [ ] = [ ] ;
@@ -156,4 +157,107 @@ describe('inspect() fixtures', () => {
156157
157158 expect ( result . dependencyGraph ?. toJSON ( ) ) . toEqual ( expectedDepGraphJson ) ;
158159 } ) ;
160+
161+ describe ( 'kts-basic-gradle9 fixture tests - requires JDK 17+' , ( ) => {
162+ const isGradle9Compatible = jdkVersionAtLeast ( 17 ) ;
163+ const testOrSkip = isGradle9Compatible ? test : test . skip ;
164+
165+ // Sanitizes DepGraph data to normalize platform-specific dependencies
166+ // Specifically converts kotlin-native-prebuilt entries with any architecture
167+ // to use macos-aarch64 for consistent test expectations
168+ const sanitizeDepGraphForPlatform = ( depGraphData : DepGraphData ) : any => {
169+ if ( ! depGraphData ) return depGraphData ;
170+
171+ const sanitized = JSON . parse ( JSON . stringify ( depGraphData ) ) ; // deep clone
172+
173+ // Sanitize graph nodes
174+ if ( sanitized . graph ?. nodes ) {
175+ sanitized . graph . nodes = sanitized . graph . nodes . map ( ( node : any ) => {
176+ if ( node . nodeId && node . nodeId . includes ( 'kotlin-native-prebuilt::' ) ) {
177+ // Replace any architecture with macos-aarch64
178+ node . nodeId = node . nodeId . replace (
179+ / k o t l i n - n a t i v e - p r e b u i l t : : [ ^ @ ] + / ,
180+ 'kotlin-native-prebuilt::macos-aarch64' ,
181+ ) ;
182+ }
183+ return node ;
184+ } ) ;
185+ }
186+
187+ // Sanitize dependencies within nodes
188+ if ( sanitized . graph ?. nodes ) {
189+ sanitized . graph . nodes . forEach ( ( node : any ) => {
190+ if ( node . deps ) {
191+ node . deps = node . deps . map ( ( dep : any ) => {
192+ if (
193+ dep . nodeId &&
194+ dep . nodeId . includes ( 'kotlin-native-prebuilt::' )
195+ ) {
196+ dep . nodeId = dep . nodeId . replace (
197+ / k o t l i n - n a t i v e - p r e b u i l t : : [ ^ @ ] + / ,
198+ 'kotlin-native-prebuilt::macos-aarch64' ,
199+ ) ;
200+ }
201+ return dep ;
202+ } ) ;
203+ }
204+ } ) ;
205+ }
206+
207+ return sanitized ;
208+ } ;
209+
210+ const expectSanitizedResult = ( result : any , expectedDepGraphJson : any ) => {
211+ const sanitizedResult = sanitizeDepGraphForPlatform (
212+ result . dependencyGraph ?. toJSON ( ) as DepGraphData ,
213+ ) ;
214+ expect ( sanitizedResult ) . toEqual ( expectedDepGraphJson ) ;
215+ } ;
216+
217+ testOrSkip (
218+ 'basic inspection' ,
219+ async ( ) => {
220+ const fixturePath = getPathToFixture ( 'kts-basic-gradle9' ) ;
221+ const pathToBuildConfig = path . join ( fixturePath , 'build.gradle.kts' ) ;
222+ const expectedDepGraphJson = JSON . parse (
223+ fs . readFileSync ( `${ fixturePath } /dep-graph.json` , 'utf8' ) ,
224+ ) ;
225+
226+ const result = await inspect ( '.' , pathToBuildConfig ) ;
227+ expectSanitizedResult ( result , expectedDepGraphJson ) ;
228+ } ,
229+ 140000 ,
230+ ) ;
231+
232+ testOrSkip (
233+ 'gradleNormalizeDeps' ,
234+ async ( ) => {
235+ jest . spyOn ( search , 'getMavenPackageInfo' ) . mockImplementation (
236+ async (
237+ _sha1 : string ,
238+ depCoords : Partial < PomCoords > ,
239+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
240+ _snykHttpClient : SnykHttpClient ,
241+ ) => {
242+ const type = depCoords . type ?? '' ;
243+ const classifier = depCoords . classifier
244+ ? `:${ depCoords . classifier } `
245+ : '' ;
246+ return `${ depCoords . groupId } :${ depCoords . artifactId } :${ type } ${ classifier } @${ depCoords . version } ` ;
247+ } ,
248+ ) ;
249+ const fixturePath = getPathToFixture ( 'kts-basic-gradle9' ) ;
250+ const pathToBuildConfig = path . join ( fixturePath , 'build.gradle.kts' ) ;
251+ const expectedDepGraphJson = JSON . parse (
252+ fs . readFileSync ( `${ fixturePath } /dep-graph.json` , 'utf8' ) ,
253+ ) ;
254+
255+ const result = await inspect ( '.' , pathToBuildConfig , {
256+ gradleNormalizeDeps : true ,
257+ } ) ;
258+ expectSanitizedResult ( result , expectedDepGraphJson ) ;
259+ } ,
260+ 140000 ,
261+ ) ;
262+ } ) ;
159263} ) ;
0 commit comments