feat(core): 下沉查询密码工具与配置快照解码
Verify / verify (push) Canceled after 0s

This commit is contained in:
zhangdeman committed 2026-11-28 03:10:18 -08:00
1 parent bedcf4dd83
commit d2a9033695
23 files changed
+1544 -6

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
package config
import (
"bytes"
"errors"
"testing"
"git.zhangdeman.cn/zhangdeman/ginframework/fault"
)
func TestDecodeSnapshotAndExpansion(t *testing.T) {
t.Setenv("GINFRAMEWORK_VALUE", "${GINFRAMEWORK_NESTED}")
t.Setenv("GINFRAMEWORK_NESTED", "second-pass")
content := []byte("name: ${GINFRAMEWORK_VALUE}\ncount: 0\n")
original := bytes.Clone(content)
var (
target struct {
Name string `yaml:"name"`
Count *int `yaml:"count"`
}
)
if err := Decode(content, &target, Options{}); err != nil {
t.Fatal(err)
}
if target.Name != "${GINFRAMEWORK_NESTED}" || target.Count == nil || *target.Count != 0 || !bytes.Equal(content, original) {
t.Fatalf("snapshot or expansion changed: %+v", target)
}
if err := Decode(content, &target, Options{DisableEnvExpansion: true}); err != nil {
t.Fatal(err)
}
if target.Name != "${GINFRAMEWORK_VALUE}" {
t.Fatal("disabled expansion ignored")
}
}
func TestDecodeRejectsInvalidTargetsAndDocuments(t *testing.T) {
var (
nilTarget *struct{}
)
for _, target := range []any{nil, nilTarget, struct{}{}, new(int)} {
err := Decode([]byte("name: value"), target, Options{})
var (
classified *fault.Error
)
if !errors.As(err, &classified) || classified.Code() != CodeTarget {
t.Fatalf("invalid target %T accepted: %v", target, err)
}
}
for _, content := range []string{"name: one\n---\nname: two\n", "name: [\n", "unknown: value\n"} {
var (
target struct {
Name string `yaml:"name"`
}
)
if err := Decode([]byte(content), &target, Options{}); err == nil {
t.Fatal("invalid document accepted")
}
}
var (
target struct {
Name string `yaml:"name"`
}
)
if err := Decode([]byte("unknown: value\n"), &target, Options{AllowUnknownFields: true}); err != nil {
t.Fatal(err)
}
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.