Skip to content

Commit

Permalink
Bring back Hot Restart (#1286)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bartekpacia authored May 19, 2023
1 parent 549a4a0 commit dc6cd15
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 43 deletions.
40 changes: 9 additions & 31 deletions packages/patrol/lib/src/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -14,28 +13,15 @@ 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) {
// ignore: avoid_print
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.
///
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/patrol/lib/src/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> Function(PatrolTester $);

Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions packages/patrol/lib/src/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:meta/meta.dart';

/// Whether Hot Restart is enabled.
@internal
const bool hotRestartEnabled = bool.fromEnvironment('PATROL_HOT_RESTART');
15 changes: 6 additions & 9 deletions packages/patrol_cli/lib/src/commands/develop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -14,15 +15,13 @@ 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';

class DevelopCommand extends PatrolCommand {
DevelopCommand({
required DeviceFinder deviceFinder,
required TestFinder testFinder,
required TestBundler testBundler,
required TestRunner testRunner,
required DartDefinesReader dartDefinesReader,
required PubspecReader pubspecReader,
Expand All @@ -34,7 +33,6 @@ class DevelopCommand extends PatrolCommand {
required Logger logger,
}) : _deviceFinder = deviceFinder,
_testFinder = testFinder,
_testBundler = testBundler,
_testRunner = testRunner,
_dartDefinesReader = dartDefinesReader,
_pubspecReader = pubspecReader,
Expand All @@ -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;
Expand All @@ -85,17 +82,17 @@ 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');

if (boolArg('release')) {
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;
Expand Down Expand Up @@ -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();
Expand All @@ -147,7 +144,7 @@ class DevelopCommand extends PatrolCommand {
}

final flutterOpts = FlutterAppOptions(
target: testBundle.path,
target: target,
flavor: androidFlavor,
buildMode: buildMode,
dartDefines: dartDefines,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ class PatrolCommandRunner extends CompletionCommandRunner<int> {
DevelopCommand(
deviceFinder: deviceFinder,
testFinder: testFinder,
testBundler: testBundler,
testRunner: TestRunner(),
dartDefinesReader: DartDefinesReader(projectRoot: _fs.currentDirectory),
pubspecReader: PubspecReader(projectRoot: _fs.currentDirectory),
Expand Down
2 changes: 1 addition & 1 deletion packages/patrol_cli/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit dc6cd15

Please sign in to comment.