mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-04-24 10:20:32 +00:00
161 lines
3.7 KiB
Go
161 lines
3.7 KiB
Go
package config
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type testConfig struct {
|
|
Name string `yaml:"name" comment:"The display name."`
|
|
Port int `yaml:"port" comment:"Listen port."`
|
|
Enabled bool `yaml:"enabled" comment:"Enable the service."`
|
|
Timeout time.Duration `yaml:"timeout" comment:"Request timeout."`
|
|
Internal string `yaml:"-"`
|
|
Nested testNested `yaml:"nested" comment:"Nested section."`
|
|
}
|
|
|
|
type testNested struct {
|
|
Endpoint string `yaml:"endpoint" comment:"API endpoint URL."`
|
|
Retries int `yaml:"retries" comment:"Number of retries."`
|
|
}
|
|
|
|
func TestMarshalCommentedYAML_CommentsRendered(t *testing.T) {
|
|
cfg := testConfig{
|
|
Name: "myapp",
|
|
Port: 8080,
|
|
Enabled: true,
|
|
Timeout: 15 * time.Minute,
|
|
Nested: testNested{
|
|
Endpoint: "https://api.example.com",
|
|
Retries: 3,
|
|
},
|
|
}
|
|
|
|
out, err := MarshalCommentedYAML("Test Config", cfg)
|
|
if err != nil {
|
|
t.Fatalf("MarshalCommentedYAML() error: %v", err)
|
|
}
|
|
|
|
s := string(out)
|
|
|
|
// Title is rendered as head comment
|
|
if !strings.Contains(s, "# Test Config") {
|
|
t.Error("expected title comment in output")
|
|
}
|
|
|
|
// Field comments are rendered
|
|
for _, want := range []string{
|
|
"# The display name.",
|
|
"# Listen port.",
|
|
"# Enable the service.",
|
|
"# Request timeout.",
|
|
"# Nested section.",
|
|
"# API endpoint URL.",
|
|
"# Number of retries.",
|
|
} {
|
|
if !strings.Contains(s, want) {
|
|
t.Errorf("expected comment %q in output", want)
|
|
}
|
|
}
|
|
|
|
// Field values are rendered
|
|
if !strings.Contains(s, "name: myapp") {
|
|
t.Error("expected name field value")
|
|
}
|
|
if !strings.Contains(s, "port: 8080") {
|
|
t.Error("expected port field value")
|
|
}
|
|
if !strings.Contains(s, "enabled: true") {
|
|
t.Error("expected enabled field value")
|
|
}
|
|
}
|
|
|
|
func TestMarshalCommentedYAML_DurationRendersAsString(t *testing.T) {
|
|
cfg := testConfig{
|
|
Timeout: 15 * time.Minute,
|
|
}
|
|
|
|
out, err := MarshalCommentedYAML("", cfg)
|
|
if err != nil {
|
|
t.Fatalf("MarshalCommentedYAML() error: %v", err)
|
|
}
|
|
|
|
s := string(out)
|
|
if !strings.Contains(s, "timeout: 15m0s") {
|
|
t.Errorf("expected duration as string, got:\n%s", s)
|
|
}
|
|
}
|
|
|
|
func TestMarshalCommentedYAML_ZeroDuration(t *testing.T) {
|
|
cfg := testConfig{}
|
|
|
|
out, err := MarshalCommentedYAML("", cfg)
|
|
if err != nil {
|
|
t.Fatalf("MarshalCommentedYAML() error: %v", err)
|
|
}
|
|
|
|
s := string(out)
|
|
if !strings.Contains(s, "timeout: 0s") {
|
|
t.Errorf("expected zero duration as '0s', got:\n%s", s)
|
|
}
|
|
}
|
|
|
|
func TestMarshalCommentedYAML_YamlDashExcluded(t *testing.T) {
|
|
cfg := testConfig{
|
|
Internal: "should-not-appear",
|
|
}
|
|
|
|
out, err := MarshalCommentedYAML("", cfg)
|
|
if err != nil {
|
|
t.Fatalf("MarshalCommentedYAML() error: %v", err)
|
|
}
|
|
|
|
s := string(out)
|
|
if strings.Contains(s, "should-not-appear") {
|
|
t.Error("yaml:\"-\" field should not appear in output")
|
|
}
|
|
if strings.Contains(s, "internal") {
|
|
t.Error("yaml:\"-\" field key should not appear in output")
|
|
}
|
|
}
|
|
|
|
func TestMarshalCommentedYAML_NestedStructs(t *testing.T) {
|
|
cfg := testConfig{
|
|
Nested: testNested{
|
|
Endpoint: "https://api.example.com",
|
|
Retries: 3,
|
|
},
|
|
}
|
|
|
|
out, err := MarshalCommentedYAML("", cfg)
|
|
if err != nil {
|
|
t.Fatalf("MarshalCommentedYAML() error: %v", err)
|
|
}
|
|
|
|
s := string(out)
|
|
if !strings.Contains(s, "endpoint: https://api.example.com") {
|
|
t.Errorf("expected nested endpoint value in output:\n%s", s)
|
|
}
|
|
if !strings.Contains(s, "retries: 3") {
|
|
t.Errorf("expected nested retries value in output:\n%s", s)
|
|
}
|
|
}
|
|
|
|
func TestMarshalCommentedYAML_PointerStruct(t *testing.T) {
|
|
cfg := &testConfig{
|
|
Name: "ptr-test",
|
|
Port: 9090,
|
|
}
|
|
|
|
out, err := MarshalCommentedYAML("Pointer Test", cfg)
|
|
if err != nil {
|
|
t.Fatalf("MarshalCommentedYAML() error: %v", err)
|
|
}
|
|
|
|
s := string(out)
|
|
if !strings.Contains(s, "name: ptr-test") {
|
|
t.Error("expected name field from pointer struct")
|
|
}
|
|
}
|