-
Notifications
You must be signed in to change notification settings - Fork 80
/
cli.test.ts
192 lines (163 loc) · 5.63 KB
/
cli.test.ts
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
import consola from 'consola'
import fs from 'fs-extra'
import path from 'path'
import { CatId } from './consts'
import { run } from '../src/cli'
import { uniqueId, wait } from 'vtils'
const tempDir = path.join(__dirname, '../.tmp-ytt-test')
beforeAll(() => {
fs.ensureDirSync(tempDir)
})
afterAll(() => {
fs.removeSync(tempDir)
})
function getTempPaths() {
const targetDir = path.join(tempDir, uniqueId('case'))
fs.ensureDirSync(targetDir)
const generatedConfigFile = path.join(targetDir, 'ytt.config.ts')
const generatedApiFile = path.join(targetDir, 'src/api/index.ts')
const generatedRequestFile = path.join(targetDir, 'src/api/request.ts')
return {
targetDir,
generatedConfigFile,
generatedApiFile,
generatedRequestFile,
}
}
async function runCli(cmd: string, configFile: string) {
await run(cmd, {
configFile,
})
await wait(100)
}
beforeEach(() => {
require('prompts').setAnswer('configFileType', 'ts')
})
describe('cli', () => {
test('help', async () => {
const tempPaths = getTempPaths()
let text = ''
const log = jest.fn(message => {
text = message
})
jest.spyOn(console, 'log').mockImplementationOnce(log)
await runCli('help', tempPaths.generatedConfigFile)
expect(text).toMatchSnapshot('help')
})
test('没有配置文件生成将会报错', async () => {
const tempPaths = getTempPaths()
const errorHandler = jest.fn()
jest.spyOn(consola, 'error').mockImplementationOnce(errorHandler)
await runCli('', tempPaths.generatedConfigFile)
expect(errorHandler).toBeCalledTimes(1)
})
test('正确初始化配置文件 & 生成结果', async () => {
const tempPaths = getTempPaths()
// 初始化配置文件
await runCli('init', tempPaths.generatedConfigFile)
expect(
fs.readFileSync(tempPaths.generatedConfigFile).toString(),
).toMatchSnapshot('配置文件')
// 生成结果
fs.writeFileSync(
tempPaths.generatedConfigFile,
fs
.readFileSync(tempPaths.generatedConfigFile)
.toString()
.replace(
"'yapi-to-typescript'",
JSON.stringify(path.join(__dirname, '../src')),
)
.replace(`dataKey: 'data',`, '')
.replace(`id: 50,`, `id: ${CatId.test},`),
)
await runCli('', tempPaths.generatedConfigFile)
expect(
fs.readFileSync(tempPaths.generatedApiFile).toString(),
).toMatchSnapshot('接口文件')
expect(
fs.readFileSync(tempPaths.generatedRequestFile).toString(),
).toMatchSnapshot('请求文件')
})
test('检查到已有配置,可以选择覆盖', async () => {
const tempPaths = getTempPaths()
// 初始化配置文件
await runCli('init', tempPaths.generatedConfigFile)
expect(
fs.readFileSync(tempPaths.generatedConfigFile).toString(),
).toMatchSnapshot('配置文件')
// 修改配置文件
fs.writeFileSync(tempPaths.generatedConfigFile, 'hello')
expect(
fs.readFileSync(tempPaths.generatedConfigFile).toString(),
).toMatchSnapshot('修改过的配置文件')
// 覆盖配置文件
require('prompts').setAnswer('override', true)
await runCli('init', tempPaths.generatedConfigFile)
expect(
fs.readFileSync(tempPaths.generatedConfigFile).toString(),
).toMatchSnapshot('覆盖后的配置文件')
})
test('检查到已有配置,可以选择不覆盖', async () => {
const tempPaths = getTempPaths()
// 初始化配置文件
await runCli('init', tempPaths.generatedConfigFile)
expect(
fs.readFileSync(tempPaths.generatedConfigFile).toString(),
).toMatchSnapshot('配置文件')
// 修改配置文件
fs.writeFileSync(tempPaths.generatedConfigFile, 'hello')
expect(
fs.readFileSync(tempPaths.generatedConfigFile).toString(),
).toMatchSnapshot('修改过的配置文件')
// 不覆盖配置文件
require('prompts').setAnswer('override', false)
await runCli('init', tempPaths.generatedConfigFile)
await wait(1000)
expect(
fs.readFileSync(tempPaths.generatedConfigFile).toString(),
).toMatchSnapshot('不覆盖后的配置文件')
})
test('支持钩子', async () => {
const tempPaths = getTempPaths()
// 初始化配置文件
await runCli('init', tempPaths.generatedConfigFile)
// 更新配置文件
const successFile = path.join(tempPaths.targetDir, 'success.txt')
const failFile = path.join(tempPaths.targetDir, 'fail.txt')
const completeFile = path.join(tempPaths.targetDir, 'complete.txt')
fs.writeFileSync(
tempPaths.generatedConfigFile,
fs
.readFileSync(tempPaths.generatedConfigFile)
.toString()
.replace(
"'yapi-to-typescript'",
JSON.stringify(path.join(__dirname, '../src')),
)
.replace(`dataKey: 'data',`, '')
.replace(`id: 50,`, `id: ${CatId.test},`)
.replace(
/(?=\)\s*$)/s,
`, {
success: () => require('fs').writeFileSync(${JSON.stringify(
successFile,
)}, 'success'),
fail: () => require('fs').writeFileSync(${JSON.stringify(
failFile,
)}, 'fail'),
complete: () => require('fs').writeFileSync(${JSON.stringify(
completeFile,
)}, 'complete'),
}`,
),
)
// 执行
await runCli('', tempPaths.generatedConfigFile)
expect(fs.existsSync(successFile)).toBe(true)
expect(fs.readFileSync(successFile).toString()).toBe('success')
expect(fs.existsSync(failFile)).toBe(false)
expect(fs.existsSync(completeFile)).toBe(true)
expect(fs.readFileSync(completeFile).toString()).toBe('complete')
})
})