Skip to content

Commit ef05173

Browse files
committed
internal/helm: tweak and test chart build summary
This makes the string less verbose and deals with the safe handling of some edge-case build states. Signed-off-by: Hidde Beydals <hello@hidde.co>
1 parent dd3afce commit ef05173

File tree

3 files changed

+93
-12
lines changed

3 files changed

+93
-12
lines changed

internal/helm/chart/builder.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ type Build struct {
138138

139139
// Summary returns a human-readable summary of the Build.
140140
func (b *Build) Summary() string {
141-
if b == nil {
142-
return "no chart build"
141+
if b == nil || b.Name == "" || b.Version == "" {
142+
return "No chart build."
143143
}
144144

145145
var s strings.Builder
@@ -148,25 +148,26 @@ func (b *Build) Summary() string {
148148
if b.Packaged {
149149
action = "Packaged"
150150
}
151-
s.WriteString(fmt.Sprintf("%s '%s' chart with version '%s'.", action, b.Name, b.Version))
151+
s.WriteString(fmt.Sprintf("%s '%s' chart with version '%s'", action, b.Name, b.Version))
152152

153-
if b.Packaged && b.ResolvedDependencies > 0 {
154-
s.WriteString(fmt.Sprintf(" Resolved %d dependencies before packaging.", b.ResolvedDependencies))
153+
if b.Packaged && len(b.ValueFiles) > 0 {
154+
s.WriteString(fmt.Sprintf(", with merged value files %v", b.ValueFiles))
155155
}
156156

157-
if len(b.ValueFiles) > 0 {
158-
s.WriteString(fmt.Sprintf(" Merged %v value files into default chart values.", b.ValueFiles))
157+
if b.Packaged && b.ResolvedDependencies > 0 {
158+
s.WriteString(fmt.Sprintf(", resolving %d dependencies before packaging", b.ResolvedDependencies))
159159
}
160160

161+
s.WriteString(".")
161162
return s.String()
162163
}
163164

164165
// String returns the Path of the Build.
165166
func (b *Build) String() string {
166-
if b != nil {
167-
return b.Path
167+
if b == nil {
168+
return ""
168169
}
169-
return ""
170+
return b.Path
170171
}
171172

172173
// packageToPath attempts to package the given chart to the out filepath.

internal/helm/chart/builder_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,90 @@ import (
2525

2626
. "github.com/onsi/gomega"
2727
"helm.sh/helm/v3/pkg/chart/loader"
28+
"helm.sh/helm/v3/pkg/chartutil"
2829
)
2930

31+
func TestBuildOptions_GetValueFiles(t *testing.T) {
32+
tests := []struct {
33+
name string
34+
valueFiles []string
35+
want []string
36+
}{
37+
{
38+
name: "Default values.yaml",
39+
valueFiles: []string{chartutil.ValuesfileName},
40+
want: nil,
41+
},
42+
{
43+
name: "Value files",
44+
valueFiles: []string{chartutil.ValuesfileName, "foo.yaml"},
45+
want: []string{chartutil.ValuesfileName, "foo.yaml"},
46+
},
47+
}
48+
for _, tt := range tests {
49+
t.Run(tt.name, func(t *testing.T) {
50+
g := NewWithT(t)
51+
52+
o := BuildOptions{ValueFiles: tt.valueFiles}
53+
g.Expect(o.GetValueFiles()).To(Equal(tt.want))
54+
})
55+
}
56+
}
57+
58+
func TestChartBuildResult_Summary(t *testing.T) {
59+
tests := []struct {
60+
name string
61+
build *Build
62+
want string
63+
}{
64+
{
65+
name: "Simple",
66+
build: &Build{
67+
Name: "chart",
68+
Version: "1.2.3-rc.1+bd6bf40",
69+
},
70+
want: "Fetched 'chart' chart with version '1.2.3-rc.1+bd6bf40'.",
71+
},
72+
{
73+
name: "With value files",
74+
build: &Build{
75+
Name: "chart",
76+
Version: "arbitrary-version",
77+
Packaged: true,
78+
ValueFiles: []string{"a.yaml", "b.yaml"},
79+
},
80+
want: "Packaged 'chart' chart with version 'arbitrary-version', with merged value files [a.yaml b.yaml].",
81+
},
82+
{
83+
name: "With dependencies",
84+
build: &Build{
85+
Name: "chart",
86+
Version: "arbitrary-version",
87+
Packaged: true,
88+
ResolvedDependencies: 5,
89+
},
90+
want: "Packaged 'chart' chart with version 'arbitrary-version', resolving 5 dependencies before packaging.",
91+
},
92+
{
93+
name: "Empty build",
94+
build: &Build{},
95+
want: "No chart build.",
96+
},
97+
{
98+
name: "Nil build",
99+
build: nil,
100+
want: "No chart build.",
101+
},
102+
}
103+
for _, tt := range tests {
104+
t.Run(tt.name, func(t *testing.T) {
105+
g := NewWithT(t)
106+
107+
g.Expect(tt.build.Summary()).To(Equal(tt.want))
108+
})
109+
}
110+
}
111+
30112
func TestChartBuildResult_String(t *testing.T) {
31113
g := NewWithT(t)
32114

internal/helm/repository/chart_repository.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,6 @@ func (r *ChartRepository) CacheIndex() (string, error) {
287287
// LoadFromCache if it does not HasIndex.
288288
// If it not HasCacheFile, a cache attempt is made using CacheIndex
289289
// before continuing to load.
290-
// It returns a boolean indicating if it cached the index before
291-
// loading, or an error.
292290
func (r *ChartRepository) StrategicallyLoadIndex() (err error) {
293291
if r.HasIndex() {
294292
return

0 commit comments

Comments
 (0)