Skip to content

Commit 6480ac8

Browse files
committed
fix newlines at start of generated files
1 parent 361d7bd commit 6480ac8

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

pkg/helmgen/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (p *Patcher) doResource(resource string) error {
7979
return fmt.Errorf("could not convert patched JSON to YAML: %v", err)
8080
}
8181

82-
contents := make([]string, 2)
82+
contents := make([]string, 0)
8383
contents = append(contents, fmt.Sprintf("#filename %s", filename))
8484
contents = append(contents, string(yamlData))
8585

pkg/helmgen/generate_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package helmgen
33
import (
44
"os"
55
"path/filepath"
6+
"strings"
67
"testing"
78

89
jsonIterator "github.com/json-iterator/go"
@@ -55,3 +56,56 @@ func TestParseCRD(t *testing.T) {
5556
assert.NoError(t, err)
5657
})
5758
}
59+
func TestParseAll(t *testing.T) {
60+
t.Run("crd with escape chars in descriptions", func(t *testing.T) {
61+
data, err := os.ReadFile(filepath.Join("..", "..", "test", "operator-helm-gen", "testdata", "kustomize-output.yaml"))
62+
63+
if assert.NoError(t, err) {
64+
assert.NotEmpty(t, data)
65+
}
66+
67+
outputDir := "generated_tmp"
68+
69+
err = os.Mkdir(outputDir, 0777)
70+
if assert.Nil(t, err) {
71+
files, err := os.ReadDir(outputDir)
72+
if assert.Nil(t, err) {
73+
assert.Equal(t, 0, len(files))
74+
}
75+
}
76+
77+
resources := GetResources(data)
78+
assert.NotEmpty(t, resources)
79+
80+
keepers := make(map[string]string)
81+
keepers["CustomResourceDefinition"] = "ok"
82+
keepers["ClusterRoleBinding"] = "ok"
83+
keepers["RoleBinding"] = "ok"
84+
keepers["Role"] = "ok"
85+
keepers["ClusterRole"] = "ok"
86+
87+
p := NewPatcher(Spec{KeepRresources: keepers, OutputDir: outputDir})
88+
err = p.Generate(resources)
89+
90+
assert.NoError(t, err)
91+
92+
generatedFiles, err := os.ReadDir(outputDir)
93+
if assert.Nil(t, err) {
94+
assert.NotEqual(t, 0, len(generatedFiles))
95+
96+
for _, f := range generatedFiles {
97+
assert.False(t, f.IsDir())
98+
99+
contents, err := os.ReadFile(filepath.Join(outputDir, f.Name()))
100+
if assert.NoError(t, err) {
101+
if assert.NotEmpty(t, contents) {
102+
y := string(contents)
103+
assert.False(t, strings.HasPrefix(y, "\n"), "did not expect file %s to start with newline", f.Name())
104+
}
105+
}
106+
}
107+
108+
_ = os.RemoveAll(outputDir) //ignore error
109+
}
110+
})
111+
}

0 commit comments

Comments
 (0)