-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathfilename-processing.test.ts
More file actions
275 lines (246 loc) · 10.5 KB
/
filename-processing.test.ts
File metadata and controls
275 lines (246 loc) · 10.5 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { describe, expect, it } from 'vitest'
import { createMemoryEnvironment } from '../src/environment.js'
import { createTemplateFile } from '../src/template-file.js'
import type { Options } from '../src/types.js'
const simpleOptions = {
projectName: 'test',
targetDir: '/test',
framework: {
id: 'test',
name: 'Test',
},
chosenAddOns: [],
packageManager: 'pnpm',
typescript: true,
tailwind: true,
mode: 'file-router',
addOnOptions: {},
} as unknown as Options
describe('Filename Processing - Prefix Stripping', () => {
it('should strip single prefix from filename', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, {
...simpleOptions,
addOnOptions: {
testAddon: { database: 'postgres' }
}
})
environment.startRun()
await templateFile(
'__postgres__testAddon.config.ts.ejs',
'<% if (addOnOption.testAddon.database !== "postgres") { ignoreFile() } %>\n// PostgreSQL config\nexport default { driver: "postgres" }'
)
environment.finishRun()
// File should be created with prefix stripped
expect(output.files['/test/testAddon.config.ts']).toBeDefined()
expect(output.files['/test/testAddon.config.ts'].trim()).toEqual('// PostgreSQL config\nexport default { driver: \'postgres\' }')
// Original prefixed filename should not exist
expect(output.files['/test/__postgres__testAddon.config.ts']).toBeUndefined()
})
it('should strip prefix from nested directory paths', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, {
...simpleOptions,
addOnOptions: {
testAddon: { database: 'mysql' }
}
})
environment.startRun()
await templateFile(
'src/db/__mysql__connection.ts.ejs',
'<% if (addOnOption.testAddon.database !== "mysql") { ignoreFile() } %>\n// MySQL connection\nexport const connection = "mysql"'
)
environment.finishRun()
// File should be created with prefix stripped, preserving directory structure
expect(output.files['/test/src/db/connection.ts']).toBeDefined()
expect(output.files['/test/src/db/connection.ts'].trim()).toEqual('// MySQL connection\nexport const connection = \'mysql\'')
// Original prefixed path should not exist
expect(output.files['/test/src/db/__mysql__connection.ts']).toBeUndefined()
})
it('should handle multiple prefixed files in same directory', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, {
...simpleOptions,
addOnOptions: {
testAddon: { database: 'sqlite' }
}
})
environment.startRun()
await templateFile(
'__postgres__testAddon.config.ts.ejs',
'<% if (addOnOption.testAddon.database !== "postgres") { ignoreFile() } %>\n// PostgreSQL config'
)
await templateFile(
'__mysql__testAddon.config.ts.ejs',
'<% if (addOnOption.testAddon.database !== "mysql") { ignoreFile() } %>\n// MySQL config'
)
await templateFile(
'__sqlite__testAddon.config.ts.ejs',
'<% if (addOnOption.testAddon.database !== "sqlite") { ignoreFile() } %>\n// SQLite config'
)
environment.finishRun()
// Only SQLite file should exist (others ignored via ignoreFile())
expect(output.files['/test/testAddon.config.ts']).toBeDefined()
expect(output.files['/test/testAddon.config.ts'].trim()).toEqual('// SQLite config')
// Prefixed filenames should not exist
expect(output.files['/test/__postgres__testAddon.config.ts']).toBeUndefined()
expect(output.files['/test/__mysql__testAddon.config.ts']).toBeUndefined()
expect(output.files['/test/__sqlite__testAddon.config.ts']).toBeUndefined()
})
it('should handle complex filename patterns', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, {
...simpleOptions,
addOnOptions: {
auth: { provider: 'auth0' }
}
})
environment.startRun()
await templateFile(
'__auth0__auth.config.js.ejs',
'<% if (addOnOption.auth.provider !== "auth0") { ignoreFile() } %>\n// Auth0 configuration\nmodule.exports = { provider: "auth0" }'
)
environment.finishRun()
// File should be created with prefix stripped
expect(output.files['/test/auth.config.js']).toBeDefined()
expect(output.files['/test/auth.config.js'].trim()).toEqual('// Auth0 configuration\nmodule.exports = { provider: "auth0" }')
})
it('should handle prefixed files with .append.ejs extension', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, {
...simpleOptions,
addOnOptions: {
testAddon: { database: 'postgres' }
}
})
environment.startRun()
// Create base file first
await templateFile(
'.env.ejs',
'BASE_VAR=value\n'
)
// Then append with prefixed filename
await templateFile(
'__postgres__.env.append.ejs',
'<% if (addOnOption.testAddon.database !== "postgres") { ignoreFile() } %>\nDATABASE_URL=postgresql://localhost:5432/mydb\n'
)
environment.finishRun()
// File should be created with prefix stripped and content appended
expect(output.files['/test/.env']).toBeDefined()
expect(output.files['/test/.env']).toEqual('BASE_VAR=value\n\n\nDATABASE_URL=postgresql://localhost:5432/mydb\n')
})
it('should handle files without prefixes normally', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, simpleOptions)
environment.startRun()
await templateFile(
'regular-file.ts.ejs',
'export const config = "normal"'
)
environment.finishRun()
// File should be created with normal filename processing
expect(output.files['/test/regular-file.ts']).toBeDefined()
expect(output.files['/test/regular-file.ts']).toEqual('export const config = \'normal\'\n')
})
it('should handle malformed prefixes gracefully', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, simpleOptions)
environment.startRun()
await templateFile(
'__malformed_prefix.ts.ejs',
'export const config = "malformed"'
)
await templateFile(
'__only_one_underscore.ts.ejs',
'export const config = "malformed2"'
)
await templateFile(
'____.ts.ejs',
'export const config = "empty"'
)
environment.finishRun()
// Files with malformed prefixes should be created as-is (minus .ejs extension)
expect(output.files['/test/__malformed_prefix.ts']).toBeDefined()
expect(output.files['/test/__only_one_underscore.ts']).toBeDefined()
expect(output.files['/test/____.ts']).toBeDefined()
})
it('should handle deeply nested prefixed files', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, {
...simpleOptions,
addOnOptions: {
styling: { framework: 'tailwind' }
}
})
environment.startRun()
await templateFile(
'src/styles/components/__tailwind__button.css.ejs',
'<% if (addOnOption.styling.framework !== "tailwind") { ignoreFile() } %>\n@tailwind base;\n@tailwind components;\n@tailwind utilities;'
)
environment.finishRun()
// File should be created with prefix stripped, preserving deep directory structure
expect(output.files['/test/src/styles/components/button.css']).toBeDefined()
expect(output.files['/test/src/styles/components/button.css'].trim()).toEqual('@tailwind base;\n@tailwind components;\n@tailwind utilities;')
})
it('should handle prefix stripping with different option values', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, {
...simpleOptions,
addOnOptions: {
ui: { library: 'chakra' }
}
})
environment.startRun()
await templateFile(
'__chakra__theme.ts.ejs',
'<% if (addOnOption.ui.library !== "chakra") { ignoreFile() } %>\n// Chakra UI theme\nexport const theme = { colors: {} }'
)
await templateFile(
'__mui__theme.ts.ejs',
'<% if (addOnOption.ui.library !== "mui") { ignoreFile() } %>\n// Material-UI theme\nexport const theme = { palette: {} }'
)
environment.finishRun()
// Only Chakra file should exist (MUI ignored via ignoreFile())
expect(output.files['/test/theme.ts']).toBeDefined()
expect(output.files['/test/theme.ts'].trim()).toEqual('// Chakra UI theme\nexport const theme = { colors: {} }')
// Prefixed filenames should not exist
expect(output.files['/test/__chakra__theme.ts']).toBeUndefined()
expect(output.files['/test/__mui__theme.ts']).toBeUndefined()
})
it('should handle complex prefix with special characters', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, {
...simpleOptions,
addOnOptions: {
deployment: { platform: 'vercel-edge' }
}
})
environment.startRun()
await templateFile(
'__vercel-edge__api.ts.ejs',
'<% if (addOnOption.deployment.platform !== "vercel-edge") { ignoreFile() } %>\n// Vercel Edge API\nexport const runtime = "edge"'
)
environment.finishRun()
// File should be created with prefix stripped
expect(output.files['/test/api.ts']).toBeDefined()
expect(output.files['/test/api.ts'].trim()).toEqual('// Vercel Edge API\nexport const runtime = \'edge\'')
})
it('should handle multiple prefixes in same filename (edge case)', async () => {
const { environment, output } = createMemoryEnvironment()
const templateFile = createTemplateFile(environment, {
...simpleOptions,
addOnOptions: {
test: { value: 'postgres' }
}
})
environment.startRun()
await templateFile(
'__postgres__file__with__underscores.ts.ejs',
'<% if (addOnOption.test.value !== "postgres") { ignoreFile() } %>\n// File with underscores\nexport const value = "test"'
)
environment.finishRun()
// Should only strip the first valid prefix pattern
expect(output.files['/test/file__with__underscores.ts']).toBeDefined()
expect(output.files['/test/file__with__underscores.ts'].trim()).toEqual('// File with underscores\nexport const value = \'test\'')
})
})