1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
2//
3// SPDX-License-Identifier: LicenseRef-MutuaL-1.2
4
5package configvalue
6
7import (
8 "context"
9 "runtime"
10 "testing"
11 "time"
12)
13
14func TestResolveACIDConfigValuesResolution1InterpolatesEnvironment(t *testing.T) {
15 t.Setenv("COOKED_USERNAME_FROM_MY_SHELL", "ada")
16 t.Setenv("COOKED_SUFFIX", "lovelace")
17
18 tests := []struct {
19 name string
20 config string
21 want string
22 }{
23 {name: "plain literal", config: "literal", want: "literal"},
24 {name: "dollar prefix", config: "$COOKED_USERNAME_FROM_MY_SHELL", want: "ada"},
25 {name: "braced reference", config: "${COOKED_USERNAME_FROM_MY_SHELL}-${COOKED_SUFFIX}", want: "ada-lovelace"},
26 {name: "escaped dollar and bang", config: "$$$COOKED_USERNAME_FROM_MY_SHELL$!", want: "$ada!"},
27 }
28
29 resolver := NewResolver()
30 for _, tt := range tests {
31 t.Run(tt.name, func(t *testing.T) {
32 got, ok := resolver.Resolve(context.Background(), tt.config)
33 if !ok {
34 t.Fatalf("Resolve(%q) ok = false", tt.config)
35 }
36
37 if got != tt.want {
38 t.Fatalf("Resolve(%q) = %q, want %q", tt.config, got, tt.want)
39 }
40 })
41 }
42}
43
44func TestResolveACIDConfigValuesResolution1RejectsMissingEnvironment(t *testing.T) {
45 resolver := NewResolver()
46
47 got, ok := resolver.Resolve(context.Background(), "$COOKED_MISSING_VALUE")
48 if ok {
49 t.Fatalf("Resolve ok = true, got %q", got)
50 }
51}
52
53func TestResolveACIDConfigValuesCommands3CachesCommandResults(t *testing.T) {
54 if runtime.GOOS == "windows" {
55 t.Skip("shell command syntax is POSIX-specific")
56 }
57
58 resolver := NewResolver()
59 command := "!printf cooked"
60
61 first, ok := resolver.Resolve(context.Background(), command)
62 if !ok {
63 t.Fatal("first command resolution failed")
64 }
65
66 second, ok := resolver.Resolve(context.Background(), command)
67 if !ok {
68 t.Fatal("second command resolution failed")
69 }
70
71 if first != "cooked" || second != "cooked" {
72 t.Fatalf("resolved command values = %q and %q, want cooked", first, second)
73 }
74
75 resolver.CommandTimeout = time.Nanosecond
76 third, ok := resolver.Resolve(context.Background(), command)
77 if !ok || third != "cooked" {
78 t.Fatalf("cached command value = %q, %v; want cooked, true", third, ok)
79 }
80}