/
bundle.test.ts
157 lines (118 loc) · 4.93 KB
/
bundle.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
import * as path from 'path';
import * as fs from 'fs-extra';
import { Bundle } from '../../src';
import { Package } from '../_package';
test('validate', () => {
const pkg = Package.create({ name: 'consumer', licenses: ['Apache-2.0'], circular: true });
const dep1 = pkg.addDependency({ name: 'dep1', licenses: ['INVALID'] });
const dep2 = pkg.addDependency({ name: 'dep2', licenses: ['Apache-2.0', 'MIT'] });
pkg.write();
pkg.install();
const bundle = new Bundle({
packageDir: pkg.dir,
entryPoints: [pkg.entrypoint],
resources: { missing: 'bin/missing' },
allowedLicenses: ['Apache-2.0'],
});
const actual = new Set(bundle.validate().violations.map(v => `${v.type}: ${v.message}`));
const expected = new Set([
'circular-import: lib/bar.js -> lib/foo.js',
'missing-resource: Unable to find resource (missing) relative to the package directory',
'outdated-attributions: THIRD_PARTY_LICENSES is outdated',
`invalid-license: Dependency ${dep1.name}@${dep2.version} has an invalid license: UNKNOWN`,
`multiple-license: Dependency ${dep2.name}@${dep2.version} has multiple licenses: Apache-2.0,MIT`,
]);
expect(actual).toEqual(expected);
});
test('write', () => {
const pkg = Package.create({ name: 'consumer', licenses: ['Apache-2.0'] });
pkg.addDependency({ name: 'dep1', licenses: ['MIT'] });
pkg.addDependency({ name: 'dep2', licenses: ['Apache-2.0'] });
pkg.write();
pkg.install();
const bundle = new Bundle({
packageDir: pkg.dir,
entryPoints: [pkg.entrypoint],
allowedLicenses: ['Apache-2.0', 'MIT'],
});
const bundleDir = bundle.write();
expect(fs.existsSync(path.join(bundleDir, pkg.entrypoint))).toBeTruthy();
expect(fs.existsSync(path.join(bundleDir, 'package.json'))).toBeTruthy();
expect(fs.existsSync(path.join(bundleDir, 'THIRD_PARTY_LICENSES'))).toBeTruthy();
expect(fs.existsSync(path.join(bundleDir, 'lib', 'foo.js'))).toBeTruthy();
expect(fs.existsSync(path.join(bundleDir, 'lib', 'bar.js'))).toBeTruthy();
expect(fs.existsSync(path.join(bundleDir, 'node_modules'))).toBeFalsy();
expect(fs.existsSync(path.join(bundleDir, '.git'))).toBeFalsy();
const manifest = fs.readJSONSync(path.join(bundleDir, 'package.json'));
expect(manifest.dependencies).toEqual({});
});
test('pack', () => {
const pkg = Package.create({ name: 'consumer', licenses: ['Apache-2.0'] });
const dep1 = pkg.addDependency({ name: 'dep1', licenses: ['MIT'] });
const dep2 = pkg.addDependency({ name: 'dep2', licenses: ['Apache-2.0'] });
const attributions = [
'The consumer package includes the following third-party software/licensing:',
'',
`** ${dep1.name}@${dep1.version} - https://www.npmjs.com/package/${dep1.name}/v/${dep1.version} | MIT`,
'',
'----------------',
'',
`** ${dep2.name}@${dep2.version} - https://www.npmjs.com/package/${dep2.name}/v/${dep2.version} | Apache-2.0`,
'',
'----------------',
'',
];
pkg.attributions = attributions.join('\n');
pkg.write();
pkg.install();
const bundle = new Bundle({
packageDir: pkg.dir,
entryPoints: [pkg.entrypoint],
allowedLicenses: ['Apache-2.0', 'MIT'],
});
bundle.pack();
const tarball = path.join(pkg.dir, `${pkg.name}-${pkg.version}.tgz`);
expect(fs.existsSync(tarball)).toBeTruthy();
});
test('validate and fix', () => {
const pkg = Package.create({ name: 'consumer', licenses: ['Apache-2.0'] });
pkg.addDependency({ name: 'dep1', licenses: ['MIT'] });
pkg.addDependency({ name: 'dep2', licenses: ['Apache-2.0'] });
pkg.write();
pkg.install();
const bundle = new Bundle({
packageDir: pkg.dir,
entryPoints: [pkg.entrypoint],
allowedLicenses: ['Apache-2.0', 'MIT'],
});
try {
bundle.pack();
throw new Error('Expected packing to fail before fixing');
} catch (e) {
// this should fix the fact we don't generate
// the project with the correct notice
bundle.validate({ fix: true });
}
bundle.pack();
const tarball = path.join(pkg.dir, `${pkg.name}-${pkg.version}.tgz`);
expect(fs.existsSync(tarball)).toBeTruthy();
});
test('write ignores only .git and node_modules directories', () => {
const pkg = Package.create({ name: 'consumer', licenses: ['Apache-2.0'] });
pkg.addDependency({ name: 'dep1', licenses: ['MIT'] });
pkg.addDependency({ name: 'dep2', licenses: ['Apache-2.0'] });
pkg.write();
pkg.install();
const bundle = new Bundle({
packageDir: pkg.dir,
entryPoints: [pkg.entrypoint],
allowedLicenses: ['Apache-2.0', 'MIT'],
});
// add a gitignore file to the package - it should be included
fs.writeFileSync(path.join(pkg.dir, '.gitignore'), 'something');
// add a silly node_modules_file to the package - it should be included
fs.writeFileSync(path.join(pkg.dir, 'node_modules_file'), 'something');
const bundleDir = bundle.write();
expect(fs.existsSync(path.join(bundleDir, '.gitignore'))).toBeTruthy();
expect(fs.existsSync(path.join(bundleDir, 'node_modules_file'))).toBeTruthy();
});