forked from supabase/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeno_test.go
More file actions
216 lines (202 loc) · 6.12 KB
/
deno_test.go
File metadata and controls
216 lines (202 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package function
import (
"embed"
"io"
"os"
"testing"
fs "testing/fstest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
//go:embed testdata
var testImports embed.FS
type MockFS struct {
mock.Mock
}
func (m *MockFS) ReadFile(srcPath string, w io.Writer) error {
_ = m.Called(srcPath)
data, err := testImports.ReadFile(srcPath)
if err != nil {
return err
}
if _, err := w.Write(data); err != nil {
return err
}
return nil
}
func TestImportPaths(t *testing.T) {
t.Run("iterates all import paths", func(t *testing.T) {
// Setup in-memory fs
fsys := MockFS{}
fsys.On("ReadFile", "/modules/my-module.ts").Once()
fsys.On("ReadFile", "testdata/modules/imports.ts").Once()
fsys.On("ReadFile", "testdata/geometries/Geometries.js").Once()
// Run test
im := ImportMap{}
err := im.WalkImportPaths("testdata/modules/imports.ts", fsys.ReadFile)
// Check error
assert.NoError(t, err)
fsys.AssertExpectations(t)
})
t.Run("iterates with import map", func(t *testing.T) {
// Setup in-memory fs
fsys := MockFS{}
fsys.On("ReadFile", "/modules/my-module.ts").Once()
fsys.On("ReadFile", "testdata/modules/imports.ts").Once()
fsys.On("ReadFile", "testdata/geometries/Geometries.js").Once()
fsys.On("ReadFile", "testdata/shared/whatever.ts").Once()
fsys.On("ReadFile", "testdata/shared/mod.ts").Once()
fsys.On("ReadFile", "testdata/nested/index.ts").Once()
// Setup deno.json
im := ImportMap{Imports: map[string]string{
"module-name/": "../shared/",
}}
assert.NoError(t, im.Resolve("testdata/modules/deno.json"))
// Run test
err := im.WalkImportPaths("testdata/modules/imports.ts", fsys.ReadFile)
// Check error
assert.NoError(t, err)
fsys.AssertExpectations(t)
})
t.Run("resolves legacy import map", func(t *testing.T) {
// Setup in-memory fs
fsys := MockFS{}
fsys.On("ReadFile", "/modules/my-module.ts").Once()
fsys.On("ReadFile", "testdata/modules/imports.ts").Once()
fsys.On("ReadFile", "testdata/geometries/Geometries.js").Once()
fsys.On("ReadFile", "testdata/shared/whatever.ts").Once()
fsys.On("ReadFile", "testdata/shared/mod.ts").Once()
fsys.On("ReadFile", "testdata/nested/index.ts").Once()
// Setup legacy import map
im := ImportMap{Imports: map[string]string{
"module-name/": "./shared/",
}}
assert.NoError(t, im.Resolve("testdata/import_map.json"))
// Run test
err := im.WalkImportPaths("testdata/modules/imports.ts", fsys.ReadFile)
// Check error
assert.NoError(t, err)
fsys.AssertExpectations(t)
})
t.Run("iterates multiline import type statements", func(t *testing.T) {
// This test verifies that multiline import type statements are correctly parsed
// The regex must use [\s\S]*? instead of .*? to match newlines
// Setup in-memory fs
fsys := MockFS{}
fsys.On("ReadFile", "testdata/modules/import_types.ts").Once()
fsys.On("ReadFile", "testdata/types/database.ts").Once()
// Run test
im := ImportMap{}
err := im.WalkImportPaths("testdata/modules/import_types.ts", fsys.ReadFile)
// Check error
assert.NoError(t, err)
fsys.AssertExpectations(t)
})
}
func TestResolveImports(t *testing.T) {
t.Run("resolves relative directory", func(t *testing.T) {
imPath := "supabase/functions/import_map.json"
// Setup in-memory fs
fsys := fs.MapFS{
imPath: &fs.MapFile{Data: []byte(`{
"imports": {
"abs/": "/tmp/",
"root": "../../common",
"parent": "../tests",
"child": "child/",
"missing": "../missing"
}
}`)},
"/tmp/": &fs.MapFile{},
"common": &fs.MapFile{},
"supabase/tests": &fs.MapFile{},
"supabase/functions/child": &fs.MapFile{},
}
// Run test
resolved := ImportMap{}
err := resolved.Load(imPath, fsys)
// Check error
assert.NoError(t, err)
assert.Equal(t, "/tmp/", resolved.Imports["abs/"])
assert.Equal(t, "./common", resolved.Imports["root"])
assert.Equal(t, "./supabase/tests", resolved.Imports["parent"])
assert.Equal(t, "./supabase/functions/child/", resolved.Imports["child"])
assert.Equal(t, "./supabase/missing", resolved.Imports["missing"])
})
t.Run("resolves parent scopes", func(t *testing.T) {
imPath := "supabase/functions/import_map.json"
// Setup in-memory fs
fsys := fs.MapFS{
imPath: &fs.MapFile{Data: []byte(`{
"scopes": {
"my-scope": {
"my-mod": "https://deno.land"
}
}
}`)},
}
// Run test
resolved := ImportMap{}
err := resolved.Load(imPath, fsys)
// Check error
assert.NoError(t, err)
assert.Equal(t, "https://deno.land", resolved.Scopes["my-scope"]["my-mod"])
})
}
func TestResolveDeno(t *testing.T) {
t.Run("resolves deno.json", func(t *testing.T) {
imPath := "supabase/functions/slug/deno.json"
// Setup in-memory fs
fsys := fs.MapFS{
imPath: &fs.MapFile{Data: []byte(`{
"imports": {
"@mod": "./mod.ts"
},
"importMap": "../../import_map.json"
}`)},
"supabase/functions/slug/mod.ts": &fs.MapFile{},
}
// Run test
resolved := ImportMap{}
err := resolved.LoadAsDeno(imPath, fsys)
// Check error
assert.NoError(t, err)
assert.Equal(t, "./supabase/functions/slug/mod.ts", resolved.Imports["@mod"])
})
t.Run("resolves fallback imports", func(t *testing.T) {
imPath := "supabase/functions/slug/deno.json"
// Setup in-memory fs
fsys := fs.MapFS{
imPath: &fs.MapFile{Data: []byte(`{
"importMap": "../../import_map.json"
}`)},
"supabase/import_map.json": &fs.MapFile{Data: []byte(`{
"imports": {
"my-mod": "https://deno.land"
}
}`)},
}
// Run test
resolved := ImportMap{}
err := resolved.LoadAsDeno(imPath, fsys)
// Check error
assert.NoError(t, err)
assert.Equal(t, "https://deno.land", resolved.Imports["my-mod"])
})
t.Run("throws error on missing import", func(t *testing.T) {
imPath := "supabase/functions/slug/deno.jsonc"
// Setup in-memory fs
fsys := fs.MapFS{
imPath: &fs.MapFile{Data: []byte(`{
"importMap": "../../import_map.json"
}`)},
}
// Run test
resolved := ImportMap{}
err := resolved.LoadAsDeno(imPath, fsys)
// Check error
assert.ErrorIs(t, err, os.ErrNotExist)
assert.Empty(t, resolved.Imports)
assert.Empty(t, resolved.Scopes)
})
}