From 15703cb19942bba7b2f5f8355dee2947e542fdda Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Helgi=20=C3=9Eormar=20=C3=9Eorbj=C3=B6rnsson?=
 <70530+helgi@users.noreply.github.com>
Date: Wed, 5 Dec 2018 13:30:42 -0800
Subject: [PATCH] Return empty string instead of nil when linting on required
 (#4748)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Return empty string instead of nil when linting on required

This allows lint to work in scenarios when required is used in secrets or it's output is passed to another function.
Due to lint mode no longer failing on missing value in required it is passing nil through which not all functions can accept.

Fixes #4747

Signed-off-by: Helgi ГћorbjГ¶rnsson <helgith@gmail.com>

* Apply suggestions from code review

Co-Authored-By: helgi <70530+helgi@users.noreply.github.com>
Signed-off-by: Helgi ГћorbjГ¶rnsson <helgith@gmail.com>

* Add tests

Signed-off-by: Helgi ГћorbjГ¶rnsson <helgith@gmail.com>
---
 pkg/engine/engine.go      |  5 +++--
 pkg/engine/engine_test.go | 27 ++++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go
index 9f212ba09..f3dd869c9 100644
--- a/pkg/engine/engine.go
+++ b/pkg/engine/engine.go
@@ -159,9 +159,10 @@ func (e *Engine) alterFuncMap(t *template.Template, referenceTpls map[string]ren
 			if e.LintMode {
 				// Don't fail on missing required values when linting
 				log.Printf("[INFO] Missing required value: %s", warn)
-				return val, nil
+				return "", nil
 			}
-			return val, fmt.Errorf(warn)
+			// Convert nil to "" in case required is piped into other functions
+			return "", fmt.Errorf(warn)
 		} else if _, ok := val.(string); ok {
 			if val == "" {
 				if e.LintMode {
diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go
index 91a3fd795..712b3b3df 100644
--- a/pkg/engine/engine_test.go
+++ b/pkg/engine/engine_test.go
@@ -466,7 +466,6 @@ func TestAlterFuncMap(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
-
 	expectStr := "All your base are belong to us"
 	if gotStr := outReq["conan/templates/quote"]; gotStr != expectStr {
 		t.Errorf("Expected %q, got %q (%v)", expectStr, gotStr, outReq)
@@ -476,6 +475,32 @@ func TestAlterFuncMap(t *testing.T) {
 		t.Errorf("Expected %q, got %q (%v)", expectNum, gotNum, outReq)
 	}
 
+	// test required without passing in needed values with lint mode on
+	// verifies lint replaces required with an empty string (should not fail)
+	lintValues := chartutil.Values{
+		"Values": chartutil.Values{
+			"who": "us",
+		},
+		"Chart": reqChart.Metadata,
+		"Release": chartutil.Values{
+			"Name": "That 90s meme",
+		},
+	}
+	e := New()
+	e.LintMode = true
+	outReq, err = e.Render(reqChart, lintValues)
+	if err != nil {
+		t.Fatal(err)
+	}
+	expectStr = "All your base are belong to us"
+	if gotStr := outReq["conan/templates/quote"]; gotStr != expectStr {
+		t.Errorf("Expected %q, got %q (%v)", expectStr, gotStr, outReq)
+	}
+	expectNum = "All  of them!"
+	if gotNum := outReq["conan/templates/bases"]; gotNum != expectNum {
+		t.Errorf("Expected %q, got %q (%v)", expectNum, gotNum, outReq)
+	}
+
 	tplChart := &chart.Chart{
 		Metadata: &chart.Metadata{Name: "TplFunction"},
 		Templates: []*chart.Template{
-- 
GitLab