|
| 1 | +//go:build !integration |
| 2 | +// +build !integration |
| 3 | + |
| 4 | +package pip |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/snyk/cli-extension-dep-graph/pkg/ecosystems" |
| 13 | +) |
| 14 | + |
| 15 | +func TestReport_ToDepgraph(t *testing.T) { |
| 16 | + t.Run("simple report with direct and transitive deps", func(t *testing.T) { |
| 17 | + report := &Report{ |
| 18 | + Install: []InstallItem{ |
| 19 | + { |
| 20 | + Metadata: PackageMetadata{ |
| 21 | + Name: "requests", |
| 22 | + Version: "2.31.0", |
| 23 | + RequiresDist: []string{ |
| 24 | + "urllib3 (<3,>=1.21.1)", |
| 25 | + "certifi (>=2017.4.17)", |
| 26 | + }, |
| 27 | + }, |
| 28 | + Requested: true, // Direct dependency |
| 29 | + }, |
| 30 | + { |
| 31 | + Metadata: PackageMetadata{ |
| 32 | + Name: "urllib3", |
| 33 | + Version: "2.0.4", |
| 34 | + RequiresDist: []string{ |
| 35 | + "certifi", |
| 36 | + }, |
| 37 | + }, |
| 38 | + Requested: false, // Transitive dependency |
| 39 | + }, |
| 40 | + { |
| 41 | + Metadata: PackageMetadata{ |
| 42 | + Name: "certifi", |
| 43 | + Version: "2023.7.22", |
| 44 | + RequiresDist: []string{}, // Leaf package |
| 45 | + }, |
| 46 | + Requested: false, // Transitive dependency |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + depgraph, err := report.ToDepgraph() |
| 52 | + require.NoError(t, err) |
| 53 | + |
| 54 | + // Verify structure |
| 55 | + assert.Equal(t, ecosystems.PackageID("root"), depgraph.RootPackageID) |
| 56 | + assert.Len(t, depgraph.Packages, 3) |
| 57 | + assert.Len(t, depgraph.Graph, 4) // 3 packages + root |
| 58 | + |
| 59 | + // Verify root points to direct dependency |
| 60 | + assert. Equal( t, []ecosystems. PackageID{ "[email protected]"}, depgraph. Graph[ "root"]) |
| 61 | + |
| 62 | + // Verify dependency chain |
| 63 | + assert. ElementsMatch( t, []ecosystems. PackageID{ "[email protected]", "[email protected]"}, |
| 64 | + depgraph. Graph[ "[email protected]"]) |
| 65 | + assert. Equal( t, []ecosystems. PackageID{ "[email protected]"}, |
| 66 | + depgraph. Graph[ "[email protected]"]) |
| 67 | + assert. Empty( t, depgraph. Graph[ "[email protected]"]) |
| 68 | + }) |
| 69 | + |
| 70 | + t.Run("multiple direct dependencies", func(t *testing.T) { |
| 71 | + report := &Report{ |
| 72 | + Install: []InstallItem{ |
| 73 | + { |
| 74 | + Metadata: PackageMetadata{ |
| 75 | + Name: "requests", |
| 76 | + Version: "2.31.0", |
| 77 | + RequiresDist: []string{}, |
| 78 | + }, |
| 79 | + Requested: true, |
| 80 | + }, |
| 81 | + { |
| 82 | + Metadata: PackageMetadata{ |
| 83 | + Name: "flask", |
| 84 | + Version: "2.3.0", |
| 85 | + RequiresDist: []string{}, |
| 86 | + }, |
| 87 | + Requested: true, |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + depgraph, err := report.ToDepgraph() |
| 93 | + require.NoError(t, err) |
| 94 | + |
| 95 | + assert.Len(t, depgraph.Packages, 2) |
| 96 | + assert. ElementsMatch( t, []ecosystems. PackageID{ "[email protected]", "[email protected]"}, |
| 97 | + depgraph.Graph["root"]) |
| 98 | + }) |
| 99 | + |
| 100 | + t.Run("empty report", func(t *testing.T) { |
| 101 | + report := &Report{ |
| 102 | + Install: []InstallItem{}, |
| 103 | + } |
| 104 | + |
| 105 | + depgraph, err := report.ToDepgraph() |
| 106 | + require.NoError(t, err) |
| 107 | + |
| 108 | + assert.Empty(t, depgraph.Packages) |
| 109 | + assert.Empty(t, depgraph.Graph["root"]) |
| 110 | + }) |
| 111 | + |
| 112 | + t.Run("nil report", func(t *testing.T) { |
| 113 | + var report *Report |
| 114 | + _, err := report.ToDepgraph() |
| 115 | + assert.Error(t, err) |
| 116 | + assert.Contains(t, err.Error(), "cannot be nil") |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +func TestExtractPackageName(t *testing.T) { |
| 121 | + tests := map[string]struct { |
| 122 | + depString string |
| 123 | + want string |
| 124 | + }{ |
| 125 | + "with_constraints": {"urllib3 (<3,>=1.21.1)", "urllib3"}, |
| 126 | + "with_extras": {"requests[security] (>=2.20.0)", "requests"}, |
| 127 | + "special_chars": {"some-package_name.py (>=1.0)", "some-package_name.py"}, |
| 128 | + "no_constraints": {"certifi", "certifi"}, |
| 129 | + "empty": {"", ""}, |
| 130 | + "no_space_version": {"idna>=3.3", "idna"}, |
| 131 | + "with_extra_marker": {"mypy; extra == \"dev\"", "mypy"}, |
| 132 | + "hyphenated_with_extra": {"pre-commit; extra == \"dev\"", "pre-commit"}, |
| 133 | + "hyphenated_with_marker": {"pytest-cov; extra == \"dev\"", "pytest-cov"}, |
| 134 | + "multiple_hyphens": {"pytest-socket; extra == \"dev\"", "pytest-socket"}, |
| 135 | + "simple_with_extra": {"pytest; extra == \"dev\"", "pytest"}, |
| 136 | + "single_char_with_marker": {"ruff; extra == \"dev\"", "ruff"}, |
| 137 | + } |
| 138 | + |
| 139 | + for name, tt := range tests { |
| 140 | + t.Run(name, func(t *testing.T) { |
| 141 | + assert.Equal(t, tt.want, extractPackageName(tt.depString)) |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func TestToPackageID(t *testing.T) { |
| 147 | + tests := map[string]struct { |
| 148 | + name string |
| 149 | + version string |
| 150 | + want ecosystems.PackageID |
| 151 | + }{ |
| 152 | + "standard package": { |
| 153 | + name: "requests", |
| 154 | + version: "2.31.0", |
| 155 | + |
| 156 | + }, |
| 157 | + "package with dash": { |
| 158 | + name: "some-package", |
| 159 | + version: "1.0.0", |
| 160 | + |
| 161 | + }, |
| 162 | + "empty version": { |
| 163 | + name: "package", |
| 164 | + version: "", |
| 165 | + want: "package@", // toPackageID doesn't handle fallback |
| 166 | + }, |
| 167 | + } |
| 168 | + |
| 169 | + for name, tt := range tests { |
| 170 | + t.Run(name, func(t *testing.T) { |
| 171 | + got := toPackageID(tt.name, tt.version) |
| 172 | + assert.Equal(t, tt.want, got) |
| 173 | + }) |
| 174 | + } |
| 175 | +} |
0 commit comments