Open History to see the last commit
1import { beforeEach, describe, expect, it, vi } from 'vitest'
2import { fs, vol } from 'memfs'
3
4import { createMemoryEnvironment } from '../src/environment.js'
5import {
6 addToApp,
7 getCurrentConfiguration,
8 hasPendingGitChanges,
9 runNewCommands,
10 writeFiles,
11} from '../src/add-to-app.js'
12import {
13 __testClearFrameworks,
14 __testRegisterFramework,
15} from '../src/frameworks.js'
16
17import type { PersistedOptions } from '../src/config-file.js'
18
19vi.mock('node:fs', () => fs)
20vi.mock('node:fs/promises', () => fs.promises)
21
22const fakeCTAJSON: PersistedOptions = {
23 projectName: 'test',
24 targetDir: '/foo',
25 framework: 'test',
26 mode: 'code-router',
27 chosenAddOns: [],
28 version: 1,
29 typescript: true,
30 tailwind: true,
31 packageManager: 'npm',
32 git: true,
33}
34
35beforeEach(() => {
36 const fakeFiles = {
37 './package.json': JSON.stringify({
38 name: 'test',
39 version: '1.0.0',
40 dependencies: {},
41 }),
42 }
43
44 vol.reset()
45 __testClearFrameworks()
46 __testRegisterFramework({
47 id: 'test',
48 name: 'Test',
49 description: 'Test',
50 version: '1.0.0',
51 baseDirectory: '/foo',
52 addOnsDirectories: [],
53 basePackageJSON: {},
54 optionalPackages: {},
55 getAddOns: () => [
56 {
57 id: 'test',
58 name: 'Test',
59 description: 'Test',
60 version: '1.0.0',
61 type: 'add-on',
62 phase: 'add-on',
63 modes: ['code-router', 'file-router'],
64 command: {
65 command: 'echo',
66 args: ['baz'],
67 },
68 packageAdditions: {
69 dependencies: {
70 'test-package': '1.0.0',
71 },
72 },
73 dependsOn: [],
74 getFiles: () => Promise.resolve(['./jack.txt']),
75 getFileContents: () => Promise.resolve('foo'),
76 getDeletedFiles: () => Promise.resolve([]),
77 },
78 ],
79 getFiles: () => Promise.resolve(Object.keys(fakeFiles)),
80 getFileContents: (path) => Promise.resolve(fakeFiles[path]),
81 getDeletedFiles: () => Promise.resolve([]),
82 })
83})
84
85const configFile: PersistedOptions = {
86 projectName: 'test',
87 targetDir: '/foo',
88 framework: 'test',
89 mode: 'code-router',
90 chosenAddOns: [],
91 version: 1,
92 typescript: true,
93 tailwind: true,
94 packageManager: 'npm',
95 git: true,
96}
97
98describe('getCurrentConfiguration', () => {
99 it('should check for the config file', async () => {
100 const { environment } = createMemoryEnvironment()
101 const out = await getCurrentConfiguration(environment, '/foo')
102 expect(out).toBeUndefined()
103 })
104
105 it('should read the config file', async () => {
106 const { environment } = createMemoryEnvironment()
107
108 environment.writeFile('/foo/.cta.json', JSON.stringify(configFile, null, 2))
109
110 const out = await getCurrentConfiguration(environment, '/foo')
111 expect(out).toEqual(configFile)
112 })
113})
114
115describe('hasPendingGitChanges', () => {
116 it('should check for pending git changes', async () => {
117 const { environment } = createMemoryEnvironment()
118 environment.execute = () => Promise.resolve({ stdout: '' })
119 const out = await hasPendingGitChanges(environment, '/foo')
120 expect(out).toBe(false)
121 })
122
123 it('should check for pending git changes', async () => {
124 const { environment } = createMemoryEnvironment()
125 environment.execute = () => Promise.resolve({ stdout: 'M foo' })
126 const out = await hasPendingGitChanges(environment, '/foo')
127 expect(out).toBe(true)
128 })
129})
130
131describe('writeFiles', () => {
132 it('should prompt for confirmation when not forced', async () => {
133 const { environment } = createMemoryEnvironment('/foo')
134 environment.writeFile('/foo/bloop.txt', 'bloop')
135 environment.writeFile(
136 '/foo/package.json',
137 JSON.stringify(
138 {
139 name: 'test',
140 version: '1.0.0',
141 dependencies: {},
142 },
143 null,
144 2,
145 ),
146 )
147 environment.writeFile('/foo/bar.txt', 'bar')
148 environment.confirm = () => Promise.resolve(false)
149 let thrown = false
150 writeFiles(
151 environment,
152 '/foo',
153 {
154 files: {
155 './bar.txt': 'baz',
156 './blarg.txt': 'blarg',
157 },
158 deletedFiles: [],
159 },
160 false,
161 )
162 .catch((e) => {
163 thrown = true
164 })
165 .finally(() => {
166 expect(thrown).toBe(true)
167 })
168 })
169
170 it('should not prompt for confirmation when forced', async () => {
171 const { environment, output } = createMemoryEnvironment('/foo')
172 environment.startRun()
173 environment.writeFile('/foo/.cta.json', JSON.stringify(configFile, null, 2))
174 environment.writeFile('/foo/blooop.txt', 'blooop')
175 await writeFiles(
176 environment,
177 '/foo',
178 {
179 files: {
180 './bar.txt': 'baz',
181 './blarg.txt': 'blarg',
182 },
183 deletedFiles: [],
184 },
185 true,
186 )
187 environment.finishRun()
188 expect(output.files).toEqual({
189 './blooop.txt': 'blooop',
190 './bar.txt': 'baz',
191 './blarg.txt': 'blarg',
192 })
193 })
194
195 it('should handle binary files', async () => {
196 const { environment, output } = createMemoryEnvironment('/foo')
197 environment.startRun()
198 environment.writeFile('/foo/.cta.json', JSON.stringify(configFile, null, 2))
199 environment.writeFile('/foo/unchanged.jpg', 'base64::foobaz')
200 environment.writeFile('/foo/changing.jpg', 'base64::foobaz')
201 await writeFiles(
202 environment,
203 '/foo',
204 {
205 files: {
206 './unchanged.jpg': 'base64::foobaz',
207 './changing.jpg': 'base64::aGVsbG8=',
208 './new.jpg': 'base64::aGVsbG8=',
209 },
210 deletedFiles: [],
211 },
212 true,
213 )
214 environment.finishRun()
215 // It's ok for unchanged.jpg not to be written, because it matches the existing file
216 expect(output.files).toEqual({
217 './unchanged.jpg': 'base64::foobaz',
218 './changing.jpg': 'base64::aGVsbG8=',
219 './new.jpg': 'base64::aGVsbG8=',
220 })
221 })
222
223 it('should handle package.json', async () => {
224 const { environment, output } = createMemoryEnvironment('/foo')
225 environment.startRun()
226 environment.writeFile(
227 '/foo/package.json',
228 JSON.stringify(
229 {
230 name: 'test',
231 version: '1.0.0',
232 scripts: {
233 dev: 'echo "test"',
234 },
235 dependencies: {
236 'test-package-2': '1.0.0',
237 },
238 },
239 null,
240 2,
241 ),
242 )
243 await writeFiles(
244 environment,
245 '/foo',
246 {
247 files: {
248 './package.json': JSON.stringify(
249 {
250 scripts: {
251 test: 'echo "test"',
252 },
253 dependencies: {
254 'test-package': '1.0.0',
255 },
256 },
257 null,
258 2,
259 ),
260 },
261 deletedFiles: [],
262 },
263 true,
264 )
265 environment.finishRun()
266 expect(output.files).toEqual({
267 './package.json': JSON.stringify(
268 {
269 name: 'test',
270 version: '1.0.0',
271 scripts: {
272 dev: 'echo "test"',
273 test: 'echo "test"',
274 },
275 dependencies: {
276 'test-package-2': '1.0.0',
277 'test-package': '1.0.0',
278 },
279 devDependencies: {},
280 },
281 null,
282 2,
283 ),
284 })
285 })
286
287 it('should delete files', async () => {
288 const { environment, output } = createMemoryEnvironment('/foo')
289 environment.startRun()
290 environment.writeFile('/foo/bloop.txt', 'bloop')
291 await writeFiles(
292 environment,
293 '/foo',
294 { files: {}, deletedFiles: ['./bloop.txt'] },
295 true,
296 )
297 environment.finishRun()
298 expect(output.deletedFiles).toEqual(['./bloop.txt'])
299 })
300})
301
302describe('runNewCommands', () => {
303 it('should run new commands', async () => {
304 const { environment, output } = createMemoryEnvironment('/foo')
305 environment.startRun()
306 await runNewCommands(environment, fakeCTAJSON, '/foo', {
307 commands: [{ command: 'echo', args: ['bloop'] }],
308 })
309 environment.finishRun()
310 expect(output.commands).toEqual([{ command: 'echo', args: ['bloop'] }])
311 })
312})
313
314describe('addToApp', () => {
315 it('should add an add-on', async () => {
316 const { environment, output } = createMemoryEnvironment('/foo')
317 environment.startRun()
318 environment.writeFile(
319 '/foo/.cta.json',
320 JSON.stringify(fakeCTAJSON, null, 2),
321 )
322 environment.writeFile(
323 '/foo/package.json',
324 JSON.stringify(
325 {
326 name: 'test',
327 version: '1.0.0',
328 scripts: {},
329 dependencies: {},
330 devDependencies: {},
331 },
332 null,
333 2,
334 ),
335 )
336 await addToApp(environment, ['test'], '/foo', {
337 forced: true,
338 })
339 environment.finishRun()
340 expect(output.files).toEqual({
341 './jack.txt': 'foo',
342 './package.json': JSON.stringify(
343 {
344 name: 'test',
345 version: '1.0.0',
346 scripts: {},
347 dependencies: {
348 'test-package': '1.0.0',
349 },
350 devDependencies: {},
351 },
352 null,
353 2,
354 ),
355 })
356 })
357})