1 | import {
|
2 | fs,
|
3 | getUserHomeDir,
|
4 | TARO_BASE_CONFIG,
|
5 | TARO_CONFIG_FOLDER
|
6 | } from '@tarojs/helper'
|
7 | import {
|
8 | CONFIG_DIR_NAME,
|
9 | DEFAULT_CONFIG_FILE
|
10 | } from '@tarojs/service/src/utils/constants'
|
11 | import * as path from 'path'
|
12 |
|
13 | import { run } from './utils'
|
14 |
|
15 | jest.mock('@tarojs/helper', () => {
|
16 | const helper = jest.requireActual('@tarojs/helper')
|
17 | const fs = helper.fs
|
18 | return {
|
19 | __esModule: true,
|
20 | ...helper,
|
21 | getUserHomeDir: jest.fn(),
|
22 | fs: {
|
23 | ...fs,
|
24 | existsSync: jest.fn(),
|
25 | readJSONSync: jest.fn(),
|
26 | writeJSONSync: jest.fn(),
|
27 | ensureFileSync: jest.fn()
|
28 | }
|
29 | }
|
30 | })
|
31 |
|
32 | const runConfig = run('config', ['commands/config'])
|
33 |
|
34 | describe('config', () => {
|
35 | const getUserHomeDirMocked = getUserHomeDir as jest.Mock<any>
|
36 | const existsSyncMocked = fs.existsSync as jest.Mock<any>
|
37 | const readJSONSyncMocked = fs.readJSONSync as jest.Mock<any>
|
38 | const writeJSONSyncMocked = fs.writeJSONSync as jest.Mock<any>
|
39 | const ensureFileSyncMocked = fs.ensureFileSync as jest.Mock<any>
|
40 | const appPath = '/'
|
41 |
|
42 | beforeEach(() => {
|
43 | getUserHomeDirMocked.mockReturnValue(appPath)
|
44 |
|
45 | existsSyncMocked.mockImplementation((filePath = '') =>
|
46 | !filePath.includes(path.join(appPath, CONFIG_DIR_NAME, DEFAULT_CONFIG_FILE))
|
47 | )
|
48 | })
|
49 |
|
50 | afterEach(() => {
|
51 | getUserHomeDirMocked.mockReset()
|
52 | existsSyncMocked.mockReset()
|
53 | })
|
54 |
|
55 | it('should exit because can\'t find home dir', async () => {
|
56 | const logSpy = jest.spyOn(console, 'log')
|
57 | logSpy.mockImplementation(() => {})
|
58 |
|
59 | getUserHomeDirMocked.mockReturnValue('')
|
60 |
|
61 | await runConfig(appPath)
|
62 |
|
63 | expect(logSpy).toBeCalledWith('找不到用户根目录')
|
64 | logSpy.mockRestore()
|
65 | })
|
66 |
|
67 | it('should warn when getting config without args key', async () => {
|
68 | const logSpy = jest.spyOn(console, 'log')
|
69 | logSpy.mockImplementation(() => {})
|
70 |
|
71 | await runConfig(appPath, { args: ['get'] })
|
72 |
|
73 | expect(logSpy).toBeCalledWith('Usage: taro config get <key>')
|
74 | logSpy.mockRestore()
|
75 | })
|
76 |
|
77 | it('should get config', async () => {
|
78 | const key = 'k'
|
79 | const value = 'v'
|
80 | const configPath = path.join('/', `${TARO_CONFIG_FOLDER}/${TARO_BASE_CONFIG}`)
|
81 |
|
82 | const logSpy = jest.spyOn(console, 'log')
|
83 | logSpy.mockImplementation(() => {})
|
84 | readJSONSyncMocked.mockImplementation(() => ({
|
85 | [key]: value
|
86 | }))
|
87 |
|
88 | await runConfig(appPath, { args: ['get', key] })
|
89 |
|
90 | expect(logSpy).nthCalledWith(1, `Config path: ${configPath}`)
|
91 | expect(logSpy).nthCalledWith(2)
|
92 | expect(logSpy).nthCalledWith(3, `key: ${key}, value: ${value}`)
|
93 |
|
94 | logSpy.mockRestore()
|
95 | readJSONSyncMocked.mockReset()
|
96 | })
|
97 |
|
98 | it('should warn when getting config without args value', async () => {
|
99 | const logSpy = jest.spyOn(console, 'log')
|
100 | logSpy.mockImplementation(() => {})
|
101 |
|
102 | await runConfig(appPath, { args: ['set', 'k'] })
|
103 |
|
104 | expect(logSpy).toBeCalledWith('Usage: taro config set <key> <value>')
|
105 | logSpy.mockRestore()
|
106 | })
|
107 |
|
108 | it('should set config', async () => {
|
109 | const key = 'k'
|
110 | const value = 'v'
|
111 | const configPath = path.join('/', `${TARO_CONFIG_FOLDER}/${TARO_BASE_CONFIG}`)
|
112 |
|
113 | const logSpy = jest.spyOn(console, 'log')
|
114 | logSpy.mockImplementation(() => {})
|
115 | readJSONSyncMocked.mockReturnValue({ a: 1 })
|
116 |
|
117 | await runConfig(appPath, { args: ['set', key, value] })
|
118 |
|
119 | expect(writeJSONSyncMocked).toBeCalledWith(configPath, {
|
120 | a: 1,
|
121 | [key]: value
|
122 | })
|
123 | expect(logSpy).nthCalledWith(1, `Config path: ${configPath}`)
|
124 | expect(logSpy).nthCalledWith(2)
|
125 | expect(logSpy).nthCalledWith(3, `set key: ${key}, value: ${value}`)
|
126 |
|
127 | logSpy.mockRestore()
|
128 | readJSONSyncMocked.mockReset()
|
129 | writeJSONSyncMocked.mockClear()
|
130 | })
|
131 |
|
132 | it('should set config with init', async () => {
|
133 | const key = 'k'
|
134 | const value = 'v'
|
135 | const configPath = path.join('/', `${TARO_CONFIG_FOLDER}/${TARO_BASE_CONFIG}`)
|
136 |
|
137 | const logSpy = jest.spyOn(console, 'log')
|
138 | logSpy.mockImplementation(() => {})
|
139 | existsSyncMocked.mockReturnValue(false)
|
140 |
|
141 | await runConfig(appPath, { args: ['set', key, value] })
|
142 |
|
143 | expect(ensureFileSyncMocked).toBeCalledWith(configPath)
|
144 | expect(writeJSONSyncMocked).toBeCalledWith(configPath, { [key]: value })
|
145 | expect(logSpy).toBeCalledWith(`set key: ${key}, value: ${value}`)
|
146 |
|
147 | logSpy.mockRestore()
|
148 | existsSyncMocked.mockReset()
|
149 | ensureFileSyncMocked.mockClear()
|
150 | writeJSONSyncMocked.mockClear()
|
151 | })
|
152 |
|
153 | it('should warn when deleting config without args key', async () => {
|
154 | const logSpy = jest.spyOn(console, 'log')
|
155 | logSpy.mockImplementation(() => {})
|
156 |
|
157 | await runConfig(appPath, { args: ['delete'] })
|
158 |
|
159 | expect(logSpy).toBeCalledWith('Usage: taro config delete <key>')
|
160 | logSpy.mockRestore()
|
161 | })
|
162 |
|
163 | it('should delete config', async () => {
|
164 | const key = 'k'
|
165 | const configPath = path.join('/', `${TARO_CONFIG_FOLDER}/${TARO_BASE_CONFIG}`)
|
166 |
|
167 | const logSpy = jest.spyOn(console, 'log')
|
168 | logSpy.mockImplementation(() => {})
|
169 | readJSONSyncMocked.mockReturnValue({
|
170 | a: 1,
|
171 | [key]: 'v'
|
172 | })
|
173 |
|
174 | await runConfig(appPath, { args: ['delete', key] })
|
175 |
|
176 | expect(writeJSONSyncMocked).toBeCalledWith(configPath, { a: 1 })
|
177 | expect(logSpy).nthCalledWith(1, `Config path: ${configPath}`)
|
178 | expect(logSpy).nthCalledWith(2)
|
179 | expect(logSpy).nthCalledWith(3, `deleted: ${key}`)
|
180 |
|
181 | logSpy.mockRestore()
|
182 | readJSONSyncMocked.mockReset()
|
183 | writeJSONSyncMocked.mockClear()
|
184 | })
|
185 |
|
186 | it('should list config', async () => {
|
187 | const configPath = path.join('/', `${TARO_CONFIG_FOLDER}/${TARO_BASE_CONFIG}`)
|
188 | const logSpy = jest.spyOn(console, 'log')
|
189 | logSpy.mockImplementation(() => {})
|
190 | readJSONSyncMocked.mockReturnValue({
|
191 | a: 1,
|
192 | b: 2
|
193 | })
|
194 |
|
195 | await runConfig(appPath, { args: ['list'] })
|
196 |
|
197 | expect(logSpy).nthCalledWith(1, `Config path: ${configPath}`)
|
198 | expect(logSpy).nthCalledWith(2)
|
199 | expect(logSpy).nthCalledWith(3, 'Config info:')
|
200 | expect(logSpy).nthCalledWith(4, 'a=1')
|
201 | expect(logSpy).nthCalledWith(5, 'b=2')
|
202 |
|
203 | logSpy.mockRestore()
|
204 | readJSONSyncMocked.mockReset()
|
205 | })
|
206 |
|
207 | it('should list config in json', async () => {
|
208 | const configPath = path.join('/', `${TARO_CONFIG_FOLDER}/${TARO_BASE_CONFIG}`)
|
209 | const logSpy = jest.spyOn(console, 'log')
|
210 | logSpy.mockImplementation(() => {})
|
211 | readJSONSyncMocked.mockReturnValue({
|
212 | a: 1,
|
213 | b: 2
|
214 | })
|
215 |
|
216 | await runConfig(appPath, {
|
217 | args: ['list'],
|
218 | options: {
|
219 | json: true
|
220 | }
|
221 | })
|
222 |
|
223 | expect(logSpy).nthCalledWith(1, `Config path: ${configPath}`)
|
224 | expect(logSpy).nthCalledWith(2)
|
225 | expect(logSpy).nthCalledWith(3, 'Config info:')
|
226 | expect(logSpy).nthCalledWith(4, JSON.stringify({ a: 1, b: 2 }, null, 2))
|
227 |
|
228 | logSpy.mockRestore()
|
229 | readJSONSyncMocked.mockReset()
|
230 | })
|
231 | })
|