feat(configuration): export Validate for archied setup to call
Loader.finalize's decode-then-validate sequence called an unexported validate(cfg *config.Config), so nothing outside the package -- e.g. archied setup, which builds a config.Config in memory before writing it to disk -- could run the same checks. Validate wraps it; Loader now calls the exported form internally (single call site, no behavior change). Documented the sharp edge this alone doesn't solve: Validate doesn't apply defaults (agent.mode, dispatch.trigger, forge.type all only pass once defaulted), and defaulting is still an unexported method on *Loader. A cfg setup builds by hand needs those fields filled in itself, or -- safer, since it can't drift from the real load path -- setup should render to TOML and load it back through a Loader rather than call Validate on the in-memory value directly. Refs archie-core-ewk.1
This commit is contained in:
1 parent
32bd705286
commit
1579b027b8
3 files changed
+143
-1
No files matched your search
@@ -175,7 +175,7 @@ func (l *Loader) decodeMain(cfg *config.Config, path string, isYAMLFile bool) er
|
||||
// wrote.
|
||||
func (l *Loader) finalize(doc *Document) (*Document, error) {
|
||||
l.applyDefaults(&doc.Config)
|
||||
if err := validate(&doc.Config); err != nil {
|
||||
if err := Validate(&doc.Config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l.log.Debug("configuration loaded", "sources", doc.Provenance.String())
|
||||
|
||||
@@ -35,6 +35,24 @@ var (
|
||||
dispatchTriggers = []string{dispatchTriggerAssignee, dispatchTriggerLabel, dispatchTriggerEither}
|
||||
)
|
||||
|
||||
// Validate runs the same checks Loader applies before accepting a config,
|
||||
// against a config.Config value already built in memory -- e.g. by archied
|
||||
// setup, before it has written anything to disk.
|
||||
//
|
||||
// Like validate, it does not apply defaults. Several checks (agent.mode,
|
||||
// dispatch.trigger, forge.type) only pass once a default has been filled
|
||||
// in, and defaulting is currently unexported ((*Loader).applyDefaults). A
|
||||
// cfg built by hand, with those fields left zero-valued, will fail
|
||||
// validation that a config loaded through [Loader.File] would pass. Callers
|
||||
// that build a config directly (rather than decoding one through a Loader)
|
||||
// must fill in the same defaults themselves, or -- the safer option, since
|
||||
// it can't drift from what the real load path does -- render the config to
|
||||
// TOML and load it back through a Loader instead of calling Validate on the
|
||||
// in-memory value directly.
|
||||
func Validate(cfg *config.Config) error {
|
||||
return validate(cfg)
|
||||
}
|
||||
|
||||
// validate reports the first problem that would stop the daemon running.
|
||||
// It does not modify cfg -- run applyDefaults first.
|
||||
func validate(cfg *config.Config) error {
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
package configuration
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
)
|
||||
|
||||
// A minimal config that satisfies every check validate applies without
|
||||
// relying on applyDefaults -- Validate documents that it does not apply
|
||||
// defaults, so this fixture fills in every field a default would otherwise
|
||||
// have supplied.
|
||||
func minimalValidConfig() config.Config {
|
||||
return config.Config{
|
||||
BotUser: "archie-bot",
|
||||
Agent: config.Agent{Mode: "inprocess"},
|
||||
Dispatch: config.Dispatch{
|
||||
Trigger: "assignee",
|
||||
},
|
||||
Forge: config.Forge{Type: "none"},
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_IsCallableOutsideThePackage(t *testing.T) {
|
||||
cfg := minimalValidConfig()
|
||||
if err := Validate(&cfg); err != nil {
|
||||
t.Fatalf("Validate(minimal valid config) = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_RejectsTheSameProblemsAsLoaderLoad(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
mutate func(cfg *config.Config)
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid config",
|
||||
mutate: func(cfg *config.Config) {},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "missing bot_user with no identities",
|
||||
mutate: func(cfg *config.Config) { cfg.BotUser = "" },
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "unrecognised agent.mode",
|
||||
mutate: func(cfg *config.Config) { cfg.Agent.Mode = "not-a-real-mode" },
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "unrecognised dispatch.trigger",
|
||||
mutate: func(cfg *config.Config) { cfg.Dispatch.Trigger = "not-a-real-trigger" },
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "unrecognised forge.type",
|
||||
mutate: func(cfg *config.Config) { cfg.Forge.Type = "not-a-real-forge" },
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "provider base_url carries userinfo",
|
||||
mutate: func(cfg *config.Config) {
|
||||
cfg.Providers = map[string]config.Provider{
|
||||
"acme": {BaseURL: "https://user:pass@example.com"},
|
||||
}
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cfg := minimalValidConfig()
|
||||
tc.mutate(&cfg)
|
||||
err := Validate(&cfg)
|
||||
if tc.wantErr && err == nil {
|
||||
t.Fatal("Validate() = nil, want an error")
|
||||
}
|
||||
if tc.wantErr && !errors.Is(err, ErrInvalidInput) {
|
||||
t.Errorf("Validate() = %v, want it to wrap ErrInvalidInput", err)
|
||||
}
|
||||
if !tc.wantErr && err != nil {
|
||||
t.Fatalf("Validate() = %v, want nil", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Validate must run the exact same checks Loader.File applies internally --
|
||||
// this pins that a config Validate accepts on its own, once the same
|
||||
// defaults Loader.File would apply are filled in by hand, is also accepted
|
||||
// when it goes through the real file-loading path.
|
||||
func TestValidate_AgreesWithLoaderLoad(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := dir + "/config.toml"
|
||||
const doc = `
|
||||
bot_user = "archie-bot"
|
||||
|
||||
[agent]
|
||||
mode = "inprocess"
|
||||
|
||||
[dispatch]
|
||||
trigger = "assignee"
|
||||
|
||||
[forge]
|
||||
type = "none"
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(doc), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg := minimalValidConfig()
|
||||
if err := Validate(&cfg); err != nil {
|
||||
t.Fatalf("Validate(in-memory) = %v, want nil", err)
|
||||
}
|
||||
|
||||
if _, err := New(nil).File(path); err != nil {
|
||||
t.Fatalf("Loader.File(equivalent config on disk) = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user