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