forked from DevExpress/DevExtreme
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.e2e.spec.ts
More file actions
325 lines (244 loc) · 10.6 KB
/
executor.e2e.spec.ts
File metadata and controls
325 lines (244 loc) · 10.6 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import * as fs from 'fs';
import * as path from 'path';
import executor from './executor';
import { AddLicenseHeadersExecutorSchema } from './schema';
import { createTempDir, cleanupTempDir, createMockContext } from '../../utils/test-utils';
import { writeFileText, writeJson, readFileText } from '../../utils';
describe('AddLicenseHeadersExecutor E2E', () => {
let tempDir: string;
let context = createMockContext();
beforeEach(async () => {
tempDir = createTempDir('nx-license-e2e-');
context = createMockContext({ root: tempDir });
const projectDir = path.join(tempDir, 'packages', 'test-lib');
const npmDir = path.join(projectDir, 'npm');
fs.mkdirSync(npmDir, { recursive: true });
await writeJson(path.join(projectDir, 'package.json'), {
name: 'test-package',
version: '1.0.0',
repository: 'https://github.com/DevExpress/test-package',
});
await writeFileText(
path.join(npmDir, 'index.js'),
`export function hello() {\n return 'Hello';\n}\n`,
);
await writeFileText(path.join(npmDir, 'utils.js'), `export const add = (a, b) => a + b;\n`);
fs.mkdirSync(path.join(npmDir, 'components'), { recursive: true });
await writeFileText(
path.join(npmDir, 'components', 'button.js'),
`export const Button = () => {};\n`,
);
await writeFileText(
path.join(npmDir, 'types.ts'),
`export interface Config { name: string; }\n`,
);
});
afterEach(() => {
cleanupTempDir(tempDir);
});
describe('Basic functionality', () => {
it('should add license headers to all JS and TS files', async () => {
const options: AddLicenseHeadersExecutorSchema = {
targetDirectory: './npm',
packageJsonPath: './package.json',
};
const result = await executor(options, context);
expect(result.success).toBe(true);
const npmDir = path.join(tempDir, 'packages', 'test-lib', 'npm');
const indexContent = await readFileText(path.join(npmDir, 'index.js'));
expect(indexContent).toMatch(/^\/\*!/);
expect(indexContent).toContain('test-package');
expect(indexContent).toContain('Version: 1.0.0');
expect(indexContent).toContain('Developer Express Inc.');
expect(indexContent).toContain('MIT license');
const currentYear = new Date().getFullYear();
expect(indexContent).toContain(`2012 - ${currentYear}`);
expect(indexContent).toMatch(/Build date:/);
const utilsContent = await readFileText(path.join(npmDir, 'utils.js'));
expect(utilsContent).toMatch(/^\/\*!/);
const typesContent = await readFileText(path.join(npmDir, 'types.ts'));
expect(typesContent).toMatch(/^\/\*!/);
});
it('should add headers to nested files', async () => {
const options: AddLicenseHeadersExecutorSchema = {
targetDirectory: './npm',
packageJsonPath: './package.json',
};
const result = await executor(options, context);
expect(result.success).toBe(true);
const npmDir = path.join(tempDir, 'packages', 'test-lib', 'npm');
const buttonContent = await readFileText(path.join(npmDir, 'components', 'button.js'));
expect(buttonContent).toMatch(/^\/\*!/);
expect(buttonContent).toContain('test-package');
});
it('should preserve original file content after header', async () => {
const options: AddLicenseHeadersExecutorSchema = {
targetDirectory: './npm',
packageJsonPath: './package.json',
};
const npmDir = path.join(tempDir, 'packages', 'test-lib', 'npm');
const originalContent = await readFileText(path.join(npmDir, 'index.js'));
await executor(options, context);
const newContent = await readFileText(path.join(npmDir, 'index.js'));
expect(newContent).toMatch(/^\/\*!/);
expect(newContent).toContain(originalContent.trim());
});
it('should support custom license template', async () => {
const projectDir = path.join(tempDir, 'packages', 'test-lib');
const buildDir = path.join(projectDir, 'build', 'gulp');
fs.mkdirSync(buildDir, { recursive: true });
await writeFileText(
path.join(buildDir, 'license-header.txt'),
`/*!
* DevExtreme (<%= file.relative %>)
* Version: <%= version %>
* Build date: <%= date %>
*
* Copyright (c) 2012 - <%= year %> Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: <%= eula %>
*/
`,
);
const options: AddLicenseHeadersExecutorSchema = {
targetDirectory: './npm',
packageJsonPath: './package.json',
licenseTemplateFile: './build/gulp/license-header.txt',
eulaUrl: 'https://js.devexpress.com/Licensing/',
prependAfterLicense: '"use strict";\n\n',
includePatterns: ['**/*.js'],
};
const result = await executor(options, context);
expect(result.success).toBe(true);
const npmDir = path.join(projectDir, 'npm');
const content = await readFileText(path.join(npmDir, 'index.js'));
expect(content).toMatch(/^\/\*!/);
expect(content).toContain('DevExtreme (index.js)');
expect(content).toContain('https://js.devexpress.com/Licensing/');
expect(content).toContain('"use strict";');
expect(content).toContain("return 'Hello'");
});
});
describe('Idempotence', () => {
it('should not add duplicate headers on multiple runs', async () => {
const options: AddLicenseHeadersExecutorSchema = {
targetDirectory: './npm',
packageJsonPath: './package.json',
};
const npmDir = path.join(tempDir, 'packages', 'test-lib', 'npm');
const result1 = await executor(options, context);
expect(result1.success).toBe(true);
const contentAfterFirst = await readFileText(path.join(npmDir, 'index.js'));
const headerCount1 = (contentAfterFirst.match(/\/\*!/g) || []).length;
const result2 = await executor(options, context);
expect(result2.success).toBe(true);
const contentAfterSecond = await readFileText(path.join(npmDir, 'index.js'));
const headerCount2 = (contentAfterSecond.match(/\/\*!/g) || []).length;
expect(headerCount1).toBe(1);
expect(headerCount2).toBe(1);
expect(contentAfterFirst).toBe(contentAfterSecond);
});
it('should skip files that already have license headers', async () => {
const npmDir = path.join(tempDir, 'packages', 'test-lib', 'npm');
await writeFileText(
path.join(npmDir, 'with-header.js'),
`/*!\n * Existing header\n */\nexport const foo = 'bar';\n`,
);
const options: AddLicenseHeadersExecutorSchema = {
targetDirectory: './npm',
packageJsonPath: './package.json',
};
const result = await executor(options, context);
expect(result.success).toBe(true);
const content = await readFileText(path.join(npmDir, 'with-header.js'));
expect(content).toMatch(/^\/\*!/);
expect(content).toContain('Existing header');
expect(content).not.toContain('test-package');
});
});
describe('Error handling', () => {
it('should fail gracefully with missing package.json', async () => {
const options: AddLicenseHeadersExecutorSchema = {
targetDirectory: './npm',
packageJsonPath: './nonexistent-package.json',
};
const result = await executor(options, context);
expect(result.success).toBe(false);
});
it('should fail gracefully with invalid package.json', async () => {
const projectDir = path.join(tempDir, 'packages', 'test-lib');
await writeFileText(path.join(projectDir, 'package.json'), 'not valid json {{{}');
const options: AddLicenseHeadersExecutorSchema = {
targetDirectory: './npm',
packageJsonPath: './package.json',
};
const result = await executor(options, context);
expect(result.success).toBe(false);
});
it('should handle empty target directory', async () => {
const projectDir = path.join(tempDir, 'packages', 'test-lib');
const emptyDir = path.join(projectDir, 'empty');
fs.mkdirSync(emptyDir, { recursive: true });
const options: AddLicenseHeadersExecutorSchema = {
targetDirectory: './empty',
packageJsonPath: './package.json',
};
const result = await executor(options, context);
expect(result.success).toBe(true);
});
});
describe('Custom paths', () => {
it('should work with custom target directory', async () => {
const projectDir = path.join(tempDir, 'packages', 'test-lib');
const customDir = path.join(projectDir, 'dist');
fs.mkdirSync(customDir, { recursive: true });
await writeFileText(path.join(customDir, 'custom.js'), `export const custom = true;\n`);
const options: AddLicenseHeadersExecutorSchema = {
targetDirectory: './dist',
packageJsonPath: './package.json',
};
const result = await executor(options, context);
expect(result.success).toBe(true);
const content = await readFileText(path.join(customDir, 'custom.js'));
expect(content).toMatch(/^\/\*!/);
expect(content).toContain('test-package');
});
it('should work with custom package.json path', async () => {
const projectDir = path.join(tempDir, 'packages', 'test-lib');
await writeJson(path.join(projectDir, 'custom-package.json'), {
name: 'custom-package-name',
version: '2.0.0',
repository: 'https://github.com/DevExpress/custom-package',
});
const options: AddLicenseHeadersExecutorSchema = {
targetDirectory: './npm',
packageJsonPath: './custom-package.json',
};
const result = await executor(options, context);
expect(result.success).toBe(true);
const npmDir = path.join(projectDir, 'npm');
const content = await readFileText(path.join(npmDir, 'index.js'));
expect(content).toContain('custom-package-name');
expect(content).toContain('Version: 2.0.0');
});
});
it('should preserve formatting and whitespace', async () => {
const npmDir = path.join(tempDir, 'packages', 'test-lib', 'npm');
const originalContent = `export function complex() {
if (true) {
return 'formatted';
}
}
export const value = 42;
`;
await writeFileText(path.join(npmDir, 'formatted.js'), originalContent);
const options: AddLicenseHeadersExecutorSchema = {
targetDirectory: './npm',
packageJsonPath: './package.json',
separatorBetweenBannerAndContent: '\n',
};
await executor(options, context);
const newContent = await readFileText(path.join(npmDir, 'formatted.js'));
const contentWithoutHeader = newContent.replace(/^\/\*![\s\S]*?\*\/\n\n/, '');
expect(contentWithoutHeader).toBe(originalContent);
});
});