Skip to content

Commit 6a24ad5

Browse files
committed
fix: improve error handling for Helm chart directory validation and add tests
1 parent 7849516 commit 6a24ad5

2 files changed

Lines changed: 97 additions & 5 deletions

File tree

artifactory/commands/helm/package.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import (
1111
"github.com/jfrog/jfrog-client-go/artifactory"
1212
)
1313

14+
var ErrNoChartDirFound = errors.New("no valid Helm chart directory found in the provided paths")
15+
1416
func isChartDir(dir string) bool {
1517
chartPath := filepath.Join(dir, flexpack.ChartYaml)
1618
_, err := os.Stat(chartPath)
17-
return !errors.Is(err, os.ErrNotExist)
19+
return err == nil
1820
}
1921

2022
func handlePackageCommand(buildInfoOld *entities.BuildInfo, args []string, serviceManager artifactory.ArtifactoryServicesManager, buildName, buildNumber, project string) error {
@@ -33,7 +35,7 @@ func handlePackageCommand(buildInfoOld *entities.BuildInfo, args []string, servi
3335
}
3436
}
3537
if chartPath == "" {
36-
return fmt.Errorf("no valid Helm chart directory found in the provided paths")
38+
return ErrNoChartDirFound
3739
}
3840
buildInfo, err := collectBuildInfoWithFlexPack(chartPath, buildName, buildNumber)
3941
if err != nil {
@@ -49,7 +51,7 @@ func handlePackageCommand(buildInfoOld *entities.BuildInfo, args []string, servi
4951
removeDuplicateDependencies(buildInfoOld)
5052
err = saveBuildInfo(buildInfoOld, buildName, buildNumber, project)
5153
if err != nil {
52-
return fmt.Errorf("failed to save build info")
54+
return fmt.Errorf("failed to save build info: %w", err)
5355
}
5456
return nil
5557
}

artifactory/commands/helm/package_test.go

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ func TestHandlePackageCommand(t *testing.T) {
4343
},
4444
},
4545
{
46-
name: "chart path with destination flag",
46+
name: "chart path with destination flag - long form",
4747
args: func(chartPath, destination string) []string {
4848
return []string{chartPath, "--destination", destination}
4949
},
5050
},
51+
{
52+
name: "chart path with destination flag - short form",
53+
args: func(chartPath, destination string) []string {
54+
return []string{chartPath, "-d", destination}
55+
},
56+
},
5157
}
5258

5359
for _, tt := range tests {
@@ -69,6 +75,90 @@ func TestHandlePackageCommand(t *testing.T) {
6975
}
7076
}
7177

78+
func TestHandlePackageCommand_NoChartDir(t *testing.T) {
79+
t.Setenv("JFROG_CLI_HOME_DIR", t.TempDir())
80+
tests := []struct {
81+
name string
82+
args func(chartPath, destination string) []string
83+
}{
84+
{
85+
name: "Destination exists but no Chart.yaml",
86+
args: func(chartPath, destination string) []string {
87+
return []string{chartPath, "-d", destination}
88+
},
89+
},
90+
{
91+
name: "No chart among paths",
92+
args: func(chartPath, _ string) []string {
93+
return []string{chartPath}
94+
},
95+
},
96+
{
97+
name: "No paths",
98+
args: func(_, _ string) []string {
99+
return []string{}
100+
},
101+
},
102+
}
103+
104+
for _, tt := range tests {
105+
t.Run(tt.name, func(t *testing.T) {
106+
notAChartDir := t.TempDir()
107+
destination := t.TempDir()
108+
buildInfo := entities.New()
109+
err := handlePackageCommand(
110+
buildInfo,
111+
tt.args(notAChartDir, destination),
112+
&mockPackageServicesManager{},
113+
"helm-package-test",
114+
"1",
115+
"",
116+
)
117+
require.ErrorIs(t, err, ErrNoChartDirFound, "Expected error when no valid chart directory is specified")
118+
require.Empty(t, buildInfo.Modules, "Expected no modules in build info when no chart directory is found")
119+
})
120+
}
121+
}
122+
123+
func TestIsChartDir(t *testing.T) {
124+
tests := []struct {
125+
name string
126+
dir string
127+
expected bool
128+
}{
129+
{
130+
name: "Chart directory exists and contains Chart.yaml",
131+
dir: createHelmChartWithDependencies(t),
132+
expected: true,
133+
},
134+
{
135+
name: "Chart directory exists but does not contain Chart.yaml",
136+
dir: t.TempDir(),
137+
expected: false,
138+
},
139+
{
140+
name: "Chart directory does not exist",
141+
dir: filepath.Join(t.TempDir(), "nonexistent"),
142+
expected: false,
143+
},
144+
{
145+
name: "Chart directory exists but permission denied",
146+
dir: func() string {
147+
chartPath := t.TempDir()
148+
_ = os.Chmod(chartPath, 0o000) // Remove all permissions
149+
return chartPath
150+
}(),
151+
expected: false,
152+
},
153+
}
154+
155+
for _, tt := range tests {
156+
t.Run(tt.name, func(t *testing.T) {
157+
require.Equal(t, tt.expected, isChartDir(tt.dir))
158+
})
159+
}
160+
}
161+
72162
func createHelmChartWithDependencies(t *testing.T) string {
73163
t.Helper()
74164

@@ -203,4 +293,4 @@ func requireBuildInfoDetails(t *testing.T, buildInfo *entities.BuildInfo) {
203293
}
204294
require.True(t, depIds["subchart-a:0.1.0"])
205295
require.True(t, depIds["subchart-b:0.2.0"])
206-
}
296+
}

0 commit comments

Comments
 (0)