Commit a6730f5a authored by Matt Butcher's avatar Matt Butcher Committed by GitHub

Merge pull request #1376 from technosophos/fix/1366-use-real-funcmap

fix(linter): add engine.FuncMap so linter can use real function list
parents e963e1cc 36f7eb0b
...@@ -48,15 +48,36 @@ type Engine struct { ...@@ -48,15 +48,36 @@ type Engine struct {
// The FuncMap sets all of the Sprig functions except for those that provide // The FuncMap sets all of the Sprig functions except for those that provide
// access to the underlying OS (env, expandenv). // access to the underlying OS (env, expandenv).
func New() *Engine { func New() *Engine {
f := FuncMap()
return &Engine{
FuncMap: f,
}
}
// FuncMap returns a mapping of all of the functions that Engine has.
//
// Because some functions are late-bound (e.g. contain context-sensitive
// data), the functions may not all perform identically outside of an
// Engine as they will inside of an Engine.
//
// Known late-bound functions:
//
// - "include": This is late-bound in Engine.Render(). The version
// included in the FuncMap is a placeholder.
func FuncMap() template.FuncMap {
f := sprig.TxtFuncMap() f := sprig.TxtFuncMap()
delete(f, "env") delete(f, "env")
delete(f, "expandenv") delete(f, "expandenv")
// Add a function to convert to YAML: // Add a function to convert to YAML:
f["toYaml"] = toYaml f["toYaml"] = toYaml
return &Engine{
FuncMap: f, // This is a placeholder for the "include" function, which is
} // late-bound to a template. By declaring it here, we preserve the
// integrity of the linter.
f["include"] = func(string, interface{}) string { return "not implemented" }
return f
} }
func toYaml(v interface{}) string { func toYaml(v interface{}) string {
......
...@@ -52,6 +52,24 @@ func TestEngine(t *testing.T) { ...@@ -52,6 +52,24 @@ func TestEngine(t *testing.T) {
} }
} }
func TestFuncMap(t *testing.T) {
fns := FuncMap()
forbidden := []string{"env", "expandenv"}
for _, f := range forbidden {
if _, ok := fns[f]; ok {
t.Errorf("Forbidden function %s exists in FuncMap.", f)
}
}
// Test for Engine-specific template functions.
expect := []string{"include", "toYaml"}
for _, f := range expect {
if _, ok := fns[f]; !ok {
t.Errorf("Expected add-on function %q", f)
}
}
}
func TestRender(t *testing.T) { func TestRender(t *testing.T) {
c := &chart.Chart{ c := &chart.Chart{
Metadata: &chart.Metadata{ Metadata: &chart.Metadata{
......
...@@ -25,7 +25,6 @@ import ( ...@@ -25,7 +25,6 @@ import (
"regexp" "regexp"
"text/template" "text/template"
"github.com/Masterminds/sprig"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"k8s.io/helm/pkg/chartutil" "k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/engine" "k8s.io/helm/pkg/engine"
...@@ -138,7 +137,7 @@ func validateAllowedExtension(fileName string) error { ...@@ -138,7 +137,7 @@ func validateAllowedExtension(fileName string) error {
func validateNoMissingValues(templatesPath string, chartValues chartutil.Values, templateContent []byte) error { func validateNoMissingValues(templatesPath string, chartValues chartutil.Values, templateContent []byte) error {
// 1 - Load Main and associated templates // 1 - Load Main and associated templates
// Main template that we will parse dynamically // Main template that we will parse dynamically
tmpl := template.New("tpl").Funcs(sprig.TxtFuncMap()) tmpl := template.New("tpl").Funcs(engine.FuncMap())
// If the templatesPath includes any *.tpl files we will parse and import them as associated templates // If the templatesPath includes any *.tpl files we will parse and import them as associated templates
associatedTemplates, err := filepath.Glob(filepath.Join(templatesPath, "*.tpl")) associatedTemplates, err := filepath.Glob(filepath.Join(templatesPath, "*.tpl"))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment