github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/format/internal/testutil/directory_input.go (about) 1 package testutil 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 10 "github.com/anchore/syft/syft/cpe" 11 "github.com/anchore/syft/syft/file" 12 "github.com/anchore/syft/syft/linux" 13 "github.com/anchore/syft/syft/pkg" 14 "github.com/anchore/syft/syft/sbom" 15 "github.com/anchore/syft/syft/source/directorysource" 16 ) 17 18 func DirectoryInput(t testing.TB, dir string) sbom.SBOM { 19 catalog := newDirectoryCatalog() 20 21 path := filepath.Join(dir, "some", "path") 22 23 require.NoError(t, os.MkdirAll(path, 0755)) 24 25 src, err := directorysource.New( 26 directorysource.Config{ 27 Path: path, 28 Base: dir, 29 }, 30 ) 31 require.NoError(t, err) 32 33 return sbom.SBOM{ 34 Artifacts: sbom.Artifacts{ 35 Packages: catalog, 36 LinuxDistribution: &linux.Release{ 37 PrettyName: "debian", 38 Name: "debian", 39 ID: "debian", 40 IDLike: []string{"like!"}, 41 Version: "1.2.3", 42 VersionID: "1.2.3", 43 }, 44 }, 45 Source: src.Describe(), 46 Descriptor: sbom.Descriptor{ 47 Name: "syft", 48 Version: "v0.42.0-bogus", 49 // the application configuration should be persisted here, however, we do not want to import 50 // the application configuration in this package (it's reserved only for ingestion by the cmd package) 51 Configuration: map[string]string{ 52 "config-key": "config-value", 53 }, 54 }, 55 } 56 } 57 58 func DirectoryInputWithAuthorField(t testing.TB) sbom.SBOM { 59 catalog := newDirectoryCatalogWithAuthorField() 60 61 dir := t.TempDir() 62 path := filepath.Join(dir, "some", "path") 63 64 require.NoError(t, os.MkdirAll(path, 0755)) 65 66 src, err := directorysource.New( 67 directorysource.Config{ 68 Path: path, 69 Base: dir, 70 }, 71 ) 72 require.NoError(t, err) 73 74 return sbom.SBOM{ 75 Artifacts: sbom.Artifacts{ 76 Packages: catalog, 77 LinuxDistribution: &linux.Release{ 78 PrettyName: "debian", 79 Name: "debian", 80 ID: "debian", 81 IDLike: []string{"like!"}, 82 Version: "1.2.3", 83 VersionID: "1.2.3", 84 }, 85 }, 86 Source: src.Describe(), 87 Descriptor: sbom.Descriptor{ 88 Name: "syft", 89 Version: "v0.42.0-bogus", 90 // the application configuration should be persisted here, however, we do not want to import 91 // the application configuration in this package (it's reserved only for ingestion by the cmd package) 92 Configuration: map[string]string{ 93 "config-key": "config-value", 94 }, 95 }, 96 } 97 } 98 99 func newDirectoryCatalog() *pkg.Collection { 100 catalog := pkg.NewCollection() 101 102 // populate catalog with test data 103 catalog.Add(pkg.Package{ 104 Name: "package-1", 105 Version: "1.0.1", 106 Type: pkg.PythonPkg, 107 FoundBy: "the-cataloger-1", 108 Locations: file.NewLocationSet( 109 file.NewLocation("/some/path/pkg1"), 110 ), 111 Language: pkg.Python, 112 Licenses: pkg.NewLicenseSet( 113 pkg.NewLicense("MIT"), 114 ), 115 Metadata: pkg.PythonPackage{ 116 Name: "package-1", 117 Version: "1.0.1", 118 Files: []pkg.PythonFileRecord{ 119 { 120 Path: "/some/path/pkg1/dependencies/foo", 121 }, 122 }, 123 }, 124 PURL: "a-purl-2", // intentionally a bad pURL for test fixtures 125 CPEs: []cpe.CPE{ 126 cpe.Must("cpe:2.3:*:some:package:2:*:*:*:*:*:*:*", cpe.Source("")), 127 }, 128 }) 129 catalog.Add(pkg.Package{ 130 Name: "package-2", 131 Version: "2.0.1", 132 Type: pkg.DebPkg, 133 FoundBy: "the-cataloger-2", 134 Locations: file.NewLocationSet( 135 file.NewLocation("/some/path/pkg1"), 136 ), 137 Metadata: pkg.DpkgDBEntry{ 138 Package: "package-2", 139 Version: "2.0.1", 140 }, 141 PURL: "pkg:deb/debian/package-2@2.0.1", 142 CPEs: []cpe.CPE{ 143 cpe.Must("cpe:2.3:*:some:package:2:*:*:*:*:*:*:*", cpe.Source("")), 144 }, 145 }) 146 147 return catalog 148 } 149 150 func newDirectoryCatalogWithAuthorField() *pkg.Collection { 151 catalog := pkg.NewCollection() 152 153 // populate catalog with test data 154 catalog.Add(pkg.Package{ 155 Name: "package-1", 156 Version: "1.0.1", 157 Type: pkg.PythonPkg, 158 FoundBy: "the-cataloger-1", 159 Locations: file.NewLocationSet( 160 file.NewLocation("/some/path/pkg1"), 161 ), 162 Language: pkg.Python, 163 Licenses: pkg.NewLicenseSet( 164 pkg.NewLicense("MIT"), 165 ), 166 Metadata: pkg.PythonPackage{ 167 Name: "package-1", 168 Version: "1.0.1", 169 Author: "test-author", 170 Files: []pkg.PythonFileRecord{ 171 { 172 Path: "/some/path/pkg1/dependencies/foo", 173 }, 174 }, 175 }, 176 PURL: "a-purl-2", // intentionally a bad pURL for test fixtures 177 CPEs: []cpe.CPE{ 178 cpe.Must("cpe:2.3:*:some:package:2:*:*:*:*:*:*:*", cpe.GeneratedSource), 179 }, 180 }) 181 catalog.Add(pkg.Package{ 182 Name: "package-2", 183 Version: "2.0.1", 184 Type: pkg.DebPkg, 185 FoundBy: "the-cataloger-2", 186 Locations: file.NewLocationSet( 187 file.NewLocation("/some/path/pkg1"), 188 ), 189 Metadata: pkg.DpkgDBEntry{ 190 Package: "package-2", 191 Version: "2.0.1", 192 }, 193 PURL: "pkg:deb/debian/package-2@2.0.1", 194 CPEs: []cpe.CPE{ 195 cpe.Must("cpe:2.3:*:some:package:2:*:*:*:*:*:*:*", "another-test-source"), 196 }, 197 }) 198 199 return catalog 200 }