-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathunused-files-warning_spec_large.ts
281 lines (228 loc) · 8.56 KB
/
unused-files-warning_spec_large.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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Architect } from '@angular-devkit/architect';
import { TestLogger } from '@angular-devkit/architect/testing';
import { debounceTime, take, tap } from 'rxjs/operators';
import { BrowserBuilderOutput } from '../../src/browser/index';
import { createArchitect, host, veEnabled } from '../utils';
// tslint:disable-next-line:no-big-function
describe('Browser Builder unused files warnings', () => {
const warningMessageSuffix = `is part of the TypeScript compilation but it's unused`;
const targetSpec = { project: 'app', target: 'build' };
let architect: Architect;
beforeEach(async () => {
await host.initialize().toPromise();
architect = (await createArchitect(host.root())).architect;
});
afterEach(async () => host.restore().toPromise());
it('should not show warning when all files are used', async () => {
if (veEnabled) {
// TODO: https://github.com/angular/angular-cli/issues/15056
pending('Only supported in Ivy.');
return;
}
const logger = new TestLogger('unused-files-warnings');
const run = await architect.scheduleTarget(targetSpec, undefined, { logger });
const output = await run.result as BrowserBuilderOutput;
expect(output.success).toBe(true);
expect(logger.includes(warningMessageSuffix)).toBe(false);
logger.clear();
await run.stop();
});
it('should show warning when some files are unused', async () => {
if (veEnabled) {
// TODO: https://github.com/angular/angular-cli/issues/15056
pending('Only supported in Ivy.');
return;
}
host.replaceInFile(
'src/tsconfig.app.json',
'"main.ts"',
'"main.ts", "environments/environment.prod.ts"',
);
const logger = new TestLogger('unused-files-warnings');
const run = await architect.scheduleTarget(targetSpec, undefined, { logger });
const output = await run.result as BrowserBuilderOutput;
expect(output.success).toBe(true);
expect(logger.includes(`environment.prod.ts ${warningMessageSuffix}`)).toBe(true);
logger.clear();
await run.stop();
});
it('should not show warning when excluded files are unused', async () => {
if (veEnabled) {
// TODO: https://github.com/angular/angular-cli/issues/15056
pending('Only supported in Ivy.');
return;
}
const ignoredFiles = {
'src/file.d.ts': 'export type MyType = number;',
'src/file.ngsummary.ts': 'export const hello = 42;',
'src/file.ngfactory.ts': 'export const hello = 42;',
'src/file.ngstyle.ts': 'export const hello = 42;',
'src/file.ng_typecheck__.ts': 'export const hello = 42;',
};
host.writeMultipleFiles(ignoredFiles);
host.replaceInFile(
'src/tsconfig.app.json',
'"main.ts"',
`"main.ts", ${Object.keys(ignoredFiles).map(f => `"${f.replace('src/', '')}"`).join(',')}`,
);
const logger = new TestLogger('unused-files-warnings');
const run = await architect.scheduleTarget(targetSpec, undefined, { logger });
const output = await run.result as BrowserBuilderOutput;
expect(output.success).toBe(true);
expect(logger.includes(warningMessageSuffix)).toBe(false);
logger.clear();
await run.stop();
});
it('should not show warning when type files are used', async () => {
if (veEnabled) {
// TODO: https://github.com/angular/angular-cli/issues/15056
pending('Only supported in Ivy.');
return;
}
host.writeMultipleFiles({
'src/app/type.ts': 'export type MyType = number;',
});
host.replaceInFile(
'src/app/app.component.ts',
`'@angular/core';`,
`'@angular/core';\nimport { MyType } from './type';\n`,
);
const logger = new TestLogger('unused-files-warnings');
const run = await architect.scheduleTarget(targetSpec, undefined, { logger });
const output = await run.result as BrowserBuilderOutput;
expect(output.success).toBe(true);
expect(logger.includes(warningMessageSuffix)).toBe(false);
logger.clear();
await run.stop();
});
it('should not show warning when type files are used transitively', async () => {
if (veEnabled) {
// TODO: https://github.com/angular/angular-cli/issues/15056
pending('Only supported in Ivy.');
return;
}
host.writeMultipleFiles({
'src/app/type.ts':
`import {Myinterface} from './interface'; export type MyType = Myinterface;`,
'src/app/interface.ts': 'export interface Myinterface {nbr: number;}',
});
host.replaceInFile(
'src/app/app.component.ts',
`'@angular/core';`,
`'@angular/core';\nimport { MyType } from './type';\n`,
);
const logger = new TestLogger('unused-files-warnings');
const run = await architect.scheduleTarget(targetSpec, undefined, { logger });
const output = await run.result as BrowserBuilderOutput;
expect(output.success).toBe(true);
expect(logger.includes(warningMessageSuffix)).toBe(false);
logger.clear();
await run.stop();
});
it('works for rebuilds', async () => {
if (veEnabled) {
// TODO: https://github.com/angular/angular-cli/issues/15056
pending('Only supported in Ivy.');
return;
}
host.replaceInFile(
'src/tsconfig.app.json',
'"**/*.d.ts"',
'"**/*.d.ts", "testing/**/*.ts"',
);
const logger = new TestLogger('unused-files-warnings');
let buildNumber = 0;
const run = await architect.scheduleTarget(targetSpec, { watch: true }, { logger });
await run.output
.pipe(
debounceTime(1000),
tap(buildEvent => {
expect(buildEvent.success).toBe(true);
buildNumber ++;
switch (buildNumber) {
case 1:
// The first should not have unused files
expect(logger.includes(warningMessageSuffix)).toBe(false, `Case ${buildNumber} failed.`);
// Write a used file
host.writeMultipleFiles({
'src/testing/type.ts': 'export type MyType = number;',
});
// touch file to trigger build
host.replaceInFile(
'src/app/app.component.ts',
`'@angular/core';`,
`'@angular/core';\n`,
);
break;
case 2:
// The second should have type.ts as unused
expect(logger.includes(`type.ts ${warningMessageSuffix}`)).toBe(true, `Case ${buildNumber} failed.`);
host.replaceInFile(
'src/app/app.component.ts',
`'@angular/core';`,
`'@angular/core';\nimport { MyType } from '../testing/type';`,
);
break;
case 3:
// The third should not have any unused files
expect(logger.includes(warningMessageSuffix)).toBe(false, `Case ${buildNumber} failed.`);
break;
}
logger.clear();
}),
take(3),
)
.toPromise();
await run.stop();
});
it('should only show warning once per file', async () => {
if (veEnabled) {
// TODO: https://github.com/angular/angular-cli/issues/15056
pending('Only supported in Ivy.');
return;
}
host.replaceInFile(
'src/tsconfig.app.json',
'"**/*.d.ts"',
'"**/*.d.ts", "testing/**/*.ts"',
);
// Write a used file
host.writeMultipleFiles({
'src/testing/type.ts': 'export type MyType = number;',
});
const logger = new TestLogger('unused-files-warnings');
let buildNumber = 0;
const run = await architect.scheduleTarget(targetSpec, { watch: true }, { logger });
await run.output
.pipe(
debounceTime(1000),
tap(buildEvent => {
expect(buildEvent.success).toBe(true);
buildNumber ++;
switch (buildNumber) {
case 1:
// The first should have type.ts as unused.
expect(logger.includes(`type.ts ${warningMessageSuffix}`)).toBe(true, `Case ${buildNumber} failed.`);
// touch a file to trigger a rebuild
host.appendToFile('src/main.ts', '');
break;
case 2:
// The second should should have type.ts as unused but shouldn't warn.
expect(logger.includes(warningMessageSuffix)).toBe(false, `Case ${buildNumber} failed.`);
break;
}
logger.clear();
}),
take(2),
)
.toPromise();
await run.stop();
});
});