From 17bc0b384533974d745985014141785c25a6690e Mon Sep 17 00:00:00 2001 From: George Jenkins Date: Fri, 27 Dec 2024 21:40:47 -0800 Subject: [PATCH] refactor: Remove ChartRepository Load() function Signed-off-by: George Jenkins --- pkg/repo/chartrepo.go | 34 ---------------------------- pkg/repo/chartrepo_test.go | 45 ++++++++++++-------------------------- 2 files changed, 14 insertions(+), 65 deletions(-) diff --git a/pkg/repo/chartrepo.go b/pkg/repo/chartrepo.go index 3629bd24b..ee30a9b73 100644 --- a/pkg/repo/chartrepo.go +++ b/pkg/repo/chartrepo.go @@ -79,40 +79,6 @@ func NewChartRepository(cfg *Entry, getters getter.Providers) (*ChartRepository, }, nil } -// Load loads a directory of charts as if it were a repository. -// -// It requires the presence of an index.yaml file in the directory. -// -// Deprecated: remove in Helm 4. -func (r *ChartRepository) Load() error { - dirInfo, err := os.Stat(r.Config.Name) - if err != nil { - return err - } - if !dirInfo.IsDir() { - return errors.Errorf("%q is not a directory", r.Config.Name) - } - - // FIXME: Why are we recursively walking directories? - // FIXME: Why are we not reading the repositories.yaml to figure out - // what repos to use? - filepath.Walk(r.Config.Name, func(path string, f os.FileInfo, _ error) error { - if !f.IsDir() { - if strings.Contains(f.Name(), "-index.yaml") { - i, err := LoadIndexFile(path) - if err != nil { - return err - } - r.IndexFile = i - } else if strings.HasSuffix(f.Name(), ".tgz") { - r.ChartPaths = append(r.ChartPaths, path) - } - } - return nil - }) - return nil -} - // DownloadIndexFile fetches the index from a repository. func (r *ChartRepository) DownloadIndexFile() (string, error) { indexURL, err := ResolveReferenceURL(r.Config.URL, "index.yaml") diff --git a/pkg/repo/chartrepo_test.go b/pkg/repo/chartrepo_test.go index e3330b8eb..97f98f7e6 100644 --- a/pkg/repo/chartrepo_test.go +++ b/pkg/repo/chartrepo_test.go @@ -22,12 +22,12 @@ import ( "net/http/httptest" "os" "path/filepath" - "reflect" "runtime" "strings" "testing" "time" + "github.com/stretchr/testify/require" "sigs.k8s.io/yaml" "helm.sh/helm/v4/pkg/chart" @@ -40,37 +40,22 @@ const ( testURL = "http://example-charts.com" ) -func TestLoadChartRepository(t *testing.T) { - r, err := NewChartRepository(&Entry{ - Name: testRepository, - URL: testURL, - }, getter.All(&cli.EnvSettings{})) - if err != nil { - t.Errorf("Problem creating chart repository from %s: %v", testRepository, err) - } +// loadFromDir a directory of charts archives (including sub-directories), +// appending to the repositores ChartPath +func loadFromDir(t *testing.T, r *ChartRepository, dir string) { + dirInfo, err := os.Stat(dir) + require.Nil(t, err) + require.True(t, dirInfo.IsDir()) - if err := r.Load(); err != nil { - t.Errorf("Problem loading chart repository from %s: %v", testRepository, err) - } - - paths := []string{ - filepath.Join(testRepository, "frobnitz-1.2.3.tgz"), - filepath.Join(testRepository, "sprocket-1.1.0.tgz"), - filepath.Join(testRepository, "sprocket-1.2.0.tgz"), - filepath.Join(testRepository, "universe/zarthal-1.0.0.tgz"), - } + globArchives := func(pattern string) []string { + archives, err := filepath.Glob(filepath.Join(dir, pattern)) + require.Nil(t, err) - if r.Config.Name != testRepository { - t.Errorf("Expected %s as Name but got %s", testRepository, r.Config.Name) + return archives } - if !reflect.DeepEqual(r.ChartPaths, paths) { - t.Errorf("Expected %#v but got %#v\n", paths, r.ChartPaths) - } - - if r.Config.URL != testURL { - t.Errorf("Expected url for chart repository to be %s but got %s", testURL, r.Config.URL) - } + r.ChartPaths = append(r.ChartPaths, globArchives("*.tgz")...) + r.ChartPaths = append(r.ChartPaths, globArchives("**/*.tgz")...) } func TestIndex(t *testing.T) { @@ -82,9 +67,7 @@ func TestIndex(t *testing.T) { t.Errorf("Problem creating chart repository from %s: %v", testRepository, err) } - if err := r.Load(); err != nil { - t.Errorf("Problem loading chart repository from %s: %v", testRepository, err) - } + loadFromDir(t, r, testRepository) err = r.Index() if err != nil {