From 0bfd616a73c7c1cd239a368c1f176c90a517e8e3 Mon Sep 17 00:00:00 2001 From: Miguel Mendoza Date: Wed, 11 Dec 2024 11:30:36 -0800 Subject: [PATCH] fix: set-file was not reading file content --- pkg/flags/setfile.go | 6 +++--- pkg/render/render_test.go | 21 +++++++++++++++++++ pkg/render/testdata/render/set-file/data.json | 5 +++++ .../testdata/render/set-file/exp-input.yaml | 15 +++++++++++++ .../testdata/render/set-file/input.yaml | 16 ++++++++++++++ 5 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 pkg/render/testdata/render/set-file/data.json create mode 100644 pkg/render/testdata/render/set-file/exp-input.yaml create mode 100644 pkg/render/testdata/render/set-file/input.yaml diff --git a/pkg/flags/setfile.go b/pkg/flags/setfile.go index e14bb0d..fa8644b 100644 --- a/pkg/flags/setfile.go +++ b/pkg/flags/setfile.go @@ -25,8 +25,8 @@ type SetFile struct { Data *values.Map } -func NewSetFile(data *values.Map) SetString { - return SetString{Data: data} +func NewSetFile(data *values.Map) SetFile { + return SetFile{Data: data} } func (v *SetFile) Type() string { @@ -48,6 +48,6 @@ func (v *SetFile) Set(entry string) error { return errors.New(err) } - v.Data.Set(key, fileText) + v.Data.Set(key, string(fileText)) return nil } diff --git a/pkg/render/render_test.go b/pkg/render/render_test.go index a876794..9331a86 100644 --- a/pkg/render/render_test.go +++ b/pkg/render/render_test.go @@ -22,6 +22,7 @@ import ( "os" "path" "path/filepath" + "strings" "testing" ) @@ -32,6 +33,7 @@ func TestRenderGeneric(t *testing.T) { templateFile string valuesFile string includesFile string + setFileFlag string wantErr error }{ { @@ -39,6 +41,7 @@ func TestRenderGeneric(t *testing.T) { "input.yaml", "", "", + "", nil, }, { @@ -46,6 +49,7 @@ func TestRenderGeneric(t *testing.T) { "input.yaml", "", "_helpers.tmpl", + "", nil, }, { @@ -53,6 +57,15 @@ func TestRenderGeneric(t *testing.T) { "apiproxy.yaml", "values.yaml", "", + "", + nil, + }, + { + "set-file", + "input.yaml", + "", + "", + "data=./data.json", nil, }, } @@ -87,6 +100,14 @@ func TestRenderGeneric(t *testing.T) { require.NoError(t, err) } + if tt.setFileFlag != "" { + key, filePath, _ := strings.Cut(tt.setFileFlag, "=") + tt.setFileFlag = fmt.Sprintf("%s=%s", key, path.Join(testDir, filePath)) + f := flags.NewSetFile(cFlags.Values) + err := f.Set(tt.setFileFlag) + require.NoError(t, err) + } + err = RenderGenericTemplate(cFlags, false) if tt.wantErr != nil { diff --git a/pkg/render/testdata/render/set-file/data.json b/pkg/render/testdata/render/set-file/data.json new file mode 100644 index 0000000..e6c0a5d --- /dev/null +++ b/pkg/render/testdata/render/set-file/data.json @@ -0,0 +1,5 @@ +[ + { + "name": "hello world" + } +] \ No newline at end of file diff --git a/pkg/render/testdata/render/set-file/exp-input.yaml b/pkg/render/testdata/render/set-file/exp-input.yaml new file mode 100644 index 0000000..4da561c --- /dev/null +++ b/pkg/render/testdata/render/set-file/exp-input.yaml @@ -0,0 +1,15 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: hello world \ No newline at end of file diff --git a/pkg/render/testdata/render/set-file/input.yaml b/pkg/render/testdata/render/set-file/input.yaml new file mode 100644 index 0000000..2fef151 --- /dev/null +++ b/pkg/render/testdata/render/set-file/input.yaml @@ -0,0 +1,16 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#{{ $data := index ($.Values.data | fromJson) 0 }} +name: {{ $data.name }}