Skip to content

Commit 45a8ff8

Browse files
committed
chore: test form writer
1 parent f314d33 commit 45a8ff8

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--test
2+
Content-Disposition: form-data; name="metadata"
3+
4+
{"entrypoint_path":"testdata/nested/index.ts","import_map_path":"testdata/nested/deno.json","name":"nested","static_patterns":["testdata/*/*.js"],"verify_jwt":true}
5+
6+
--test
7+
Content-Disposition: form-data; name="file"; filename="testdata/nested/deno.json"
8+
Content-Type: application/octet-stream
9+
10+
{}
11+
12+
--test
13+
Content-Disposition: form-data; name="file"; filename="testdata/geometries/Geometries.js"
14+
Content-Type: application/octet-stream
15+
16+
17+
--test
18+
Content-Disposition: form-data; name="file"; filename="testdata/nested/index.ts"
19+
Content-Type: application/octet-stream
20+

internal/functions/deploy/upload_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package deploy
22

33
import (
4+
"bytes"
45
"context"
56
"embed"
67
"errors"
78
"io"
9+
"mime/multipart"
810
"net/http"
11+
"os"
12+
"path"
913
"testing"
1014

1115
"github.com/h2non/gock"
@@ -16,6 +20,7 @@ import (
1620
"github.com/supabase/cli/internal/utils"
1721
"github.com/supabase/cli/internal/utils/flags"
1822
"github.com/supabase/cli/pkg/api"
23+
"github.com/supabase/cli/pkg/cast"
1924
"github.com/supabase/cli/pkg/config"
2025
)
2126

@@ -72,6 +77,53 @@ func TestImportPaths(t *testing.T) {
7277
})
7378
}
7479

80+
func assertFormEqual(t *testing.T, actual []byte) {
81+
snapshot := path.Join("testdata", path.Base(t.Name())+".form")
82+
expected, err := testImports.ReadFile(snapshot)
83+
if errors.Is(err, os.ErrNotExist) {
84+
assert.NoError(t, os.WriteFile(snapshot, actual, 0600))
85+
}
86+
assert.Equal(t, string(expected), string(actual))
87+
}
88+
89+
func TestWriteForm(t *testing.T) {
90+
t.Run("writes import map", func(t *testing.T) {
91+
var buf bytes.Buffer
92+
form := multipart.NewWriter(&buf)
93+
form.SetBoundary("test")
94+
// Setup in-memory fs
95+
fsys := afero.FromIOFS{FS: testImports}
96+
// Run test
97+
err := writeForm(form, api.FunctionDeployMetadata{
98+
Name: cast.Ptr("nested"),
99+
VerifyJwt: cast.Ptr(true),
100+
EntrypointPath: "testdata/nested/index.ts",
101+
ImportMapPath: cast.Ptr("testdata/nested/deno.json"),
102+
StaticPatterns: cast.Ptr([]string{
103+
"testdata/*/*.js",
104+
"testdata/shared",
105+
}),
106+
}, fsys)
107+
// Check error
108+
assert.NoError(t, err)
109+
assertFormEqual(t, buf.Bytes())
110+
})
111+
112+
t.Run("throws error on missing file", func(t *testing.T) {
113+
var buf bytes.Buffer
114+
form := multipart.NewWriter(&buf)
115+
form.SetBoundary("test")
116+
// Setup in-memory fs
117+
fsys := afero.NewMemMapFs()
118+
// Run test
119+
err := writeForm(form, api.FunctionDeployMetadata{
120+
ImportMapPath: cast.Ptr("testdata/import_map.json"),
121+
}, fsys)
122+
// Check error
123+
assert.ErrorIs(t, err, os.ErrNotExist)
124+
})
125+
}
126+
75127
func TestDeployAll(t *testing.T) {
76128
flags.ProjectRef = apitest.RandomProjectRef()
77129
// Setup valid access token

0 commit comments

Comments
 (0)