From b5a4781099d4cfa9b3d8746d6cf0bef61422da18 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Thu, 15 May 2025 14:53:15 -0400 Subject: [PATCH] Adding test for list command Signed-off-by: Matt Farina --- pkg/cmd/repo_list.go | 3 +++ pkg/cmd/repo_list_test.go | 25 +++++++++++++++++++++ pkg/cmd/testdata/output/repo-list-empty.txt | 1 + pkg/cmd/testdata/output/repo-list.txt | 4 ++++ 4 files changed, 33 insertions(+) create mode 100644 pkg/cmd/testdata/output/repo-list-empty.txt create mode 100644 pkg/cmd/testdata/output/repo-list.txt diff --git a/pkg/cmd/repo_list.go b/pkg/cmd/repo_list.go index b7a36cbf7..70f57992e 100644 --- a/pkg/cmd/repo_list.go +++ b/pkg/cmd/repo_list.go @@ -37,6 +37,9 @@ func newRepoListCmd(out io.Writer) *cobra.Command { Args: require.NoArgs, ValidArgsFunction: noMoreArgsCompFunc, RunE: func(cmd *cobra.Command, _ []string) error { + // The error is silently ignored. If no repository file exists, it cannot be loaded, + // or the file isn't the right format to be parsed the error is ignored. The + // repositories will be 0. f, _ := repo.LoadFile(settings.RepositoryConfig) if len(f.Repositories) == 0 && outfmt != output.JSON && outfmt != output.YAML { fmt.Fprintln(cmd.ErrOrStderr(), "no repositories to show") diff --git a/pkg/cmd/repo_list_test.go b/pkg/cmd/repo_list_test.go index 1da5484cc..2f6a9e4ad 100644 --- a/pkg/cmd/repo_list_test.go +++ b/pkg/cmd/repo_list_test.go @@ -17,6 +17,8 @@ limitations under the License. package cmd import ( + "fmt" + "path/filepath" "testing" ) @@ -27,3 +29,26 @@ func TestRepoListOutputCompletion(t *testing.T) { func TestRepoListFileCompletion(t *testing.T) { checkFileCompletion(t, "repo list", false) } + +func TestRepoList(t *testing.T) { + rootDir := t.TempDir() + repoFile := filepath.Join(rootDir, "repositories.yaml") + repoFile2 := "testdata/repositories.yaml" + + tests := []cmdTestCase{ + { + name: "list with no repos", + cmd: fmt.Sprintf("repo list --repository-config %s --repository-cache %s", repoFile, rootDir), + golden: "output/repo-list-empty.txt", + wantError: false, + }, + { + name: "list with repos", + cmd: fmt.Sprintf("repo list --repository-config %s --repository-cache %s", repoFile2, rootDir), + golden: "output/repo-list.txt", + wantError: false, + }, + } + + runTestCmd(t, tests) +} diff --git a/pkg/cmd/testdata/output/repo-list-empty.txt b/pkg/cmd/testdata/output/repo-list-empty.txt new file mode 100644 index 000000000..c6edb659a --- /dev/null +++ b/pkg/cmd/testdata/output/repo-list-empty.txt @@ -0,0 +1 @@ +no repositories to show diff --git a/pkg/cmd/testdata/output/repo-list.txt b/pkg/cmd/testdata/output/repo-list.txt new file mode 100644 index 000000000..edbd0ecc1 --- /dev/null +++ b/pkg/cmd/testdata/output/repo-list.txt @@ -0,0 +1,4 @@ +NAME URL +charts https://charts.helm.sh/stable +firstexample http://firstexample.com +secondexample http://secondexample.com