add tests for get plugins

This commit is contained in:
Kasper Juul Hermansen 2022-11-02 16:48:33 +01:00
parent a5859107f1
commit ebc2d7aa8f
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
3 changed files with 112 additions and 5 deletions

View File

@ -24,14 +24,27 @@ func ParseFile(ctx context.Context, path string) (*CharSchema, error) {
return nil, fmt.Errorf("could not read file: %w", err)
}
return Parse(file)
}
func Parse(content []byte) (*CharSchema, error) {
var schema CharSchema
if err = yaml.Unmarshal(file, &schema); err != nil {
return nil, fmt.Errorf("could not deserialize yaml file into CharSchema: %w", err)
if err := yaml.Unmarshal(content, &schema); err != nil {
return nil, fmt.Errorf("could not deserialize yaml into CharSchema: %w", err)
}
return &schema, nil
}
func (cs *CharSchema) GetPlugins(ctx context.Context) (*PluginOps, error) {
return nil, nil
func (cs *CharSchema) GetPlugins(ctx context.Context) (CharSchemaPlugins, error) {
plugins := make(map[CharSchemaPluginName]*CharSchemaPlugin, len(cs.Plugins))
for n, plugin := range cs.Plugins {
po, err := n.Get()
if err != nil {
return nil, err
}
plugin.Opts = po
plugins[n] = plugin
}
return plugins, nil
}

View File

@ -87,7 +87,8 @@ func (cspn CharSchemaPluginName) Get() (*PluginOps, error) {
return po, nil
}
type CharSchemaPlugins map[CharSchemaPluginName]CharSchemaPlugin
type CharSchemaPlugins map[CharSchemaPluginName]*CharSchemaPlugin
type CharSchemaPlugin struct {
Opts *PluginOps
}

93
pkg/schema/schema_test.go Normal file
View File

@ -0,0 +1,93 @@
package schema_test
import (
"context"
"testing"
"git.front.kjuulh.io/kjuulh/char/pkg/schema"
"github.com/stretchr/testify/require"
)
func TestSchemaParse(t *testing.T) {
t.Parallel()
tt := []struct {
name string
input string
expected *schema.CharSchema
}{
{
name: "with plugins",
input: `
registry: git.front.kjuulh.io
plugins:
"kjuulh/char#plugins/gocli": {}
"kjuulh/char#plugins/rust": {}
`,
expected: &schema.CharSchema{
Registry: "git.front.kjuulh.io",
Plugins: map[schema.CharSchemaPluginName]*schema.CharSchemaPlugin{
"kjuulh/char#plugins/gocli": {},
"kjuulh/char#plugins/rust": {},
},
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
s, err := schema.Parse([]byte(tc.input))
require.NoError(t, err)
require.Equal(t, tc.expected, s)
})
}
}
func TestGetPlugins(t *testing.T) {
t.Parallel()
tt := []struct {
name string
input string
expected schema.CharSchemaPlugins
}{
{
name: "with plugins",
input: `
registry: git.front.kjuulh.io
plugins:
"kjuulh/char#plugins/gocli": {}
"kjuulh/char#plugins/rust": {}
`,
expected: map[schema.CharSchemaPluginName]*schema.CharSchemaPlugin{
"kjuulh/char#plugins/gocli": {
Opts: &schema.PluginOps{
Org: "kjuulh",
RepositoryName: "char",
Path: "plugins/gocli",
Version: "",
},
},
"kjuulh/char#plugins/rust": {
Opts: &schema.PluginOps{
Org: "kjuulh",
RepositoryName: "char",
Path: "plugins/rust",
Version: "",
},
},
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
s, err := schema.Parse([]byte(tc.input))
require.NoError(t, err)
plugins, err := s.GetPlugins(context.Background())
require.NoError(t, err)
require.Equal(t, tc.expected, plugins)
})
}
}