From dc6cd15fe87c149567163eff8c1b052e876c33ba Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Fri, 19 May 2023 10:37:23 +0200 Subject: [PATCH] Bring back Hot Restart (#1286) * fix Hot Restart * DevelopCommand: remove `TestBundler` argument * delete PATROL_INTEGRATION_TEST_SHOULD_REPORT_RESULTS_TO_NATIVE, which is not used anywhere * delete patrolChannel (not used anymore) * patrol_cli: bump Dart constraint in pubspec.lock --- packages/patrol/lib/src/binding.dart | 40 +++++-------------- packages/patrol/lib/src/common.dart | 4 +- packages/patrol/lib/src/constants.dart | 5 +++ .../patrol_cli/lib/src/commands/develop.dart | 15 +++---- .../lib/src/runner/patrol_command_runner.dart | 1 - packages/patrol_cli/pubspec.lock | 2 +- 6 files changed, 24 insertions(+), 43 deletions(-) create mode 100644 packages/patrol/lib/src/constants.dart diff --git a/packages/patrol/lib/src/binding.dart b/packages/patrol/lib/src/binding.dart index 476c61ac9..378f8bbb1 100644 --- a/packages/patrol/lib/src/binding.dart +++ b/packages/patrol/lib/src/binding.dart @@ -4,7 +4,6 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/common.dart'; import 'package:integration_test/integration_test.dart'; @@ -14,6 +13,8 @@ import 'package:test_api/src/backend/invoker.dart'; // ignore: implementation_imports, depend_on_referenced_packages import 'package:test_api/src/backend/live_test.dart'; +import 'constants.dart' as constants; + const _success = 'success'; void _defaultPrintLogger(String message) { @@ -21,21 +22,6 @@ void _defaultPrintLogger(String message) { print('PatrolBinding: $message'); } -/// An escape hatch if, for any reason, the test reporting has to be -/// disabled. -/// -/// Patrol CLI doesn't pass this dart define anywhere. -const bool _shouldReportResultsToNative = bool.fromEnvironment( - 'PATROL_INTEGRATION_TEST_SHOULD_REPORT_RESULTS_TO_NATIVE', - defaultValue: true, -); - -const bool _hotRestartEnabled = bool.fromEnvironment('PATROL_HOT_RESTART'); - -/// The method channel used to report the results of the tests to the underlying -/// platform's testing framework (JUnit on Android and XCTest on iOS). -const patrolChannel = MethodChannel('pl.leancode.patrol/main'); - /// Binding that enables some of Patrol's custom functionality, such as tapping /// on WebViews during a test. /// @@ -60,20 +46,16 @@ class PatrolBinding extends IntegrationTestWidgetsFlutterBinding { final oldTestExceptionReporter = reportTestException; reportTestException = (details, testDescription) { final currentDartTestFile = _currentDartTestFile; - if (currentDartTestFile == null) { - throw StateError('currentDartTestFile is null'); + if (currentDartTestFile != null) { + assert(!constants.hotRestartEnabled); + _testResults[currentDartTestFile] = + Failure(testDescription, '$details'); + oldTestExceptionReporter(details, testDescription); } - - _testResults[currentDartTestFile] = Failure(testDescription, '$details'); - oldTestExceptionReporter(details, testDescription); }; setUp(() { - if (!_shouldReportResultsToNative) { - return; - } - - if (_hotRestartEnabled) { + if (constants.hotRestartEnabled) { // Sending results ends the test, which we don't want for Hot Restart return; } @@ -82,11 +64,7 @@ class PatrolBinding extends IntegrationTestWidgetsFlutterBinding { }); tearDown(() async { - if (!_shouldReportResultsToNative) { - return; - } - - if (_hotRestartEnabled) { + if (constants.hotRestartEnabled) { // Sending results ends the test, which we don't want for Hot Restart return; } diff --git a/packages/patrol/lib/src/common.dart b/packages/patrol/lib/src/common.dart index 6e1e82562..887a5c6cf 100644 --- a/packages/patrol/lib/src/common.dart +++ b/packages/patrol/lib/src/common.dart @@ -12,6 +12,8 @@ import 'package:test_api/src/backend/group.dart'; import 'package:test_api/src/backend/invoker.dart'; import 'package:test_api/src/backend/test.dart'; +import 'constants.dart' as constants; + /// Signature for callback to [patrolTest]. typedef PatrolTesterCallback = Future Function(PatrolTester $); @@ -81,7 +83,7 @@ void patrolTest( variant: variant, tags: tags, (widgetTester) async { - if (patrolBinding != null) { + if (patrolBinding != null && !constants.hotRestartEnabled) { // If Patrol's native automation feature is enabled, then this test will // be executed only if the native side requested it to be executed. // Otherwise, it returns early. diff --git a/packages/patrol/lib/src/constants.dart b/packages/patrol/lib/src/constants.dart new file mode 100644 index 000000000..01440e6ce --- /dev/null +++ b/packages/patrol/lib/src/constants.dart @@ -0,0 +1,5 @@ +import 'package:meta/meta.dart'; + +/// Whether Hot Restart is enabled. +@internal +const bool hotRestartEnabled = bool.fromEnvironment('PATROL_HOT_RESTART'); diff --git a/packages/patrol_cli/lib/src/commands/develop.dart b/packages/patrol_cli/lib/src/commands/develop.dart index 8dc6787e8..4925c134b 100644 --- a/packages/patrol_cli/lib/src/commands/develop.dart +++ b/packages/patrol_cli/lib/src/commands/develop.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:io'; import 'package:dispose_scope/dispose_scope.dart'; +import 'package:path/path.dart' show basename; import 'package:patrol_cli/src/analytics/analytics.dart'; import 'package:patrol_cli/src/android/android_test_backend.dart'; import 'package:patrol_cli/src/base/exceptions.dart'; @@ -14,7 +15,6 @@ import 'package:patrol_cli/src/devices.dart'; import 'package:patrol_cli/src/ios/ios_test_backend.dart'; import 'package:patrol_cli/src/pubspec_reader.dart'; import 'package:patrol_cli/src/runner/patrol_command.dart'; -import 'package:patrol_cli/src/test_bundler.dart'; import 'package:patrol_cli/src/test_finder.dart'; import 'package:patrol_cli/src/test_runner.dart'; @@ -22,7 +22,6 @@ class DevelopCommand extends PatrolCommand { DevelopCommand({ required DeviceFinder deviceFinder, required TestFinder testFinder, - required TestBundler testBundler, required TestRunner testRunner, required DartDefinesReader dartDefinesReader, required PubspecReader pubspecReader, @@ -34,7 +33,6 @@ class DevelopCommand extends PatrolCommand { required Logger logger, }) : _deviceFinder = deviceFinder, _testFinder = testFinder, - _testBundler = testBundler, _testRunner = testRunner, _dartDefinesReader = dartDefinesReader, _pubspecReader = pubspecReader, @@ -61,7 +59,6 @@ class DevelopCommand extends PatrolCommand { final DeviceFinder _deviceFinder; final TestFinder _testFinder; - final TestBundler _testBundler; final TestRunner _testRunner; final DartDefinesReader _dartDefinesReader; final PubspecReader _pubspecReader; @@ -85,7 +82,10 @@ class DevelopCommand extends PatrolCommand { final targets = stringsArg('target'); if (targets.isEmpty) { throwToolExit('No target provided with --target'); + } else if (targets.length > 1) { + throwToolExit('Only one target can be provided with --target'); } + final target = _testFinder.findTest(targets.first); _logger.detail('Received test target: $target'); @@ -93,9 +93,6 @@ class DevelopCommand extends PatrolCommand { throwToolExit('Cannot use release build mode with develop'); } - final testBundle = _testBundler.createTestBundle([target]); - _logger.detail('Bundled test target $target in ${testBundle.path}'); - final config = _pubspecReader.read(); final androidFlavor = stringArg('flavor') ?? config.android.flavor; final iosFlavor = stringArg('flavor') ?? config.ios.flavor; @@ -127,7 +124,7 @@ class DevelopCommand extends PatrolCommand { 'PATROL_ANDROID_APP_NAME': config.android.appName, 'PATROL_IOS_APP_NAME': config.ios.appName, 'INTEGRATION_TEST_SHOULD_REPORT_RESULTS_TO_NATIVE': 'false', - if (displayLabel) 'PATROL_TEST_LABEL': testBundle.basename, + if (displayLabel) 'PATROL_TEST_LABEL': basename(target), // develop-specific ...{'PATROL_HOT_RESTART': 'true'}, }.withNullsRemoved(); @@ -147,7 +144,7 @@ class DevelopCommand extends PatrolCommand { } final flutterOpts = FlutterAppOptions( - target: testBundle.path, + target: target, flavor: androidFlavor, buildMode: buildMode, dartDefines: dartDefines, diff --git a/packages/patrol_cli/lib/src/runner/patrol_command_runner.dart b/packages/patrol_cli/lib/src/runner/patrol_command_runner.dart index 8f3b6081f..2531fa6ec 100644 --- a/packages/patrol_cli/lib/src/runner/patrol_command_runner.dart +++ b/packages/patrol_cli/lib/src/runner/patrol_command_runner.dart @@ -142,7 +142,6 @@ class PatrolCommandRunner extends CompletionCommandRunner { DevelopCommand( deviceFinder: deviceFinder, testFinder: testFinder, - testBundler: testBundler, testRunner: TestRunner(), dartDefinesReader: DartDefinesReader(projectRoot: _fs.currentDirectory), pubspecReader: PubspecReader(projectRoot: _fs.currentDirectory), diff --git a/packages/patrol_cli/pubspec.lock b/packages/patrol_cli/pubspec.lock index 98d56d9df..e7599c5e0 100644 --- a/packages/patrol_cli/pubspec.lock +++ b/packages/patrol_cli/pubspec.lock @@ -674,4 +674,4 @@ packages: source: hosted version: "3.1.1" sdks: - dart: ">=2.19.0 <3.0.0" + dart: ">=2.19.0 <4.0.0"