From ee499502b44b49bc4a70da4a4dcb6f17e5cf94ac Mon Sep 17 00:00:00 2001 From: christopher dykstra Date: Sat, 12 Jun 2021 15:16:04 -0500 Subject: [PATCH] avoid side effects changing functions for testing, return the correct error when rendering Signed-off-by: christopher dykstra --- pkg/action/action.go | 8 ++------ pkg/action/action_test.go | 35 +++++++++++++++++------------------ 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/pkg/action/action.go b/pkg/action/action.go index d485cf901..3073410fc 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -149,13 +149,9 @@ func (cfg *Configuration) render(ch *chart.Chart, values chartutil.Values, relea } } - var files map[string]string - var err2 error - - files, err = render(ch, values) - + files, err := render(ch, values) if err != nil { - return hs, b, "", err2 + return hs, b, "", err } // NOTES.txt gets rendered like all the other files, but because it's not a hook nor a resource, diff --git a/pkg/action/action_test.go b/pkg/action/action_test.go index 96b3b1653..e94190a7f 100644 --- a/pkg/action/action_test.go +++ b/pkg/action/action_test.go @@ -322,26 +322,28 @@ func TestGetVersionSet(t *testing.T) { } } +var originalRenderStrategy = renderStrategy +var originalLocalRenderStrategy = localRenderStrategy + +func renderStrategyDecorator(decoratorAction func(ch *chart.Chart, values chartutil.Values), strategy renderStrategyFunction) renderStrategyFunction { + return func(ch *chart.Chart, values chartutil.Values) (map[string]string, error) { + decoratorAction(ch, values) + return strategy(ch, values) + } +} + func TestRenderResources_RendersWithStrategy(t *testing.T) { - var renderedLocally = false var rendered = false var renderedChart *chart.Chart var renderedValues *chartutil.Values - var nullRenderStrategy = func(ch *chart.Chart, values chartutil.Values) (map[string]string, error) { + var captureChartAndValues = func(ch *chart.Chart, values chartutil.Values) { renderedChart = ch renderedValues = &values - - return make(map[string]string), nil } - renderStrategy = func(cfg *Configuration) func(ch *chart.Chart, values chartutil.Values) (map[string]string, error) { + renderStrategy = func(cfg *Configuration) renderStrategyFunction { rendered = true - return nullRenderStrategy - } - localRenderStrategy = func(ch *chart.Chart, values chartutil.Values) (map[string]string, error) { - renderedLocally = true - - return nullRenderStrategy(ch, values) + return renderStrategyDecorator(captureChartAndValues, originalRenderStrategy(cfg)) } var chart = buildChart() @@ -352,7 +354,6 @@ func TestRenderResources_RendersWithStrategy(t *testing.T) { actionConfigFixture(t).renderResources(chart, values, "", "", false, false, false, postRender) assert.True(t, rendered, "the chart should be rendered with cluster contact") - assert.False(t, renderedLocally, "the chart should not be rendered without cluster contact") assert.Same(t, chart, renderedChart, "the rendered chart and the chart resource should be the same") assert.Equal(t, values, *renderedValues, "the rendered values and the values resource should be the same") } @@ -362,21 +363,19 @@ func TestRenderResourcesLocally_RendersWithLocalStrategy(t *testing.T) { var rendered = false var renderedChart *chart.Chart var renderedValues *chartutil.Values - var nullRenderStrategy = func(ch *chart.Chart, values chartutil.Values) (map[string]string, error) { + var captureChartAndValues = func(ch *chart.Chart, values chartutil.Values) { renderedChart = ch renderedValues = &values - - return make(map[string]string), nil } - renderStrategy = func(cfg *Configuration) func(ch *chart.Chart, values chartutil.Values) (map[string]string, error) { + renderStrategy = func(cfg *Configuration) renderStrategyFunction { rendered = true - return nullRenderStrategy + return renderStrategyDecorator(captureChartAndValues, originalRenderStrategy(cfg)) } localRenderStrategy = func(ch *chart.Chart, values chartutil.Values) (map[string]string, error) { renderedLocally = true - return nullRenderStrategy(ch, values) + return renderStrategyDecorator(captureChartAndValues, originalLocalRenderStrategy)(ch, values) } var chart = buildChart()