Files
syft/internal/capabilities/generate/io_test.go
T
f7f3c6efc3 fix(ai): use go.yaml.in/yaml/v3 to parse model card frontmatter (#5328)
* fix(ai): use go.yaml.in/yaml/v3 to parse model card frontmatter

gopkg.in/yaml.v3 panics on some malformed merge-key payloads (for
example "<<:\r? 0:"), which crashed the gguf and safetensors catalogers
when a model README contained such frontmatter. Switch the Hugging Face
frontmatter parser to the maintained go.yaml.in/yaml/v3 fork, which is
already used by the other catalogers and returns an error instead of
panicking. The existing error path logs and skips the frontmatter.

Fixes #5327

Signed-off-by: tk1475 <118763009+tk1475@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* chore: finish moving off gopkg.in/yaml.v3

- move the snap catalogers and `internal/capabilities` to `go.yaml.in/yaml/v3`. The snap parsers decode yaml from scanned sources and hit the same merge-key panic as #5327
- enable `depguard` with a deny rule for `gopkg.in/yaml.v3` so it can't be reintroduced

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

---------

Signed-off-by: tk1475 <118763009+tk1475@users.noreply.github.com>
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Alex Goodman <wagoodman@users.noreply.github.com>
2026-09-28 19:22:45 +00:00

186 lines
4.3 KiB
Go

package main
import (
"testing"
"github.com/stretchr/testify/require"
"go.yaml.in/yaml/v3"
)
func TestFindFieldValue(t *testing.T) {
tests := []struct {
name string
yamlContent string
fieldName string
want string
}{
{
name: "finds simple string field",
yamlContent: `
name: test-cataloger
type: custom
`,
fieldName: "name",
want: "test-cataloger",
},
{
name: "finds type field",
yamlContent: `
name: test-cataloger
type: generic
`,
fieldName: "type",
want: "generic",
},
{
name: "returns empty for non-existent field",
yamlContent: `
name: test-cataloger
`,
fieldName: "nonexistent",
want: "",
},
{
name: "finds parser_function field",
yamlContent: `
parser_function: parseGoMod
metadata_types: [GoModMetadata]
`,
fieldName: "parser_function",
want: "parseGoMod",
},
{
name: "handles empty mapping",
yamlContent: `{}`,
fieldName: "any",
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var rootNode yaml.Node
err := yaml.Unmarshal([]byte(tt.yamlContent), &rootNode)
require.NoError(t, err)
// get the mapping node
var mappingNode *yaml.Node
if rootNode.Kind == yaml.DocumentNode && len(rootNode.Content) > 0 {
mappingNode = rootNode.Content[0]
} else {
mappingNode = &rootNode
}
got := findFieldValue(mappingNode, tt.fieldName)
require.Equal(t, tt.want, got)
})
}
}
func TestAddCatalogerFieldComment(t *testing.T) {
tests := []struct {
name string
fieldName string
fieldValue string
catalogerName string
wantLineComment string
}{
{
name: "ecosystem is MANUAL",
fieldName: "ecosystem",
catalogerName: "test-cataloger",
wantLineComment: "MANUAL",
},
{
name: "name is AUTO-GENERATED",
fieldName: "name",
catalogerName: "test-cataloger",
wantLineComment: autoGeneratedComment,
},
{
name: "type is AUTO-GENERATED",
fieldName: "type",
catalogerName: "test-cataloger",
wantLineComment: autoGeneratedComment,
},
{
name: "source is AUTO-GENERATED",
fieldName: "source",
catalogerName: "test-cataloger",
wantLineComment: autoGeneratedComment,
},
{
name: "config is AUTO-GENERATED",
fieldName: "config",
catalogerName: "test-cataloger",
wantLineComment: autoGeneratedComment,
},
{
name: "selectors is AUTO-GENERATED",
fieldName: "selectors",
catalogerName: "test-cataloger",
wantLineComment: autoGeneratedComment,
},
{
name: "parsers is AUTO-GENERATED structure",
fieldName: "parsers",
catalogerName: "test-cataloger",
wantLineComment: "AUTO-GENERATED structure",
},
{
name: "detectors for binary-classifier-cataloger is AUTO-GENERATED",
fieldName: "detectors",
catalogerName: "binary-classifier-cataloger",
wantLineComment: autoGeneratedComment,
},
{
name: "detectors for other catalogers is MANUAL",
fieldName: "detectors",
catalogerName: "java-archive-cataloger",
wantLineComment: "MANUAL - edit detectors here",
},
{
name: "metadata_types is AUTO-GENERATED",
fieldName: "metadata_types",
catalogerName: "test-cataloger",
wantLineComment: autoGeneratedComment,
},
{
name: "package_types is AUTO-GENERATED",
fieldName: "package_types",
catalogerName: "test-cataloger",
wantLineComment: autoGeneratedComment,
},
{
name: "json_schema_types is AUTO-GENERATED",
fieldName: "json_schema_types",
catalogerName: "test-cataloger",
wantLineComment: autoGeneratedComment,
},
{
name: "capabilities is MANUAL",
fieldName: "capabilities",
catalogerName: "test-cataloger",
wantLineComment: "MANUAL - edit capabilities here",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// create key and value nodes
keyNode := &yaml.Node{
Kind: yaml.ScalarNode,
Value: tt.fieldName,
}
valueNode := &yaml.Node{
Kind: yaml.ScalarNode,
Value: tt.fieldValue,
}
addCatalogerFieldComment(keyNode, valueNode, tt.catalogerName)
require.Equal(t, tt.wantLineComment, keyNode.LineComment)
})
}
}