@adobe/aio-cli-plugin-api-mesh 2.1.0 → 2.2.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/oclif.manifest.json +1 -1
  2. package/package.json +4 -2
  3. package/src/commands/__fixtures__/env_invalid +8 -0
  4. package/src/commands/__fixtures__/env_valid +3 -0
  5. package/src/commands/__fixtures__/files/requestParams.json +3 -0
  6. package/src/commands/__fixtures__/openapi-schema.json +4 -0
  7. package/src/commands/__fixtures__/requestParams.json +3 -0
  8. package/src/commands/__fixtures__/sample_fully_qualified_mesh.json +29 -0
  9. package/src/commands/__fixtures__/sample_invalid_mesh.txt +17 -0
  10. package/src/commands/__fixtures__/sample_mesh_files.json +23 -0
  11. package/src/commands/__fixtures__/sample_mesh_invalid_file_content.json +14 -0
  12. package/src/commands/__fixtures__/sample_mesh_invalid_file_name.json +27 -0
  13. package/src/commands/__fixtures__/sample_mesh_invalid_paths.json +23 -0
  14. package/src/commands/__fixtures__/sample_mesh_invalid_type.json +27 -0
  15. package/src/commands/__fixtures__/sample_mesh_mismatching_path.json +29 -0
  16. package/src/commands/__fixtures__/sample_mesh_outside_workspace_dir.json +23 -0
  17. package/src/commands/__fixtures__/sample_mesh_path_from_home.json +14 -0
  18. package/src/commands/__fixtures__/sample_mesh_subdirectory.json +23 -0
  19. package/src/commands/__fixtures__/sample_mesh_with_files_array.json +29 -0
  20. package/src/commands/__fixtures__/sample_mesh_with_placeholder +17 -0
  21. package/src/commands/api-mesh/__tests__/create.test.js +1202 -1
  22. package/src/commands/api-mesh/__tests__/init.test.js +390 -0
  23. package/src/commands/api-mesh/__tests__/update.test.js +419 -4
  24. package/src/commands/api-mesh/create.js +47 -14
  25. package/src/commands/api-mesh/init.js +168 -0
  26. package/src/commands/api-mesh/update.js +49 -17
  27. package/src/helpers.js +254 -3
  28. package/src/lib/devConsole.js +1 -2
  29. package/src/templates/gitignore +1 -0
  30. package/src/templates/package.json +38 -0
  31. package/src/utils.js +337 -33
@@ -0,0 +1,390 @@
1
+ const InitCommand = require('../init');
2
+
3
+ jest.mock('../../../helpers', () => ({
4
+ promptConfirm: jest.fn().mockResolvedValue(true),
5
+ promptSelect: jest.fn(),
6
+ runCliCommand: jest.fn().mockResolvedValue({}),
7
+ }));
8
+
9
+ const { promptConfirm, runCliCommand, promptSelect } = require('../../../helpers');
10
+
11
+ const fs = require('fs/promises');
12
+
13
+ const mockProjectName = 'sample mesh test workspace';
14
+
15
+ const mockGitDefaultFlag = 'n';
16
+
17
+ const mockPMDefaultFlag = 'npm';
18
+
19
+ const mockPathDefaultFlag = '.';
20
+
21
+ let errorLogSpy = null;
22
+
23
+ let createPackageJsonSpy = null;
24
+
25
+ let parseSpy = null;
26
+
27
+ let writeFile,
28
+ access,
29
+ mkdir = null;
30
+
31
+ describe('Workspace init command tests', () => {
32
+ beforeEach(() => {
33
+ promptConfirm.mockResolvedValue(true);
34
+ runCliCommand.mockResolvedValue({});
35
+ errorLogSpy = jest.spyOn(InitCommand.prototype, 'error');
36
+ parseSpy = jest.spyOn(InitCommand.prototype, 'parse');
37
+ createPackageJsonSpy = jest.spyOn(InitCommand.prototype, 'createPackageJson');
38
+ createPackageJsonSpy.mockResolvedValue({});
39
+ access = jest.spyOn(fs, 'access').mockRejectedValue(new Error());
40
+ writeFile = jest.spyOn(fs, 'writeFile').mockResolvedValue({});
41
+ mkdir = jest.spyOn(fs, 'mkdir').mockResolvedValue({});
42
+ });
43
+
44
+ test('Snapshot of Init command', () => {
45
+ expect(InitCommand.description).toMatchInlineSnapshot(
46
+ `"This command will create a workspace where you can organise your API mesh configuration and other files"`,
47
+ );
48
+ expect(InitCommand.summary).toMatchInlineSnapshot('"Initiate API Mesh workspace"');
49
+ expect(InitCommand.args).toMatchInlineSnapshot(`
50
+ [
51
+ {
52
+ "description": "Project name",
53
+ "name": "projectName",
54
+ "required": true,
55
+ },
56
+ ]
57
+ `);
58
+ expect(InitCommand.flags).toMatchInlineSnapshot(`
59
+ {
60
+ "git": {
61
+ "char": "g",
62
+ "input": [],
63
+ "multiple": false,
64
+ "options": [
65
+ "y",
66
+ "n",
67
+ ],
68
+ "parse": [Function],
69
+ "summary": "Should the workspace be initiated as a git project.",
70
+ "type": "option",
71
+ },
72
+ "packageManager": {
73
+ "char": "m",
74
+ "input": [],
75
+ "multiple": false,
76
+ "options": [
77
+ "npm",
78
+ "yarn",
79
+ ],
80
+ "parse": [Function],
81
+ "summary": "select yarn or npm for package management",
82
+ "type": "option",
83
+ },
84
+ "path": {
85
+ "char": "p",
86
+ "default": ".",
87
+ "input": [],
88
+ "multiple": false,
89
+ "parse": [Function],
90
+ "summary": "workspace path",
91
+ "type": "option",
92
+ },
93
+ }
94
+ `);
95
+ });
96
+
97
+ test('Command should pass with no flags', async () => {
98
+ parseSpy.mockResolvedValue({
99
+ args: {
100
+ projectName: mockProjectName,
101
+ },
102
+ flags: {
103
+ path: './template',
104
+ },
105
+ });
106
+ promptSelect.mockResolvedValue('yarn');
107
+ await InitCommand.run();
108
+ expect(promptConfirm).toHaveBeenCalled();
109
+ expect(promptSelect).toHaveBeenCalled();
110
+
111
+ // workspace directory creation
112
+ expect(access).toHaveBeenCalled();
113
+ expect(mkdir).toHaveBeenCalled();
114
+ expect(runCliCommand.mock.calls[0][0]).toBe('git init');
115
+ // env file creation
116
+ expect(writeFile).toHaveBeenCalled();
117
+ // package json file creation
118
+ expect(createPackageJsonSpy).toHaveBeenCalled();
119
+ // yarn install
120
+ expect(runCliCommand.mock.calls[1][0]).toBe('yarn install');
121
+ });
122
+
123
+ test('Command should pass and create git project if git flag is provided', async () => {
124
+ parseSpy.mockResolvedValue({
125
+ args: {
126
+ projectName: mockProjectName,
127
+ },
128
+ flags: {
129
+ path: mockPathDefaultFlag,
130
+ git: true,
131
+ packageManager: mockPMDefaultFlag,
132
+ },
133
+ });
134
+
135
+ await InitCommand.run();
136
+ expect(promptConfirm).toHaveBeenCalled();
137
+
138
+ // initiate git repo
139
+ expect(runCliCommand.mock.calls[0][0]).toBe('git init');
140
+ // no workspace directory creation
141
+ expect(access).toHaveBeenCalled();
142
+ expect(mkdir).toHaveBeenCalled();
143
+ // creating env file
144
+ expect(writeFile).toHaveBeenCalled();
145
+ // creating package json
146
+ expect(createPackageJsonSpy).toHaveBeenCalled();
147
+ // run npm install
148
+ expect(runCliCommand.mock.calls[1][0]).toBe('npm install');
149
+ });
150
+
151
+ test('Command should fail if git flag is provided and git init fails', async () => {
152
+ parseSpy.mockResolvedValue({
153
+ args: {
154
+ projectName: mockProjectName,
155
+ },
156
+ flags: {
157
+ path: mockPathDefaultFlag,
158
+ git: true,
159
+ packageManager: mockPMDefaultFlag,
160
+ },
161
+ });
162
+
163
+ runCliCommand.mockRejectedValue('');
164
+
165
+ // git comman failed
166
+ await expect(InitCommand.run()).rejects.toThrow();
167
+ // prompt flag not set
168
+ expect(promptConfirm).toHaveBeenCalled();
169
+ // git project
170
+ expect(runCliCommand).toHaveBeenCalled();
171
+ // no workspace directory creation
172
+ expect(access).toHaveBeenCalled();
173
+ expect(mkdir).toHaveBeenCalled();
174
+ // no env file creation
175
+ expect(writeFile).not.toHaveBeenCalled();
176
+ // not creating package json
177
+ expect(createPackageJsonSpy).not.toHaveBeenCalled();
178
+ });
179
+
180
+ test('Command should pass and create yarn project if yarn is package manager', async () => {
181
+ parseSpy.mockResolvedValue({
182
+ args: {
183
+ projectName: mockProjectName,
184
+ },
185
+ flags: {
186
+ path: mockPathDefaultFlag,
187
+ packageManager: 'yarn',
188
+ git: mockGitDefaultFlag,
189
+ },
190
+ });
191
+
192
+ await InitCommand.run();
193
+
194
+ // workspace directory creation
195
+ expect(access).toHaveBeenCalled();
196
+ expect(mkdir).toHaveBeenCalled();
197
+ // env file
198
+ expect(writeFile).toHaveBeenCalled();
199
+ // creating package json
200
+ expect(createPackageJsonSpy).toHaveBeenCalled();
201
+ // yarn install
202
+ expect(runCliCommand.mock.calls[0][0]).toBe('yarn install');
203
+ });
204
+
205
+ test('Command should pass and create yarn + git project if yarn is package manager and git flag is set', async () => {
206
+ parseSpy.mockResolvedValue({
207
+ args: {
208
+ projectName: mockProjectName,
209
+ },
210
+ flags: {
211
+ path: mockPathDefaultFlag,
212
+ git: true,
213
+ packageManager: 'yarn',
214
+ },
215
+ });
216
+ await InitCommand.run();
217
+ expect(promptConfirm).toHaveBeenCalled();
218
+ // create directory
219
+ expect(access).toHaveBeenCalled();
220
+ expect(mkdir).toHaveBeenCalled();
221
+ // env file
222
+ expect(writeFile).toHaveBeenCalled();
223
+ // creating package json
224
+ expect(createPackageJsonSpy).toHaveBeenCalled();
225
+ // yarn install
226
+ expect(runCliCommand.mock.calls[1][0]).toBe('yarn install');
227
+ });
228
+
229
+ test('Command should pass with creating the sub directory if the directory already exists', async () => {
230
+ parseSpy.mockResolvedValue({
231
+ args: {
232
+ projectName: mockProjectName,
233
+ },
234
+ flags: {
235
+ path: mockPathDefaultFlag,
236
+ git: mockGitDefaultFlag,
237
+ packageManager: mockPMDefaultFlag,
238
+ },
239
+ });
240
+ access.mockResolvedValue({});
241
+
242
+ await InitCommand.run();
243
+
244
+ expect(promptConfirm).toHaveBeenCalled();
245
+ // no directory creation
246
+ expect(access).toHaveBeenCalled();
247
+ expect(mkdir).toHaveBeenCalled();
248
+ // env file
249
+ expect(writeFile).toHaveBeenCalled();
250
+ // creating package json
251
+ expect(createPackageJsonSpy).toHaveBeenCalled();
252
+ });
253
+
254
+ test('Command should fail if the directory already exists and sub directory of project name exists', async () => {
255
+ parseSpy.mockResolvedValue({
256
+ args: {
257
+ projectName: mockProjectName,
258
+ },
259
+ flags: {
260
+ path: mockPathDefaultFlag,
261
+ git: mockGitDefaultFlag,
262
+ packageManager: mockPMDefaultFlag,
263
+ },
264
+ });
265
+ access.mockResolvedValue({});
266
+ mkdir.mockRejectedValue(new Error());
267
+
268
+ await expect(InitCommand.run()).rejects.toThrow();
269
+ expect(promptConfirm).toHaveBeenCalled();
270
+ // no directory creation
271
+ expect(access).toHaveBeenCalled();
272
+ expect(mkdir).toHaveBeenCalled();
273
+
274
+ // error log called
275
+ expect(errorLogSpy).toHaveBeenCalled();
276
+ // env file
277
+ expect(writeFile).not.toHaveBeenCalled();
278
+ // creating package json
279
+ expect(createPackageJsonSpy).not.toHaveBeenCalled();
280
+ });
281
+
282
+ test('Command should fail if directory creation fails', async () => {
283
+ parseSpy.mockResolvedValue({
284
+ args: {
285
+ projectName: mockProjectName,
286
+ },
287
+ flags: {
288
+ path: mockPathDefaultFlag,
289
+ git: mockGitDefaultFlag,
290
+ packageManager: mockPMDefaultFlag,
291
+ },
292
+ });
293
+ mkdir.mockRejectedValue(new Error());
294
+ await expect(InitCommand.run()).rejects.toThrow();
295
+
296
+ expect(errorLogSpy.mock.calls[0][0]).toMatch(/Could not create directory/);
297
+
298
+ expect(promptConfirm).toHaveBeenCalled();
299
+ // no directory creation
300
+ expect(access).toHaveBeenCalled();
301
+ expect(mkdir).toHaveBeenCalled();
302
+ // env file
303
+ expect(writeFile).not.toHaveBeenCalled();
304
+ // creating package json
305
+ expect(createPackageJsonSpy).not.toHaveBeenCalled();
306
+ });
307
+
308
+ test('Command should fail if npm install fails', async () => {
309
+ parseSpy.mockResolvedValue({
310
+ args: {
311
+ projectName: mockProjectName,
312
+ },
313
+ flags: {
314
+ path: mockPathDefaultFlag,
315
+ git: mockGitDefaultFlag,
316
+ packageManager: mockPMDefaultFlag,
317
+ },
318
+ });
319
+
320
+ runCliCommand.mockRejectedValueOnce('');
321
+
322
+ await expect(InitCommand.run()).rejects.toThrow();
323
+
324
+ expect(promptConfirm).toHaveBeenCalled();
325
+ // create directory
326
+ expect(access).toHaveBeenCalled();
327
+ expect(mkdir).toHaveBeenCalled();
328
+ // env file
329
+ expect(writeFile).toHaveBeenCalled();
330
+ // creating package json
331
+ expect(createPackageJsonSpy).toHaveBeenCalled();
332
+ expect(runCliCommand.mock.calls[0][0]).toBe('npm install');
333
+ });
334
+
335
+ test('Command should fail if yarn install fails', async () => {
336
+ parseSpy.mockResolvedValue({
337
+ args: {
338
+ projectName: mockProjectName,
339
+ },
340
+ flags: {
341
+ path: mockPathDefaultFlag,
342
+ git: mockGitDefaultFlag,
343
+ packageManager: 'yarn',
344
+ },
345
+ });
346
+ // npm command will fail
347
+ runCliCommand.mockRejectedValueOnce('');
348
+
349
+ await expect(InitCommand.run()).rejects.toThrow();
350
+
351
+ expect(promptConfirm).toHaveBeenCalled();
352
+ // no directory creation
353
+ expect(access).toHaveBeenCalled();
354
+ expect(mkdir).toHaveBeenCalled();
355
+ // env file
356
+ expect(writeFile).toHaveBeenCalled();
357
+ // creating package json
358
+ expect(createPackageJsonSpy).toHaveBeenCalled();
359
+ expect(runCliCommand.mock.calls[0][0]).toBe('yarn install');
360
+ });
361
+
362
+ test('Command should exit if sub directory prompt is provided with no', async () => {
363
+ parseSpy.mockResolvedValue({
364
+ args: {
365
+ projectName: mockProjectName,
366
+ },
367
+ flags: {
368
+ path: mockPathDefaultFlag,
369
+ git: mockGitDefaultFlag,
370
+ packageManager: mockPMDefaultFlag,
371
+ },
372
+ });
373
+ // create workspace prompt
374
+ promptConfirm.mockResolvedValueOnce(true);
375
+ // access passes
376
+ access.mockResolvedValue({});
377
+ // create in sub directory prompt
378
+ promptConfirm.mockResolvedValueOnce(false);
379
+ await InitCommand.run();
380
+ expect(access).toHaveBeenCalled();
381
+ expect(mkdir).not.toHaveBeenCalled();
382
+ });
383
+ });
384
+
385
+ test('test createPackageJson', async () => {
386
+ jest.spyOn(fs, 'readFile').mockResolvedValue({});
387
+ jest.spyOn(fs, 'writeFile').mockResolvedValue({});
388
+
389
+ await expect(InitCommand.prototype.createPackageJson()).resolves;
390
+ });