| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 library analyzer.test.src.context.context_builder_test; | 5 library analyzer.test.src.context.context_builder_test; |
| 6 | 6 |
| 7 import 'package:analyzer/file_system/file_system.dart'; | 7 import 'package:analyzer/file_system/file_system.dart'; |
| 8 import 'package:analyzer/file_system/memory_file_system.dart'; | 8 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 9 import 'package:analyzer/plugin/options.dart'; | |
| 10 import 'package:analyzer/src/context/builder.dart'; | 9 import 'package:analyzer/src/context/builder.dart'; |
| 11 import 'package:analyzer/src/context/source.dart'; | 10 import 'package:analyzer/src/context/source.dart'; |
| 12 import 'package:analyzer/src/generated/bazel.dart'; | 11 import 'package:analyzer/src/generated/bazel.dart'; |
| 13 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
| 14 import 'package:analyzer/src/generated/sdk.dart'; | 13 import 'package:analyzer/src/generated/sdk.dart'; |
| 15 import 'package:analyzer/src/generated/source.dart'; | 14 import 'package:analyzer/src/generated/source.dart'; |
| 16 import 'package:analyzer/src/plugin/options_plugin.dart'; | |
| 17 import 'package:package_config/packages.dart'; | 15 import 'package:package_config/packages.dart'; |
| 18 import 'package:package_config/src/packages_impl.dart'; | 16 import 'package:package_config/src/packages_impl.dart'; |
| 19 import 'package:path/path.dart' as path; | 17 import 'package:path/path.dart' as path; |
| 20 import 'package:plugin/src/plugin_impl.dart'; | |
| 21 import 'package:test/test.dart'; | 18 import 'package:test/test.dart'; |
| 22 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 19 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 23 | 20 |
| 24 import '../../embedder_tests.dart'; | 21 import '../../embedder_tests.dart'; |
| 25 import '../../generated/test_support.dart'; | 22 import '../../generated/test_support.dart'; |
| 26 import 'mock_sdk.dart'; | 23 import 'mock_sdk.dart'; |
| 27 | 24 |
| 28 main() { | 25 main() { |
| 29 defineReflectiveSuite(() { | 26 defineReflectiveSuite(() { |
| 30 defineReflectiveTests(ContextBuilderTest); | 27 defineReflectiveTests(ContextBuilderTest); |
| (...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 String filePath = | 503 String filePath = |
| 507 pathContext.join(path, AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); | 504 pathContext.join(path, AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); |
| 508 resourceProvider.newFile( | 505 resourceProvider.newFile( |
| 509 filePath, | 506 filePath, |
| 510 ''' | 507 ''' |
| 511 linter: | 508 linter: |
| 512 rules: | 509 rules: |
| 513 - empty_constructor_bodies | 510 - empty_constructor_bodies |
| 514 '''); | 511 '''); |
| 515 | 512 |
| 516 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext(); | 513 AnalysisOptions options = builder.getAnalysisOptions(path); |
| 517 AnalysisOptions options = builder.getAnalysisOptions(context, path); | |
| 518 _expectEqualOptions(options, expected); | 514 _expectEqualOptions(options, expected); |
| 519 } | 515 } |
| 520 | 516 |
| 521 void test_getAnalysisOptions_default_overrides() { | 517 void test_getAnalysisOptions_default_overrides() { |
| 522 AnalysisOptionsImpl defaultOptions = new AnalysisOptionsImpl(); | 518 AnalysisOptionsImpl defaultOptions = new AnalysisOptionsImpl(); |
| 523 defaultOptions.enableLazyAssignmentOperators = true; | 519 defaultOptions.enableLazyAssignmentOperators = true; |
| 524 builderOptions.defaultOptions = defaultOptions; | 520 builderOptions.defaultOptions = defaultOptions; |
| 525 AnalysisOptionsImpl expected = new AnalysisOptionsImpl(); | 521 AnalysisOptionsImpl expected = new AnalysisOptionsImpl(); |
| 526 expected.enableSuperMixins = true; | 522 expected.enableSuperMixins = true; |
| 527 expected.enableLazyAssignmentOperators = true; | 523 expected.enableLazyAssignmentOperators = true; |
| 528 String path = resourceProvider.convertPath('/some/directory/path'); | 524 String path = resourceProvider.convertPath('/some/directory/path'); |
| 529 String filePath = | 525 String filePath = |
| 530 pathContext.join(path, AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); | 526 pathContext.join(path, AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); |
| 531 resourceProvider.newFile( | 527 resourceProvider.newFile( |
| 532 filePath, | 528 filePath, |
| 533 ''' | 529 ''' |
| 534 analyzer: | 530 analyzer: |
| 535 language: | 531 language: |
| 536 enableSuperMixins : true | 532 enableSuperMixins : true |
| 537 '''); | 533 '''); |
| 538 | 534 |
| 539 AnalysisEngine engine = AnalysisEngine.instance; | 535 AnalysisOptions options = builder.getAnalysisOptions(path); |
| 540 OptionsPlugin plugin = engine.optionsPlugin; | 536 _expectEqualOptions(options, expected); |
| 541 plugin.registerExtensionPoints((_) {}); | |
| 542 try { | |
| 543 _TestOptionsProcessor processor = new _TestOptionsProcessor(); | |
| 544 processor.expectedOptions = <String, Object>{ | |
| 545 'analyzer': { | |
| 546 'language': {'enableSuperMixins': true} | |
| 547 } | |
| 548 }; | |
| 549 (plugin.optionsProcessorExtensionPoint as ExtensionPointImpl) | |
| 550 .add(processor); | |
| 551 AnalysisContext context = engine.createAnalysisContext(); | |
| 552 AnalysisOptions options = builder.getAnalysisOptions(context, path); | |
| 553 _expectEqualOptions(options, expected); | |
| 554 } finally { | |
| 555 plugin.registerExtensionPoints((_) {}); | |
| 556 } | |
| 557 } | 537 } |
| 558 | 538 |
| 559 void test_getAnalysisOptions_includes() { | 539 void test_getAnalysisOptions_includes() { |
| 560 AnalysisOptionsImpl defaultOptions = new AnalysisOptionsImpl(); | 540 AnalysisOptionsImpl defaultOptions = new AnalysisOptionsImpl(); |
| 561 builderOptions.defaultOptions = defaultOptions; | 541 builderOptions.defaultOptions = defaultOptions; |
| 562 AnalysisOptionsImpl expected = new AnalysisOptionsImpl(); | 542 AnalysisOptionsImpl expected = new AnalysisOptionsImpl(); |
| 563 expected.enableSuperMixins = true; | 543 expected.enableSuperMixins = true; |
| 564 resourceProvider.newFile( | 544 resourceProvider.newFile( |
| 565 resourceProvider.convertPath('/mypkgs/somepkg/lib/here.yaml'), | 545 resourceProvider.convertPath('/mypkgs/somepkg/lib/here.yaml'), |
| 566 ''' | 546 ''' |
| (...skipping 15 matching lines...) Expand all Loading... |
| 582 pathContext.join(path, AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); | 562 pathContext.join(path, AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); |
| 583 resourceProvider.newFile( | 563 resourceProvider.newFile( |
| 584 filePath, | 564 filePath, |
| 585 ''' | 565 ''' |
| 586 include: bar.yaml | 566 include: bar.yaml |
| 587 analyzer: | 567 analyzer: |
| 588 language: | 568 language: |
| 589 enableSuperMixins : true | 569 enableSuperMixins : true |
| 590 '''); | 570 '''); |
| 591 | 571 |
| 592 AnalysisEngine engine = AnalysisEngine.instance; | 572 AnalysisOptions options = builder.getAnalysisOptions(path); |
| 593 OptionsPlugin plugin = engine.optionsPlugin; | 573 _expectEqualOptions(options, expected); |
| 594 plugin.registerExtensionPoints((_) {}); | |
| 595 try { | |
| 596 _TestOptionsProcessor processor = new _TestOptionsProcessor(); | |
| 597 processor.expectedOptions = <String, Object>{ | |
| 598 'analyzer': { | |
| 599 'language': {'enableSuperMixins': true} | |
| 600 }, | |
| 601 'foo': {'bar': 'baz'}, | |
| 602 'two': {'boo': 'newt'}, | |
| 603 }; | |
| 604 (plugin.optionsProcessorExtensionPoint as ExtensionPointImpl) | |
| 605 .add(processor); | |
| 606 AnalysisContext context = engine.createAnalysisContext(); | |
| 607 AnalysisOptions options = builder.getAnalysisOptions(context, path); | |
| 608 _expectEqualOptions(options, expected); | |
| 609 } finally { | |
| 610 plugin.registerExtensionPoints((_) {}); | |
| 611 } | |
| 612 } | 574 } |
| 613 | 575 |
| 614 void test_getAnalysisOptions_invalid() { | 576 void test_getAnalysisOptions_invalid() { |
| 615 String path = resourceProvider.convertPath('/some/directory/path'); | 577 String path = resourceProvider.convertPath('/some/directory/path'); |
| 616 String filePath = | 578 String filePath = |
| 617 pathContext.join(path, AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); | 579 pathContext.join(path, AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); |
| 618 resourceProvider.newFile(filePath, ';'); | 580 resourceProvider.newFile(filePath, ';'); |
| 619 | 581 |
| 620 AnalysisEngine engine = AnalysisEngine.instance; | 582 AnalysisOptions options = builder.getAnalysisOptions(path); |
| 621 OptionsPlugin plugin = engine.optionsPlugin; | 583 expect(options, isNotNull); |
| 622 plugin.registerExtensionPoints((_) {}); | |
| 623 try { | |
| 624 _TestOptionsProcessor processor = new _TestOptionsProcessor(); | |
| 625 (plugin.optionsProcessorExtensionPoint as ExtensionPointImpl) | |
| 626 .add(processor); | |
| 627 AnalysisContext context = engine.createAnalysisContext(); | |
| 628 AnalysisOptions options = builder.getAnalysisOptions(context, path); | |
| 629 expect(options, isNotNull); | |
| 630 expect(processor.errorCount, 1); | |
| 631 } finally { | |
| 632 plugin.registerExtensionPoints((_) {}); | |
| 633 } | |
| 634 } | 584 } |
| 635 | 585 |
| 636 void test_getAnalysisOptions_noDefault_noOverrides() { | 586 void test_getAnalysisOptions_noDefault_noOverrides() { |
| 637 String path = resourceProvider.convertPath('/some/directory/path'); | 587 String path = resourceProvider.convertPath('/some/directory/path'); |
| 638 String filePath = | 588 String filePath = |
| 639 pathContext.join(path, AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); | 589 pathContext.join(path, AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); |
| 640 resourceProvider.newFile( | 590 resourceProvider.newFile( |
| 641 filePath, | 591 filePath, |
| 642 ''' | 592 ''' |
| 643 linter: | 593 linter: |
| 644 rules: | 594 rules: |
| 645 - empty_constructor_bodies | 595 - empty_constructor_bodies |
| 646 '''); | 596 '''); |
| 647 | 597 |
| 648 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext(); | 598 AnalysisOptions options = builder.getAnalysisOptions(path); |
| 649 AnalysisOptions options = builder.getAnalysisOptions(context, path); | |
| 650 _expectEqualOptions(options, new AnalysisOptionsImpl()); | 599 _expectEqualOptions(options, new AnalysisOptionsImpl()); |
| 651 } | 600 } |
| 652 | 601 |
| 653 void test_getAnalysisOptions_noDefault_overrides() { | 602 void test_getAnalysisOptions_noDefault_overrides() { |
| 654 AnalysisOptionsImpl expected = new AnalysisOptionsImpl(); | 603 AnalysisOptionsImpl expected = new AnalysisOptionsImpl(); |
| 655 expected.enableSuperMixins = true; | 604 expected.enableSuperMixins = true; |
| 656 String path = resourceProvider.convertPath('/some/directory/path'); | 605 String path = resourceProvider.convertPath('/some/directory/path'); |
| 657 String filePath = | 606 String filePath = |
| 658 pathContext.join(path, AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); | 607 pathContext.join(path, AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE); |
| 659 resourceProvider.newFile( | 608 resourceProvider.newFile( |
| 660 filePath, | 609 filePath, |
| 661 ''' | 610 ''' |
| 662 analyzer: | 611 analyzer: |
| 663 language: | 612 language: |
| 664 enableSuperMixins : true | 613 enableSuperMixins : true |
| 665 '''); | 614 '''); |
| 666 | 615 |
| 667 AnalysisContext context = AnalysisEngine.instance.createAnalysisContext(); | 616 AnalysisOptions options = builder.getAnalysisOptions(path); |
| 668 AnalysisOptions options = builder.getAnalysisOptions(context, path); | |
| 669 _expectEqualOptions(options, expected); | 617 _expectEqualOptions(options, expected); |
| 670 } | 618 } |
| 671 | 619 |
| 672 void test_getOptionsFile_explicit() { | 620 void test_getOptionsFile_explicit() { |
| 673 String path = resourceProvider.convertPath('/some/directory/path'); | 621 String path = resourceProvider.convertPath('/some/directory/path'); |
| 674 String filePath = resourceProvider.convertPath('/options/analysis.yaml'); | 622 String filePath = resourceProvider.convertPath('/options/analysis.yaml'); |
| 675 resourceProvider.newFile(filePath, ''); | 623 resourceProvider.newFile(filePath, ''); |
| 676 | 624 |
| 677 builderOptions.defaultAnalysisOptionsFilePath = filePath; | 625 builderOptions.defaultAnalysisOptionsFilePath = filePath; |
| 678 File result = builder.getOptionsFile(path); | 626 File result = builder.getOptionsFile(path); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 776 expect(locator.embedderYamls, hasLength(0)); | 724 expect(locator.embedderYamls, hasLength(0)); |
| 777 } | 725 } |
| 778 | 726 |
| 779 void test_valid() { | 727 void test_valid() { |
| 780 EmbedderYamlLocator locator = new EmbedderYamlLocator({ | 728 EmbedderYamlLocator locator = new EmbedderYamlLocator({ |
| 781 'fox': <Folder>[pathTranslator.getResource(foxLib)] | 729 'fox': <Folder>[pathTranslator.getResource(foxLib)] |
| 782 }); | 730 }); |
| 783 expect(locator.embedderYamls, hasLength(1)); | 731 expect(locator.embedderYamls, hasLength(1)); |
| 784 } | 732 } |
| 785 } | 733 } |
| 786 | |
| 787 class _TestOptionsProcessor implements OptionsProcessor { | |
| 788 Map<String, Object> expectedOptions = null; | |
| 789 | |
| 790 int errorCount = 0; | |
| 791 | |
| 792 @override | |
| 793 void onError(Exception exception) { | |
| 794 errorCount++; | |
| 795 } | |
| 796 | |
| 797 @override | |
| 798 void optionsProcessed(AnalysisContext context, Map<String, Object> options) { | |
| 799 if (expectedOptions == null) { | |
| 800 fail('Unexpected invocation of optionsProcessed'); | |
| 801 } | |
| 802 expect(options, hasLength(expectedOptions.length)); | |
| 803 for (String key in expectedOptions.keys) { | |
| 804 expect(options.containsKey(key), isTrue, reason: 'missing key $key'); | |
| 805 expect(options[key], expectedOptions[key], | |
| 806 reason: 'values for key $key do not match'); | |
| 807 } | |
| 808 } | |
| 809 } | |
| OLD | NEW |