-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathenvironment.test.ts
More file actions
115 lines (89 loc) · 3.44 KB
/
environment.test.ts
File metadata and controls
115 lines (89 loc) · 3.44 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
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { fs, vol } from 'memfs'
vi.mock('node:fs', () => fs)
vi.mock('node:fs/promises', () => fs.promises)
beforeEach(() => {
vol.reset()
})
import {
createMemoryEnvironment,
createDefaultEnvironment,
} from '../src/environment.js'
describe('createMemoryEnvironment', () => {
it('should handle basic file operations', async () => {
const { environment, output } = createMemoryEnvironment()
environment.startRun()
await environment.writeFile('/test.txt', 'test')
environment.deleteFile('/test.txt')
expect(environment.exists('/test.txt')).toBe(false)
await environment.writeFile('/test.txt', 'test')
environment.finishRun()
expect(output.files['/test.txt']).toEqual('test')
})
it('should remove empty directories', async () => {
const { environment, output } = createMemoryEnvironment()
environment.startRun()
await environment.writeFile('/test.txt', 'test')
await environment.writeFile('/foo/test1.txt', 'test')
await environment.writeFile('/foo/test2.txt', 'test')
environment.deleteFile('/foo/test1.txt')
environment.deleteFile('/foo/test2.txt')
await environment.writeFile('/bar/test1.txt', 'test')
environment.finishRun()
expect(Object.keys(output.files)).toEqual(['/test.txt', '/bar/test1.txt'])
})
it('should track command execution', async () => {
const { environment, output } = createMemoryEnvironment()
environment.startRun()
await environment.execute('echo', ['test'], '')
environment.finishRun()
expect(output.commands.length).toEqual(1)
expect(output.commands[0].command).toEqual('echo')
expect(output.commands[0].args).toEqual(['test'])
})
})
describe('createDefaultEnvironment', () => {
it('should create a default environment', async () => {
const environment = createDefaultEnvironment()
expect(environment).toBeDefined()
})
it('should write to the file system', async () => {
const environment = createDefaultEnvironment()
environment.startRun()
await environment.writeFile('/test.txt', 'test')
await environment.appendFile('/test.txt', 'test2')
await environment.copyFile('/test.txt', '/test2.txt')
environment.finishRun()
expect(fs.readFileSync('/test.txt', 'utf8')).toEqual('test\n\ntest2')
})
it('should allow deletes', async () => {
const environment = createDefaultEnvironment()
await environment.writeFile('/test.txt', 'test')
expect(fs.readFileSync('/test.txt', 'utf8')).toEqual('test')
await environment.deleteFile('/test.txt')
expect(environment.exists('/test.txt')).toBe(false)
})
it('should record errors', async () => {
const environment = createDefaultEnvironment()
environment.startRun()
await environment.execute('command-that-does-not-exist', ['test'], '')
environment.finishRun()
expect(environment.getErrors()).toEqual([
'Command "command-that-does-not-exist test" did not run successfully. Please run this manually in your project.',
])
})
it('should have UI methods', async () => {
const environment = createDefaultEnvironment()
environment.startRun()
environment.intro('test')
environment.outro('test')
environment.info('test')
environment.error('test')
environment.warn('test')
const s = environment.spinner()
s.start('foo')
s.stop('bar')
environment.finishRun()
expect(await environment.confirm('test')).toEqual(true)
})
})