1
- import ComponentDetection from "./componentDetection" ;
1
+ import ComponentDetection , { DependencyGraphs } from "./componentDetection" ;
2
2
import fs from "fs" ;
3
3
4
4
test ( "Downloads CLI" , async ( ) => {
@@ -70,7 +70,7 @@ describe("ComponentDetection.makePackageUrl", () => {
70
70
} ) ;
71
71
72
72
describe ( "ComponentDetection.processComponentsToManifests" , ( ) => {
73
- test ( "adds package as direct dependency when no top level referrers " , ( ) => {
73
+ test ( "adds package as direct dependency when it is listed as an explicitlyReferencedComponentIds " , ( ) => {
74
74
const componentsFound = [
75
75
{
76
76
component : {
@@ -86,20 +86,29 @@ describe("ComponentDetection.processComponentsToManifests", () => {
86
86
} ,
87
87
isDevelopmentDependency : false ,
88
88
topLevelReferrers : [ ] , // Empty = direct dependency
89
- locationsFoundAt : [ "package.json" ]
89
+ locationsFoundAt : [ "/ package.json" ]
90
90
}
91
91
] ;
92
92
93
- const manifests = ComponentDetection . processComponentsToManifests ( componentsFound ) ;
93
+ const dependencyGraphs : DependencyGraphs = {
94
+ "/package.json" : {
95
+ graph : { "test-package" : null } ,
96
+ explicitlyReferencedComponentIds : [ "test-package 1.0.0 - npm" ] ,
97
+ developmentDependencies : [ ] ,
98
+ dependencies : [ ]
99
+ }
100
+ } ;
101
+
102
+ const manifests = ComponentDetection . processComponentsToManifests ( componentsFound , dependencyGraphs ) ;
94
103
95
104
expect ( manifests ) . toHaveLength ( 1 ) ;
96
- expect ( manifests [ 0 ] . name ) . toBe ( "package.json" ) ;
105
+ expect ( manifests [ 0 ] . name ) . toBe ( "/ package.json" ) ;
97
106
expect ( manifests [ 0 ] . directDependencies ( ) ) . toHaveLength ( 1 ) ;
98
107
expect ( manifests [ 0 ] . indirectDependencies ( ) ) . toHaveLength ( 0 ) ;
99
108
expect ( manifests [ 0 ] . countDependencies ( ) ) . toBe ( 1 ) ;
100
109
} ) ;
101
110
102
- test ( "adds package as indirect dependency when has top level referrers " , ( ) => {
111
+ test ( "adds package as indirect dependency when it is not in explicitlyReferencedComponentIds " , ( ) => {
103
112
const componentsFound = [
104
113
{
105
114
component : {
@@ -126,56 +135,75 @@ describe("ComponentDetection.processComponentsToManifests", () => {
126
135
}
127
136
}
128
137
] ,
129
- locationsFoundAt : [ "package.json" ]
138
+ locationsFoundAt : [ "/ package.json" ]
130
139
}
131
140
] ;
132
141
133
- const manifests = ComponentDetection . processComponentsToManifests ( componentsFound ) ;
142
+ const dependencyGraphs : DependencyGraphs = {
143
+ "/package.json" : {
144
+ graph : { "parent-package" : null } ,
145
+ explicitlyReferencedComponentIds : [ ] ,
146
+ developmentDependencies : [ ] ,
147
+ dependencies : [ ]
148
+ }
149
+ } ;
150
+
151
+ const manifests = ComponentDetection . processComponentsToManifests ( componentsFound , dependencyGraphs ) ;
134
152
135
153
expect ( manifests ) . toHaveLength ( 1 ) ;
136
- expect ( manifests [ 0 ] . name ) . toBe ( "package.json" ) ;
154
+ expect ( manifests [ 0 ] . name ) . toBe ( "/ package.json" ) ;
137
155
expect ( manifests [ 0 ] . directDependencies ( ) ) . toHaveLength ( 0 ) ;
138
156
expect ( manifests [ 0 ] . indirectDependencies ( ) ) . toHaveLength ( 1 ) ;
139
157
expect ( manifests [ 0 ] . countDependencies ( ) ) . toBe ( 1 ) ;
140
158
} ) ;
159
+ } ) ;
141
160
142
- test ( "adds package as direct dependency when top level referrer is itself" , ( ) => {
143
- const componentsFound = [
144
- {
145
- component : {
146
- name : "test-package" ,
147
- version : "1.0.0" ,
148
- packageUrl : {
149
- Scheme : "pkg" ,
150
- Type : "npm" ,
151
- Name : "test-package" ,
152
- Version : "1.0.0"
153
- } ,
154
- id : "test-package 1.0.0 - npm"
155
- } ,
156
- isDevelopmentDependency : false ,
157
- topLevelReferrers : [
158
- {
159
- name : "test-package" ,
160
- version : "1.0.0" ,
161
- packageUrl : {
162
- Scheme : "pkg" ,
163
- Type : "npm" ,
164
- Name : "test-package" ,
165
- Version : "1.0.0"
166
- }
167
- }
168
- ] ,
169
- locationsFoundAt : [ "package.json" ]
161
+ describe ( 'normalizeDependencyGraphPaths' , ( ) => {
162
+ test ( 'converts absolute paths to relative paths based on filePath input' , ( ) => {
163
+ // Simulate a repo at /repo and a scan root at /repo/packages
164
+ const fakeCwd = '/workspaces' ;
165
+ const filePathInput = 'my-super-cool-repo' ;
166
+ const absBase = '/workspaces/my-super-cool-repo' ;
167
+ const dependencyGraphs : DependencyGraphs = {
168
+ '/workspaces/my-super-cool-repo/a/package.json' : {
169
+ graph : { 'foo' : null } ,
170
+ explicitlyReferencedComponentIds : [ ] ,
171
+ developmentDependencies : [ ] ,
172
+ dependencies : [ ]
173
+ } ,
174
+ '/workspaces/my-super-cool-repo/b/package.json' : {
175
+ graph : { 'bar' : null } ,
176
+ explicitlyReferencedComponentIds : [ ] ,
177
+ developmentDependencies : [ ] ,
178
+ dependencies : [ ]
170
179
}
171
- ] ;
172
-
173
- const manifests = ComponentDetection . processComponentsToManifests ( componentsFound ) ;
180
+ } ;
181
+ // Patch process.cwd for this test
182
+ const originalCwd = process . cwd ;
183
+ ( process as any ) . cwd = ( ) => fakeCwd ;
184
+ const normalized = ComponentDetection . normalizeDependencyGraphPaths ( dependencyGraphs , filePathInput ) ;
185
+ // Restore process.cwd
186
+ ( process as any ) . cwd = originalCwd ;
187
+ expect ( Object . keys ( normalized ) ) . toContain ( '/a/package.json' ) ;
188
+ expect ( Object . keys ( normalized ) ) . toContain ( '/b/package.json' ) ;
189
+ expect ( normalized [ '/a/package.json' ] . graph ) . toEqual ( { 'foo' : null } ) ;
190
+ expect ( normalized [ '/b/package.json' ] . graph ) . toEqual ( { 'bar' : null } ) ;
191
+ } ) ;
192
+ } ) ;
174
193
175
- expect ( manifests ) . toHaveLength ( 1 ) ;
176
- expect ( manifests [ 0 ] . name ) . toBe ( "package.json" ) ;
177
- expect ( manifests [ 0 ] . directDependencies ( ) ) . toHaveLength ( 1 ) ;
178
- expect ( manifests [ 0 ] . indirectDependencies ( ) ) . toHaveLength ( 0 ) ;
179
- expect ( manifests [ 0 ] . countDependencies ( ) ) . toBe ( 1 ) ;
194
+ describe ( 'normalizeDependencyGraphPaths with real output.json' , ( ) => {
195
+ test ( 'converts absolute paths in output.json to relative paths using current cwd and filePath' , ( ) => {
196
+ const output = JSON . parse ( fs . readFileSync ( './output.json' , 'utf8' ) ) ;
197
+ const dependencyGraphs = output . dependencyGraphs ;
198
+ // Use the same filePath as the action default (".")
199
+ const normalized = ComponentDetection . normalizeDependencyGraphPaths ( dependencyGraphs , 'test' ) ;
200
+ // Should contain /package.json and /package-lock.json as keys
201
+ expect ( Object . keys ( normalized ) ) . toContain ( '/package.json' ) ;
202
+ expect ( Object . keys ( normalized ) ) . toContain ( '/package-lock.json' ) ;
203
+ // All keys should now be relative to the repo root (cwd) and start with '/'
204
+ for ( const key of Object . keys ( normalized ) ) {
205
+ expect ( key . startsWith ( '/' ) ) . toBe ( true ) ;
206
+ expect ( key ) . not . toMatch ( / ^ \w : \\ | ^ \/ \/ | ^ \. { 1 , 2 } \/ / ) ; // Not windows absolute, not network, not relative
207
+ }
180
208
} ) ;
181
209
} ) ;
0 commit comments