Skip to content

Commit 57fe4e3

Browse files
Copilotyxxhero
andauthored
fix: Address 4 PR reviewer comments on kustomize fallback implementation
Agent-Logs-Url: https://github.com/helmfile/chartify/sessions/6499602a-94b2-481c-ac49-a2fc2d015090 Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com>
1 parent 16b8235 commit 57fe4e3

4 files changed

Lines changed: 66 additions & 10 deletions

File tree

kubectl_kustomize_test.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package chartify
22

33
import (
44
"os"
5-
"os/exec"
65
"path/filepath"
76
"testing"
87

@@ -14,9 +13,32 @@ import (
1413
// in TestKustomizeBin.
1514
func TestKubectlKustomize(t *testing.T) {
1615
t.Run("KustomizeBuild succeeds with kubectl kustomize option", func(t *testing.T) {
17-
if _, err := exec.LookPath("kubectl"); err != nil {
18-
t.Skip("kubectl binary not found in PATH")
19-
}
16+
// Create a stub kubectl that handles the kustomize subcommand,
17+
// so this test is self-contained and not skipped when kubectl is absent.
18+
stubDir := t.TempDir()
19+
stubKubectl := filepath.Join(stubDir, "kubectl")
20+
// The stub writes minimal valid YAML to the -o output file.
21+
stubScript := []byte(`#!/bin/sh
22+
# Stub kubectl for testing kubectl kustomize
23+
if [ "$1" = "kustomize" ]; then
24+
shift
25+
OUTPUT=""
26+
while [ $# -gt 0 ]; do
27+
case "$1" in
28+
-o) OUTPUT="$2"; shift 2;;
29+
*) shift;;
30+
esac
31+
done
32+
if [ -n "$OUTPUT" ]; then
33+
printf 'apiVersion: v1\nkind: List\nitems: []\n' > "$OUTPUT"
34+
fi
35+
fi
36+
`)
37+
require.NoError(t, os.WriteFile(stubKubectl, stubScript, 0755))
38+
39+
origPath := os.Getenv("PATH")
40+
defer os.Setenv("PATH", origPath)
41+
os.Setenv("PATH", stubDir+":"+origPath)
2042

2143
tmpDir := t.TempDir()
2244
srcDir := t.TempDir()

patch.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ resources:
187187

188188
if !r.isUsingKubectlKustomize() {
189189
kustomizeArgs = append([]string{"build", tempDir}, kustomizeArgs...)
190+
} else {
191+
// kubectl kustomize does not use the "build" subcommand; pass tempDir as the target directly.
192+
kustomizeArgs = append([]string{tempDir}, kustomizeArgs...)
190193
}
191194

192195
if u.EnableAlphaPlugins {

runner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ func (r *Runner) runBytes(envs map[string]string, dir, cmd string, args ...strin
163163
wrappedErr := fmt.Errorf(`%w
164164
165165
COMMAND:
166-
%s
166+
%s
167167
168168
OUTPUT:
169-
%s`,
169+
%s`,
170170
err,
171171
indent(c, " "),
172172
indent(string(errBytes), " "),

util_test.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,15 @@ func TestKustomizeBin(t *testing.T) {
122122
})
123123

124124
t.Run("KUSTOMIZE_BIN environment variable", func(t *testing.T) {
125-
if _, ok := os.LookupEnv("KUSTOMIZE_BIN"); ok {
126-
t.Skip("KUSTOMIZE_BIN environment variable is already set")
127-
}
125+
origVal, hadVal := os.LookupEnv("KUSTOMIZE_BIN")
126+
defer func() {
127+
if hadVal {
128+
os.Setenv("KUSTOMIZE_BIN", origVal)
129+
} else {
130+
os.Unsetenv("KUSTOMIZE_BIN")
131+
}
132+
}()
128133
os.Setenv("KUSTOMIZE_BIN", "/custom/kustomize")
129-
defer os.Unsetenv("KUSTOMIZE_BIN")
130134
r := New()
131135
got := r.kustomizeBin()
132136
want := "/custom/kustomize"
@@ -148,6 +152,15 @@ func TestKustomizeBin(t *testing.T) {
148152
defer os.Setenv("PATH", origPath)
149153
os.Setenv("PATH", binDir)
150154

155+
// Ensure KUSTOMIZE_BIN does not override PATH-based lookup.
156+
origKustomizeBin, hadKustomizeBin := os.LookupEnv("KUSTOMIZE_BIN")
157+
defer func() {
158+
if hadKustomizeBin {
159+
os.Setenv("KUSTOMIZE_BIN", origKustomizeBin)
160+
}
161+
}()
162+
os.Unsetenv("KUSTOMIZE_BIN")
163+
151164
r := New()
152165
got := r.kustomizeBin()
153166
want := "kubectl kustomize"
@@ -173,6 +186,15 @@ func TestKustomizeBin(t *testing.T) {
173186
defer os.Setenv("PATH", origPath)
174187
os.Setenv("PATH", binDir)
175188

189+
// Ensure KUSTOMIZE_BIN does not override PATH-based lookup.
190+
origKustomizeBin, hadKustomizeBin := os.LookupEnv("KUSTOMIZE_BIN")
191+
defer func() {
192+
if hadKustomizeBin {
193+
os.Setenv("KUSTOMIZE_BIN", origKustomizeBin)
194+
}
195+
}()
196+
os.Unsetenv("KUSTOMIZE_BIN")
197+
176198
r := New()
177199
got := r.kustomizeBin()
178200
want := "kustomize"
@@ -190,6 +212,15 @@ func TestKustomizeBin(t *testing.T) {
190212
defer os.Setenv("PATH", origPath)
191213
os.Setenv("PATH", binDir)
192214

215+
// Ensure KUSTOMIZE_BIN does not override PATH-based lookup.
216+
origKustomizeBin, hadKustomizeBin := os.LookupEnv("KUSTOMIZE_BIN")
217+
defer func() {
218+
if hadKustomizeBin {
219+
os.Setenv("KUSTOMIZE_BIN", origKustomizeBin)
220+
}
221+
}()
222+
os.Unsetenv("KUSTOMIZE_BIN")
223+
193224
r := New()
194225
got := r.kustomizeBin()
195226
want := "kustomize"

0 commit comments

Comments
 (0)