-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add template func
hasField
(#1754)
Signed-off-by: Lehman, Alex <[email protected]>
- Loading branch information
Showing
6 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -235,6 +235,37 @@ func DirectoryInput(t testing.TB) sbom.SBOM { | |
} | ||
} | ||
|
||
func DirectoryInputWithAuthorField(t testing.TB) sbom.SBOM { | ||
catalog := newDirectoryCatalogWithAuthorField() | ||
|
||
src, err := source.NewFromDirectory("/some/path") | ||
assert.NoError(t, err) | ||
|
||
return sbom.SBOM{ | ||
Artifacts: sbom.Artifacts{ | ||
PackageCatalog: catalog, | ||
LinuxDistribution: &linux.Release{ | ||
PrettyName: "debian", | ||
Name: "debian", | ||
ID: "debian", | ||
IDLike: []string{"like!"}, | ||
Version: "1.2.3", | ||
VersionID: "1.2.3", | ||
}, | ||
}, | ||
Source: src.Metadata, | ||
Descriptor: sbom.Descriptor{ | ||
Name: "syft", | ||
Version: "v0.42.0-bogus", | ||
// the application configuration should be persisted here, however, we do not want to import | ||
// the application configuration in this package (it's reserved only for ingestion by the cmd package) | ||
Configuration: map[string]string{ | ||
"config-key": "config-value", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func newDirectoryCatalog() *pkg.Catalog { | ||
catalog := pkg.NewCatalog() | ||
|
||
|
@@ -286,6 +317,58 @@ func newDirectoryCatalog() *pkg.Catalog { | |
return catalog | ||
} | ||
|
||
func newDirectoryCatalogWithAuthorField() *pkg.Catalog { | ||
catalog := pkg.NewCatalog() | ||
|
||
// populate catalog with test data | ||
catalog.Add(pkg.Package{ | ||
Name: "package-1", | ||
Version: "1.0.1", | ||
Type: pkg.PythonPkg, | ||
FoundBy: "the-cataloger-1", | ||
Locations: source.NewLocationSet( | ||
source.NewLocation("/some/path/pkg1"), | ||
), | ||
Language: pkg.Python, | ||
MetadataType: pkg.PythonPackageMetadataType, | ||
Licenses: []string{"MIT"}, | ||
Metadata: pkg.PythonPackageMetadata{ | ||
Name: "package-1", | ||
Version: "1.0.1", | ||
Author: "test-author", | ||
Files: []pkg.PythonFileRecord{ | ||
{ | ||
Path: "/some/path/pkg1/dependencies/foo", | ||
}, | ||
}, | ||
}, | ||
PURL: "a-purl-2", // intentionally a bad pURL for test fixtures | ||
CPEs: []cpe.CPE{ | ||
cpe.Must("cpe:2.3:*:some:package:2:*:*:*:*:*:*:*"), | ||
}, | ||
}) | ||
catalog.Add(pkg.Package{ | ||
Name: "package-2", | ||
Version: "2.0.1", | ||
Type: pkg.DebPkg, | ||
FoundBy: "the-cataloger-2", | ||
Locations: source.NewLocationSet( | ||
source.NewLocation("/some/path/pkg1"), | ||
), | ||
MetadataType: pkg.DpkgMetadataType, | ||
Metadata: pkg.DpkgMetadata{ | ||
Package: "package-2", | ||
Version: "2.0.1", | ||
}, | ||
PURL: "pkg:deb/debian/[email protected]", | ||
CPEs: []cpe.CPE{ | ||
cpe.Must("cpe:2.3:*:some:package:2:*:*:*:*:*:*:*"), | ||
}, | ||
}) | ||
|
||
return catalog | ||
} | ||
|
||
//nolint:gosec | ||
func AddSampleFileRelationships(s *sbom.SBOM) { | ||
catalog := s.Artifacts.PackageCatalog.Sorted() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"Package","Version Installed","Found by","Author" | ||
{{- range .Artifacts}} | ||
"{{.Name}}","{{.Version}}","{{.FoundBy}}","{{ if hasField .Metadata "Author" }}{{.Metadata.Author}}{{ else }}NO AUTHOR SUPPLIED{{end}}" | ||
{{- end}} |
3 changes: 3 additions & 0 deletions
3
syft/formats/template/test-fixtures/snapshot/TestFormatWithOptionAndHasField.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"Package","Version Installed","Found by","Author" | ||
"package-1","1.0.1","the-cataloger-1","test-author" | ||
"package-2","2.0.1","the-cataloger-2","NO AUTHOR SUPPLIED" |