github.com/supabase/cli@v1.168.1/internal/unlink/unlink_test.go (about) 1 package unlink 2 3 import ( 4 "context" 5 "os" 6 "testing" 7 8 "github.com/spf13/afero" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 "github.com/supabase/cli/internal/testing/apitest" 12 "github.com/supabase/cli/internal/utils" 13 "github.com/supabase/cli/internal/utils/credentials" 14 "github.com/zalando/go-keyring" 15 ) 16 17 func TestUnlinkCommand(t *testing.T) { 18 keyring.MockInit() 19 project := apitest.RandomProjectRef() 20 21 t.Run("unlinks project", func(t *testing.T) { 22 // Setup in-memory fs 23 fsys := afero.NewMemMapFs() 24 require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(project), 0644)) 25 // Save database password 26 require.NoError(t, credentials.Set(project, "test")) 27 // Run test 28 err := Run(context.Background(), fsys) 29 // Check error 30 assert.NoError(t, err) 31 // Validate file does not exist 32 exists, err := afero.Exists(fsys, utils.ProjectRefPath) 33 assert.NoError(t, err) 34 assert.False(t, exists) 35 // Check credentials does not exist 36 _, err = credentials.Get(project) 37 assert.ErrorIs(t, err, keyring.ErrNotFound) 38 }) 39 40 t.Run("unlinks project without credentials", func(t *testing.T) { 41 // Setup in-memory fs 42 fsys := afero.NewMemMapFs() 43 require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(project), 0644)) 44 // Run test 45 err := Run(context.Background(), fsys) 46 // Check error 47 assert.NoError(t, err) 48 }) 49 50 t.Run("throws error if not linked", func(t *testing.T) { 51 // Setup in-memory fs 52 fsys := afero.NewMemMapFs() 53 // Run test 54 err := Run(context.Background(), fsys) 55 // Check error 56 assert.ErrorIs(t, err, utils.ErrNotLinked) 57 }) 58 59 t.Run("throws error on permission denied", func(t *testing.T) { 60 // Setup in-memory fs 61 fsys := afero.NewMemMapFs() 62 require.NoError(t, afero.WriteFile(fsys, utils.ProjectRefPath, []byte(project), 0644)) 63 // Run test 64 err := Run(context.Background(), afero.NewReadOnlyFs(fsys)) 65 // Check error 66 assert.ErrorIs(t, err, os.ErrPermission) 67 }) 68 }