github.com/supabase/cli@v1.168.1/internal/db/branch/list/list_test.go (about) 1 package list 2 3 import ( 4 "bytes" 5 "io" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/spf13/afero" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 "github.com/supabase/cli/internal/utils" 14 ) 15 16 func TestListCommand(t *testing.T) { 17 t.Run("lists all branches", func(t *testing.T) { 18 // Setup in-memory fs 19 fsys := afero.NewMemMapFs() 20 require.NoError(t, afero.WriteFile(fsys, utils.CurrBranchPath, []byte("main"), 0644)) 21 base := filepath.Dir(utils.CurrBranchPath) 22 require.NoError(t, fsys.Mkdir(filepath.Join(base, "main"), 0755)) 23 require.NoError(t, fsys.Mkdir(filepath.Join(base, "test"), 0755)) 24 // Run test 25 var out bytes.Buffer 26 require.NoError(t, Run(fsys, &out)) 27 // Validate output 28 lines := strings.Split(out.String(), "\n") 29 assert.ElementsMatch(t, []string{ 30 "* main", 31 " test", 32 "", 33 }, lines) 34 }) 35 36 t.Run("lists without current branch", func(t *testing.T) { 37 // Setup in-memory fs 38 fsys := afero.NewMemMapFs() 39 base := filepath.Dir(utils.CurrBranchPath) 40 require.NoError(t, fsys.Mkdir(filepath.Join(base, "main"), 0755)) 41 require.NoError(t, fsys.Mkdir(filepath.Join(base, "test"), 0755)) 42 // Run test 43 var out bytes.Buffer 44 require.NoError(t, Run(fsys, &out)) 45 // Validate output 46 lines := strings.Split(out.String(), "\n") 47 assert.ElementsMatch(t, []string{ 48 " main", 49 " test", 50 "", 51 }, lines) 52 }) 53 54 t.Run("lists uninitialized branch", func(t *testing.T) { 55 require.NoError(t, Run(afero.NewMemMapFs(), io.Discard)) 56 }) 57 58 t.Run("throws error on unreadable directory", func(t *testing.T) { 59 // Setup in-memory fs 60 fsys := afero.NewMemMapFs() 61 _, err := fsys.Create(filepath.Dir(utils.CurrBranchPath)) 62 require.NoError(t, err) 63 // Run test 64 require.Error(t, Run(fsys, io.Discard)) 65 }) 66 }