From f6a96b68ae55b1382cc6417b7abc4b75fc58dc6b Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Thu, 18 May 2023 14:10:45 +0200 Subject: [PATCH 1/5] fix Hot Restart --- packages/patrol/lib/src/binding.dart | 31 +++++++------------ packages/patrol/lib/src/common.dart | 4 ++- packages/patrol/lib/src/constants.dart | 15 +++++++++ .../patrol_cli/lib/src/commands/develop.dart | 11 ++++--- 4 files changed, 35 insertions(+), 26 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..9e664347b 100644 --- a/packages/patrol/lib/src/binding.dart +++ b/packages/patrol/lib/src/binding.dart @@ -14,6 +14,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,17 +23,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'); @@ -60,20 +51,20 @@ 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) { + if (!constants.shouldReportResultsToNative) { return; } - if (_hotRestartEnabled) { + if (constants.hotRestartEnabled) { // Sending results ends the test, which we don't want for Hot Restart return; } @@ -82,11 +73,11 @@ class PatrolBinding extends IntegrationTestWidgetsFlutterBinding { }); tearDown(() async { - if (!_shouldReportResultsToNative) { + if (!constants.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..f8b32d69c --- /dev/null +++ b/packages/patrol/lib/src/constants.dart @@ -0,0 +1,15 @@ +import 'package:meta/meta.dart'; + +/// An escape hatch if, for any reason, the test reporting has to be +/// disabled. +/// +/// Patrol CLI doesn't pass this dart define anywhere. +@internal +const bool shouldReportResultsToNative = bool.fromEnvironment( + 'PATROL_INTEGRATION_TEST_SHOULD_REPORT_RESULTS_TO_NATIVE', + defaultValue: true, +); + +/// 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..4908323a8 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'; @@ -85,7 +86,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 +97,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 +128,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 +148,7 @@ class DevelopCommand extends PatrolCommand { } final flutterOpts = FlutterAppOptions( - target: testBundle.path, + target: target, flavor: androidFlavor, buildMode: buildMode, dartDefines: dartDefines, From 99307bb30cf3bb45cbb4246297a5ad3ccc316ecc Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Thu, 18 May 2023 14:11:23 +0200 Subject: [PATCH 2/5] DevelopCommand: remove `TestBundler` argument --- packages/patrol_cli/lib/src/commands/develop.dart | 4 ---- packages/patrol_cli/lib/src/runner/patrol_command_runner.dart | 1 - 2 files changed, 5 deletions(-) diff --git a/packages/patrol_cli/lib/src/commands/develop.dart b/packages/patrol_cli/lib/src/commands/develop.dart index 4908323a8..4925c134b 100644 --- a/packages/patrol_cli/lib/src/commands/develop.dart +++ b/packages/patrol_cli/lib/src/commands/develop.dart @@ -15,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'; @@ -23,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, @@ -35,7 +33,6 @@ class DevelopCommand extends PatrolCommand { required Logger logger, }) : _deviceFinder = deviceFinder, _testFinder = testFinder, - _testBundler = testBundler, _testRunner = testRunner, _dartDefinesReader = dartDefinesReader, _pubspecReader = pubspecReader, @@ -62,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; 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), From aacec8e26224733614f4b9cb8c4878b2b63fe8e8 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Thu, 18 May 2023 14:14:03 +0200 Subject: [PATCH 3/5] delete PATROL_INTEGRATION_TEST_SHOULD_REPORT_RESULTS_TO_NATIVE, which is not used anywhere --- packages/patrol/lib/src/binding.dart | 8 -------- packages/patrol/lib/src/constants.dart | 10 ---------- 2 files changed, 18 deletions(-) diff --git a/packages/patrol/lib/src/binding.dart b/packages/patrol/lib/src/binding.dart index 9e664347b..3d57cd747 100644 --- a/packages/patrol/lib/src/binding.dart +++ b/packages/patrol/lib/src/binding.dart @@ -60,10 +60,6 @@ class PatrolBinding extends IntegrationTestWidgetsFlutterBinding { }; setUp(() { - if (!constants.shouldReportResultsToNative) { - return; - } - if (constants.hotRestartEnabled) { // Sending results ends the test, which we don't want for Hot Restart return; @@ -73,10 +69,6 @@ class PatrolBinding extends IntegrationTestWidgetsFlutterBinding { }); tearDown(() async { - if (!constants.shouldReportResultsToNative) { - return; - } - if (constants.hotRestartEnabled) { // Sending results ends the test, which we don't want for Hot Restart return; diff --git a/packages/patrol/lib/src/constants.dart b/packages/patrol/lib/src/constants.dart index f8b32d69c..01440e6ce 100644 --- a/packages/patrol/lib/src/constants.dart +++ b/packages/patrol/lib/src/constants.dart @@ -1,15 +1,5 @@ import 'package:meta/meta.dart'; -/// An escape hatch if, for any reason, the test reporting has to be -/// disabled. -/// -/// Patrol CLI doesn't pass this dart define anywhere. -@internal -const bool shouldReportResultsToNative = bool.fromEnvironment( - 'PATROL_INTEGRATION_TEST_SHOULD_REPORT_RESULTS_TO_NATIVE', - defaultValue: true, -); - /// Whether Hot Restart is enabled. @internal const bool hotRestartEnabled = bool.fromEnvironment('PATROL_HOT_RESTART'); From 78dc41d038d73378f0cf3929b2c54f3925457494 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Thu, 18 May 2023 14:14:41 +0200 Subject: [PATCH 4/5] delete patrolChannel (not used anymore) --- packages/patrol/lib/src/binding.dart | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/patrol/lib/src/binding.dart b/packages/patrol/lib/src/binding.dart index 3d57cd747..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'; @@ -23,10 +22,6 @@ void _defaultPrintLogger(String message) { print('PatrolBinding: $message'); } -/// 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. /// From 05566ecb6959c5658a1cd1c97b4ff5fa3df33548 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Thu, 18 May 2023 14:14:56 +0200 Subject: [PATCH 5/5] patrol_cli: bump Dart constraint in pubspec.lock --- packages/patrol_cli/pubspec.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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"