Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

environment.test.ts

115 lines3.4 KB

1import { beforeEach, describe, expect, it, vi } from 'vitest'
2import { fs, vol } from 'memfs'
3​
4vi.mock('node:fs', () => fs)
5vi.mock('node:fs/promises', () => fs.promises)
6​
7beforeEach(() => {
8 vol.reset()
9})
10​
11import {
12 createMemoryEnvironment,
13 createDefaultEnvironment,
14} from '../src/environment.js'
15​
16describe('createMemoryEnvironment', () => {
17 it('should handle basic file operations', async () => {
18 const { environment, output } = createMemoryEnvironment()
19​
20 environment.startRun()
21​
22 await environment.writeFile('/test.txt', 'test')
23​
24 environment.deleteFile('/test.txt')
25 expect(environment.exists('/test.txt')).toBe(false)
26​
27 await environment.writeFile('/test.txt', 'test')
28​
29 environment.finishRun()
30​
31 expect(output.files['/test.txt']).toEqual('test')
32 })
33​
34 it('should remove empty directories', async () => {
35 const { environment, output } = createMemoryEnvironment()
36​
37 environment.startRun()
38​
39 await environment.writeFile('/test.txt', 'test')
40​
41 await environment.writeFile('/foo/test1.txt', 'test')
42 await environment.writeFile('/foo/test2.txt', 'test')
43 environment.deleteFile('/foo/test1.txt')
44 environment.deleteFile('/foo/test2.txt')
45​
46 await environment.writeFile('/bar/test1.txt', 'test')
47​
48 environment.finishRun()
49​
50 expect(Object.keys(output.files)).toEqual(['/test.txt', '/bar/test1.txt'])
51 })
52​
53 it('should track command execution', async () => {
54 const { environment, output } = createMemoryEnvironment()
55​
56 environment.startRun()
57 await environment.execute('echo', ['test'], '')
58 environment.finishRun()
59​
60 expect(output.commands.length).toEqual(1)
61 expect(output.commands[0].command).toEqual('echo')
62 expect(output.commands[0].args).toEqual(['test'])
63 })
64})
65​
66describe('createDefaultEnvironment', () => {
67 it('should create a default environment', async () => {
68 const environment = createDefaultEnvironment()
69 expect(environment).toBeDefined()
70 })
71​
72 it('should write to the file system', async () => {
73 const environment = createDefaultEnvironment()
74 environment.startRun()
75 await environment.writeFile('/test.txt', 'test')
76 await environment.appendFile('/test.txt', 'test2')
77 await environment.copyFile('/test.txt', '/test2.txt')
78 environment.finishRun()
79​
80 expect(fs.readFileSync('/test.txt', 'utf8')).toEqual('testtest2')
81 })
82​
83 it('should allow deletes', async () => {
84 const environment = createDefaultEnvironment()
85 await environment.writeFile('/test.txt', 'test')
86 expect(fs.readFileSync('/test.txt', 'utf8')).toEqual('test')
87 await environment.deleteFile('/test.txt')
88 expect(environment.exists('/test.txt')).toBe(false)
89 })
90​
91 it('should record errors', async () => {
92 const environment = createDefaultEnvironment()
93 environment.startRun()
94 await environment.execute('command-that-does-not-exist', ['test'], '')
95 environment.finishRun()
96 expect(environment.getErrors()).toEqual([
97 'Command "command-that-does-not-exist test" did not run successfully. Please run this manually in your project.',
98 ])
99 })
100​
101 it('should have UI methods', async () => {
102 const environment = createDefaultEnvironment()
103 environment.startRun()
104 environment.intro('test')
105 environment.outro('test')
106 environment.info('test')
107 environment.error('test')
108 environment.warn('test')
109 const s = environment.spinner()
110 s.start('foo')
111 s.stop('bar')
112 environment.finishRun()
113 expect(await environment.confirm('test')).toEqual(true)
114 })
115})