2020-08-13 13:43:55 +00:00
|
|
|
package envtemplate
|
|
|
|
|
|
|
|
import (
|
2022-10-26 11:49:20 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2020-08-13 13:43:55 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2022-10-26 11:49:20 +00:00
|
|
|
func TestExpandTemplates(t *testing.T) {
|
|
|
|
f := func(envs, resultExpected []string) {
|
|
|
|
t.Helper()
|
|
|
|
m := parseEnvVars(envs)
|
|
|
|
mExpanded := expandTemplates(m)
|
|
|
|
result := make([]string, 0, len(mExpanded))
|
|
|
|
for k, v := range mExpanded {
|
|
|
|
result = append(result, k+"="+v)
|
|
|
|
}
|
|
|
|
sort.Strings(result)
|
|
|
|
if !reflect.DeepEqual(result, resultExpected) {
|
|
|
|
t.Fatalf("unexpected result;\ngot\n%q\nwant\n%q", result, resultExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f(nil, []string{})
|
|
|
|
f([]string{"foo=%{bar}", "bar=x"}, []string{"bar=x", "foo=x"})
|
|
|
|
f([]string{"a=x%{b}", "b=y%{c}z%{d}", "c=123", "d=qwe"}, []string{"a=xy123zqwe", "b=y123zqwe", "c=123", "d=qwe"})
|
|
|
|
f([]string{"a=x%{b}y", "b=z%{a}q", "c"}, []string{"a=xzxzxzxz%{a}qyqyqyqy", "b=zxzxzxzx%{b}yqyqyqyq", "c="})
|
2022-11-07 11:15:51 +00:00
|
|
|
f([]string{"a=%{x.y}"}, []string{"a=%{x.y}"})
|
2022-10-26 11:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupEnv(t *testing.T) {
|
|
|
|
envVars = map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
result, ok := LookupEnv("foo")
|
|
|
|
if result != "bar" {
|
|
|
|
t.Fatalf("unexpected result; got %q; want %q", result, "bar")
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected ok=false")
|
|
|
|
}
|
|
|
|
result, ok = LookupEnv("bar")
|
|
|
|
if result != "" {
|
|
|
|
t.Fatalf("unexpected non-empty result: %q", result)
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("unexpected ok=true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 07:28:39 +00:00
|
|
|
func TestReplaceSuccess(t *testing.T) {
|
2022-10-26 11:49:20 +00:00
|
|
|
envVars = map[string]string{
|
|
|
|
"foo": "bar",
|
2022-10-18 07:28:39 +00:00
|
|
|
}
|
2020-08-13 13:43:55 +00:00
|
|
|
f := func(s, resultExpected string) {
|
|
|
|
t.Helper()
|
2022-10-26 11:49:20 +00:00
|
|
|
result, err := ReplaceBytes([]byte(s))
|
2022-10-18 07:28:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2020-08-13 13:43:55 +00:00
|
|
|
if string(result) != resultExpected {
|
2022-10-26 11:49:20 +00:00
|
|
|
t.Fatalf("unexpected result for ReplaceBytes(%q);\ngot\n%q\nwant\n%q", s, result, resultExpected)
|
|
|
|
}
|
|
|
|
resultS, err := ReplaceString(s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
if resultS != resultExpected {
|
|
|
|
t.Fatalf("unexpected result for ReplaceString(%q);\ngot\n%q\nwant\n%q", s, result, resultExpected)
|
2020-08-13 13:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", "")
|
|
|
|
f("foo", "foo")
|
2022-10-18 07:28:39 +00:00
|
|
|
f("a %{foo}-x", "a bar-x")
|
2022-11-07 11:15:51 +00:00
|
|
|
f("%{foo.bar}", "%{foo.bar}")
|
2022-10-18 07:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplaceFailure(t *testing.T) {
|
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
2022-10-26 11:49:20 +00:00
|
|
|
if _, err := ReplaceBytes([]byte(s)); err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error for ReplaceBytes(%q)", s)
|
|
|
|
}
|
|
|
|
if _, err := ReplaceString(s); err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error for ReplaceString(%q)", s)
|
2022-10-18 07:28:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f("foo %{bar} %{baz}")
|
2020-08-13 13:43:55 +00:00
|
|
|
}
|