@@ -27,8 +27,7 @@ func TestBuildPipDependencyListSetuppy(t *testing.T) {
2727 defer cleanUp ()
2828 // Run getModulesDependencyTrees
2929 params := clisecurityutils.AuditBasicParams {}
30- params .AddTechnologyIfNotExist (techutils .Pip .String ())
31- rootNode , uniqueDeps , _ , err := BuildDependencyTree (& params )
30+ rootNode , uniqueDeps , _ , err := BuildDependencyTree (& params , techutils .Pip )
3231 assert .NoError (t , err )
3332 assert .Contains (t , uniqueDeps , PythonPackageTypeIdentifier + "pexpect:4.8.0" )
3433 assert .Contains (t , uniqueDeps , PythonPackageTypeIdentifier + "ptyprocess:0.7.0" )
@@ -55,9 +54,8 @@ func TestPipDependencyListCustomInstallArgs(t *testing.T) {
5554 assert .NoError (t , os .Chdir (filepath .Join (actualMainPath , "referenceproject" )))
5655 // Run getModulesDependencyTrees
5756 params := clisecurityutils.AuditBasicParams {}
58- params .AddTechnologyIfNotExist (techutils .Pip .String ())
5957 params .SetInstallCommandArgs ([]string {"--force-reinstall" })
60- rootNode , uniqueDeps , _ , err := BuildDependencyTree (& params )
58+ rootNode , uniqueDeps , _ , err := BuildDependencyTree (& params , techutils . Pip )
6159 validatePipRequirementsProject (t , err , uniqueDeps , rootNode )
6260}
6361
@@ -67,9 +65,8 @@ func TestBuildPipDependencyListSetuppyForCuration(t *testing.T) {
6765 defer cleanUp ()
6866 // Run getModulesDependencyTrees
6967 params := clisecurityutils.AuditBasicParams {}
70- params .AddTechnologyIfNotExist (techutils .Pip .String ())
7168 params .SetIsCurationCmd (true )
72- rootNode , uniqueDeps , downloadUrls , err := BuildDependencyTree (& params )
69+ rootNode , uniqueDeps , downloadUrls , err := BuildDependencyTree (& params , techutils . Pip )
7370 assert .NoError (t , err )
7471 assert .Contains (t , uniqueDeps , PythonPackageTypeIdentifier + "pexpect:4.8.0" )
7572 assert .Contains (t , uniqueDeps , PythonPackageTypeIdentifier + "ptyprocess:0.7.0" )
@@ -100,8 +97,7 @@ func TestPipDependencyListRequirementsFallback(t *testing.T) {
10097 defer cleanUp ()
10198 // No requirements file field specified, expect the command to use the fallback 'pip install -r requirements.txt' command
10299 params := clisecurityutils.AuditBasicParams {}
103- params .AddTechnologyIfNotExist (techutils .Pip .String ())
104- rootNode , uniqueDeps , _ , err := BuildDependencyTree (& params )
100+ rootNode , uniqueDeps , _ , err := BuildDependencyTree (& params , techutils .Pip )
105101 validatePipRequirementsProject (t , err , uniqueDeps , rootNode )
106102}
107103
@@ -125,9 +121,8 @@ func TestBuildPipDependencyListRequirements(t *testing.T) {
125121 defer cleanUp ()
126122 // Run getModulesDependencyTrees
127123 params := clisecurityutils.AuditBasicParams {}
128- params .AddTechnologyIfNotExist (techutils .Pip .String ())
129124 params .SetPipRequirementsFile ("requirements.txt" )
130- rootNode , uniqueDeps , _ , err := BuildDependencyTree (& params )
125+ rootNode , uniqueDeps , _ , err := BuildDependencyTree (& params , techutils . Pip )
131126 assert .NoError (t , err )
132127 assert .Contains (t , uniqueDeps , PythonPackageTypeIdentifier + "pexpect:4.7.0" )
133128 assert .Contains (t , uniqueDeps , PythonPackageTypeIdentifier + "ptyprocess:0.7.0" )
@@ -154,8 +149,7 @@ func TestBuildPipenvDependencyList(t *testing.T) {
154149 }
155150 // Run getModulesDependencyTrees
156151 params := clisecurityutils.AuditBasicParams {}
157- params .AddTechnologyIfNotExist (techutils .Pipenv .String ())
158- rootNode , uniqueDeps , _ , err := BuildDependencyTree (& params )
152+ rootNode , uniqueDeps , _ , err := BuildDependencyTree (& params , techutils .Pipenv )
159153 if err != nil {
160154 t .Fatal (err )
161155 }
@@ -191,8 +185,7 @@ func TestBuildPoetryDependencyList(t *testing.T) {
191185 }
192186 // Run getModulesDependencyTrees
193187 params := clisecurityutils.AuditBasicParams {}
194- params .AddTechnologyIfNotExist (techutils .Poetry .String ())
195- rootNode , uniqueDeps , _ , err := BuildDependencyTree (& params )
188+ rootNode , uniqueDeps , _ , err := BuildDependencyTree (& params , techutils .Poetry )
196189 if err != nil {
197190 t .Fatal (err )
198191 }
@@ -210,25 +203,38 @@ func TestBuildPoetryDependencyList(t *testing.T) {
210203}
211204
212205func TestBuildDependencyTreeWhenInstallForbidden (t * testing.T ) {
206+ // This feature is currently supported and tested for Pip and Poetry only
213207 testcases := []struct {
214208 name string
215209 testDir string
216- technology string
210+ technology techutils. Technology
217211 installBeforeFetchingInitialDeps bool
218212 }{
213+ // pip
219214 {
220215 name : "pip: project not installed | install forbidden" ,
221216 testDir : filepath .Join ("projects" , "package-managers" , "python" , "pip" , "pip" , "requirementsproject" ),
222- technology : techutils .Pip . String () ,
217+ technology : techutils .Pip ,
223218 installBeforeFetchingInitialDeps : false ,
224219 },
225220 {
226221 name : "pip: project installed before dep tree construction| install forbidden" ,
227222 testDir : filepath .Join ("projects" , "package-managers" , "python" , "pip" , "pip" , "requirementsproject" ),
228- technology : techutils .Pip .String (),
223+ technology : techutils .Pip ,
224+ installBeforeFetchingInitialDeps : true ,
225+ },
226+ {
227+ name : "poetry: project not installed | install forbidden" ,
228+ testDir : filepath .Join ("projects" , "package-managers" , "python" , "poetry" , "poetry" ),
229+ technology : techutils .Poetry ,
230+ installBeforeFetchingInitialDeps : false ,
231+ },
232+ {
233+ name : "poetry: project installed before dep tree construction| install forbidden" ,
234+ testDir : filepath .Join ("projects" , "package-managers" , "python" , "poetry" , "poetry" ),
235+ technology : techutils .Poetry ,
229236 installBeforeFetchingInitialDeps : true ,
230237 },
231- // TODO add similar test cases for pipenv and poetry
232238 }
233239
234240 for _ , test := range testcases {
@@ -239,27 +245,25 @@ func TestBuildDependencyTreeWhenInstallForbidden(t *testing.T) {
239245 // Create virtual env according to package manager if needed
240246 if ! test .installBeforeFetchingInitialDeps {
241247 // If we install before calling BuildDependencyTree a virtual environment is going to be created, and we don't have to do it manually
242- switch test .technology {
243- case techutils .Pip .String ():
248+ if test .technology == techutils .Pip {
244249 restoreEnv , err := SetPipVirtualEnvPath ()
245250 defer func () {
246- assert .NoError (t , restoreEnv (), "restoring env after pip virtual env creation failed" )
251+ assert .NoError (t , restoreEnv (), "restoring env after setting pip virtual env creation failed" )
247252 }()
248253 require .NoError (t , err )
249- default :
250254 }
251255 }
252256
253257 // Setting scan params
254- params := (& clisecurityutils.AuditBasicParams {}).SetSkipAutoInstall (true ). AddTechnologyIfNotExist ( test . technology )
255- if test .technology == techutils .Pip . String () {
258+ params := (& clisecurityutils.AuditBasicParams {}).SetSkipAutoInstall (true )
259+ if test .technology == techutils .Pip {
256260 params .SetPipRequirementsFile ("requirements.txt" )
257261 }
258262
259263 if test .installBeforeFetchingInitialDeps {
260264 restoreEnv , err := runPythonInstall (params , pythonutils .PythonTool (test .technology ))
261265 defer func () {
262- assert .NoError (t , restoreEnv (), "restoring env after pip virtual env creation failed" )
266+ assert .NoError (t , restoreEnv (), "restoring env after setting " + test . technology + " virtual env creation failed" )
263267 }()
264268 require .NoError (t , err )
265269 }
@@ -269,12 +273,21 @@ func TestBuildDependencyTreeWhenInstallForbidden(t *testing.T) {
269273 assert .NoError (t , err )
270274 // We use the dependencies graph and not the list of dependencies since the list includes only direct dependencies
271275 dependenciesGraphBeforeBuildDepTree , _ , err := pythonutils .GetPythonDependencies (pythonutils .PythonTool (test .technology ), testDir , localDependenciesPath , log .GetLogger ())
272- print (dependenciesGraphBeforeBuildDepTree )
273- dependenciesBeforeBuildDepTree := maps .Keys (dependenciesGraphBeforeBuildDepTree )
274276 assert .NoError (t , err )
275277
278+ var dependenciesBeforeBuildDepTree []string
279+ switch test .technology {
280+ case techutils .Pip :
281+ dependenciesBeforeBuildDepTree = maps .Keys (dependenciesGraphBeforeBuildDepTree )
282+ case techutils .Poetry :
283+ if len (dependenciesGraphBeforeBuildDepTree ) != 0 {
284+ mapKey := maps .Keys (dependenciesGraphBeforeBuildDepTree )[0 ]
285+ dependenciesBeforeBuildDepTree = dependenciesGraphBeforeBuildDepTree [mapKey ]
286+ }
287+ }
288+
276289 // Build dependency tree
277- _ , uniqueDeps , _ , err := BuildDependencyTree (params )
290+ _ , uniqueDeps , _ , err := BuildDependencyTree (params , test . technology )
278291 require .NoError (t , err )
279292 var trimmedUniqueDeps []string
280293 for _ , dep := range uniqueDeps {
0 commit comments