@@ -349,6 +349,59 @@ def test_attribution_partitions_direct_vs_deep():
349349 assert deep_names == ["b" ]
350350
351351
352+ def test_dependency_paths_direct_package_is_name_only ():
353+ from socketsecurity .fossa_compat import _compute_dependency_paths
354+ pkg = Package (
355+ type = "pypi" , name = "requests" , version = "2.31.0" ,
356+ id = "pip+requests$2.31.0" , score = {}, alerts = [], direct = True ,
357+ )
358+ paths = _compute_dependency_paths (pkg , {"pip+requests$2.31.0" : pkg })
359+ assert paths == ["requests" ]
360+
361+
362+ def test_dependency_paths_transitive_chains_through_ancestor_name ():
363+ from socketsecurity .fossa_compat import _compute_dependency_paths
364+ parent = Package (
365+ type = "pypi" , name = "requests" , version = "2.31.0" ,
366+ id = "parent-id" , score = {}, alerts = [], direct = True ,
367+ )
368+ child = Package (
369+ type = "pypi" , name = "certifi" , version = "2024.7.4" ,
370+ id = "child-id" , score = {}, alerts = [], direct = False ,
371+ topLevelAncestors = ["parent-id" ],
372+ )
373+ lookup = {"parent-id" : parent , "child-id" : child }
374+ assert _compute_dependency_paths (child , lookup ) == ["requests > certifi" ]
375+
376+
377+ def test_dependency_paths_multi_ancestor_emits_one_per_root ():
378+ from socketsecurity .fossa_compat import _compute_dependency_paths
379+ p1 = Package (type = "pypi" , name = "boto3" , version = "1.0" , id = "p1" ,
380+ score = {}, alerts = [], direct = True )
381+ p2 = Package (type = "pypi" , name = "botocore" , version = "1.0" , id = "p2" ,
382+ score = {}, alerts = [], direct = True )
383+ child = Package (
384+ type = "pypi" , name = "jmespath" , version = "1.0" , id = "c" ,
385+ score = {}, alerts = [], direct = False ,
386+ topLevelAncestors = ["p1" , "p2" ],
387+ )
388+ lookup = {"p1" : p1 , "p2" : p2 , "c" : child }
389+ assert sorted (_compute_dependency_paths (child , lookup )) == [
390+ "boto3 > jmespath" ,
391+ "botocore > jmespath" ,
392+ ]
393+
394+
395+ def test_dependency_paths_missing_ancestor_falls_back_to_name ():
396+ from socketsecurity .fossa_compat import _compute_dependency_paths
397+ pkg = Package (
398+ type = "pypi" , name = "orphan" , version = "1.0" , id = "o" ,
399+ score = {}, alerts = [], direct = False ,
400+ topLevelAncestors = ["missing-id" ],
401+ )
402+ assert _compute_dependency_paths (pkg , {"o" : pkg }) == ["orphan" ]
403+
404+
352405def test_vulnerability_version_ranges_sourced_from_socket_fields ():
353406 """affectedVersionRanges/patchedVersionRanges come from Socket's singular fields, wrapped."""
354407 from socketsecurity .fossa_compat import _build_vulnerability_entry
0 commit comments