Carry collection selections through configuration
This commit is contained in:
@@ -69,8 +69,9 @@ type ValidatorDocument struct {
|
||||
// validation establishes which locations permit generated references and
|
||||
// requires exactly one populated branch.
|
||||
type ReferenceDocument struct {
|
||||
Path *string `yaml:"path,omitempty"`
|
||||
Artifact *ArtifactReferenceDocument `yaml:"artifact,omitempty"`
|
||||
Path *string `yaml:"path,omitempty"`
|
||||
Artifact *ArtifactReferenceDocument `yaml:"artifact,omitempty"`
|
||||
Collections []string `yaml:"collections,omitempty"`
|
||||
}
|
||||
|
||||
type ArtifactReferenceDocument struct {
|
||||
|
||||
@@ -243,7 +243,11 @@ func documentReferencesToRuntime(documents map[string]ReferenceDocument) map[str
|
||||
continue
|
||||
}
|
||||
if document.Path != nil {
|
||||
references[name] = pipeline.ExternalReference(*document.Path)
|
||||
reference := pipeline.ExternalReference(*document.Path)
|
||||
if document.Collections != nil {
|
||||
reference.Collections = append([]string{}, document.Collections...)
|
||||
}
|
||||
references[name] = reference
|
||||
}
|
||||
}
|
||||
return references
|
||||
|
||||
@@ -403,6 +403,14 @@ func validateDocumentReferences(references map[string]ReferenceDocument, path co
|
||||
sourcePath := path.Key(name)
|
||||
hasPath := source.Path != nil
|
||||
hasArtifact := source.Artifact != nil
|
||||
for index, collection := range source.Collections {
|
||||
if collection == "" {
|
||||
validation.Error(sourcePath.Key("collections").Index(index), "must not be empty")
|
||||
}
|
||||
}
|
||||
if hasArtifact && source.Collections != nil {
|
||||
validation.Error(sourcePath.Key("collections"), "is only supported with an external path")
|
||||
}
|
||||
if hasPath == hasArtifact {
|
||||
validation.Error(sourcePath, "must select exactly one reference source")
|
||||
continue
|
||||
|
||||
57
internal/core/config/reference_collections_test.go
Normal file
57
internal/core/config/reference_collections_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
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")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user