From 289718a4844d60ff2db14c7cf3424ef678813ec5 Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Thu, 30 Nov 2023 18:11:32 -0800 Subject: [PATCH] Update lints, bump min SDK to ^3.1.0 (dart-lang/test_reflective_loader#56) --- .../.github/workflows/build.yaml | 14 +- pkgs/test_reflective_loader/CHANGELOG.md | 4 + .../analysis_options.yaml | 8 +- .../lib/test_reflective_loader.dart | 210 +++++++----------- pkgs/test_reflective_loader/pubspec.yaml | 6 +- .../test/test_reflective_loader_test.dart | 4 +- 6 files changed, 100 insertions(+), 146 deletions(-) diff --git a/pkgs/test_reflective_loader/.github/workflows/build.yaml b/pkgs/test_reflective_loader/.github/workflows/build.yaml index a07546bea..9ebdc1f3d 100644 --- a/pkgs/test_reflective_loader/.github/workflows/build.yaml +++ b/pkgs/test_reflective_loader/.github/workflows/build.yaml @@ -18,7 +18,7 @@ jobs: strategy: fail-fast: false matrix: - sdk: [dev, stable] + sdk: [dev, 3.1] steps: - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 @@ -26,14 +26,8 @@ jobs: with: sdk: ${{ matrix.sdk }} - - name: pub get - run: dart pub get - + - run: dart pub get - name: dart format run: dart format --output=none --set-exit-if-changed . - - - name: dart analyze - run: dart analyze - - - name: dart test - run: dart test + - run: dart analyze --fatal-infos + - run: dart test diff --git a/pkgs/test_reflective_loader/CHANGELOG.md b/pkgs/test_reflective_loader/CHANGELOG.md index e233fef89..5fd4c553e 100644 --- a/pkgs/test_reflective_loader/CHANGELOG.md +++ b/pkgs/test_reflective_loader/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.3-wip + +- Require Dart `^3.1.0`. + ## 0.2.2 - Update to package:lints 2.0.0 and move it to a dev dependency. diff --git a/pkgs/test_reflective_loader/analysis_options.yaml b/pkgs/test_reflective_loader/analysis_options.yaml index fc2a9d30e..ea6115827 100644 --- a/pkgs/test_reflective_loader/analysis_options.yaml +++ b/pkgs/test_reflective_loader/analysis_options.yaml @@ -1,11 +1,5 @@ -include: package:lints/recommended.yaml - -analyzer: - errors: - slash_for_doc_comments: ignore +include: package:dart_flutter_team_lints/analysis_options.yaml linter: rules: - - always_declare_return_types - - directives_ordering - public_member_api_docs diff --git a/pkgs/test_reflective_loader/lib/test_reflective_loader.dart b/pkgs/test_reflective_loader/lib/test_reflective_loader.dart index 3881cf1c2..cb69bf3ba 100644 --- a/pkgs/test_reflective_loader/lib/test_reflective_loader.dart +++ b/pkgs/test_reflective_loader/lib/test_reflective_loader.dart @@ -2,47 +2,34 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -library test_reflective_loader; - import 'dart:async'; import 'dart:mirrors'; import 'package:test/test.dart' as test_package; -/** - * A marker annotation used to annotate test methods which are expected to fail - * when asserts are enabled. - */ +/// A marker annotation used to annotate test methods which are expected to fail +/// when asserts are enabled. const Object assertFailingTest = _AssertFailingTest(); -/** - * A marker annotation used to annotate test methods which are expected to fail. - */ +/// A marker annotation used to annotate test methods which are expected to +/// fail. const Object failingTest = FailingTest(); -/** - * A marker annotation used to instruct dart2js to keep reflection information - * for the annotated classes. - */ +/// A marker annotation used to instruct dart2js to keep reflection information +/// for the annotated classes. const Object reflectiveTest = _ReflectiveTest(); -/** - * A marker annotation used to annotate test methods that should be skipped. - */ +/// A marker annotation used to annotate test methods that should be skipped. const Object skippedTest = SkippedTest(); -/** - * A marker annotation used to annotate "solo" groups and tests. - */ +/// A marker annotation used to annotate "solo" groups and tests. const Object soloTest = _SoloTest(); final List<_Group> _currentGroups = <_Group>[]; int _currentSuiteLevel = 0; String _currentSuiteName = ''; -/** - * Is `true` the application is running in the checked mode. - */ +/// Is `true` the application is running in the checked mode. final bool _isCheckedMode = () { try { assert(false); @@ -52,14 +39,12 @@ final bool _isCheckedMode = () { } }(); -/** - * Run the [define] function parameter that calls [defineReflectiveTests] to - * add normal and "solo" tests, and also calls [defineReflectiveSuite] to - * create embedded suites. If the current suite is the top-level one, perform - * check for "solo" groups and tests, and run all or only "solo" items. - */ +/// Run the [define] function parameter that calls [defineReflectiveTests] to +/// add normal and "solo" tests, and also calls [defineReflectiveSuite] to +/// create embedded suites. If the current suite is the top-level one, perform +/// check for "solo" groups and tests, and run all or only "solo" items. void defineReflectiveSuite(void Function() define, {String name = ''}) { - String groupName = _currentSuiteName; + var groupName = _currentSuiteName; _currentSuiteLevel++; try { _currentSuiteName = _combineNames(_currentSuiteName, name); @@ -71,39 +56,37 @@ void defineReflectiveSuite(void Function() define, {String name = ''}) { _addTestsIfTopLevelSuite(); } -/** - * Runs test methods existing in the given [type]. - * - * If there is a "solo" test method in the top-level suite, only "solo" methods - * are run. - * - * If there is a "solo" test type, only its test methods are run. - * - * Otherwise all tests methods of all test types are run. - * - * Each method is run with a new instance of [type]. - * So, [type] should have a default constructor. - * - * If [type] declares method `setUp`, it methods will be invoked before any test - * method invocation. - * - * If [type] declares method `tearDown`, it will be invoked after any test - * method invocation. If method returns [Future] to test some asynchronous - * behavior, then `tearDown` will be invoked in `Future.complete`. - */ +/// Runs test methods existing in the given [type]. +/// +/// If there is a "solo" test method in the top-level suite, only "solo" methods +/// are run. +/// +/// If there is a "solo" test type, only its test methods are run. +/// +/// Otherwise all tests methods of all test types are run. +/// +/// Each method is run with a new instance of [type]. +/// So, [type] should have a default constructor. +/// +/// If [type] declares method `setUp`, it methods will be invoked before any +/// test method invocation. +/// +/// If [type] declares method `tearDown`, it will be invoked after any test +/// method invocation. If method returns [Future] to test some asynchronous +/// behavior, then `tearDown` will be invoked in `Future.complete`. void defineReflectiveTests(Type type) { - ClassMirror classMirror = reflectClass(type); + var classMirror = reflectClass(type); if (!classMirror.metadata.any((InstanceMirror annotation) => annotation.type.reflectedType == _ReflectiveTest)) { - String name = MirrorSystem.getName(classMirror.qualifiedName); + var name = MirrorSystem.getName(classMirror.qualifiedName); throw Exception('Class $name must have annotation "@reflectiveTest" ' 'in order to be run by runReflectiveTests.'); } _Group group; { - bool isSolo = _hasAnnotationInstance(classMirror, soloTest); - String className = MirrorSystem.getName(classMirror.simpleName); + var isSolo = _hasAnnotationInstance(classMirror, soloTest); + var className = MirrorSystem.getName(classMirror.simpleName); group = _Group(isSolo, _combineNames(_currentSuiteName, className)); _currentGroups.add(group); } @@ -115,8 +98,8 @@ void defineReflectiveTests(Type type) { return; } // prepare information about the method - String memberName = MirrorSystem.getName(symbol); - bool isSolo = memberName.startsWith('solo_') || + var memberName = MirrorSystem.getName(symbol); + var isSolo = memberName.startsWith('solo_') || _hasAnnotationInstance(memberMirror, soloTest); // test_ if (memberName.startsWith('test_')) { @@ -162,15 +145,13 @@ void defineReflectiveTests(Type type) { _addTestsIfTopLevelSuite(); } -/** - * If the current suite is the top-level one, add tests to the `test` package. - */ +/// If the current suite is the top-level one, add tests to the `test` package. void _addTestsIfTopLevelSuite() { if (_currentSuiteLevel == 0) { void runTests({required bool allGroups, required bool allTests}) { - for (_Group group in _currentGroups) { + for (var group in _currentGroups) { if (allGroups || group.isSolo) { - for (_Test test in group.tests) { + for (var test in group.tests) { if (allTests || test.isSolo) { test_package.test(test.name, test.function, timeout: test.timeout, skip: test.isSkipped); @@ -191,10 +172,8 @@ void _addTestsIfTopLevelSuite() { } } -/** - * Return the combination of the [base] and [addition] names. - * If any other two is `null`, then the other one is returned. - */ +/// Return the combination of the [base] and [addition] names. +/// If any other two is `null`, then the other one is returned. String _combineNames(String base, String addition) { if (base.isEmpty) { return addition; @@ -206,15 +185,15 @@ String _combineNames(String base, String addition) { } Object? _getAnnotationInstance(DeclarationMirror declaration, Type type) { - for (InstanceMirror annotation in declaration.metadata) { - if (annotation.reflectee.runtimeType == type) { + for (var annotation in declaration.metadata) { + if ((annotation.reflectee as Object).runtimeType == type) { return annotation.reflectee; } } return null; } -bool _hasAnnotationInstance(DeclarationMirror declaration, instance) => +bool _hasAnnotationInstance(DeclarationMirror declaration, Object instance) => declaration.metadata.any((InstanceMirror annotation) => identical(annotation.reflectee, instance)); @@ -233,6 +212,7 @@ Future _invokeSymbolIfExists( InstanceMirror? closure; try { closure = instanceMirror.getField(symbol); + // ignore: avoid_catching_errors } on NoSuchMethodError { // ignore } @@ -243,24 +223,23 @@ Future _invokeSymbolIfExists( return Future.value(invocationResult); } -/** - * Run a test that is expected to fail, and confirm that it fails. - * - * This properly handles the following cases: - * - The test fails by throwing an exception - * - The test returns a future which completes with an error. - * - An exception is thrown to the zone handler from a timer task. - */ +/// Run a test that is expected to fail, and confirm that it fails. +/// +/// This properly handles the following cases: +/// - The test fails by throwing an exception +/// - The test returns a future which completes with an error. +/// - An exception is thrown to the zone handler from a timer task. Future? _runFailingTest(ClassMirror classMirror, Symbol symbol) { - bool passed = false; + var passed = false; return runZonedGuarded(() { // ignore: void_checks return Future.sync(() => _runTest(classMirror, symbol)).then((_) { passed = true; test_package.fail('Test passed - expected to fail.'); - }).catchError((e) { + }).catchError((Object e) { // if passed, and we call fail(), rethrow this exception if (passed) { + // ignore: only_throw_errors throw e; } // otherwise, an exception is not a failure for _runFailingTest @@ -268,71 +247,60 @@ Future? _runFailingTest(ClassMirror classMirror, Symbol symbol) { }, (e, st) { // if passed, and we call fail(), rethrow this exception if (passed) { + // ignore: only_throw_errors throw e; } // otherwise, an exception is not a failure for _runFailingTest }); } -Future _runTest(ClassMirror classMirror, Symbol symbol) { - InstanceMirror instanceMirror = classMirror.newInstance(Symbol(''), []); - return _invokeSymbolIfExists(instanceMirror, #setUp) - .then((_) => instanceMirror.invoke(symbol, []).reflectee) - .whenComplete(() => _invokeSymbolIfExists(instanceMirror, #tearDown)); +Future _runTest(ClassMirror classMirror, Symbol symbol) async { + var instanceMirror = classMirror.newInstance(const Symbol(''), []); + try { + await _invokeSymbolIfExists(instanceMirror, #setUp); + await instanceMirror.invoke(symbol, []).reflectee; + } finally { + await _invokeSymbolIfExists(instanceMirror, #tearDown); + } } typedef _TestFunction = dynamic Function(); -/** - * A marker annotation used to annotate test methods which are expected to fail. - */ +/// A marker annotation used to annotate test methods which are expected to +/// fail. class FailingTest { - /** - * Initialize this annotation with the given arguments. - * - * [issue] is a full URI describing the failure and used for tracking. - * [reason] is a free form textual description. - */ + /// Initialize this annotation with the given arguments. + /// + /// [issue] is a full URI describing the failure and used for tracking. + /// [reason] is a free form textual description. const FailingTest({String? issue, String? reason}); } -/** - * A marker annotation used to annotate test methods which are skipped. - */ +/// A marker annotation used to annotate test methods which are skipped. class SkippedTest { - /** - * Initialize this annotation with the given arguments. - * - * [issue] is a full URI describing the failure and used for tracking. - * [reason] is a free form textual description. - */ + /// Initialize this annotation with the given arguments. + /// + /// [issue] is a full URI describing the failure and used for tracking. + /// [reason] is a free form textual description. const SkippedTest({String? issue, String? reason}); } -/** - * A marker annotation used to annotate test methods with additional timeout - * information. - */ +/// A marker annotation used to annotate test methods with additional timeout +/// information. class TestTimeout { final test_package.Timeout _timeout; - /** - * Initialize this annotation with the given timeout. - */ + /// Initialize this annotation with the given timeout. const TestTimeout(test_package.Timeout timeout) : _timeout = timeout; } -/** - * A marker annotation used to annotate test methods which are expected to fail - * when asserts are enabled. - */ +/// A marker annotation used to annotate test methods which are expected to fail +/// when asserts are enabled. class _AssertFailingTest { const _AssertFailingTest(); } -/** - * Information about a type based test group. - */ +/// Information about a type based test group. class _Group { final bool isSolo; final String name; @@ -356,24 +324,18 @@ class _Group { } } -/** - * A marker annotation used to instruct dart2js to keep reflection information - * for the annotated classes. - */ +/// A marker annotation used to instruct dart2js to keep reflection information +/// for the annotated classes. class _ReflectiveTest { const _ReflectiveTest(); } -/** - * A marker annotation used to annotate "solo" groups and tests. - */ +/// A marker annotation used to annotate "solo" groups and tests. class _SoloTest { const _SoloTest(); } -/** - * Information about a test. - */ +/// Information about a test. class _Test { final bool isSolo; final String name; diff --git a/pkgs/test_reflective_loader/pubspec.yaml b/pkgs/test_reflective_loader/pubspec.yaml index e4de919ee..c30323c7c 100644 --- a/pkgs/test_reflective_loader/pubspec.yaml +++ b/pkgs/test_reflective_loader/pubspec.yaml @@ -1,13 +1,13 @@ name: test_reflective_loader -version: 0.2.2 +version: 0.2.3-wip description: Support for discovering tests and test suites using reflection. repository: https://github.com/dart-lang/test_reflective_loader environment: - sdk: '>=2.12.0 <3.0.0' + sdk: ^3.1.0 dependencies: test: ^1.16.0 dev_dependencies: - lints: ^2.0.0 + dart_flutter_team_lints: ^2.0.0 diff --git a/pkgs/test_reflective_loader/test/test_reflective_loader_test.dart b/pkgs/test_reflective_loader/test/test_reflective_loader_test.dart index ea7911f7f..fad98a5a1 100644 --- a/pkgs/test_reflective_loader/test/test_reflective_loader_test.dart +++ b/pkgs/test_reflective_loader/test/test_reflective_loader_test.dart @@ -28,7 +28,7 @@ class TestReflectiveLoaderTest { @failingTest void test_fails_throws_sync() { - throw 'foo'; + throw StateError('foo'); } @failingTest @@ -38,7 +38,7 @@ class TestReflectiveLoaderTest { @skippedTest void test_fails_but_skipped() { - throw 'foo'; + throw StateError('foo'); } @skippedTest