-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathreport.spec.ts
More file actions
223 lines (184 loc) · 8.84 KB
/
report.spec.ts
File metadata and controls
223 lines (184 loc) · 8.84 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
import child_process from 'node:child_process';
import { platform } from 'node:os';
import {
dependencies as licenseKitDependenciesObj,
devDependencies as licenseKitDevDependenciesObj,
version as licenseKitVersion,
} from '../../../packages/license-kit/package.json';
import {
dependencies as sharedDependenciesObj,
devDependencies as sharedDevDependenciesObj,
} from '../../../packages/licenses-api/package.json';
import {
dependencyMappingToCorrespondingKey,
getDependencyCorrespondingKey,
stripVersionSuffixes,
} from '../__utils__/utils';
import {
dependencies as dependenciesObj,
devDependencies as devDependenciesObj,
optionalDependencies as optionalDependenciesObj,
} from '../package.json';
// do not expect the private test package to be in the assertions baseline, it shall be excluded
const dependencies = dependencyMappingToCorrespondingKey(dependenciesObj).filter(
(dep) => dep !== '@callstack/example-private-package@workspace:*',
);
const devDependencies = dependencyMappingToCorrespondingKey(devDependenciesObj);
const optionalDependencies = dependencyMappingToCorrespondingKey(optionalDependenciesObj);
const licenseKitDependencies = dependencyMappingToCorrespondingKey(licenseKitDependenciesObj);
const licenseKitDevDependencies = dependencyMappingToCorrespondingKey(licenseKitDevDependenciesObj);
const sharedDependencies = dependencyMappingToCorrespondingKey(sharedDependenciesObj);
const sharedDevDependencies = dependencyMappingToCorrespondingKey(sharedDevDependenciesObj);
// below: correct the expected license-kit version (expected result) to match the actual version instead of specified version
devDependencies[Object.keys(devDependenciesObj).indexOf('license-kit')] = `license-kit@${licenseKitVersion}`;
async function runReportCommandForJsonOutput(args: string[] = []) {
const command = `yarn workspace license-kit-node-example report${args ? ` ${args.join(' ')}` : ''}`;
const output = await new Promise<string>((resolve) => {
child_process.exec(
command,
{
maxBuffer: 1024 * 1024 * 100, // 100MB
},
(_, stdout) => {
resolve(stdout);
},
);
});
return JSON.parse(output);
}
describe('license-kit report', () => {
it('including transitive deps, with dev deps with default settings', async () => {
const json = await runReportCommandForJsonOutput();
const isEvenPackageKey = getDependencyCorrespondingKey(dependenciesObj, 'is-even');
const mariadbPackageKey = getDependencyCorrespondingKey(dependenciesObj, 'mariadb');
const zustandPackageKey = getDependencyCorrespondingKey(dependenciesObj, 'zustand');
expect(json[getDependencyCorrespondingKey(dependenciesObj, 'dhtmlx-gantt')].type).toMatch('GPL-2.0');
expect(json[isEvenPackageKey].content).toMatch('MIT License');
expect(json[isEvenPackageKey].type).toMatch('MIT');
expect(json[mariadbPackageKey].content).toMatch('GNU LESSER GENERAL PUBLIC LICENSE');
expect(json[mariadbPackageKey].type).toMatch('LGPL-2.1-or-later');
expect(json[zustandPackageKey].content).toMatch('MIT License');
expect(json[zustandPackageKey].type).toMatch('MIT');
});
it('without transitive deps and without dev deps', async () => {
const json = await runReportCommandForJsonOutput(['--dev-deps-mode', 'none', '--transitive-deps-mode', 'none']);
expect(Object.keys(json).toSorted()).toEqual([...dependencies, ...optionalDependencies].toSorted());
});
it('without transitive deps and with root-only dev deps', async () => {
const json = await runReportCommandForJsonOutput([
'--dev-deps-mode',
'root-only',
'--transitive-deps-mode',
'none',
]);
expect(Object.keys(json).toSorted()).toEqual(
Array.from(new Set([...dependencies, ...optionalDependencies, ...devDependencies])).toSorted(),
);
});
it("does not include private packages' licenses", async () => {
const json = await runReportCommandForJsonOutput();
expect(Object.keys(json).toSorted()).not.toIncludeAllMembers(['@callstack/example-private-package']);
});
it('with root-only dev deps and with transitive dependencies of workspace-specifier-only dependencies', async () => {
const json = await runReportCommandForJsonOutput([
'--dev-deps-mode',
'root-only',
'--transitive-deps-mode',
'from-workspace-only',
]);
expect(stripVersionSuffixes(Object.keys(json).toSorted())).toEqual(
stripVersionSuffixes(
Array.from(
new Set([...dependencies, ...devDependencies, ...licenseKitDependencies, ...optionalDependencies]),
).toSorted(),
),
);
});
it('without dev deps and with transitive dependencies of external dependencies only', async () => {
const json = await runReportCommandForJsonOutput([
'--dev-deps-mode',
'none',
'--transitive-deps-mode',
'from-external-only',
]);
const resultKeys = Object.keys(json);
expect(stripVersionSuffixes(resultKeys.toSorted())).toEqual([
'@types/geojson',
'@types/node',
'chartjs-plugin-dragdata',
'd3-dispatch',
'd3-drag',
'd3-selection',
'denque',
'dhtmlx-gantt',
'iconv-lite',
'is-buffer',
'is-even',
'is-number',
'is-odd',
'kind-of',
'lru-cache',
'mariadb',
'safer-buffer',
'undici-types',
'zustand',
]);
expect(resultKeys).not.toIncludeAnyMembers(licenseKitDependencies);
expect(resultKeys).not.toIncludeAnyMembers(licenseKitDevDependencies);
});
it('with root-only dev deps and with transitive dependencies of external dependencies only', async () => {
const json = await runReportCommandForJsonOutput([
'--dev-deps-mode',
'root-only',
'--transitive-deps-mode',
'from-external-only',
]);
const resultKeys = Object.keys(json);
expect(resultKeys).toContain(`license-kit@${licenseKitVersion}`);
// this time, the result should also include all dependencies of the direct devDependencies of the root package.json
expect(resultKeys.length).toBeGreaterThan(200);
expect(stripVersionSuffixes(resultKeys)).toIncludeAllMembers([
'@babel/plugin-transform-async-to-generator',
'@babel/plugin-transform-block-scoped-functions',
'@babel/plugin-transform-block-scoping',
'@babel/plugin-transform-class-properties',
'@babel/helper-create-class-features-plugin',
'@babel/helper-member-expression-to-functions',
]);
expect(resultKeys).not.toIncludeAllMembers(licenseKitDependencies);
expect(resultKeys).not.toIncludeAllMembers(licenseKitDevDependencies);
// note: sharedDependencies should not be included, yet they contain glob, which is a transitive dependency of something else
expect(resultKeys).not.toIncludeAllMembers(sharedDevDependencies);
});
it('with root-only dev deps and with all transitive dependencies', async () => {
const json = await runReportCommandForJsonOutput(['--dev-deps-mode', 'root-only', '--transitive-deps-mode', 'all']);
const resultKeys = Object.keys(json);
const resultKeysWithoutVersions = stripVersionSuffixes(resultKeys);
// this time, the result should also include all transitive dependencies of direct devDependencies of the root package.json,
// such as that of license-kit itself (which is a direct devDependency of the root package.json)
expect(resultKeys.length).toBeGreaterThan(300);
expect(resultKeysWithoutVersions).toContain('license-kit');
expect(resultKeysWithoutVersions).toContain('@callstack/licenses');
expect(resultKeysWithoutVersions).toIncludeAllMembers(stripVersionSuffixes(licenseKitDependencies));
expect(resultKeysWithoutVersions).not.toIncludeAllMembers(stripVersionSuffixes(licenseKitDevDependencies));
expect(resultKeysWithoutVersions).toIncludeAllMembers(stripVersionSuffixes(sharedDependencies));
expect(resultKeysWithoutVersions).not.toIncludeAllMembers(stripVersionSuffixes(sharedDevDependencies));
});
it('without optional dependencies disabled', async () => {
const json = await runReportCommandForJsonOutput(['--include-optional-deps', 'false']);
const resultKeys = Object.keys(json);
// license-kit has a devDependency on tsx, which has an optionalDependency on fsevents
expect(stripVersionSuffixes(resultKeys)).not.toContain('fsevents');
expect(resultKeys).not.toIncludeAnyMembers(optionalDependencies);
});
it('with optional dependencies enabled', async () => {
const json = await runReportCommandForJsonOutput(['--include-optional-deps', 'true']);
const resultKeys = Object.keys(json);
// license-kit has a devDependency on tsx, which has an optionalDependency on fsevents
if (platform() === 'darwin') {
// eslint-disable-next-line jest/no-conditional-expect -- fsevents is macOS-only
expect(stripVersionSuffixes(resultKeys)).toContain('fsevents');
}
expect(resultKeys).toIncludeAnyMembers(optionalDependencies);
});
});