2020-08-13 13:43:55 +00:00
|
|
|
package envtemplate
|
|
|
|
|
|
|
|
import (
|
2022-10-18 07:28:39 +00:00
|
|
|
"os"
|
2020-08-13 13:43:55 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2022-10-18 07:28:39 +00:00
|
|
|
func TestReplaceSuccess(t *testing.T) {
|
|
|
|
if err := os.Setenv("foo", "bar"); err != nil {
|
|
|
|
t.Fatalf("cannot set env var: %s", err)
|
|
|
|
}
|
2020-08-13 13:43:55 +00:00
|
|
|
f := func(s, resultExpected string) {
|
|
|
|
t.Helper()
|
2022-10-18 07:28:39 +00:00
|
|
|
result, err := Replace([]byte(s))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2020-08-13 13:43:55 +00:00
|
|
|
if string(result) != resultExpected {
|
|
|
|
t.Fatalf("unexpected result;\ngot\n%q\nwant\n%q", result, resultExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", "")
|
|
|
|
f("foo", "foo")
|
2022-10-18 07:28:39 +00:00
|
|
|
f("a %{foo}-x", "a bar-x")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplaceFailure(t *testing.T) {
|
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
|
|
|
_, err := Replace([]byte(s))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("foo %{bar} %{baz}")
|
2020-08-13 13:43:55 +00:00
|
|
|
}
|