Skip to content

Commit 7c68d1a

Browse files
committed
fix: stabilize logout tests and scope config env overrides
- use viper YES flag in logout tests instead of stdin timing\n- only apply SUPABASE_* overrides to user-configurable config fields\n- recurse into embedded base config fields while skipping internal and map-backed fields
1 parent fe59a66 commit 7c68d1a

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

internal/logout/logout_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/spf13/afero"
9+
"github.com/spf13/viper"
910
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
1112
"github.com/supabase/cli/internal/testing/apitest"
@@ -19,7 +20,8 @@ func TestLogoutCommand(t *testing.T) {
1920

2021
t.Run("login with token and logout", func(t *testing.T) {
2122
keyring.MockInitWithError(keyring.ErrUnsupportedPlatform)
22-
t.Setenv("YES", "true")
23+
viper.Set("YES", true)
24+
t.Cleanup(viper.Reset)
2325
// Setup in-memory fs
2426
fsys := afero.NewMemMapFs()
2527
require.NoError(t, utils.SaveAccessToken(token, fsys))
@@ -34,10 +36,11 @@ func TestLogoutCommand(t *testing.T) {
3436

3537
t.Run("removes all Supabase CLI credentials", func(t *testing.T) {
3638
keyring.MockInit()
39+
viper.Set("YES", true)
40+
t.Cleanup(viper.Reset)
3741
require.NoError(t, credentials.StoreProvider.Set(utils.CurrentProfile.Name, token))
3842
require.NoError(t, credentials.StoreProvider.Set("project1", "password1"))
3943
require.NoError(t, credentials.StoreProvider.Set("project2", "password2"))
40-
t.Setenv("YES", "true")
4144
// Run test
4245
err := Run(context.Background(), os.Stdout, afero.NewMemMapFs())
4346
// Check error
@@ -69,7 +72,8 @@ func TestLogoutCommand(t *testing.T) {
6972

7073
t.Run("exits 0 if not logged in", func(t *testing.T) {
7174
keyring.MockInit()
72-
t.Setenv("YES", "true")
75+
viper.Set("YES", true)
76+
t.Cleanup(viper.Reset)
7377
// Setup in-memory fs
7478
fsys := afero.NewMemMapFs()
7579
// Run test
@@ -80,7 +84,8 @@ func TestLogoutCommand(t *testing.T) {
8084

8185
t.Run("throws error on failure to delete", func(t *testing.T) {
8286
keyring.MockInitWithError(keyring.ErrNotFound)
83-
t.Setenv("YES", "true")
87+
viper.Set("YES", true)
88+
t.Cleanup(viper.Reset)
8489
// Setup empty home directory
8590
t.Setenv("HOME", "")
8691
// Setup in-memory fs

pkg/config/config.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ func (c *config) Eject(w io.Writer) error {
462462
// Loads custom config file to struct fields tagged with toml.
463463
func (c *config) loadFromFile(filename string, fsys fs.FS) error {
464464
v := viper.New()
465-
v.SetEnvPrefix("SUPABASE")
466465
if err := c.mergeDefaultValues(v); err != nil {
467466
return err
468467
} else if err := mergeFileConfig(v, filename, fsys); err != nil {
@@ -494,15 +493,15 @@ func bindUserConfigEnv(v *viper.Viper, t reflect.Type, prefix string) error {
494493
textUnmarshaler := reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
495494
for i := 0; i < t.NumField(); i++ {
496495
field := t.Field(i)
497-
if field.PkgPath != "" {
498-
continue
499-
}
500496
if field.Anonymous {
501497
if err := bindUserConfigEnv(v, field.Type, prefix); err != nil {
502498
return err
503499
}
504500
continue
505501
}
502+
if field.PkgPath != "" {
503+
continue
504+
}
506505
if tag := strings.Split(field.Tag.Get("toml"), ",")[0]; tag == "-" {
507506
continue
508507
}
@@ -519,14 +518,18 @@ func bindUserConfigEnv(v *viper.Viper, t reflect.Type, prefix string) error {
519518
if fieldType.Kind() == reflect.Ptr {
520519
fieldType = fieldType.Elem()
521520
}
521+
if fieldType.Kind() == reflect.Map {
522+
continue
523+
}
522524
if fieldType.Kind() == reflect.Struct && !fieldType.Implements(textUnmarshaler) && !reflect.PointerTo(fieldType).Implements(textUnmarshaler) {
523525
if err := bindUserConfigEnv(v, fieldType, key); err != nil {
524526
return err
525527
}
526528
continue
527529
}
528-
if err := v.BindEnv(key); err != nil {
529-
return errors.Errorf("failed to bind env override for %s: %w", key, err)
530+
envKey := "SUPABASE_" + strings.ToUpper(strings.ReplaceAll(key, ".", "_"))
531+
if value, ok := os.LookupEnv(envKey); ok {
532+
v.Set(key, value)
530533
}
531534
}
532535
return nil

0 commit comments

Comments
 (0)