Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: pkg/analyzer_cli/test/driver_test.dart

Issue 1462143002: Revert "Move `analyzer_cli` into the SDK." (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analyzer_cli/test/data/test_options.yaml ('k') | pkg/analyzer_cli/test/error_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 @TestOn("vm")
6 library analyzer_cli.test.driver;
7
8 import 'dart:io';
9
10 import 'package:analyzer/plugin/options.dart';
11 import 'package:analyzer/source/analysis_options_provider.dart';
12 import 'package:analyzer/src/generated/engine.dart';
13 import 'package:analyzer/src/generated/error.dart';
14 import 'package:analyzer/src/generated/source.dart';
15 import 'package:analyzer/src/plugin/plugin_configuration.dart';
16 import 'package:analyzer/src/services/lint.dart';
17 import 'package:analyzer_cli/src/bootloader.dart';
18 import 'package:analyzer_cli/src/driver.dart';
19 import 'package:analyzer_cli/src/options.dart';
20 import 'package:path/path.dart' as path;
21 import 'package:plugin/plugin.dart';
22 import 'package:test/test.dart';
23 import 'package:yaml/src/yaml_node.dart';
24
25 main() {
26 group('Driver', () {
27 StringSink savedOutSink, savedErrorSink;
28 int savedExitCode;
29 setUp(() {
30 savedOutSink = outSink;
31 savedErrorSink = errorSink;
32 savedExitCode = exitCode;
33 outSink = new StringBuffer();
34 errorSink = new StringBuffer();
35 });
36 tearDown(() {
37 outSink = savedOutSink;
38 errorSink = savedErrorSink;
39 exitCode = savedExitCode;
40 });
41
42 group('options', () {
43 test('custom processor', () {
44 Driver driver = new Driver();
45 TestProcessor processor = new TestProcessor();
46 driver.userDefinedPlugins = [new TestPlugin(processor)];
47 driver.start([
48 '--options',
49 'test/data/test_options.yaml',
50 'test/data/test_file.dart'
51 ]);
52 expect(processor.options['test_plugin'], isNotNull);
53 expect(processor.exception, isNull);
54 });
55 });
56
57 group('exit codes', () {
58 StringSink savedOutSink, savedErrorSink;
59 int savedExitCode;
60 ExitHandler savedExitHandler;
61 setUp(() {
62 savedOutSink = outSink;
63 savedErrorSink = errorSink;
64 savedExitCode = exitCode;
65 savedExitHandler = exitHandler;
66 exitHandler = (code) => exitCode = code;
67 outSink = new StringBuffer();
68 errorSink = new StringBuffer();
69 });
70 tearDown(() {
71 outSink = savedOutSink;
72 errorSink = savedErrorSink;
73 exitCode = savedExitCode;
74 exitHandler = savedExitHandler;
75 });
76
77 test('fatal hints', () {
78 drive('test/data/file_with_hint.dart', args: ['--fatal-hints']);
79 expect(exitCode, 3);
80 });
81
82 test('not fatal hints', () {
83 drive('test/data/file_with_hint.dart');
84 expect(exitCode, 0);
85 });
86
87 test('fatal errors', () {
88 drive('test/data/file_with_error.dart');
89 expect(exitCode, 3);
90 });
91
92 test('not fatal warnings', () {
93 drive('test/data/file_with_warning.dart');
94 expect(exitCode, 0);
95 });
96
97 test('fatal warnings', () {
98 drive('test/data/file_with_warning.dart', args: ['--fatal-warnings']);
99 expect(exitCode, 3);
100 });
101
102 test('missing options file', () {
103 drive('test/data/test_file.dart', options: 'test/data/NO_OPTIONS_HERE');
104 expect(exitCode, 3);
105 });
106
107 test('missing dart file', () {
108 drive('test/data/NO_DART_FILE_HERE.dart');
109 expect(exitCode, 3);
110 });
111
112 test('part file', () {
113 drive('test/data/library_and_parts/part2.dart');
114 expect(exitCode, 3);
115 });
116
117 test('non-dangling part file', () {
118 Driver driver = new Driver();
119 driver.start([
120 'test/data/library_and_parts/lib.dart',
121 'test/data/library_and_parts/part1.dart',
122 ]);
123 expect(exitCode, 0);
124 });
125
126 test('extra part file', () {
127 Driver driver = new Driver();
128 driver.start([
129 'test/data/library_and_parts/lib.dart',
130 'test/data/library_and_parts/part1.dart',
131 'test/data/library_and_parts/part2.dart',
132 ]);
133 expect(exitCode, 3);
134 });
135 });
136
137 group('linter', () {
138 group('lints in options', () {
139 StringSink savedOutSink;
140 Driver driver;
141
142 setUp(() {
143 savedOutSink = outSink;
144 outSink = new StringBuffer();
145
146 driver = new Driver();
147 driver.start([
148 '--options',
149 'test/data/linter_project/.analysis_options',
150 '--lints',
151 'test/data/linter_project/test_file.dart'
152 ]);
153 });
154 tearDown(() {
155 outSink = savedOutSink;
156 });
157
158 test('gets analysis options', () {
159 /// Lints should be enabled.
160 expect(driver.context.analysisOptions.lint, isTrue);
161
162 /// The .analysis_options file only specifies 'camel_case_types'.
163 var lintNames = getLints(driver.context).map((r) => r.name);
164 expect(lintNames, orderedEquals(['camel_case_types']));
165 });
166
167 test('generates lints', () {
168 expect(outSink.toString(),
169 contains('[lint] Name types using UpperCamelCase.'));
170 });
171 });
172
173 group('default lints', () {
174 StringSink savedOutSink;
175 Driver driver;
176
177 setUp(() {
178 savedOutSink = outSink;
179 outSink = new StringBuffer();
180
181 driver = new Driver();
182 driver.start([
183 '--lints',
184 'test/data/linter_project/test_file.dart',
185 '--options',
186 'test/data/linter_project/.analysis_options'
187 ]);
188 });
189 tearDown(() {
190 outSink = savedOutSink;
191 });
192
193 test('gets default lints', () {
194 /// Lints should be enabled.
195 expect(driver.context.analysisOptions.lint, isTrue);
196
197 /// Default list should include camel_case_types.
198 var lintNames = getLints(driver.context).map((r) => r.name);
199 expect(lintNames, contains('camel_case_types'));
200 });
201
202 test('generates lints', () {
203 expect(outSink.toString(),
204 contains('[lint] Name types using UpperCamelCase.'));
205 });
206 });
207
208 group('no `--lints` flag (none in options)', () {
209 StringSink savedOutSink;
210 Driver driver;
211
212 setUp(() {
213 savedOutSink = outSink;
214 outSink = new StringBuffer();
215
216 driver = new Driver();
217 driver.start([
218 'test/data/no_lints_project/test_file.dart',
219 '--options',
220 'test/data/no_lints_project/.analysis_options'
221 ]);
222 });
223 tearDown(() {
224 outSink = savedOutSink;
225 });
226
227 test('lints disabled', () {
228 expect(driver.context.analysisOptions.lint, isFalse);
229 });
230
231 test('no registered lints', () {
232 expect(getLints(driver.context), isEmpty);
233 });
234
235 test('no generated warnings', () {
236 expect(outSink.toString(), contains('No issues found'));
237 });
238 });
239 });
240
241 test('containsLintRuleEntry', () {
242 Map<String, YamlNode> options;
243 options = parseOptions('''
244 linter:
245 rules:
246 - foo
247 ''');
248 expect(containsLintRuleEntry(options), true);
249 options = parseOptions('''
250 ''');
251 expect(containsLintRuleEntry(options), false);
252 options = parseOptions('''
253 linter:
254 rules:
255 # - foo
256 ''');
257 expect(containsLintRuleEntry(options), true);
258 options = parseOptions('''
259 linter:
260 # rules:
261 # - foo
262 ''');
263 expect(containsLintRuleEntry(options), false);
264 });
265
266 group('options processing', () {
267 group('error filters', () {
268 StringSink savedOutSink;
269 Driver driver;
270
271 setUp(() {
272 savedOutSink = outSink;
273 outSink = new StringBuffer();
274
275 driver = new Driver();
276 driver.start([
277 'test/data/options_tests_project/test_file.dart',
278 '--options',
279 'test/data/options_tests_project/.analysis_options'
280 ]);
281 });
282 tearDown(() {
283 outSink = savedOutSink;
284 });
285
286 test('filters', () {
287 var filters =
288 driver.context.getConfigurationData(CONFIGURED_ERROR_FILTERS);
289 expect(filters, hasLength(1));
290
291 var unused_error = new AnalysisError(
292 new TestSource(), 0, 1, HintCode.UNUSED_LOCAL_VARIABLE, [
293 ['x']
294 ]);
295 expect(filters.any((filter) => filter(unused_error)), isTrue);
296 });
297
298 test('language config', () {
299 expect(driver.context.analysisOptions.enableSuperMixins, isTrue);
300 });
301 });
302 });
303
304 group('in temp directory', () {
305 StringSink savedOutSink, savedErrorSink;
306 int savedExitCode;
307 Directory savedCurrentDirectory;
308 Directory tempDir;
309 setUp(() {
310 savedOutSink = outSink;
311 savedErrorSink = errorSink;
312 savedExitCode = exitCode;
313 outSink = new StringBuffer();
314 errorSink = new StringBuffer();
315 savedCurrentDirectory = Directory.current;
316 tempDir = Directory.systemTemp.createTempSync('analyzer_');
317 });
318 tearDown(() {
319 outSink = savedOutSink;
320 errorSink = savedErrorSink;
321 exitCode = savedExitCode;
322 Directory.current = savedCurrentDirectory;
323 tempDir.deleteSync(recursive: true);
324 });
325
326 test('packages folder', () {
327 Directory.current = tempDir;
328 new File(path.join(tempDir.path, 'test.dart')).writeAsStringSync('''
329 import 'package:foo/bar.dart';
330 main() {
331 baz();
332 }
333 ''');
334 Directory packagesDir =
335 new Directory(path.join(tempDir.path, 'packages'));
336 packagesDir.createSync();
337 Directory fooDir = new Directory(path.join(packagesDir.path, 'foo'));
338 fooDir.createSync();
339 new File(path.join(fooDir.path, 'bar.dart')).writeAsStringSync('''
340 void baz() {}
341 ''');
342 new Driver().start(['test.dart']);
343 expect(exitCode, 0);
344 });
345
346 test('no package resolution', () {
347 Directory.current = tempDir;
348 new File(path.join(tempDir.path, 'test.dart')).writeAsStringSync('''
349 import 'package:path/path.dart';
350 main() {}
351 ''');
352 new Driver().start(['test.dart']);
353 expect(exitCode, 3);
354 String stdout = outSink.toString();
355 expect(stdout, contains('[error] Target of URI does not exist'));
356 expect(stdout, contains('1 error found.'));
357 expect(errorSink.toString(), '');
358 });
359
360 test('bad package root', () {
361 new Driver().start(['--package-root', 'does/not/exist', 'test.dart']);
362 String stdout = outSink.toString();
363 expect(exitCode, 3);
364 expect(
365 stdout,
366 contains(
367 'Package root directory (does/not/exist) does not exist.'));
368 });
369 });
370 });
371 group('Bootloader', () {
372 group('plugin processing', () {
373 StringSink savedErrorSink;
374 setUp(() {
375 savedErrorSink = errorSink;
376 errorSink = new StringBuffer();
377 });
378 tearDown(() {
379 errorSink = savedErrorSink;
380 });
381 test('bad format', () {
382 BootLoader loader = new BootLoader();
383 loader.createImage([
384 '--options',
385 'test/data/bad_plugin_options.yaml',
386 'test/data/test_file.dart'
387 ]);
388 expect(
389 errorSink.toString(),
390 equals('Plugin configuration skipped: Unrecognized plugin config '
391 'format, expected `YamlMap`, got `YamlList` '
392 '(line 2, column 4)\n'));
393 });
394 test('plugin config', () {
395 BootLoader loader = new BootLoader();
396 Image image = loader.createImage([
397 '--options',
398 'test/data/plugin_options.yaml',
399 'test/data/test_file.dart'
400 ]);
401 var plugins = image.config.plugins;
402 expect(plugins, hasLength(1));
403 expect(plugins.first.name, equals('my_plugin1'));
404 });
405 group('plugin validation', () {
406 test('requires class name', () {
407 expect(
408 validate(new PluginInfo(
409 name: 'test_plugin', libraryUri: 'my_package/foo.dart')),
410 isNotNull);
411 });
412 test('requires library URI', () {
413 expect(
414 validate(
415 new PluginInfo(name: 'test_plugin', className: 'MyPlugin')),
416 isNotNull);
417 });
418 test('check', () {
419 expect(
420 validate(new PluginInfo(
421 name: 'test_plugin',
422 className: 'MyPlugin',
423 libraryUri: 'my_package/foo.dart')),
424 isNull);
425 });
426 });
427 });
428 });
429 }
430
431 const emptyOptionsFile = 'test/data/empty_options.yaml';
432
433 /// Start a driver for the given [source], optionally providing additional
434 /// [args] and an [options] file path. The value of [options] defaults to
435 /// an empty options file to avoid unwanted configuration from an otherwise
436 /// discovered options file.
437 void drive(String source,
438 {String options: emptyOptionsFile,
439 List<String> args: const <String>[]}) =>
440 new Driver().start(['--options', options, source]..addAll(args));
441
442 Map<String, YamlNode> parseOptions(String src) =>
443 new AnalysisOptionsProvider().getOptionsFromString(src);
444
445 class TestPlugin extends Plugin {
446 TestProcessor processor;
447 TestPlugin(this.processor);
448
449 @override
450 String get uniqueIdentifier => 'test_plugin.core';
451
452 @override
453 void registerExtensionPoints(RegisterExtensionPoint register) {
454 // None
455 }
456
457 @override
458 void registerExtensions(RegisterExtension register) {
459 register(OPTIONS_PROCESSOR_EXTENSION_POINT_ID, processor);
460 }
461 }
462
463 class TestProcessor extends OptionsProcessor {
464 Map<String, YamlNode> options;
465 Exception exception;
466
467 @override
468 void onError(Exception exception) {
469 this.exception = exception;
470 }
471
472 @override
473 void optionsProcessed(
474 AnalysisContext context, Map<String, YamlNode> options) {
475 this.options = options;
476 }
477 }
478
479 class TestSource implements Source {
480 TestSource();
481
482 @override
483 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
484 }
OLDNEW
« no previous file with comments | « pkg/analyzer_cli/test/data/test_options.yaml ('k') | pkg/analyzer_cli/test/error_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698