Skip to content

Commit b63c9d4

Browse files
committed
chore: add test data
1 parent 327dfc1 commit b63c9d4

File tree

10 files changed

+128
-26
lines changed

10 files changed

+128
-26
lines changed

internal/bootstrap/bootstrap_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/spf13/afero"
1212
"github.com/stretchr/testify/assert"
1313
"github.com/stretchr/testify/require"
14+
"github.com/supabase/cli/internal/utils"
1415
"github.com/supabase/cli/internal/utils/flags"
1516
"github.com/supabase/cli/pkg/api"
1617
)
@@ -64,6 +65,7 @@ func TestWriteEnv(t *testing.T) {
6465

6566
t.Run("writes .env", func(t *testing.T) {
6667
flags.ProjectRef = "testing"
68+
utils.CurrentProfile.ProjectHost = "supabase.co"
6769
// Setup in-memory fs
6870
fsys := afero.NewMemMapFs()
6971
// Run test
@@ -80,6 +82,7 @@ SUPABASE_URL="https://testing.supabase.co"`, string(env))
8082

8183
t.Run("merges with .env.example", func(t *testing.T) {
8284
flags.ProjectRef = "testing"
85+
utils.CurrentProfile.ProjectHost = "supabase.co"
8386
// Setup in-memory fs
8487
fsys := afero.NewMemMapFs()
8588
example, err := godotenv.Marshal(map[string]string{

internal/functions/list/list_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package list
33
import (
44
"context"
55
"errors"
6+
"net/http"
67
"testing"
78

89
"github.com/h2non/gock"
@@ -14,14 +15,15 @@ import (
1415
)
1516

1617
func TestFunctionsListCommand(t *testing.T) {
18+
// Setup valid project ref
19+
project := apitest.RandomProjectRef()
20+
// Setup valid access token
21+
token := apitest.RandomAccessToken(t)
22+
t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
23+
1724
t.Run("lists all functions", func(t *testing.T) {
1825
// Setup in-memory fs
1926
fsys := afero.NewMemMapFs()
20-
// Setup valid project ref
21-
project := apitest.RandomProjectRef()
22-
// Setup valid access token
23-
token := apitest.RandomAccessToken(t)
24-
t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
2527
// Flush pending mocks after test execution
2628
defer gock.OffAll()
2729

@@ -53,23 +55,23 @@ func TestFunctionsListCommand(t *testing.T) {
5355
assert.Empty(t, apitest.ListUnmatchedRequests())
5456
})
5557

56-
t.Run("throws error on missing access token", func(t *testing.T) {
58+
t.Run("throws error on service unavailable", func(t *testing.T) {
5759
// Setup in-memory fs
5860
fsys := afero.NewMemMapFs()
61+
// Flush pending mocks after test execution
62+
defer gock.OffAll()
63+
gock.New(utils.DefaultApiHost).
64+
Get("/v1/projects/" + project + "/functions").
65+
Reply(http.StatusServiceUnavailable)
5966
// Run test
60-
err := Run(context.Background(), "", fsys)
67+
err := Run(context.Background(), project, fsys)
6168
// Check error
6269
assert.ErrorContains(t, err, "Unexpected error retrieving functions")
6370
})
6471

6572
t.Run("throws error on network error", func(t *testing.T) {
6673
// Setup in-memory fs
6774
fsys := afero.NewMemMapFs()
68-
// Setup valid project ref
69-
project := apitest.RandomProjectRef()
70-
// Setup valid access token
71-
token := apitest.RandomAccessToken(t)
72-
t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
7375
// Flush pending mocks after test execution
7476
defer gock.OffAll()
7577
gock.New(utils.DefaultApiHost).

internal/hostnames/activate/activate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Run(ctx context.Context, projectRef string, includeRawOutput bool, fsys afe
2727
{
2828
resp, err := utils.GetSupabase().V1ActivateCustomHostnameWithResponse(ctx, projectRef)
2929
if err != nil {
30-
return errors.Errorf("failed to active custom hostname: %w", err)
30+
return errors.Errorf("failed to activate custom hostname: %w", err)
3131
}
3232
if resp.JSON201 == nil {
3333
return errors.New("failed to activate custom hostname config: " + string(resp.Body))

internal/hostnames/common_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77

88
"github.com/h2non/gock"
99
"github.com/stretchr/testify/assert"
10+
"github.com/supabase/cli/internal/utils"
1011
)
1112

1213
func TestVerifyCNAME(t *testing.T) {
14+
utils.CurrentProfile.ProjectHost = "supabase.co"
1315
defer gock.OffAll()
1416
gock.New("https://1.1.1.1").
1517
Get("/dns-query").
@@ -40,5 +42,5 @@ func TestVerifyCNAMEFailures(t *testing.T) {
4042
},
4143
}})
4244
err := VerifyCNAME(context.Background(), "foobarbaz", "hello.custom-domain.com")
43-
assert.ErrorContains(t, err, "expected custom hostname 'hello.custom-domain.com' to have a CNAME record pointing to your project at 'foobarbaz.supabase.co.', but it failed to resolve: failed to locate appropriate CNAME record for hello.custom-domain.com")
45+
assert.ErrorContains(t, err, "failed to locate appropriate CNAME record for hello.custom-domain.com")
4446
}

internal/utils/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ func GetSupabase() *supabase.ClientWithResponses {
138138
return apiClient
139139
}
140140

141-
const DefaultApiHost = "https://api.supabase.com"
141+
// Used by unit tests
142+
var DefaultApiHost = CurrentProfile.APIURL
142143

143144
var RegionMap = map[string]string{
144145
"ap-northeast-1": "Northeast Asia (Tokyo)",

internal/utils/profile.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/go-errors/errors"
88
"github.com/go-playground/validator/v10"
9-
"github.com/go-viper/mapstructure/v2"
109
"github.com/spf13/afero"
1110
"github.com/spf13/viper"
1211
)
@@ -20,7 +19,7 @@ type Profile struct {
2019
StudioImage string `mapstructure:"studio_image"`
2120
}
2221

23-
var allProfile = []Profile{{
22+
var allProfiles = []Profile{{
2423
Name: "supabase",
2524
APIURL: "https://api.supabase.com",
2625
DashboardURL: "https://supabase.com/dashboard",
@@ -32,32 +31,33 @@ var allProfile = []Profile{{
3231
DashboardURL: "https://supabase.green/dashboard",
3332
DocsURL: "https://supabase.com/docs",
3433
ProjectHost: "supabase.red",
34+
}, {
35+
Name: "supabase-local",
36+
APIURL: "http://localhost:8080",
37+
DashboardURL: "http://localhost:8082",
38+
DocsURL: "https://supabase.com/docs",
39+
ProjectHost: "supabase.red",
3540
}}
3641

3742
var CurrentProfile Profile
3843

3944
func LoadProfile(ctx context.Context, fsys afero.Fs) error {
4045
prof := viper.GetString("PROFILE")
41-
for _, p := range allProfile {
46+
for _, p := range allProfiles {
4247
if strings.EqualFold(p.Name, prof) {
4348
CurrentProfile = p
4449
return nil
4550
}
4651
}
47-
// Instantiate to prevent accidentally leaking viper state to pkg internal calls
52+
// Instantiate to avoid leaking profile into global viper state
4853
v := viper.New()
4954
v.SetFs(fsys)
5055
v.SetConfigFile(prof)
5156
if err := v.ReadInConfig(); err != nil {
5257
return errors.Errorf("failed to read profile: %w", err)
5358
}
54-
// Load envs into viper, rejecting keys not defined by config
55-
if err := v.UnmarshalExact(&CurrentProfile, viper.DecodeHook(mapstructure.ComposeDecodeHookFunc(
56-
mapstructure.StringToTimeDurationHookFunc(),
57-
mapstructure.StringToIPHookFunc(),
58-
mapstructure.StringToSliceHookFunc(","),
59-
mapstructure.TextUnmarshallerHookFunc(),
60-
))); err != nil {
59+
// Load profile into viper, rejecting keys not defined by config
60+
if err := v.UnmarshalExact(&CurrentProfile); err != nil {
6161
return errors.Errorf("failed to parse profile: %w", err)
6262
}
6363
validate := validator.New(validator.WithRequiredStructEnabled())

internal/utils/profile_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package utils
2+
3+
import (
4+
"context"
5+
"embed"
6+
"os"
7+
"testing"
8+
9+
"github.com/go-playground/validator/v10"
10+
"github.com/spf13/afero"
11+
"github.com/spf13/viper"
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
//go:embed testdata/*.json
16+
var testdata embed.FS
17+
18+
func TestLoadProfile(t *testing.T) {
19+
validate := validator.New(validator.WithRequiredStructEnabled())
20+
for _, p := range allProfiles {
21+
t.Run("loads profile "+p.Name, func(t *testing.T) {
22+
viper.Set("PROFILE", p.Name)
23+
t.Cleanup(viper.Reset)
24+
// Setup in-memory fs
25+
fsys := afero.NewMemMapFs()
26+
// Run test
27+
err := LoadProfile(context.Background(), fsys)
28+
// Check error
29+
assert.NoError(t, err)
30+
assert.NoError(t, validate.Struct(&CurrentProfile))
31+
})
32+
}
33+
34+
t.Run("loads from json", func(t *testing.T) {
35+
viper.Set("PROFILE", "testdata/profile.json")
36+
t.Cleanup(viper.Reset)
37+
// Setup in-memory fs
38+
fsys := afero.FromIOFS{FS: testdata}
39+
// Run test
40+
err := LoadProfile(context.Background(), fsys)
41+
// Check error
42+
assert.NoError(t, err)
43+
})
44+
45+
t.Run("throws error on invalid profile", func(t *testing.T) {
46+
viper.Set("PROFILE", "testdata/invalid.json")
47+
t.Cleanup(viper.Reset)
48+
// Setup in-memory fs
49+
fsys := afero.FromIOFS{FS: testdata}
50+
// Run test
51+
err := LoadProfile(context.Background(), fsys)
52+
// Check error
53+
assert.ErrorContains(t, err, "Field validation for 'APIURL' failed on the 'http_url' tag")
54+
assert.ErrorContains(t, err, "Field validation for 'ProjectHost' failed on the 'hostname_rfc1123' tag")
55+
})
56+
57+
t.Run("throws error on malformed profile", func(t *testing.T) {
58+
viper.Set("PROFILE", "testdata/malformed.json")
59+
t.Cleanup(viper.Reset)
60+
// Setup in-memory fs
61+
fsys := afero.FromIOFS{FS: testdata}
62+
// Run test
63+
err := LoadProfile(context.Background(), fsys)
64+
// Check error
65+
assert.ErrorContains(t, err, "invalid keys: test_url")
66+
})
67+
68+
t.Run("throws error on missing profile", func(t *testing.T) {
69+
viper.Set("PROFILE", "testdata/missing.json")
70+
t.Cleanup(viper.Reset)
71+
// Setup in-memory fs
72+
fsys := afero.FromIOFS{FS: testdata}
73+
// Run test
74+
err := LoadProfile(context.Background(), fsys)
75+
// Check error
76+
assert.ErrorIs(t, err, os.ErrNotExist)
77+
})
78+
}

internal/utils/testdata/invalid.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "supabase",
3+
"api_url": "api.supabase.com",
4+
"dashboard_url": "https://supabase.com/dashboard",
5+
"project_host": "https://supabase.co"
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "supabase",
3+
"test_url": "https://api.supabase.com"
4+
}

internal/utils/testdata/profile.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "supabase",
3+
"api_url": "https://api.supabase.com",
4+
"dashboard_url": "https://supabase.com/dashboard",
5+
"project_host": "supabase.co"
6+
}

0 commit comments

Comments
 (0)