1
1
package deploy
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"embed"
6
7
"errors"
7
8
"io"
9
+ "mime/multipart"
8
10
"net/http"
11
+ "os"
12
+ "path"
9
13
"testing"
10
14
11
15
"github.com/h2non/gock"
@@ -16,6 +20,7 @@ import (
16
20
"github.com/supabase/cli/internal/utils"
17
21
"github.com/supabase/cli/internal/utils/flags"
18
22
"github.com/supabase/cli/pkg/api"
23
+ "github.com/supabase/cli/pkg/cast"
19
24
"github.com/supabase/cli/pkg/config"
20
25
)
21
26
@@ -72,6 +77,53 @@ func TestImportPaths(t *testing.T) {
72
77
})
73
78
}
74
79
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
+
75
127
func TestDeployAll (t * testing.T ) {
76
128
flags .ProjectRef = apitest .RandomProjectRef ()
77
129
// Setup valid access token
0 commit comments