Skip to content

Commit e63010a

Browse files
authored
Fix some risk conditions for golden test (#637)
1 parent 8e78fe9 commit e63010a

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

go/fn/testhelpers/golden.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package testhelpers
1616

1717
import (
1818
"bytes"
19+
"errors"
1920
"fmt"
2021
"io"
2122
"os"
@@ -67,6 +68,11 @@ func RunGoldenTests(t *testing.T, basedir string, krmFunction fn.ResourceListPro
6768
if strings.HasPrefix(f.Name(), "_") {
6869
continue
6970
}
71+
// Users can put other types of files to the test dir, but they won't be read.
72+
// A default kpt package contains README file.
73+
if !IsValidYAMLOrKptfile(f.Name()) {
74+
continue
75+
}
7076
fileItems := mustParseFile(t, filepath.Join(dir, f.Name()))
7177
items = append(items, fileItems...)
7278
}
@@ -75,33 +81,40 @@ func RunGoldenTests(t *testing.T, basedir string, krmFunction fn.ResourceListPro
7581

7682
var functionConfig *fn.KubeObject
7783
if len(config) == 0 {
78-
functionConfig = nil
84+
functionConfig = fn.NewEmptyKubeObject()
7985
} else if len(config) == 1 {
8086
functionConfig = config[0]
8187
} else {
8288
t.Fatalf("found multiple config objects in %s", filepath.Join(dir, "_fnconfig.yaml"))
8389
}
84-
8590
rl := &fn.ResourceList{Items: items, FunctionConfig: functionConfig}
86-
success, err := krmFunction.Process(rl)
91+
_, err = krmFunction.Process(rl)
8792
if err != nil {
8893
t.Fatalf("run failed unexpectedly: %v", err)
8994
}
90-
if !success {
91-
t.Fatalf("run did not succeed")
92-
}
93-
9495
rlYAML, err := rl.ToYAML()
9596
if err != nil {
9697
t.Fatalf("failed to convert resource list to yaml: %v", err)
9798
}
98-
9999
p := filepath.Join(dir, "_expected.yaml")
100100
CompareGoldenFile(t, p, rlYAML)
101101
})
102102
}
103103
}
104104

105+
func IsValidYAMLOrKptfile(fname string) bool {
106+
if strings.HasSuffix(fname, ".yaml") {
107+
return true
108+
}
109+
if strings.HasSuffix(fname, ".yml") {
110+
return true
111+
}
112+
if fname == "Kptfile" {
113+
return true
114+
}
115+
return false
116+
}
117+
105118
// MustReadFile reads the data from "expectedPath"
106119
func MustReadFile(t *testing.T, expectedPath string) []byte {
107120
b, err := os.ReadFile(expectedPath)
@@ -119,8 +132,7 @@ func CompareGoldenFile(t *testing.T, expectedPath string, got []byte) {
119132
if err == nil && bytes.Equal(b, got) {
120133
return
121134
}
122-
123-
if err := os.WriteFile(expectedPath, got, 0600); err != nil {
135+
if err = os.WriteFile(expectedPath, got, 0600); err != nil {
124136
t.Fatalf("failed to write golden output %s: %v", expectedPath, err)
125137
}
126138
t.Errorf("wrote output to %s", expectedPath)
@@ -251,7 +263,11 @@ func CompareFile(t *testing.T, expectDir, actualDir string, relPath string) {
251263
}
252264

253265
func mustParseFile(t *testing.T, path string) fn.KubeObjects {
266+
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
267+
return nil
268+
}
254269
b := MustReadFile(t, path)
270+
255271
objects, err := fn.ParseKubeObjects(b)
256272
if err != nil {
257273
t.Fatalf("failed to parse objects from file %q: %v", path, err)

0 commit comments

Comments
 (0)