Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"os"

"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -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)
},
}

Expand Down
17 changes: 8 additions & 9 deletions internal/db/branch/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down
66 changes: 66 additions & 0 deletions internal/db/branch/list/list_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
}