| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 @TestOn("vm") | 5 @TestOn("vm") |
| 6 library analyzer_cli.test.driver; | 6 library analyzer_cli.test.driver; |
| 7 | 7 |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:analyzer/plugin/options.dart'; | 10 import 'package:analyzer/plugin/options.dart'; |
| 11 import 'package:analyzer/source/analysis_options_provider.dart'; | 11 import 'package:analyzer/source/analysis_options_provider.dart'; |
| 12 import 'package:analyzer/src/generated/engine.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'; |
| 13 import 'package:analyzer/src/plugin/plugin_configuration.dart'; | 15 import 'package:analyzer/src/plugin/plugin_configuration.dart'; |
| 14 import 'package:analyzer/src/services/lint.dart'; | 16 import 'package:analyzer/src/services/lint.dart'; |
| 15 import 'package:analyzer_cli/src/bootloader.dart'; | 17 import 'package:analyzer_cli/src/bootloader.dart'; |
| 16 import 'package:analyzer_cli/src/driver.dart'; | 18 import 'package:analyzer_cli/src/driver.dart'; |
| 17 import 'package:analyzer_cli/src/options.dart'; | 19 import 'package:analyzer_cli/src/options.dart'; |
| 18 import 'package:path/path.dart' as path; | 20 import 'package:path/path.dart' as path; |
| 19 import 'package:plugin/plugin.dart'; | 21 import 'package:plugin/plugin.dart'; |
| 20 import 'package:test/test.dart'; | 22 import 'package:test/test.dart'; |
| 21 import 'package:yaml/src/yaml_node.dart'; | 23 import 'package:yaml/src/yaml_node.dart'; |
| 22 | 24 |
| 23 main() { | 25 main() { |
| 24 group('Driver', () { | 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 |
| 25 group('options', () { | 42 group('options', () { |
| 26 test('custom processor', () { | 43 test('custom processor', () { |
| 27 Driver driver = new Driver(); | 44 Driver driver = new Driver(); |
| 28 TestProcessor processor = new TestProcessor(); | 45 TestProcessor processor = new TestProcessor(); |
| 29 driver.userDefinedPlugins = [new TestPlugin(processor)]; | 46 driver.userDefinedPlugins = [new TestPlugin(processor)]; |
| 30 driver.start([ | 47 driver.start([ |
| 31 '--options', | 48 '--options', |
| 32 'test/data/test_options.yaml', | 49 'test/data/test_options.yaml', |
| 33 'test/data/test_file.dart' | 50 'test/data/test_file.dart' |
| 34 ]); | 51 ]); |
| 35 expect(processor.options['test_plugin'], isNotNull); | 52 expect(processor.options['test_plugin'], isNotNull); |
| 36 expect(processor.exception, isNull); | 53 expect(processor.exception, isNull); |
| 37 }); | 54 }); |
| 38 }); | 55 }); |
| 39 | 56 |
| 40 group('exit codes', () { | 57 group('exit codes', () { |
| 58 StringSink savedOutSink, savedErrorSink; |
| 41 int savedExitCode; | 59 int savedExitCode; |
| 42 ExitHandler savedExitHandler; | 60 ExitHandler savedExitHandler; |
| 43 setUp(() { | 61 setUp(() { |
| 62 savedOutSink = outSink; |
| 63 savedErrorSink = errorSink; |
| 44 savedExitCode = exitCode; | 64 savedExitCode = exitCode; |
| 45 savedExitHandler = exitHandler; | 65 savedExitHandler = exitHandler; |
| 46 exitHandler = (code) => exitCode = code; | 66 exitHandler = (code) => exitCode = code; |
| 67 outSink = new StringBuffer(); |
| 68 errorSink = new StringBuffer(); |
| 47 }); | 69 }); |
| 48 tearDown(() { | 70 tearDown(() { |
| 71 outSink = savedOutSink; |
| 72 errorSink = savedErrorSink; |
| 49 exitCode = savedExitCode; | 73 exitCode = savedExitCode; |
| 50 exitHandler = savedExitHandler; | 74 exitHandler = savedExitHandler; |
| 51 }); | 75 }); |
| 52 | 76 |
| 53 test('fatal hints', () { | 77 test('fatal hints', () { |
| 54 Driver driver = new Driver(); | 78 drive('test/data/file_with_hint.dart', args: ['--fatal-hints']); |
| 55 driver.start(['--fatal-hints', 'test/data/file_with_hint.dart']); | |
| 56 expect(exitCode, 3); | 79 expect(exitCode, 3); |
| 57 }); | 80 }); |
| 58 | 81 |
| 59 test('not fatal hints', () { | 82 test('not fatal hints', () { |
| 60 Driver driver = new Driver(); | 83 drive('test/data/file_with_hint.dart'); |
| 61 driver.start(['test/data/file_with_hint.dart']); | |
| 62 expect(exitCode, 0); | 84 expect(exitCode, 0); |
| 63 }); | 85 }); |
| 64 | 86 |
| 65 test('fatal errors', () { | 87 test('fatal errors', () { |
| 66 Driver driver = new Driver(); | 88 drive('test/data/file_with_error.dart'); |
| 67 driver.start(['test/data/file_with_error.dart']); | |
| 68 expect(exitCode, 3); | 89 expect(exitCode, 3); |
| 69 }); | 90 }); |
| 70 | 91 |
| 71 test('not fatal warnings', () { | 92 test('not fatal warnings', () { |
| 72 Driver driver = new Driver(); | 93 drive('test/data/file_with_warning.dart'); |
| 73 driver.start(['test/data/file_with_warning.dart']); | |
| 74 expect(exitCode, 0); | 94 expect(exitCode, 0); |
| 75 }); | 95 }); |
| 76 | 96 |
| 77 test('fatal warnings', () { | 97 test('fatal warnings', () { |
| 78 Driver driver = new Driver(); | 98 drive('test/data/file_with_warning.dart', args: ['--fatal-warnings']); |
| 79 driver.start(['--fatal-warnings', 'test/data/file_with_warning.dart']); | |
| 80 expect(exitCode, 3); | 99 expect(exitCode, 3); |
| 81 }); | 100 }); |
| 82 | 101 |
| 83 test('missing options file', () { | 102 test('missing options file', () { |
| 84 Driver driver = new Driver(); | 103 drive('test/data/test_file.dart', options: 'test/data/NO_OPTIONS_HERE'); |
| 85 driver.start([ | |
| 86 '--options', | |
| 87 'test/data/NO_OPTIONS_HERE', | |
| 88 'test/data/test_file.dart' | |
| 89 ]); | |
| 90 expect(exitCode, 3); | 104 expect(exitCode, 3); |
| 91 }); | 105 }); |
| 92 | 106 |
| 93 test('missing dart file', () { | 107 test('missing dart file', () { |
| 94 Driver driver = new Driver(); | 108 drive('test/data/NO_DART_FILE_HERE.dart'); |
| 95 driver.start(['test/data/NO_DART_FILE_HERE.dart']); | |
| 96 expect(exitCode, 3); | 109 expect(exitCode, 3); |
| 97 }); | 110 }); |
| 98 | 111 |
| 99 test('part file', () { | 112 test('part file', () { |
| 100 Driver driver = new Driver(); | 113 drive('test/data/library_and_parts/part2.dart'); |
| 101 driver.start(['test/data/library_and_parts/part2.dart']); | |
| 102 expect(exitCode, 3); | 114 expect(exitCode, 3); |
| 103 }); | 115 }); |
| 104 | 116 |
| 105 test('non-dangling part file', () { | 117 test('non-dangling part file', () { |
| 106 Driver driver = new Driver(); | 118 Driver driver = new Driver(); |
| 107 driver.start([ | 119 driver.start([ |
| 108 'test/data/library_and_parts/lib.dart', | 120 'test/data/library_and_parts/lib.dart', |
| 109 'test/data/library_and_parts/part1.dart', | 121 'test/data/library_and_parts/part1.dart', |
| 110 ]); | 122 ]); |
| 111 expect(exitCode, 0); | 123 expect(exitCode, 0); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 | 172 |
| 161 group('default lints', () { | 173 group('default lints', () { |
| 162 StringSink savedOutSink; | 174 StringSink savedOutSink; |
| 163 Driver driver; | 175 Driver driver; |
| 164 | 176 |
| 165 setUp(() { | 177 setUp(() { |
| 166 savedOutSink = outSink; | 178 savedOutSink = outSink; |
| 167 outSink = new StringBuffer(); | 179 outSink = new StringBuffer(); |
| 168 | 180 |
| 169 driver = new Driver(); | 181 driver = new Driver(); |
| 170 driver.start(['--lints', 'test/data/linter_project/test_file.dart']); | 182 driver.start([ |
| 183 '--lints', |
| 184 'test/data/linter_project/test_file.dart', |
| 185 '--options', |
| 186 'test/data/linter_project/.analysis_options' |
| 187 ]); |
| 171 }); | 188 }); |
| 172 tearDown(() { | 189 tearDown(() { |
| 173 outSink = savedOutSink; | 190 outSink = savedOutSink; |
| 174 }); | 191 }); |
| 175 | 192 |
| 176 test('gets default lints', () { | 193 test('gets default lints', () { |
| 177 /// Lints should be enabled. | 194 /// Lints should be enabled. |
| 178 expect(driver.context.analysisOptions.lint, isTrue); | 195 expect(driver.context.analysisOptions.lint, isTrue); |
| 179 | 196 |
| 180 /// Default list should include camel_case_types. | 197 /// Default list should include camel_case_types. |
| 181 var lintNames = getLints(driver.context).map((r) => r.name); | 198 var lintNames = getLints(driver.context).map((r) => r.name); |
| 182 expect(lintNames, contains('camel_case_types')); | 199 expect(lintNames, contains('camel_case_types')); |
| 183 }); | 200 }); |
| 184 | 201 |
| 185 test('generates lints', () { | 202 test('generates lints', () { |
| 186 expect(outSink.toString(), | 203 expect(outSink.toString(), |
| 187 contains('[lint] Name types using UpperCamelCase.')); | 204 contains('[lint] Name types using UpperCamelCase.')); |
| 188 }); | 205 }); |
| 189 }); | 206 }); |
| 190 | 207 |
| 191 group('no `--lints` flag', () { | 208 group('no `--lints` flag (none in options)', () { |
| 192 StringSink savedOutSink; | 209 StringSink savedOutSink; |
| 193 Driver driver; | 210 Driver driver; |
| 194 | 211 |
| 195 setUp(() { | 212 setUp(() { |
| 196 savedOutSink = outSink; | 213 savedOutSink = outSink; |
| 197 outSink = new StringBuffer(); | 214 outSink = new StringBuffer(); |
| 198 | 215 |
| 199 driver = new Driver(); | 216 driver = new Driver(); |
| 200 driver.start(['test/data/linter_project/test_file.dart']); | 217 driver.start([ |
| 218 'test/data/no_lints_project/test_file.dart', |
| 219 '--options', |
| 220 'test/data/no_lints_project/.analysis_options' |
| 221 ]); |
| 201 }); | 222 }); |
| 202 tearDown(() { | 223 tearDown(() { |
| 203 outSink = savedOutSink; | 224 outSink = savedOutSink; |
| 204 }); | 225 }); |
| 205 | 226 |
| 206 test('lints disabled', () { | 227 test('lints disabled', () { |
| 207 expect(driver.context.analysisOptions.lint, isFalse); | 228 expect(driver.context.analysisOptions.lint, isFalse); |
| 208 }); | 229 }); |
| 209 | 230 |
| 210 test('no registered lints', () { | 231 test('no registered lints', () { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 235 '''); | 256 '''); |
| 236 expect(containsLintRuleEntry(options), true); | 257 expect(containsLintRuleEntry(options), true); |
| 237 options = parseOptions(''' | 258 options = parseOptions(''' |
| 238 linter: | 259 linter: |
| 239 # rules: | 260 # rules: |
| 240 # - foo | 261 # - foo |
| 241 '''); | 262 '''); |
| 242 expect(containsLintRuleEntry(options), false); | 263 expect(containsLintRuleEntry(options), false); |
| 243 }); | 264 }); |
| 244 | 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 |
| 245 group('in temp directory', () { | 304 group('in temp directory', () { |
| 246 StringSink savedOutSink, savedErrorSink; | 305 StringSink savedOutSink, savedErrorSink; |
| 247 int savedExitCode; | 306 int savedExitCode; |
| 248 Directory savedCurrentDirectory; | 307 Directory savedCurrentDirectory; |
| 249 Directory tempDir; | 308 Directory tempDir; |
| 250 setUp(() { | 309 setUp(() { |
| 251 savedOutSink = outSink; | 310 savedOutSink = outSink; |
| 252 savedErrorSink = errorSink; | 311 savedErrorSink = errorSink; |
| 253 savedExitCode = exitCode; | 312 savedExitCode = exitCode; |
| 254 outSink = new StringBuffer(); | 313 outSink = new StringBuffer(); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 name: 'test_plugin', | 421 name: 'test_plugin', |
| 363 className: 'MyPlugin', | 422 className: 'MyPlugin', |
| 364 libraryUri: 'my_package/foo.dart')), | 423 libraryUri: 'my_package/foo.dart')), |
| 365 isNull); | 424 isNull); |
| 366 }); | 425 }); |
| 367 }); | 426 }); |
| 368 }); | 427 }); |
| 369 }); | 428 }); |
| 370 } | 429 } |
| 371 | 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 |
| 372 Map<String, YamlNode> parseOptions(String src) => | 442 Map<String, YamlNode> parseOptions(String src) => |
| 373 new AnalysisOptionsProvider().getOptionsFromString(src); | 443 new AnalysisOptionsProvider().getOptionsFromString(src); |
| 374 | 444 |
| 375 class TestPlugin extends Plugin { | 445 class TestPlugin extends Plugin { |
| 376 TestProcessor processor; | 446 TestProcessor processor; |
| 377 TestPlugin(this.processor); | 447 TestPlugin(this.processor); |
| 378 | 448 |
| 379 @override | 449 @override |
| 380 String get uniqueIdentifier => 'test_plugin.core'; | 450 String get uniqueIdentifier => 'test_plugin.core'; |
| 381 | 451 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 398 void onError(Exception exception) { | 468 void onError(Exception exception) { |
| 399 this.exception = exception; | 469 this.exception = exception; |
| 400 } | 470 } |
| 401 | 471 |
| 402 @override | 472 @override |
| 403 void optionsProcessed( | 473 void optionsProcessed( |
| 404 AnalysisContext context, Map<String, YamlNode> options) { | 474 AnalysisContext context, Map<String, YamlNode> options) { |
| 405 this.options = options; | 475 this.options = options; |
| 406 } | 476 } |
| 407 } | 477 } |
| 478 |
| 479 class TestSource implements Source { |
| 480 TestSource(); |
| 481 |
| 482 @override |
| 483 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 484 } |
| OLD | NEW |