@asyncapi/generator 2.0.3 → 2.1.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 (59) hide show
  1. package/.dockerignore +2 -0
  2. package/CHANGELOG.md +15 -0
  3. package/docs/configuration-file.md +2 -2
  4. package/docs/nunjucks-render-engine.md +2 -1
  5. package/jest.config.js +9 -0
  6. package/lib/__mocks__/filtersRegistry.js +3 -0
  7. package/lib/__mocks__/hooksRegistry.js +3 -0
  8. package/lib/__mocks__/templateConfigValidator.js +3 -0
  9. package/lib/__mocks__/utils.js +25 -0
  10. package/lib/filtersRegistry.js +14 -10
  11. package/package.json +9 -9
  12. package/test/__mocks__/@npmcli/arborist.js +11 -0
  13. package/test/__mocks__/@npmcli/config.js +3 -0
  14. package/test/__mocks__/fs.extra.js +3 -0
  15. package/test/__mocks__/loglevel.js +3 -0
  16. package/test/__mocks__/resolve-from.js +8 -0
  17. package/test/__mocks__/resolve-pkg.js +8 -0
  18. package/test/__snapshots__/integration.test.js.snap +25 -0
  19. package/test/docs/apiwithref.json +41 -0
  20. package/test/docs/dummy.yml +390 -0
  21. package/test/docs/dummyV3.yml +31 -0
  22. package/test/docs/shared.json +27 -0
  23. package/test/docs/ws.yml +36 -0
  24. package/test/generator.test.js +626 -0
  25. package/test/integration.test.js +58 -0
  26. package/test/parser.test.js +107 -0
  27. package/test/renderer.test.js +62 -0
  28. package/test/templateConfigValidator.test.js +277 -0
  29. package/test/test-project/.yarncr.yml +3 -0
  30. package/test/test-project/README.md +4 -0
  31. package/test/test-project/docker-compose.yml +24 -0
  32. package/test/test-project/package.json +25 -0
  33. package/test/test-project/test-entrypoint.sh +12 -0
  34. package/test/test-project/test-global.test.js +37 -0
  35. package/test/test-project/test-project.test.js +98 -0
  36. package/test/test-project/test-registry.test.js +55 -0
  37. package/test/test-project/test.sh +99 -0
  38. package/test/test-project/verdaccio/config.yaml +16 -0
  39. package/test/test-project/verdaccio/htpasswd +1 -0
  40. package/test/test-templates/nunjucks-template/package-lock.json +4143 -0
  41. package/test/test-templates/nunjucks-template/package.json +21 -0
  42. package/test/test-templates/nunjucks-template/template/test-file.md +5 -0
  43. package/test/test-templates/react-template/__transpiled/test-file.md.js +24 -0
  44. package/test/test-templates/react-template/__transpiled/test-file.md.js.map +1 -0
  45. package/test/test-templates/react-template/package-lock.json +4143 -0
  46. package/test/test-templates/react-template/package.json +23 -0
  47. package/test/test-templates/react-template/template/test-file.md.js +11 -0
  48. package/test/utils.test.js +76 -0
  49. package/.eslintignore +0 -5
  50. package/.eslintrc +0 -113
  51. package/.npmrc.template +0 -1
  52. package/.releaserc +0 -24
  53. package/.sonarcloud.properties +0 -2
  54. package/CODEOWNERS +0 -12
  55. package/CODE_OF_CONDUCT.md +0 -46
  56. package/CONTRIBUTING.md +0 -79
  57. package/LICENSE +0 -201
  58. package/README.md +0 -98
  59. package/assets/readme-banner.png +0 -0
@@ -0,0 +1,107 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const { sanitizeTemplateApiVersion, usesNewAPI, parse } = require('../lib/parser');
4
+ const dummyV2Document = fs.readFileSync(path.resolve(__dirname, './docs/dummy.yml'), 'utf8');
5
+ const dummyV3Document = fs.readFileSync(path.resolve(__dirname, './docs/dummyV3.yml'), 'utf8');
6
+
7
+ describe('Parser', () => {
8
+ describe('sanitizeTemplateApiVersion', () => {
9
+ it('should return version number when given `v99` syntax', () => {
10
+ const rawVersion = 'v99';
11
+ const expectedVersion = 99;
12
+ const sanitizedVersion = sanitizeTemplateApiVersion(rawVersion);
13
+
14
+ expect(sanitizedVersion).toStrictEqual(expectedVersion);
15
+ });
16
+ it('should return version number when given `v1` syntax', () => {
17
+ const rawVersion = 'v1';
18
+ const expectedVersion = 1;
19
+ const sanitizedVersion = sanitizeTemplateApiVersion(rawVersion);
20
+
21
+ expect(sanitizedVersion).toStrictEqual(expectedVersion);
22
+ });
23
+ it('should return version number when given `1` syntax', () => {
24
+ const rawVersion = '1';
25
+ const expectedVersion = 1;
26
+ const sanitizedVersion = sanitizeTemplateApiVersion(rawVersion);
27
+
28
+ expect(sanitizedVersion).toStrictEqual(expectedVersion);
29
+ });
30
+ });
31
+ describe('usesNewAPI', () => {
32
+ it('should use new parser api if v1', () => {
33
+ const templateConfig = {
34
+ apiVersion: 'v1'
35
+ };
36
+ const isUsingNewAPI = usesNewAPI(templateConfig);
37
+
38
+ expect(isUsingNewAPI).toStrictEqual(true);
39
+ });
40
+ it('should use new parser api if v2', () => {
41
+ const templateConfig = {
42
+ apiVersion: 'v2'
43
+ };
44
+ const isUsingNewAPI = usesNewAPI(templateConfig);
45
+
46
+ expect(isUsingNewAPI).toStrictEqual(true);
47
+ });
48
+ it('should use new parser api if v3', () => {
49
+ const templateConfig = {
50
+ apiVersion: 'v3'
51
+ };
52
+ const isUsingNewAPI = usesNewAPI(templateConfig);
53
+
54
+ expect(isUsingNewAPI).toStrictEqual(true);
55
+ });
56
+ it('should not use new API if no apiVersion', () => {
57
+ const templateConfig = { };
58
+ const isUsingNewAPI = usesNewAPI(templateConfig);
59
+
60
+ expect(isUsingNewAPI).toStrictEqual(false);
61
+ });
62
+ });
63
+ describe('parse', () => {
64
+ it('should be able to parse AsyncAPI v2 document for parser API v1', async () => {
65
+ const parsedDocument = await parse(dummyV2Document, {}, {templateConfig: {apiVersion: 'v1'}});
66
+
67
+ expect(parsedDocument).toBeDefined();
68
+ expect(parsedDocument.document.version()).toEqual('2.3.0');
69
+ });
70
+ it('should be able to parse AsyncAPI v2 document for parser API v2', async () => {
71
+ const parsedDocument = await parse(dummyV2Document, {}, {templateConfig: {apiVersion: 'v2'}});
72
+
73
+ expect(parsedDocument).toBeDefined();
74
+ expect(parsedDocument.document.version()).toEqual('2.3.0');
75
+ });
76
+ it('should be able to parse AsyncAPI v2 document for parser API v3', async () => {
77
+ const parsedDocument = await parse(dummyV2Document, {}, {templateConfig: {apiVersion: 'v3'}});
78
+
79
+ expect(parsedDocument).toBeDefined();
80
+ expect(parsedDocument.document.version()).toEqual('2.3.0');
81
+ });
82
+ it('should not be able to parse AsyncAPI v3 document for parser API v1', async () => {
83
+ const parsedDocument = await parse(dummyV3Document, {}, {templateConfig: {apiVersion: 'v1'}});
84
+
85
+ expect(parsedDocument).toBeDefined();
86
+ expect(parsedDocument.document).not.toBeDefined();
87
+ });
88
+ it('should not be able to parse AsyncAPI v3 document for old parser', async () => {
89
+ const parsedDocument = await parse(dummyV3Document, {}, {templateConfig: {}});
90
+
91
+ expect(parsedDocument).toBeDefined();
92
+ expect(parsedDocument.document).not.toBeDefined();
93
+ });
94
+ it('should be able to parse AsyncAPI v3 document for parser API v2', async () => {
95
+ const parsedDocument = await parse(dummyV3Document, {}, {templateConfig: {apiVersion: 'v2'}});
96
+
97
+ expect(parsedDocument).toBeDefined();
98
+ expect(parsedDocument.document.version()).toEqual('3.0.0');
99
+ });
100
+ it('should be able to parse AsyncAPI v3 document for parser API v3', async () => {
101
+ const parsedDocument = await parse(dummyV3Document, {}, {templateConfig: {apiVersion: 'v3'}});
102
+
103
+ expect(parsedDocument).toBeDefined();
104
+ expect(parsedDocument.document.version()).toEqual('3.0.0');
105
+ });
106
+ });
107
+ });
@@ -0,0 +1,62 @@
1
+ /* eslint-disable sonarjs/no-duplicate-string */
2
+ const {
3
+ configureReact,
4
+ renderReact,
5
+ saveRenderedReactContent,
6
+ } = require('../lib/renderer/react');
7
+
8
+ jest.mock('../lib/utils');
9
+ jest.mock('@asyncapi/generator-react-sdk');
10
+
11
+ describe('React renderer', () => {
12
+ describe('saveRenderedReactContent', () => {
13
+ let util = undefined;
14
+ let AsyncReactSDK = undefined;
15
+ beforeAll(() => {
16
+ util = require('../lib/utils');
17
+ AsyncReactSDK = require('@asyncapi/generator-react-sdk');
18
+ });
19
+
20
+ it('works transpilation', async () => {
21
+ await configureReact('../file', '../dir', '../file');
22
+ expect(AsyncReactSDK.transpileFiles).toHaveBeenCalledTimes(1);
23
+ });
24
+
25
+ it('works rendering', async () => {
26
+ await renderReact({}, '../file', {}, '../file', '../dir', '../location', {}, false, '');
27
+ expect(AsyncReactSDK.renderTemplate).toHaveBeenCalledTimes(1);
28
+ });
29
+
30
+ it('works saving single rendered content', async () => {
31
+ const content = {
32
+ content: 'Content',
33
+ metadata: {
34
+ fileName: 'file.html',
35
+ }
36
+ };
37
+
38
+ await saveRenderedReactContent(content, '../some/path');
39
+ expect(util.writeFile).toHaveBeenCalledTimes(1);
40
+ });
41
+
42
+ it('works saving multiple rendered contents', async () => {
43
+ const content = [
44
+ {
45
+ content: 'Content1',
46
+ metadata: {
47
+ fileName: 'file1.html',
48
+ }
49
+ },
50
+ {
51
+ content: 'Content2',
52
+ metadata: {
53
+ fileName: 'file2.html',
54
+ }
55
+ },
56
+ ];
57
+
58
+ await saveRenderedReactContent(content, '../some/path');
59
+ expect(util.writeFile).toHaveBeenCalledTimes(2);
60
+ });
61
+ });
62
+ });
@@ -0,0 +1,277 @@
1
+ /* eslint-disable sonarjs/no-identical-functions */
2
+ /* eslint-disable sonarjs/no-duplicate-string */
3
+ const { validateTemplateConfig } = require('../lib/templateConfigValidator');
4
+ const fs = require('fs');
5
+ const path = require('path');
6
+ const { parse } = require('../lib/parser');
7
+ const dummyYAML = fs.readFileSync(path.resolve(__dirname, './docs/dummy.yml'), 'utf8');
8
+
9
+ jest.mock('../lib/utils');
10
+
11
+ describe('Template Configuration Validator', () => {
12
+ let asyncapiDocument;
13
+
14
+ beforeAll(async () => {
15
+ const { document } = await parse(dummyYAML, {}, {templateConfig: {}});
16
+ asyncapiDocument = document;
17
+ });
18
+
19
+ it('Validation doesn\'t throw errors if params are not passed and template has no config', () => {
20
+ const templateParams = {};
21
+ const templateConfig = {};
22
+
23
+ const isValid = validateTemplateConfig(templateConfig, templateParams, asyncapiDocument);
24
+
25
+ expect(isValid).toStrictEqual(true);
26
+ });
27
+
28
+ it('Validation doesn\'t throw errors for correct react renderer', () => {
29
+ const templateParams = {};
30
+ const templateConfig = {
31
+ renderer: 'react'
32
+ };
33
+ const isValid = validateTemplateConfig(templateConfig, templateParams, asyncapiDocument);
34
+ expect(isValid).toStrictEqual(true);
35
+ });
36
+ it('Validation doesn\'t throw errors for correct nunjucks renderer', () => {
37
+ const templateParams = {};
38
+ const templateConfig = {
39
+ renderer: 'nunjucks'
40
+ };
41
+ const isValid = validateTemplateConfig(templateConfig, templateParams, asyncapiDocument);
42
+ expect(isValid).toStrictEqual(true);
43
+ });
44
+ it('Validation throw error if renderer not supported', () => {
45
+ const templateParams = {};
46
+ const templateConfig = {
47
+ renderer: 'non_existing'
48
+ };
49
+ expect(() => validateTemplateConfig(templateConfig, templateParams)).toThrow('We do not support \'non_existing\' as a renderer for a template. Only \'react\' or \'nunjucks\' are supported.');
50
+ });
51
+
52
+ it('Validation throw error if template is not compatible because of generator version', () => {
53
+ const utils = require('../lib/utils');
54
+ utils.__generatorVersion = '1.0.0';
55
+
56
+ const templateParams = {};
57
+ const templateConfig = {
58
+ generator: '>1.0.1'
59
+ };
60
+
61
+ expect(() => validateTemplateConfig(templateConfig, templateParams)).toThrow('This template is not compatible with the current version of the generator (1.0.0). This template is compatible with the following version range: >1.0.1.');
62
+ });
63
+
64
+ it('Validation throw error if template is not compatible because of non-supported apiVersion value', () => {
65
+ const utils = require('../lib/utils');
66
+ utils.__generatorVersion = '1.0.0';
67
+
68
+ const templateParams = {};
69
+ const templateConfig = {
70
+ apiVersion: '999999'
71
+ };
72
+
73
+ expect(() => validateTemplateConfig(templateConfig, templateParams)).toThrow('The version specified in apiVersion is not supported by this Generator version. Supported versions are: v1, v2');
74
+ });
75
+
76
+ it('Validation throw error if required params not provided', () => {
77
+ const templateParams = {};
78
+ const templateConfig = {
79
+ parameters: {
80
+ test: {
81
+ description: 'test',
82
+ required: true
83
+ }
84
+ }
85
+ };
86
+
87
+ expect(() => validateTemplateConfig(templateConfig, templateParams)).toThrow('This template requires the following missing params: test.');
88
+ });
89
+
90
+ it('Validation throw error if provided param is not in the list of params supported by the template', () => {
91
+ const templateParams = {
92
+ tsets: 'myTest'
93
+ };
94
+ const templateConfig = {
95
+ parameters: {
96
+ test: {
97
+ description: 'this param distance to test1 equals 3 according to levenshtein-edit-distance'
98
+ },
99
+ thisissomethingsodifferent: {
100
+ description: 'this param distance to test1 equals 21 according to levenshtein-edit-distance'
101
+ }
102
+ }
103
+ };
104
+ expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('This template doesn\'t have the following params: tsets.\nDid you mean "test" instead of "tsets"?');
105
+ });
106
+
107
+ it('Validation throw error if provided param is not supported by the template as template has no params specified', () => {
108
+ console.warn = jest.fn();
109
+ const templateParams = {
110
+ test1: 'myTest'
111
+ };
112
+ const templateConfig = {};
113
+
114
+ expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('This template doesn\'t have any params.');
115
+ });
116
+ it('Validation throw error if subject in condition files is not string', () => {
117
+ const templateParams = {};
118
+ const templateConfig = {
119
+ conditionalFiles: {
120
+ 'my/path/to/file.js': {
121
+ subject: ['server.protocol'],
122
+ validation: {
123
+ const: 'myprotocol'
124
+ }
125
+ }
126
+ }
127
+ };
128
+
129
+ expect(() => validateTemplateConfig(templateConfig, templateParams)).toThrow('Invalid conditional file subject for my/path/to/file.js: server.protocol.');
130
+ });
131
+
132
+ it('Validation throw error if validation in condition files is not object', () => {
133
+ const templateParams = {};
134
+ const templateConfig = {
135
+ conditionalFiles: {
136
+ 'my/path/to/file.js': {
137
+ subject: 'server.url',
138
+ validation: 'http://example.com'
139
+ }
140
+ }
141
+ };
142
+
143
+ expect(() => validateTemplateConfig(templateConfig, templateParams)).toThrow('Invalid conditional file validation object for my/path/to/file.js: http://example.com.');
144
+ });
145
+
146
+ it('Validation enrich conditional files object with validate object', () => {
147
+ const templateParams = {};
148
+ const templateConfig = {
149
+ conditionalFiles: {
150
+ 'my/path/to/file.js': {
151
+ subject: 'server.protocol',
152
+ validation: {
153
+ const: 'myprotocol'
154
+ }
155
+ }
156
+ }
157
+ };
158
+ validateTemplateConfig(templateConfig, templateParams);
159
+
160
+ expect(templateConfig.conditionalFiles['my/path/to/file.js']).toBeDefined();
161
+ });
162
+
163
+ it('Validation throw error if specified server is not in asyncapi document', () => {
164
+ const templateParams = {
165
+ server: 'myserver'
166
+ };
167
+ const templateConfig = {
168
+ parameters: {
169
+ server: {
170
+ description: ''
171
+ }
172
+ }
173
+ };
174
+
175
+ expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Couldn\'t find server with name myserver.');
176
+ });
177
+
178
+ it('Validation throw error if given protocol is not supported by template', () => {
179
+ const templateParams = {
180
+ server: 'dummy-mqtt'
181
+ };
182
+ const templateConfig = {
183
+ supportedProtocols: ['myprotocol'],
184
+ parameters: {
185
+ server: {
186
+ description: ''
187
+ }
188
+ }
189
+ };
190
+
191
+ expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Server "dummy-mqtt" uses the mqtt protocol but this template only supports the following ones: myprotocol.');
192
+ });
193
+
194
+ describe('should work with v1 apiVersion', () => {
195
+ let asyncapiDocument;
196
+ const v2TemplateConfig = {apiVersion: 'v1'};
197
+ beforeAll(async () => {
198
+ const { document } = await parse(dummyYAML, {}, {templateConfig: v2TemplateConfig});
199
+ asyncapiDocument = document;
200
+ });
201
+
202
+ it('Validation throw error if specified server is not in asyncapi document', () => {
203
+ const templateParams = {
204
+ server: 'myserver'
205
+ };
206
+ const templateConfig = {
207
+ ...v2TemplateConfig,
208
+ parameters: {
209
+ server: {
210
+ description: ''
211
+ }
212
+ }
213
+ };
214
+
215
+ expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Couldn\'t find server with name myserver.');
216
+ });
217
+
218
+ it('Validation throw error if given protocol is not supported by template', () => {
219
+ const templateParams = {
220
+ server: 'dummy-mqtt'
221
+ };
222
+ const templateConfig = {
223
+ ...v2TemplateConfig,
224
+ supportedProtocols: ['myprotocol'],
225
+ parameters: {
226
+ server: {
227
+ description: ''
228
+ }
229
+ }
230
+ };
231
+
232
+ expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Server "dummy-mqtt" uses the mqtt protocol but this template only supports the following ones: myprotocol.');
233
+ });
234
+ });
235
+
236
+ describe('should work with v2 apiVersion', () => {
237
+ let asyncapiDocument;
238
+ const v2TemplateConfig = {apiVersion: 'v2'};
239
+ beforeAll(async () => {
240
+ const { document } = await parse(dummyYAML, {}, {templateConfig: v2TemplateConfig});
241
+ asyncapiDocument = document;
242
+ });
243
+
244
+ it('Validation throw error if specified server is not in asyncapi document', () => {
245
+ const templateParams = {
246
+ server: 'myserver'
247
+ };
248
+ const templateConfig = {
249
+ ...v2TemplateConfig,
250
+ parameters: {
251
+ server: {
252
+ description: ''
253
+ }
254
+ }
255
+ };
256
+
257
+ expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Couldn\'t find server with name myserver.');
258
+ });
259
+
260
+ it('Validation throw error if given protocol is not supported by template', () => {
261
+ const templateParams = {
262
+ server: 'dummy-mqtt'
263
+ };
264
+ const templateConfig = {
265
+ ...v2TemplateConfig,
266
+ supportedProtocols: ['myprotocol'],
267
+ parameters: {
268
+ server: {
269
+ description: ''
270
+ }
271
+ }
272
+ };
273
+
274
+ expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Server "dummy-mqtt" uses the mqtt protocol but this template only supports the following ones: myprotocol.');
275
+ });
276
+ });
277
+ });
@@ -0,0 +1,3 @@
1
+ npmRegistries:
2
+ "http://localhost:4873/":
3
+ npmAuthToken: LwRGijjh4ZHXm+Q3FthZhA==
@@ -0,0 +1,4 @@
1
+ It is a test project where AsyncAPI Generator and test template are used as Node.js dependencies.
2
+ The purpose of this project is to test AsyncAPI Generator library use case outside the Generator code base.
3
+
4
+ Instead of running tests with `npm test`, make sure you have Docker Compose and run `docker-compose rm -f -s test-project verdaccio && NODE_IMAGE_TAG=18 docker-compose up --abort-on-container-exit --remove-orphans`.
@@ -0,0 +1,24 @@
1
+ version: '3'
2
+
3
+ services:
4
+ verdaccio:
5
+ image: verdaccio/verdaccio:5
6
+ ports:
7
+ - '4873:4873'
8
+ volumes:
9
+ - './verdaccio:/verdaccio/conf'
10
+ networks:
11
+ - mynetwork
12
+
13
+ test-project:
14
+ privileged: true
15
+ image: "node:${NODE_IMAGE_TAG}"
16
+ volumes:
17
+ - ../../../../:/apptemp
18
+ command: bash /apptemp/apps/generator/test/test-project/test-entrypoint.sh
19
+ networks:
20
+ - mynetwork
21
+
22
+ networks:
23
+ mynetwork:
24
+ driver: bridge
@@ -0,0 +1,25 @@
1
+ {
2
+ "description": "Test project. More info in the readme.",
3
+ "scripts": {
4
+ "test": "npm run test:cleanup && npm run test:project && npm run test:global && npm run test:registry",
5
+ "test:project:A": "jest --testNamePattern='Test A' --detectOpenHandles --testPathPattern=test-project/test-project --modulePathIgnorePatterns='./__mocks__'",
6
+ "test:project:B": "jest --testNamePattern='Test B' --detectOpenHandles --testPathPattern=test-project/test-project --modulePathIgnorePatterns='./__mocks__'",
7
+ "test:project:C": "jest --testNamePattern='Test C' --detectOpenHandles --testPathPattern=test-project/test-project --modulePathIgnorePatterns='./__mocks__'",
8
+ "test:global": "jest --detectOpenHandles --testPathPattern=test-global --modulePathIgnorePatterns='./__mocks__'",
9
+ "test:registry:arg": "jest --testNamePattern='argument' --detectOpenHandles --testPathPattern=test-registry --modulePathIgnorePatterns='./__mocks__'",
10
+ "test:registry:npm-config": "jest --testNamePattern='npm config' --detectOpenHandles --testPathPattern=test-registry --modulePathIgnorePatterns='./__mocks__'",
11
+ "test:cleanup": "rimraf \"../temp\""
12
+ },
13
+ "devDependencies": {
14
+ "fs-extra": "9.1.0",
15
+ "jest": "28.1.3",
16
+ "rimraf": "3.0.2"
17
+ },
18
+ "jest": {
19
+ "moduleNameMapper": {
20
+ "^nimma/legacy$": "<rootDir>/../../../../node_modules/nimma/dist/legacy/cjs/index.js",
21
+ "^nimma/(.*)": "<rootDir>/../../../../node_modules/nimma/dist/cjs/$1",
22
+ "^nunjucks-filters$": "<rootDir>/../../../nunjucks-filters"
23
+ }
24
+ }
25
+ }
@@ -0,0 +1,12 @@
1
+ #!/bin/bash
2
+
3
+ # Goal of this script is to copy mounted drive into a separate folder
4
+ # It is to assure the test run is isolated from the host
5
+ # apptemp is mounted from host and we do not want to mess in there
6
+
7
+
8
+ # Copy the contents of /apptemp to /app
9
+ cp -r /apptemp /app
10
+
11
+ # Execute the bash script
12
+ bash /app/apps/generator/test/test-project/test.sh test-project
@@ -0,0 +1,37 @@
1
+ /**
2
+ * @jest-environment node
3
+ */
4
+
5
+ const { readFile } = require('fs').promises;
6
+ const path = require('path');
7
+ const Generator = require('@asyncapi/generator');
8
+ const dummySpecPath = path.resolve(__dirname, '../docs/dummy.yml');
9
+ const templateName = 'react-template';
10
+ const tempOutputResults = '../temp/integrationTestResult';
11
+ const fileToCheck = 'test-file.md';
12
+ const logMessage = require('../../lib/logMessages');
13
+ const newContentNotExpectedInTest = 'new content';
14
+ console.log = jest.fn();
15
+
16
+ describe('Testing if markdown was generated using global template', () => {
17
+ jest.setTimeout(1000000);
18
+
19
+ it('generated markdown should not contain information that was added to template after it was installed globally', async () => {
20
+ //you always want to generate to new directory to make sure test runs in clear environment
21
+ const outputDir = path.resolve(tempOutputResults, Math.random().toString(36).substring(7));
22
+
23
+ //we setup generator using template name, not path, without explicitly running installation
24
+ //generator picks up template that is already in node_modules as it was installed before as node dependency in global packages location
25
+ const generator = new Generator(templateName, outputDir, { forceWrite: true, debug: true, templateParams: { version: 'v1', mode: 'production' } });
26
+ await generator.generateFromFile(dummySpecPath);
27
+
28
+ const file = await readFile(path.join(outputDir, fileToCheck), 'utf8');
29
+ const isNewContentThere = file.includes(newContentNotExpectedInTest);
30
+
31
+ //global template was not modified so it should not contain new content after template modification
32
+ expect(isNewContentThere).toStrictEqual(false);
33
+ //we make sure that logs indicate that global package was used
34
+ expect(console.log).toHaveBeenCalledWith(logMessage.templateNotFound(templateName));
35
+ expect(console.log).toHaveBeenCalledWith(logMessage.templateVersion('0.0.1'));
36
+ });
37
+ });
@@ -0,0 +1,98 @@
1
+ /**
2
+ * @jest-environment node
3
+ */
4
+
5
+ const { readFile } = require('fs').promises;
6
+ const path = require('path');
7
+ const Generator = require('@asyncapi/generator');
8
+ const dummySpecPath = path.resolve(__dirname, '../docs/dummy.yml');
9
+ const templateLocation = path.resolve(__dirname,'../test-templates/react-template');
10
+ const templateName = 'react-template';
11
+ const tempOutputResults = path.resolve(__dirname, 'output');
12
+ const fileToCheck = 'test-file.md';
13
+ const source = path.join(__dirname, 'node_modules', templateName);
14
+ const logMessage = require('../../lib/logMessages.js');
15
+ const newContentNotExpectedInTest = 'new content';
16
+ const version = '0.0.1';
17
+
18
+ console.log = jest.fn();
19
+
20
+ describe('Testing if markdown was generated with proper version of the template', () => {
21
+ jest.setTimeout(1000000);
22
+
23
+ it('Test A - generated markdown should not contain new content in modified template', async () => {
24
+ //we setup generator using template name, not path, without explicitly running installation
25
+ //generator picks up template that is already in node_modules as it was installed before as node dependency
26
+ //it was installed before triggering test in test.sh
27
+ const generator = new Generator(templateName, tempOutputResults, { forceWrite: true, debug: true, templateParams: { version: 'v1', mode: 'production' } });
28
+ await generator.generateFromFile(dummySpecPath);
29
+
30
+ const file = await readFile(path.join(tempOutputResults, fileToCheck), 'utf8');
31
+ const isNewContentThere = file.includes(newContentNotExpectedInTest);
32
+
33
+ //we make sure that markdown file doesn't contain new content as old version of template do not have it
34
+ expect(isNewContentThere).toStrictEqual(false);
35
+ //we make sure that logs do not indicate that new installation is started
36
+ expect(console.log).not.toHaveBeenCalledWith(logMessage.installationDebugMessage(logMessage.TEMPLATE_INSTALL_FLAG_MSG));
37
+ expect(console.log).toHaveBeenCalledWith(logMessage.templateSource(source));
38
+ expect(console.log).toHaveBeenCalledWith(logMessage.templateVersion(version));
39
+ });
40
+
41
+ it('Test B - generated markdown should contain new content because of explicit fresh installation of different template version (install: true)', async () => {
42
+ const templateVersion = '0.0.2';
43
+
44
+ const generator = new Generator(`${templateName}@${templateVersion}`, tempOutputResults, { forceWrite: true, install: true, debug: true, templateParams: { version: 'v1', mode: 'production' } });
45
+ await generator.generateFromFile(dummySpecPath);
46
+
47
+ const file = await readFile(path.join(tempOutputResults, fileToCheck), 'utf8');
48
+ const isNewContentThere = file.includes(newContentNotExpectedInTest);
49
+
50
+ //in 0.0.2 version of template there is a new content as in test.sh we made sure new template version has new content not present in 0.0.1
51
+ expect(isNewContentThere).toStrictEqual(true);
52
+ expect(console.log).toHaveBeenCalledWith(logMessage.installationDebugMessage(logMessage.TEMPLATE_INSTALL_FLAG_MSG));
53
+ expect(console.log).toHaveBeenCalledWith(logMessage.templateVersion(templateVersion));
54
+ });
55
+
56
+ it('Test C - generated markdown should not contain new content of template because local version of the template is old and does not have new content (with and without install:true)', async () => {
57
+ let file;
58
+ let isNewContentThere;
59
+
60
+ //we need arborist to perform installation of local template
61
+ const Arborist = require('@npmcli/arborist');
62
+
63
+ //we need to install local template before we can use it
64
+ const arb = new Arborist({
65
+ path: templateLocation
66
+ });
67
+
68
+ await arb.reify({
69
+ save: false
70
+ });
71
+
72
+ //run generation by passing path to local template without passing install flag, sources should be taken from local path
73
+ const generatorWithoutInstallFlag = new Generator(templateLocation, tempOutputResults, { forceWrite: true, debug: true, templateParams: { version: 'v1', mode: 'production' } });
74
+ await generatorWithoutInstallFlag.generateFromFile(dummySpecPath);
75
+
76
+ file = await readFile(path.join(tempOutputResults, fileToCheck), 'utf8');
77
+ isNewContentThere = file.includes(newContentNotExpectedInTest);
78
+
79
+ //we make sure that markdown file doesn't contain new content as old version of template do not have it
80
+ expect(isNewContentThere).toStrictEqual(false);
81
+ expect(console.log).toHaveBeenCalledWith(logMessage.templateSource(templateLocation));
82
+ expect(console.log).toHaveBeenCalledWith(logMessage.templateVersion(version));
83
+ expect(console.log).toHaveBeenCalledWith(logMessage.NODE_MODULES_INSTALL);
84
+
85
+ //run generation by passing path to local template and passing install flag, sources should be taken from local path and simlink created
86
+ const generatorWithInstallFlag = new Generator(templateLocation, tempOutputResults, { install: true, forceWrite: true, debug: true, templateParams: { version: 'v1', mode: 'production' } });
87
+ await generatorWithInstallFlag.generateFromFile(dummySpecPath);
88
+
89
+ file = await readFile(path.join(tempOutputResults, fileToCheck), 'utf8');
90
+ isNewContentThere = file.includes(newContentNotExpectedInTest);
91
+
92
+ //we make sure that markdown file doesn't contain new content as old version of template do not have it
93
+ expect(isNewContentThere).toStrictEqual(false);
94
+ expect(console.log).toHaveBeenCalledWith(logMessage.installationDebugMessage(logMessage.TEMPLATE_INSTALL_FLAG_MSG));
95
+ expect(console.log).toHaveBeenCalledWith(logMessage.templateVersion(version));
96
+ expect(console.log).toHaveBeenCalledWith(logMessage.NPM_INSTALL_TRIGGER);
97
+ });
98
+ });