3
3
4
4
const assert = require ( 'assert' )
5
5
const { callFetchWithRetry : callFetch } = require ( '../../../lib/fetch' )
6
- const { devApiBaseUrl, prodApiBaseUrl , getComponents, origins } = require ( '../testConfig' )
6
+ const { devApiBaseUrl, getComponents, origins } = require ( '../testConfig' )
7
7
8
8
const ORIGIN_EXCLUSION_LIST = [ 'go/golang' , 'debsrc/debian' , 'maven/mavengoogle' ]
9
9
const ORIGIN_REVISIONS_EXCLUSION_LIST = [ 'debsrc/debian' ]
10
10
11
- const MAVEN_COMPONENT_GROUP_ID = 'maven/mavencentral/org.apache.httpcomponents'
12
- const MAVEN_COMPONENT_PARTIAL_GROUP_ID = 'maven/mavencentral/ org.apache.httpcompon '
11
+ const MAVEN_COMPONENT_GROUP_ID = 'maven/mavencentral/org.apache.httpcomponents// '
12
+ const MAVEN_COMPONENT_PARTIAL_GROUP_ID = 'maven/org.apache.httpcompone '
13
13
const GRADLE_COMPONENT_ENDPOINT = 'gradleplugin/io.github.lognet.grpc-spring-boot'
14
14
15
15
; ( async function validateOriginsApi ( ) {
@@ -20,52 +20,60 @@ const GRADLE_COMPONENT_ENDPOINT = 'gradleplugin/io.github.lognet.grpc-spring-boo
20
20
21
21
afterEach ( ( ) => new Promise ( resolve => setTimeout ( resolve , origins . timeout / 2 ) ) )
22
22
23
- components . filter ( isOriginAllowed ) . forEach ( component => {
24
- it ( `Validates Origins API response for ${ component } ` , ( ) => compareOrigins ( component ) )
23
+ components . filter ( isComponentIncluded ) . forEach ( component => {
24
+ it ( `Validates Origins API response for ${ component } ` , ( ) => validateOriginResponses ( component ) )
25
25
} )
26
26
27
- components . filter ( isOriginWithRevisionsAllowed ) . forEach ( component => {
28
- it ( `Validates Origins API response with revisions for ${ component } ` , ( ) => compareOriginsWithRevisions ( component ) )
27
+ components . filter ( isComponentIncludedWithRevisions ) . forEach ( component => {
28
+ it ( `Validates Origins API response with revisions for ${ component } ` , ( ) =>
29
+ validateOriginResponsesWithRevisions ( component ) )
29
30
} )
30
31
31
32
it ( 'Validates Origins API response for a Maven component with only a group ID' , ( ) =>
32
- compareOrigins ( MAVEN_COMPONENT_GROUP_ID ) )
33
+ validateOriginResponses ( MAVEN_COMPONENT_GROUP_ID ) )
33
34
34
35
it ( 'Validates Origins API response for a Maven component with a partial group ID for suggestion checks' , ( ) =>
35
- compareOrigins ( MAVEN_COMPONENT_PARTIAL_GROUP_ID ) )
36
+ validateEndpointWithRevisions ( MAVEN_COMPONENT_PARTIAL_GROUP_ID , 'httpcore' ) )
36
37
37
38
it ( 'Validates Origins API response for a Gradle plugin component' , ( ) =>
38
- compareEndpoints ( GRADLE_COMPONENT_ENDPOINT ) )
39
+ validateEndpointResponses ( GRADLE_COMPONENT_ENDPOINT , 'io.github.lognet.grpc-spring-boot' ) )
39
40
40
41
it ( 'Validates Origins API with revisions response for a Gradle plugin component' , ( ) =>
41
- compareEndpoints ( ` ${ GRADLE_COMPONENT_ENDPOINT } /revisions` ) )
42
+ validateEndpointWithRevisions ( GRADLE_COMPONENT_ENDPOINT , '4.6.0' ) )
42
43
} )
43
44
} ) ( )
44
45
45
- function extractIds ( response ) {
46
- return response . map ( item => item . id )
46
+ function isValueInResponse ( response , value ) {
47
+ return response . some ( item => item ? .id ?. includes ( value ) )
47
48
}
48
49
49
- function assertResponsesMatch ( actual , expected ) {
50
- const sortedActualIds = extractIds ( actual ) . sort ( )
51
- const sortedExpectedIds = extractIds ( expected ) . sort ( )
50
+ function isRevisionInResponse ( response , value ) {
51
+ return response . some ( item => item ?. sha ?. includes ( value ) ) || response . some ( item => item . includes ( value ) )
52
+ }
53
+
54
+ function assertPackageResponse ( actual , value ) {
55
+ assert . ok ( actual . length > 0 , `No matching package returned` )
56
+ assert . ok ( isValueInResponse ( actual , value ) , `Response does not contain expected package` )
57
+ }
52
58
53
- assert . deepStrictEqual ( sortedActualIds , sortedExpectedIds )
59
+ function assertRevisionResponse ( actual , value ) {
60
+ assert . ok ( actual . length > 0 , `No matching version returned` )
61
+ assert . ok ( isRevisionInResponse ( actual , value ) , `Response does not contain expected version` )
54
62
}
55
63
56
- function isCoordinateAllowed ( coordinate , exclusionList ) {
57
- return ! exclusionList . some ( excluded => coordinate . includes ( excluded ) )
64
+ function exclusionFilter ( item , exclusionList ) {
65
+ return ! exclusionList . some ( excluded => item . includes ( excluded ) )
58
66
}
59
67
60
- function isOriginAllowed ( coordinate ) {
61
- return isCoordinateAllowed ( coordinate , ORIGIN_EXCLUSION_LIST )
68
+ function isComponentIncluded ( coordinate ) {
69
+ return exclusionFilter ( coordinate , ORIGIN_EXCLUSION_LIST )
62
70
}
63
71
64
- function isOriginWithRevisionsAllowed ( coordinate ) {
65
- return isCoordinateAllowed ( coordinate , ORIGIN_REVISIONS_EXCLUSION_LIST )
72
+ function isComponentIncludedWithRevisions ( coordinate ) {
73
+ return exclusionFilter ( coordinate , ORIGIN_REVISIONS_EXCLUSION_LIST )
66
74
}
67
75
68
- function getProviderType ( type , provider ) {
76
+ function resolveProviderType ( type , provider ) {
69
77
switch ( type ) {
70
78
case 'git' :
71
79
case 'gem' :
@@ -81,8 +89,13 @@ function getProviderType(type, provider) {
81
89
82
90
function parseCoordinates ( coordinates ) {
83
91
const [ type , provider , namespaceOrEmpty , name , version ] = coordinates . split ( '/' )
84
- const namespace = namespaceOrEmpty === '-' ? '' : namespaceOrEmpty
85
- return { type, provider, namespace, name, version }
92
+ return {
93
+ type,
94
+ provider,
95
+ namespace : namespaceOrEmpty === '-' ? '' : namespaceOrEmpty ,
96
+ name,
97
+ version
98
+ }
86
99
}
87
100
88
101
function buildCondaUrl ( coordinates ) {
@@ -92,24 +105,32 @@ function buildCondaUrl(coordinates) {
92
105
93
106
function buildOriginUrl ( coordinates ) {
94
107
const { type, provider, namespace, name } = parseCoordinates ( coordinates )
95
- const resolvedType = getProviderType ( type , provider )
108
+ const resolvedType = resolveProviderType ( type , provider )
96
109
return `${ resolvedType } ${ namespace ? `/${ namespace } ` : '' } ${ name ? `/${ name } ` : '' } `
97
110
}
98
111
99
- async function compareEndpoints ( endpoint ) {
100
- const [ devResponse , prodResponse ] = await Promise . all ( [
101
- callFetch ( `${ devApiBaseUrl } /origins/${ endpoint } ` ) . then ( res => res . json ( ) ) ,
102
- callFetch ( `${ prodApiBaseUrl } /origins/${ endpoint } ` ) . then ( res => res . json ( ) )
103
- ] )
104
- assertResponsesMatch ( devResponse , prodResponse )
112
+ async function validateEndpointResponses ( endpoint , expectedValue ) {
113
+ const devResponse = await callFetch ( `${ devApiBaseUrl } /origins/${ endpoint } ` ) . then ( res => res . json ( ) )
114
+ assertPackageResponse ( devResponse , expectedValue )
105
115
}
106
116
107
- async function compareOriginsWithRevisions ( coordinates ) {
117
+ async function validateEndpointWithRevisions ( endpoint , expectedValue ) {
118
+ const devResponse = await callFetch ( `${ devApiBaseUrl } /origins/${ endpoint } /revisions` ) . then ( res => res . json ( ) )
119
+ assertRevisionResponse ( devResponse , expectedValue )
120
+ }
121
+
122
+ async function validateOriginResponsesWithRevisions ( coordinates ) {
108
123
const originUrl = buildOriginUrl ( coordinates )
109
- await compareEndpoints ( `${ originUrl } /revisions` )
124
+ const version = normalizeVersion ( coordinates . split ( '/' ) . at ( - 1 ) )
125
+ await validateEndpointWithRevisions ( originUrl , version )
110
126
}
111
127
112
- async function compareOrigins ( coordinates ) {
128
+ async function validateOriginResponses ( coordinates ) {
113
129
const originUrl = coordinates . startsWith ( 'conda/' ) ? buildCondaUrl ( coordinates ) : buildOriginUrl ( coordinates )
114
- await compareEndpoints ( `${ originUrl } ` )
130
+ const componentName = coordinates . split ( '/' ) . at ( - 2 )
131
+ await validateEndpointResponses ( originUrl , componentName )
132
+ }
133
+
134
+ function normalizeVersion ( version ) {
135
+ return version . replace ( / _ [ \w ] + $ / , '' ) . replace ( / ^ v / , '' )
115
136
}
0 commit comments