Repository: supabase/cli
Pull request #331 (2022-08-05 04:06:35)

test: db branch list command
## What kind of change does this PR introduce?

bug fix, unit test

## What is the current behavior?

error if current branch is not found

## What is the new behavior?

lists all branches

## Additional context

Add any other context or screenshots.


coveralls: ## Pull Request Test Coverage Report for [Build 2801209645](https://coveralls.io/builds/51438725)

* **5** of **6**   **(83.33%)**  changed or added relevant lines in **2** files are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage increased (+**0.5%**) to **22.203%**

---

|  Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
| :-----|--------------|--------|---: |
| [cmd/db.go](https://coveralls.io/builds/51438725/source?filename=cmd%2Fdb.go#L55) | 0 | 1 | 0.0%
<!-- | **Total:** | **5** | **6** | **83.33%** | -->


|  Totals | [![Coverage Status](https://coveralls.io/builds/51438725/badge)](https://coveralls.io/builds/51438725) |
| :-- | --: |
| Change from base [Build 2800935270](https://coveralls.io/builds/51437836): |  0.5% |
| Covered Lines: | 1137 |
| Relevant Lines: | 5121 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Patch:
diff --git a/cmd/db.go b/cmd/db.go
index d5a4dea7..a2e843b5 100644
--- a/cmd/db.go
+++ b/cmd/db.go
@@ -1,6 +1,8 @@
 package cmd
 
 import (
+	"os"
+
 	"github.com/spf13/afero"
 	"github.com/spf13/cobra"
 	"github.com/spf13/viper"
@@ -50,7 +52,7 @@ var (
 		Use:   "list",
 		Short: "List branches.",
 		RunE: func(cmd *cobra.Command, args []string) error {
-			return list.Run()
+			return list.Run(afero.NewOsFs(), os.Stdout)
 		},
 	}
 
diff --git a/internal/db/branch/list/list.go b/internal/db/branch/list/list.go
index c798cdc0..b54a8faa 100644
--- a/internal/db/branch/list/list.go
+++ b/internal/db/branch/list/list.go
@@ -3,33 +3,32 @@ package list
 import (
 	"errors"
 	"fmt"
+	"io"
 	"os"
+	"path/filepath"
 
+	"github.com/spf13/afero"
 	"github.com/supabase/cli/internal/utils"
 )
 
-func Run() error {
-	branches, err := os.ReadDir("supabase/.branches")
+func Run(fsys afero.Fs, out io.Writer) error {
+	branches, err := afero.ReadDir(fsys, filepath.Dir(utils.CurrBranchPath))
 	if errors.Is(err, os.ErrNotExist) {
 		return nil
 	} else if err != nil {
 		return err
 	}
 
-	currBranch, err := utils.GetCurrentBranch()
-	if err != nil {
-		return err
-	}
-
+	currBranch, _ := utils.GetCurrentBranchFS(fsys)
 	for _, branch := range branches {
 		if branch.Name() == "_current_branch" {
 			continue
 		}
 
 		if branch.Name() == currBranch {
-			fmt.Println("*", branch.Name())
+			fmt.Fprintln(out, "*", branch.Name())
 		} else {
-			fmt.Println(" ", branch.Name())
+			fmt.Fprintln(out, " ", branch.Name())
 		}
 	}
 


Test patch:
diff --git a/internal/db/branch/list/list_test.go b/internal/db/branch/list/list_test.go
new file mode 100644
index 00000000..bc76208b
--- /dev/null
+++ b/internal/db/branch/list/list_test.go
@@ -0,0 +1,66 @@
+package list
+
+import (
+	"bytes"
+	"io"
+	"path/filepath"
+	"strings"
+	"testing"
+
+	"github.com/spf13/afero"
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+	"github.com/supabase/cli/internal/utils"
+)
+
+func TestListCommand(t *testing.T) {
+	t.Run("lists all branches", func(t *testing.T) {
+		// Setup in-memory fs
+		fsys := afero.NewMemMapFs()
+		require.NoError(t, afero.WriteFile(fsys, utils.CurrBranchPath, []byte("main"), 0644))
+		base := filepath.Dir(utils.CurrBranchPath)
+		require.NoError(t, fsys.Mkdir(filepath.Join(base, "main"), 0755))
+		require.NoError(t, fsys.Mkdir(filepath.Join(base, "test"), 0755))
+		// Run test
+		var out bytes.Buffer
+		require.NoError(t, Run(fsys, &out))
+		// Validate output
+		lines := strings.Split(out.String(), "\n")
+		assert.ElementsMatch(t, []string{
+			"* main",
+			"  test",
+			"",
+		}, lines)
+	})
+
+	t.Run("lists without current branch", func(t *testing.T) {
+		// Setup in-memory fs
+		fsys := afero.NewMemMapFs()
+		base := filepath.Dir(utils.CurrBranchPath)
+		require.NoError(t, fsys.Mkdir(filepath.Join(base, "main"), 0755))
+		require.NoError(t, fsys.Mkdir(filepath.Join(base, "test"), 0755))
+		// Run test
+		var out bytes.Buffer
+		require.NoError(t, Run(fsys, &out))
+		// Validate output
+		lines := strings.Split(out.String(), "\n")
+		assert.ElementsMatch(t, []string{
+			"  main",
+			"  test",
+			"",
+		}, lines)
+	})
+
+	t.Run("lists uninitialised branch", func(t *testing.T) {
+		require.NoError(t, Run(afero.NewMemMapFs(), io.Discard))
+	})
+
+	t.Run("throws error on unreadable directory", func(t *testing.T) {
+		// Setup in-memory fs
+		fsys := afero.NewMemMapFs()
+		_, err := fsys.Create(filepath.Dir(utils.CurrBranchPath))
+		require.NoError(t, err)
+		// Run test
+		require.Error(t, Run(fsys, io.Discard))
+	})
+}