mirror of
https://github.com/anchore/syft.git
synced 2025-11-20 01:43:17 +01:00
* [wip] Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * distinct the package metadata functions Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * remove metadata type from package core model Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * incorporate review feedback for names Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add RPM archive metadata and split parser helpers Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * clarify the python package metadata type Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * rename the KB metadata type Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * break hackage and composer types by use case Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * linting fix Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix encoding and decoding for syft-json and cyclonedx Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * bump json schema to 11 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update cyclonedx-json snapshots Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update cyclonedx-xml snapshots Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update spdx-json snapshots Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update spdx-tv snapshots Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update syft-json snapshots Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * correct metadata type in stack yaml parser test Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix bom-ref redactor for cyclonedx-xml Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add tests for legacy package metadata names Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * regenerate json schema v11 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix legacy HackageMetadataType reflect type value check Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix linting Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * packagemetadata discovery should account for type shadowing Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix linting Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix cli tests Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * bump json schema version to v12 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update json schema to incorporate changes from main Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add syft-json legacy config option Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add tests around v11-v12 json decoding Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add docs for SYFT_JSON_LEGACY Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * rename structs to be compliant with new naming scheme Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
70 lines
1.2 KiB
Go
70 lines
1.2 KiB
Go
package packagemetadata
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
type CompletionTester struct {
|
|
saw []any
|
|
valid []any
|
|
ignore []any
|
|
}
|
|
|
|
func NewCompletionTester(t testing.TB, ignore ...any) *CompletionTester {
|
|
tester := &CompletionTester{
|
|
valid: AllTypes(),
|
|
ignore: ignore,
|
|
}
|
|
t.Cleanup(func() {
|
|
t.Helper()
|
|
tester.validate(t)
|
|
})
|
|
return tester
|
|
}
|
|
|
|
func (tr *CompletionTester) Tested(t testing.TB, m any) {
|
|
t.Helper()
|
|
|
|
if m == nil {
|
|
return
|
|
}
|
|
if len(tr.valid) == 0 {
|
|
t.Fatal("no valid metadata types to test against")
|
|
}
|
|
ty := reflect.TypeOf(m)
|
|
for _, v := range tr.valid {
|
|
if reflect.TypeOf(v) == ty {
|
|
tr.saw = append(tr.saw, m)
|
|
return
|
|
}
|
|
}
|
|
|
|
t.Fatalf("tested metadata type is not valid: %s", ty.Name())
|
|
}
|
|
|
|
func (tr *CompletionTester) validate(t testing.TB) {
|
|
t.Helper()
|
|
|
|
count := make(map[reflect.Type]int)
|
|
for _, m := range tr.saw {
|
|
count[reflect.TypeOf(m)]++
|
|
}
|
|
|
|
validations:
|
|
for _, v := range tr.valid {
|
|
ty := reflect.TypeOf(v)
|
|
|
|
for _, ignore := range tr.ignore {
|
|
if ty == reflect.TypeOf(ignore) {
|
|
// skip ignored types
|
|
continue validations
|
|
}
|
|
}
|
|
|
|
if c, exists := count[ty]; c == 0 || !exists {
|
|
t.Errorf("metadata type %s is not covered by a test", ty.Name())
|
|
}
|
|
}
|
|
}
|