forked from patternfly/patternfly-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.test.ts
More file actions
307 lines (251 loc) · 9.94 KB
/
create.test.ts
File metadata and controls
307 lines (251 loc) · 9.94 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
jest.mock('inquirer', () => ({
__esModule: true,
default: { prompt: jest.fn() },
}));
jest.mock('fs-extra', () => {
const real = jest.requireActual<typeof import('fs-extra')>('fs-extra');
return {
__esModule: true,
default: {
pathExists: jest.fn(),
readJson: jest.fn(),
writeJson: jest.fn(),
remove: jest.fn(),
existsSync: real.existsSync,
readFileSync: real.readFileSync,
},
};
});
jest.mock('execa', () => ({
__esModule: true,
execa: jest.fn(),
}));
jest.mock('../github.js', () => ({
...jest.requireActual('../github.js'),
offerAndCreateGitHubRepo: jest.fn(),
}));
import path from 'path';
import fs from 'fs-extra';
import { execa } from 'execa';
import inquirer from 'inquirer';
import { sanitizeRepoName, offerAndCreateGitHubRepo } from '../github.js';
import { runCreate } from '../create.js';
import { defaultTemplates } from '../templates.js';
/** Partial `fs-extra` mock: use `jest.Mock` for `mockResolvedValue` (typed mocks infer `never` here). */
const mockPathExists = fs.pathExists as jest.MockedFunction<typeof fs.pathExists> & jest.Mock;
const mockReadJson = fs.readJson as jest.MockedFunction<typeof fs.readJson>;
const mockWriteJson = fs.writeJson as jest.MockedFunction<typeof fs.writeJson>;
const mockRemove = fs.remove as jest.MockedFunction<typeof fs.remove> & jest.Mock;
const mockExeca = execa as jest.MockedFunction<typeof execa>;
const mockPrompt = inquirer.prompt as jest.MockedFunction<typeof inquirer.prompt>;
const mockOfferAndCreateGitHubRepo = offerAndCreateGitHubRepo as jest.MockedFunction<
typeof offerAndCreateGitHubRepo
>;
const projectDir = 'my-test-project';
const projectPath = path.resolve(projectDir);
const projectData = {
name: 'my-app',
version: '1.0.0',
description: 'Test app',
author: 'Test Author',
};
function setupHappyPathMocks() {
mockExeca.mockResolvedValue({ stdout: '', stderr: '', exitCode: 0 } as Awaited<ReturnType<typeof execa>>);
mockPathExists.mockResolvedValue(true);
mockReadJson.mockResolvedValue({ name: 'template-name', version: '0.0.0' });
mockWriteJson.mockResolvedValue(undefined);
mockRemove.mockResolvedValue(undefined);
mockOfferAndCreateGitHubRepo.mockResolvedValue(false);
mockPrompt.mockResolvedValue(projectData);
return projectData;
}
describe('GitHub support (create command)', () => {
it('derives repo name from package name the same way the create command does', () => {
expect(sanitizeRepoName('my-app')).toBe('my-app');
expect(sanitizeRepoName('@patternfly/my-project')).toBe('my-project');
expect(sanitizeRepoName('My Project Name')).toBe('my-project-name');
});
it('builds repo URL in the format used by the create command', () => {
const username = 'testuser';
const repoName = sanitizeRepoName('@org/my-package');
const repoUrl = `https://github.com/${username}/${repoName}`;
expect(repoUrl).toBe('https://github.com/testuser/my-package');
});
});
describe('runCreate', () => {
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
beforeEach(() => {
jest.clearAllMocks();
});
afterAll(() => {
consoleErrorSpy.mockRestore();
consoleLogSpy.mockRestore();
});
it('throws when template name is not found', async () => {
await expect(runCreate(projectDir, 'nonexistent-template')).rejects.toThrow(
'Template "nonexistent-template" not found'
);
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringContaining('Template "nonexistent-template" not found')
);
expect(mockExeca).not.toHaveBeenCalled();
});
it('prompts for project directory when not provided', async () => {
setupHappyPathMocks();
await runCreate(undefined, 'starter');
expect(mockPrompt).toHaveBeenCalledWith(
expect.arrayContaining([
expect.objectContaining({
type: 'input',
name: 'projectDirectory',
message: expect.stringContaining('directory where you want to create the project'),
default: 'my-app',
}),
])
);
});
it('prompts for template when not provided', async () => {
setupHappyPathMocks();
mockPrompt
.mockResolvedValueOnce({ templateName: 'starter' })
.mockResolvedValueOnce(projectData);
await runCreate(projectDir, undefined);
expect(mockPrompt).toHaveBeenCalledWith(
expect.arrayContaining([
expect.objectContaining({
type: 'list',
name: 'templateName',
message: 'Select a template:',
choices: expect.any(Array),
}),
])
);
});
it('uses HTTPS repo URL when ssh option is false', async () => {
setupHappyPathMocks();
await runCreate(projectDir, 'starter', { ssh: false });
const starter = defaultTemplates.find(t => t.name === 'starter');
expect(mockExeca).toHaveBeenNthCalledWith(
1,
'git',
['clone', starter!.repo, projectPath],
expect.objectContaining({ stdio: 'inherit' })
);
});
it('uses SSH repo URL when ssh option is true and template has repoSSH', async () => {
setupHappyPathMocks();
await runCreate(projectDir, 'starter', { ssh: true });
const starter = defaultTemplates.find(t => t.name === 'starter');
expect(mockExeca).toHaveBeenNthCalledWith(
1,
'git',
['clone', starter!.repoSSH, projectPath],
expect.objectContaining({ stdio: 'inherit' })
);
});
it('passes template clone options to git clone', async () => {
setupHappyPathMocks();
await runCreate(projectDir, 'compass-starter');
const compass = defaultTemplates.find(t => t.name === 'compass-starter');
expect(mockExeca).toHaveBeenNthCalledWith(
1,
'git',
['clone', ...compass!.options!, compass!.repo, projectPath],
expect.any(Object)
);
});
it('removes .git from cloned project and customizes package.json', async () => {
setupHappyPathMocks();
await runCreate(projectDir, 'starter');
expect(mockRemove).toHaveBeenCalledWith(path.join(projectPath, '.git'));
expect(mockReadJson).toHaveBeenCalledWith(path.join(projectPath, 'package.json'));
expect(mockWriteJson).toHaveBeenCalledWith(
path.join(projectPath, 'package.json'),
expect.objectContaining({
name: projectData.name,
version: projectData.version,
description: projectData.description,
author: projectData.author,
}),
{ spaces: 2 }
);
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Customized package.json'));
});
it('skips package.json customization when package.json does not exist', async () => {
setupHappyPathMocks();
mockPathExists.mockResolvedValue(false);
await runCreate(projectDir, 'starter');
expect(mockReadJson).not.toHaveBeenCalled();
expect(mockWriteJson).not.toHaveBeenCalled();
expect(consoleLogSpy).toHaveBeenCalledWith(
expect.stringContaining('No package.json found in template')
);
});
it('uses template packageManager and runs install', async () => {
setupHappyPathMocks();
await runCreate(projectDir, 'starter');
expect(mockExeca).toHaveBeenNthCalledWith(
2,
'yarn',
['install'],
expect.objectContaining({ cwd: projectPath, stdio: 'inherit' })
);
});
it('falls back to npm when template has no packageManager', async () => {
setupHappyPathMocks();
// rhoai_enabled_starter has no packageManager
const rhoai = defaultTemplates.find(t => t.name === 'rhoai_enabled_starter');
expect(rhoai?.packageManager).toBeUndefined();
await runCreate(projectDir, 'rhoai_enabled_starter');
expect(mockExeca).toHaveBeenNthCalledWith(
2,
'npm',
['install'],
expect.objectContaining({ cwd: projectPath, stdio: 'inherit' })
);
});
it('calls offerAndCreateGitHubRepo after install', async () => {
setupHappyPathMocks();
await runCreate(projectDir, 'starter');
expect(mockOfferAndCreateGitHubRepo).toHaveBeenCalledWith(projectPath);
});
it('logs success message with project directory', async () => {
setupHappyPathMocks();
await runCreate(projectDir, 'starter');
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Project created successfully'));
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining(`cd ${projectDir}`));
});
it('cleans up project directory and rethrows when clone fails', async () => {
mockExeca.mockRejectedValueOnce(new Error('clone failed'));
mockPathExists.mockResolvedValue(true);
await expect(runCreate(projectDir, 'starter')).rejects.toThrow('clone failed');
expect(mockRemove).toHaveBeenCalledWith(projectPath);
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Cleaned up failed project directory'));
});
it('cleans up project directory and rethrows when install fails', async () => {
mockExeca
.mockResolvedValueOnce({ stdout: '', stderr: '', exitCode: 0 } as Awaited<ReturnType<typeof execa>>)
.mockRejectedValueOnce(new Error('install failed'));
mockPathExists.mockResolvedValue(true);
mockReadJson.mockResolvedValue({});
mockWriteJson.mockResolvedValue(undefined);
mockRemove.mockResolvedValue(undefined);
mockPrompt.mockResolvedValue(projectData);
await expect(runCreate(projectDir, 'starter')).rejects.toThrow('install failed');
expect(mockRemove).toHaveBeenCalledWith(projectPath);
});
it('uses custom templates when templateFile option is provided', async () => {
const fixturesDir = path.join(process.cwd(), 'src', '__tests__', 'fixtures');
const customPath = path.join(fixturesDir, 'valid-templates.json');
setupHappyPathMocks();
mockPrompt.mockResolvedValueOnce(projectData);
await runCreate(projectDir, 'custom-one', { templateFile: customPath });
expect(mockExeca).toHaveBeenNthCalledWith(
1,
'git',
['clone', 'https://github.com/example/custom-one.git', projectPath],
expect.any(Object)
);
});
});