Repository: helm/helm
Pull request #30863 (2025-05-15 19:05:57)

Adding test for list command
<!--  Thanks for sending a pull request!  Here are some tips for you:
1. Make sure to read the Contributing Guide before submitting your PR: https://github.com/helm/helm/blob/main/CONTRIBUTING.md
2. If this PR closes another issue, add 'closes #<issue number>' somewhere in the PR summary. GitHub will automatically close that issue when this PR gets merged. Alternatively, adding 'refs #<issue number>' will not close the issue, but help provide the reviewer more context.-->

**What this PR does / why we need it**:

**Special notes for your reviewer**:

**If applicable**:
- [ ] this PR contains user facing changes (the `docs needed` label should be applied if so)
- [x] this PR contains unit tests
- [x] this PR has been tested for backwards compatibility



Patch:
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")


Test patch:
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,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