From aeb085c5f0e98ca4c5648335d2ad2ead6219432d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 22 Jul 2014 06:51:02 -0700 Subject: [PATCH] config: error if variable interpolation can't find variable --- config/interpolate.go | 8 +++++++- config/interpolate_test.go | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/config/interpolate.go b/config/interpolate.go index 1c6aeefc9..77bc15117 100644 --- a/config/interpolate.go +++ b/config/interpolate.go @@ -193,7 +193,13 @@ func (i *VariableInterpolation) FullString() string { func (i *VariableInterpolation) Interpolate( vs map[string]string) (string, error) { - return vs[i.key], nil + v, ok := vs[i.key] + if !ok { + return "", fmt.Errorf( + "%s: value for variable '%s' not found", v) + } + + return v, nil } func (i *VariableInterpolation) Variables() map[string]InterpolatedVariable { diff --git a/config/interpolate_test.go b/config/interpolate_test.go index 87c2be0ca..f9ccb619f 100644 --- a/config/interpolate_test.go +++ b/config/interpolate_test.go @@ -273,3 +273,18 @@ func TestVariableInterpolation(t *testing.T) { t.Fatalf("bad: %#v", actual) } } + +func TestVariableInterpolation_missing(t *testing.T) { + uv, err := NewUserVariable("var.foo") + if err != nil { + t.Fatalf("err: %s", err) + } + + i := &VariableInterpolation{Variable: uv, key: "var.foo"} + _, err = i.Interpolate(map[string]string{ + "var.bar": "bar", + }) + if err == nil { + t.Fatal("should error") + } +}