mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 08:23:15 +01:00
* [wip] try to reflect metadata types... probably wont work Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * refactor to add unit test to ensure there is coverage in the schema Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * [wip] generate metadata container Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * add generation of metadata container struct for JSON schema generation Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * fix linting Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * update linter script to account for code generation Signed-off-by: Alex Goodman <alex.goodman@anchore.com> --------- Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
40 lines
1.3 KiB
Go
40 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"reflect"
|
|
"sort"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/anchore/syft/schema/json/internal"
|
|
)
|
|
|
|
func TestAllMetadataRepresented(t *testing.T) {
|
|
// this test checks that all the metadata types are represented in the currently generated ArtifactMetadataContainer struct
|
|
// such that PRs will reflect when there is drift from the implemented set of metadata types and the generated struct
|
|
// which controls the JSON schema content.
|
|
expected, err := internal.AllSyftMetadataTypeNames()
|
|
require.NoError(t, err)
|
|
actual := allTypeNamesFromStruct(internal.ArtifactMetadataContainer{})
|
|
if !assert.ElementsMatch(t, expected, actual) {
|
|
t.Errorf("metadata types not fully represented: \n%s", cmp.Diff(expected, actual))
|
|
t.Log("did you add a new pkg.*Metadata type without updating the JSON schema?")
|
|
t.Log("if so, you need to update the schema version and regenerate the JSON schema (make generate-json-schema)")
|
|
}
|
|
}
|
|
|
|
func allTypeNamesFromStruct(instance any) []string {
|
|
// get all the type names from the struct (not recursively)
|
|
var typeNames []string
|
|
tt := reflect.TypeOf(instance)
|
|
for i := 0; i < tt.NumField(); i++ {
|
|
field := tt.Field(i)
|
|
typeNames = append(typeNames, field.Type.Name())
|
|
}
|
|
sort.Strings(typeNames)
|
|
return typeNames
|
|
}
|