template/interpolate: wd

This commit is contained in:
Mitchell Hashimoto 2015-05-15 21:10:12 -07:00
parent 125369d102
commit 5d205ec1fc
2 changed files with 37 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
// Funcs are the interpolation funcs that are available within interpolations.
var FuncGens = map[string]FuncGenerator{
"env": funcGenEnv,
"pwd": funcGenPwd,
"user": funcGenUser,
}
@ -39,6 +40,12 @@ func funcGenEnv(ctx *Context) interface{} {
}
}
func funcGenPwd(ctx *Context) interface{} {
return func() (string, error) {
return os.Getwd()
}
}
func funcGenUser(ctx *Context) interface{} {
return func() string {
return ""

View File

@ -64,3 +64,33 @@ func TestFuncEnv_disable(t *testing.T) {
}
}
}
func TestFuncPwd(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
cases := []struct {
Input string
Output string
}{
{
`{{pwd}}`,
wd,
},
}
ctx := &Context{}
for _, tc := range cases {
i := &I{Value: tc.Input}
result, err := i.Render(ctx)
if err != nil {
t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err)
}
if result != tc.Output {
t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result)
}
}
}