-
Notifications
You must be signed in to change notification settings - Fork 11
/
documentation.test.ts
299 lines (253 loc) · 12.6 KB
/
documentation.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
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
import * as child from 'child_process';
import * as os from 'os';
import * as path from 'path';
import * as fs from 'fs-extra';
import { Language, Documentation, UnInstallablePackageError, CorruptedAssemblyError, LanguageNotSupportedError } from '../../../src';
import { extractPackageName } from '../../../src/docgen/view/documentation';
import { Assemblies } from '../assemblies';
const LIBRARIES = `${__dirname}/../../__fixtures__/libraries`;
describe('extractPackageName', () => {
test('scope only', () => {
expect(extractPackageName('@aws-cdk/aws-ecr')).toEqual('@aws-cdk/aws-ecr');
});
test('scope and version', () => {
expect(extractPackageName('@aws-cdk/aws-ecr@1.100.1')).toEqual('@aws-cdk/aws-ecr');
});
test('no scope no version', () => {
expect(extractPackageName('aws-cdk-lib')).toEqual('aws-cdk-lib');
});
test('version only', () => {
expect(extractPackageName('aws-cdk-lib@1.100.1')).toEqual('aws-cdk-lib');
});
});
test('package installation does not run lifecycle hooks, includes optional dependencies', async () => {
const workdir = await fs.mkdtemp(path.join(os.tmpdir(), path.sep));
const libraryName = 'construct-library';
const libraryDir = path.join(LIBRARIES, libraryName);
await fs.copy(libraryDir, workdir);
const manifestPath = path.join(workdir, 'package.json');
const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf-8'));
const beacon = path.join(workdir, 'hook-ran-unexpected');
try {
// inject a postinstall hook
manifest.scripts.postinstall = `touch ${beacon}`;
// inject an optional peer dependency
manifest.peerDependencies.cdk8s = '^1.10.76';
manifest.peerDependenciesMeta = { cdk8s: { optional: true } };
// write out the new manifest file...
await fs.writeFile(manifestPath, JSON.stringify(manifest));
// ensure the optional peer dep is installed before attempting yarn package...
child.execSync('npm install --no-save cdk8s', { cwd: workdir });
// create the package
child.execSync('yarn package', { cwd: workdir });
// this should succeed because the failure script should be ignored
const docs = await Documentation.forPackage(
path.join(workdir, 'dist', 'js', `${libraryName}@0.0.0.jsii.tgz`),
{
verbose: false,
_postInstall: (dir) => expect(fs.pathExists(path.join(dir, 'node_modules', 'cdk8s'))).resolves.toBeTruthy(),
});
try {
const markdown = await docs.toMarkdown({ language: Language.TYPESCRIPT });
expect(markdown.render()).toMatchSnapshot();
// The postinstall hook shouldn't have run, so touchfile shouldn't have been created.
await expect(fs.pathExists(beacon)).resolves.toBeFalsy();
} finally {
await docs.cleanup();
}
} finally {
await fs.remove(workdir);
}
});
// This test is only relevant when running on non-macOS platforms.
(process.platform === 'darwin' ? test.skip : test)('package installation uses --force when EBADPLATFORM is encountered', async () => {
// The package has a hard dependency on fsevents, which only supports macOS platforms.
const docs = await Documentation.forPackage(
'@richkang/cdk-construct-network-firewall-textfile-rules@0.1.2',
{ verbose: false },
);
const markdown = await docs.toMarkdown({ language: Language.TYPESCRIPT });
expect(markdown.render()).toMatchSnapshot();
});
describe('python', () => {
test('for package', async () => {
const docs = await Documentation.forPackage('@aws-cdk/aws-ecr@1.106.0', { verbose: false });
try {
const json = await docs.toJson({ language: Language.PYTHON });
const markdown = await docs.toMarkdown({ language: Language.PYTHON });
expect(json.content).toMatchSnapshot();
expect(markdown.render()).toMatchSnapshot();
} finally {
await docs.cleanup();
}
});
test('snapshot - root module', async () => {
const docs = await Documentation.forAssembly('@aws-cdk/aws-ecr', Assemblies.AWSCDK_1_106_0);
const markdown = await docs.toMarkdown({ language: Language.PYTHON });
expect(markdown.render()).toMatchSnapshot();
});
test('snapshot - submodules', async () => {
const docs = await Documentation.forAssembly('aws-cdk-lib', Assemblies.AWSCDK_1_106_0);
try {
const markdown = await docs.toMarkdown({ language: Language.PYTHON, submodule: 'aws_eks' });
expect(markdown.render()).toMatchSnapshot();
} finally {
await docs.cleanup();
}
});
});
describe('typescript', () => {
test('for package', async () => {
const docs = await Documentation.forPackage('@aws-cdk/aws-ecr@1.106.0', { verbose: false });
try {
const markdown = await docs.toMarkdown({ language: Language.TYPESCRIPT });
expect(markdown.render()).toMatchSnapshot();
} finally {
await docs.cleanup();
}
});
test('snapshot - single module', async () => {
const docs = await Documentation.forAssembly('@aws-cdk/aws-ecr', Assemblies.AWSCDK_1_106_0);
const markdown = await docs.toMarkdown({ language: Language.TYPESCRIPT });
expect(markdown.render()).toMatchSnapshot();
});
test('snapshot - submodules', async () => {
const docs = await Documentation.forAssembly('aws-cdk-lib', Assemblies.AWSCDK_1_106_0);
const markdown = await docs.toMarkdown({ language: Language.TYPESCRIPT, submodule: 'aws_eks' });
expect(markdown.render()).toMatchSnapshot();
});
test('snapshot - nested submodules', async () => {
const docs = await Documentation.forAssembly('cdk-nag', Assemblies.AWSCDK_1_106_0);
const markdown = await docs.toMarkdown({ language: Language.TYPESCRIPT, submodule: 'rules.apigw' });
expect(markdown.render()).toMatchSnapshot();
});
});
describe('java', () => {
test('for package', async () => {
const docs = await Documentation.forPackage('@aws-cdk/aws-ecr@1.106.0', { verbose: false });
try {
const markdown = await docs.toMarkdown({ language: Language.JAVA });
expect(markdown.render()).toMatchSnapshot();
} finally {
await docs.cleanup();
}
});
test('snapshot - root module', async () => {
const docs = await Documentation.forAssembly('@aws-cdk/aws-ecr', Assemblies.AWSCDK_1_106_0);
const markdown = await docs.toMarkdown({ language: Language.JAVA });
expect(markdown.render()).toMatchSnapshot();
});
test('snapshot - submodules', async () => {
const docs = await Documentation.forAssembly('aws-cdk-lib', Assemblies.AWSCDK_1_106_0);
const markdown = await docs.toMarkdown({ language: Language.JAVA, submodule: 'aws_eks' });
expect(markdown.render()).toMatchSnapshot();
});
test('snapshot - submodules 2', async () => {
const docs = await Documentation.forAssembly('monocdk', Assemblies.AWSCDK_1_106_0);
const markdown = await docs.toMarkdown({ language: Language.JAVA, submodule: 'aws_eks' });
expect(markdown.render()).toMatchSnapshot();
});
});
describe('csharp', () => {
test('for package', async () => {
const docs = await Documentation.forPackage('@aws-cdk/aws-ecr@1.106.0', { verbose: false });
try {
const markdown = await docs.toMarkdown({ language: Language.CSHARP });
expect(markdown.render()).toMatchSnapshot();
} finally {
await docs.cleanup();
}
});
test('snapshot - root module', async () => {
const docs = await Documentation.forAssembly('@aws-cdk/aws-ecr', Assemblies.AWSCDK_1_106_0);
const markdown = await docs.toMarkdown({ language: Language.CSHARP });
expect(markdown.render()).toMatchSnapshot();
});
test('snapshot - submodules', async () => {
const docs = await Documentation.forAssembly('aws-cdk-lib', Assemblies.AWSCDK_1_106_0);
const markdown = await docs.toMarkdown({ language: Language.CSHARP, submodule: 'aws_eks' });
expect(markdown.render()).toMatchSnapshot();
});
test('snapshot - submodules 2', async () => {
const docs = await Documentation.forAssembly('monocdk', Assemblies.AWSCDK_1_106_0);
const markdown = await docs.toMarkdown({ language: Language.CSHARP, submodule: 'aws_eks' });
expect(markdown.render()).toMatchSnapshot();
});
});
describe('go', () => {
test('for package', async () => {
const docs = await Documentation.forPackage('constructs@10.0.78', { verbose: false });
try {
const markdown = await docs.toMarkdown({ language: Language.GO });
expect(markdown.render()).toMatchSnapshot();
} finally {
await docs.cleanup();
}
});
test('snapshot - root module', async () => {
const docs = await Documentation.forAssembly('constructs', Assemblies.AWSCDK_1_106_0);
const markdown = await docs.toMarkdown({ language: Language.GO });
expect(markdown.render()).toMatchSnapshot();
});
test('snapshot - submodules', async () => {
const docs = await Documentation.forAssembly('aws-cdk-lib', Assemblies.AWSCDK_1_106_0);
const markdown = await docs.toMarkdown({ language: Language.GO, submodule: 'aws_eks' });
expect(markdown.render()).toMatchSnapshot();
});
test('snapshot - submodules 2', async () => {
const docs = await Documentation.forAssembly('monocdk', Assemblies.AWSCDK_1_106_0);
const markdown = await docs.toMarkdown({ language: Language.GO, submodule: 'aws_eks' });
expect(markdown.render()).toMatchSnapshot();
});
});
test('throws uninstallable error on dependency conflict', async () => {
// this package decalres a fixed peerDependency on constructs, which conflicts with its other dependencies
return expect(Documentation.forPackage('cdk8s-mongo-sts@0.0.5', { verbose: false })).rejects.toThrowError(UnInstallablePackageError);
});
test('throws uninstallable error on missing spec in dependencies', async () => {
// this package has a corrupt package.json that doesn't contain a spec for some dependencies
return expect(Documentation.forPackage('cdk-codepipeline-bitbucket-build-result-reporter@0.0.7', { verbose: false })).rejects.toThrowError(UnInstallablePackageError);
});
test('throws corrupt assembly', async () => {
const docs = await Documentation.forPackage('@epilot/cdk-constructs@1.0.7', { verbose: false });
// this package accepts an unexported HttpApiProps in a constructor
await expect(docs.toMarkdown({ language: Language.TYPESCRIPT })).rejects.toThrowError(CorruptedAssemblyError);
await expect(docs.toJson({ language: Language.TYPESCRIPT })).rejects.toThrowError(CorruptedAssemblyError);
});
test('throws corrupt assembly 2', async () => {
const docs = await Documentation.forPackage('@pahud/cdktf-aws-ecs@v0.1.35', { verbose: false });
// this package had a peerDependency which underwent a breaking change in a minor version (cdktf.ComplexObject interface was removed)
await expect(docs.toMarkdown({ language: Language.TYPESCRIPT })).rejects.toThrowError(CorruptedAssemblyError);
await expect(docs.toJson({ language: Language.TYPESCRIPT })).rejects.toThrowError(CorruptedAssemblyError);
});
test('throws unsupported language with invalid config', async () => {
// package doesn't support Go and should throw with corresponding error
const docs = await Documentation.forPackage('@aws-cdk/pipelines@v1.144.0', { verbose: false });
await expect(docs.toMarkdown({ language: Language.GO })).rejects.toThrowError(LanguageNotSupportedError);
await expect(docs.toJson({ language: Language.GO })).rejects.toThrowError(LanguageNotSupportedError);
});
test('throws unsupported language when tablet was generated before rosetta supported go', async () => {
const docs = await Documentation.forPackage('aws-cdk-lib@2.16.0', { verbose: false });
await expect(docs.toMarkdown({ language: Language.GO })).rejects.toThrowError(LanguageNotSupportedError);
await expect(docs.toJson({ language: Language.GO })).rejects.toThrowError(LanguageNotSupportedError);
});
test('does not throw if unrelated assembly is corrupted', async () => {
const docs = await Documentation.forAssembly(
'@aws-cdk/region-info',
path.join(__dirname, '..', '..', '__fixtures__', 'corrupted', 'corrupted-unrelated-assembly'),
);
const json = await docs.toJson({ language: Language.PYTHON });
expect(json).toMatchSnapshot();
});
test('performance on large modules', async () => {
const docs = await Documentation.forPackage('@cdktf/provider-aws@4.0.1', { verbose: false });
// the assertion here is simply finishing the rendering in time.
await docs.toMarkdown({ language: Language.PYTHON, submodule: 'wafv2' });
});
test('does not reach headerSize limit for modules with method param examples', async () => {
// this module includes @example information for a method parameter
// at @aws-cdk/aws-apigateway.ProxyResource.addMethod.parameter.requestModels
const docs = await Documentation.forPackage('@aws-cdk/aws-apigateway@1.47.0', { verbose: false });
// the assertion here is simply that the rendering succeeds
await docs.toMarkdown({ language: Language.PYTHON });
});