-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_example_test.go
More file actions
48 lines (42 loc) · 1.28 KB
/
Copy pathconfig_example_test.go
File metadata and controls
48 lines (42 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package cdd_test
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/kryft-dev/cdd"
"github.com/kryft-dev/cdd/internal/config"
)
// TestConfigExampleLoads checks the embedded example is a valid config, as
// shipped and with every commented example uncommented.
func TestConfigExampleLoads(t *testing.T) {
t.Run("as shipped", func(t *testing.T) {
assertLoads(t, string(cdd.ConfigExample))
})
// The Action examples are commented out, so uncomment every one: they
// must also load, all together.
t.Run("actions uncommented", func(t *testing.T) {
_, actions, found := strings.Cut(string(cdd.ConfigExample), "# [actions.code]")
if !found {
t.Fatal("no [actions.code] example found")
}
var b strings.Builder
b.WriteString("[actions.code]\n")
for _, line := range strings.Split(actions, "\n") {
if rest, ok := strings.CutPrefix(line, "# "); ok && strings.ContainsAny(rest, "=[") {
b.WriteString(rest + "\n")
}
}
assertLoads(t, b.String())
})
}
func assertLoads(t *testing.T, body string) {
t.Helper()
path := filepath.Join(t.TempDir(), "config.toml")
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatalf("write config: %v", err)
}
if _, err := config.LoadFrom(path); err != nil {
t.Fatalf("LoadFrom: %v\n%s", err, body)
}
}