Files
notarius/internal/core/config/reference_collections_test.go

58 lines
2.0 KiB
Go

package config
import (
"bytes"
"strings"
"testing"
"go.maximumdirect.net/configkit"
)
func TestReferenceCollectionsBindingAndInspection(t *testing.T) {
loaded, err := loadConfigSource(configkit.Bytes("settings.yml", []byte(`version: 6
pipelines:
main:
input: {module: input}
references:
omitted: {path: a.yml}
"null": {path: b.yml, collections: null}
empty: {path: c.yml, collections: []}
named: {path: d.yml, collections: [spells, " Mixed "]}
`)), LoadOptions{})
if err != nil {
if native, ok := err.(*configkit.LoadError); ok {
t.Fatalf("%v: %#v", err, native.Diagnostics())
}
t.Fatal(err)
}
refs := loaded.Config.Pipelines["main"].References
if refs["omitted"].Collections != nil || refs["null"].Collections != nil || refs["empty"].Collections == nil || len(refs["empty"].Collections) != 0 || refs["named"].Collections[1] != " Mixed " {
t.Fatalf("unexpected collection selections: %#v", refs)
}
copy := loaded.RuntimeConfig()
copy.Pipelines["main"].References["named"].Collections[0] = "changed"
if refs["named"].Collections[0] != "spells" {
t.Fatal("detached runtime selection mutated source")
}
var effective bytes.Buffer
if err := loaded.WriteEffective(&effective); err != nil || !strings.Contains(effective.String(), "collections: []") {
t.Fatalf("effective config = %s, %v", effective.String(), err)
}
}
func TestReferenceCollectionsRejectInvalidInput(t *testing.T) {
for _, value := range []string{
"{path: a.yml, collections: [\"\"]}",
"{path: a.yml, collections: nope}",
"{path: a.yml, collections: [3]}",
"{artifact: {step: earlier, lane: output}, collections: []}",
} {
t.Run(value, func(t *testing.T) {
content := "version: 6\npipelines:\n main:\n input: {module: input}\n artifacts:\n output:\n extract:\n module: extract\n references:\n ref: " + value + "\n"
if _, err := loadConfigSource(configkit.Bytes("settings.yml", []byte(content)), LoadOptions{}); err == nil {
t.Fatal("accepted invalid reference selection")
}
})
}
}