Skip to content

Commit 1ab3878

Browse files
[flutter_tools] support --enable-experiment in flutter test (flutter#55564)
Support --enable-experiment in flutter test (for flutter_tester). Required minor change for null safety.
1 parent 07c451f commit 1ab3878

File tree

12 files changed

+65
-8
lines changed

12 files changed

+65
-8
lines changed

dev/bots/test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,9 @@ Future<void> _runFrameworkTests() async {
480480
await _runFlutterTest(path.join(flutterRoot, 'packages', 'flutter_localizations'), tableData: bigqueryApi?.tabledata);
481481
await _runFlutterTest(path.join(flutterRoot, 'packages', 'flutter_test'), tableData: bigqueryApi?.tabledata);
482482
await _runFlutterTest(path.join(flutterRoot, 'packages', 'fuchsia_remote_debug_protocol'), tableData: bigqueryApi?.tabledata);
483+
await _runFlutterTest(path.join(flutterRoot, 'dev', 'integration_tests', 'non_nullable'),
484+
options: <String>['--enable-experiment=non-nullable'],
485+
);
483486
await _runFlutterTest(
484487
path.join(flutterRoot, 'dev', 'tracing_tests'),
485488
options: <String>['--enable-vmservice'],

dev/integration_tests/non_nullable/analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
analyzer:
44
exclude:
55
- lib/main.dart
6+
- test/test_test.dart
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// @dart=2.9
6+
import 'package:flutter_test/flutter_test.dart';
7+
8+
String? x;
9+
10+
void main() {
11+
testWidgets('trivial', (WidgetTester tester) async {
12+
expect(true, true);
13+
});
14+
}

packages/flutter_tools/bin/fuchsia_tester.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ Future<void> run(List<String> args) async {
155155
concurrency: math.max(1, globals.platform.numberOfProcessors - 2),
156156
icudtlPath: globals.fs.path.absolute(argResults[_kOptionIcudtl] as String),
157157
coverageDirectory: coverageDirectory,
158+
dartExperiments: <String>[],
158159
);
159160

160161
if (collector != null) {

packages/flutter_tools/lib/src/commands/test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class TestCommand extends FlutterCommand {
129129
'The vmservice will be enabled no matter what in those cases.'
130130
);
131131
usesTrackWidgetCreation(verboseHelp: verboseHelp);
132+
addEnableExperimentation(hide: !verboseHelp);
132133
}
133134

134135
/// The interface for starting and configuring the tester.
@@ -171,6 +172,7 @@ class TestCommand extends FlutterCommand {
171172
final String tags = stringArg('tags');
172173
final String excludeTags = stringArg('exclude-tags');
173174
final FlutterProject flutterProject = FlutterProject.current();
175+
final List<String> dartExperiments = stringsArg(FlutterOptions.kEnableExperiment);
174176

175177
if (buildTestAssets && flutterProject.manifest.assets.isNotEmpty) {
176178
await _buildTestAsset();
@@ -276,6 +278,7 @@ class TestCommand extends FlutterCommand {
276278
flutterProject: flutterProject,
277279
web: stringArg('platform') == 'chrome',
278280
randomSeed: stringArg('test-randomize-ordering-seed'),
281+
dartExperiments: dartExperiments,
279282
);
280283

281284
if (collector != null) {

packages/flutter_tools/lib/src/test/flutter_platform.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ FlutterPlatform installHook({
8787
FlutterProject flutterProject,
8888
String icudtlPath,
8989
PlatformPluginRegistration platformPluginRegistration,
90+
@required List<String> dartExperiments,
9091
}) {
9192
assert(testWrapper != null);
9293
assert(enableObservatory || (!startPaused && observatoryPort == null));
@@ -119,6 +120,7 @@ FlutterPlatform installHook({
119120
projectRootDirectory: projectRootDirectory,
120121
flutterProject: flutterProject,
121122
icudtlPath: icudtlPath,
123+
dartExperiments: dartExperiments,
122124
);
123125
platformPluginRegistration(platform);
124126
return platform;
@@ -144,6 +146,7 @@ String generateTestBootstrap({
144146
@required InternetAddress host,
145147
File testConfigFile,
146148
bool updateGoldens = false,
149+
@required bool nullSafety,
147150
}) {
148151
assert(testUrl != null);
149152
assert(host != null);
@@ -173,11 +176,15 @@ import '$testUrl' as test;
173176
import '${Uri.file(testConfigFile.path)}' as test_config;
174177
''');
175178
}
179+
// This type is sensitive to the non-nullable experiment.
180+
final String beforeLoadTypedef = nullSafety
181+
? 'Future<dynamic> Function()?'
182+
: 'Future<dynamic> Function()';
176183
buffer.write('''
177184
178185
/// Returns a serialized test suite.
179186
StreamChannel<dynamic> serializeSuite(Function getMain(),
180-
{bool hidePrints = true, Future<dynamic> beforeLoad()}) {
187+
{bool hidePrints = true, $beforeLoadTypedef beforeLoad}) {
181188
return RemoteListener.start(getMain,
182189
hidePrints: hidePrints, beforeLoad: beforeLoad);
183190
}
@@ -259,6 +266,7 @@ class FlutterPlatform extends PlatformPlugin {
259266
this.projectRootDirectory,
260267
this.flutterProject,
261268
this.icudtlPath,
269+
@required this.dartExperiments,
262270
}) : assert(shellPath != null);
263271

264272
final String shellPath;
@@ -279,6 +287,7 @@ class FlutterPlatform extends PlatformPlugin {
279287
final Uri projectRootDirectory;
280288
final FlutterProject flutterProject;
281289
final String icudtlPath;
290+
final List<String> dartExperiments;
282291

283292
Directory fontsDirectory;
284293

@@ -447,7 +456,7 @@ class FlutterPlatform extends PlatformPlugin {
447456

448457
if (precompiledDillPath == null && precompiledDillFiles == null) {
449458
// Lazily instantiate compiler so it is built only if it is actually used.
450-
compiler ??= TestCompiler(buildMode, trackWidgetCreation, flutterProject);
459+
compiler ??= TestCompiler(buildMode, trackWidgetCreation, flutterProject, dartExperiments);
451460
mainDart = await compiler.compile(globals.fs.file(mainDart).uri);
452461

453462
if (mainDart == null) {
@@ -739,6 +748,7 @@ class FlutterPlatform extends PlatformPlugin {
739748
testConfigFile: findTestConfigFile(globals.fs.file(testUrl)),
740749
host: host,
741750
updateGoldens: updateGoldens,
751+
nullSafety: dartExperiments.contains('non-nullable'),
742752
);
743753
}
744754

packages/flutter_tools/lib/src/test/flutter_web_platform.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class FlutterWebPlatform extends PlatformPlugin {
7575

7676
_testGoldenComparator = TestGoldenComparator(
7777
shellPath,
78-
() => TestCompiler(BuildMode.debug, false, flutterProject),
78+
() => TestCompiler(BuildMode.debug, false, flutterProject, <String>[]),
7979
);
8080
}
8181

packages/flutter_tools/lib/src/test/runner.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ abstract class FlutterTestRunner {
5151
Directory coverageDirectory,
5252
bool web = false,
5353
String randomSeed,
54+
@required List<String> dartExperiments,
5455
});
5556
}
5657

@@ -84,6 +85,7 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
8485
Directory coverageDirectory,
8586
bool web = false,
8687
String randomSeed,
88+
@required List<String> dartExperiments,
8789
}) async {
8890
// Configure package:test to use the Flutter engine for child processes.
8991
final String shellPath = globals.artifacts.getArtifactPath(Artifact.flutterTester);
@@ -175,6 +177,7 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
175177
projectRootDirectory: globals.fs.currentDirectory.uri,
176178
flutterProject: flutterProject,
177179
icudtlPath: icudtlPath,
180+
dartExperiments: dartExperiments,
178181
);
179182

180183
// Make the global packages path absolute.

packages/flutter_tools/lib/src/test/test_compiler.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class TestCompiler {
4242
this.buildMode,
4343
this.trackWidgetCreation,
4444
this.flutterProject,
45+
this.dartExperiments,
4546
) : testFilePath = getKernelPathForTransformerOptions(
4647
globals.fs.path.join(flutterProject.directory.path, getBuildDirectory(), 'testfile.dill'),
4748
trackWidgetCreation: trackWidgetCreation,
@@ -65,6 +66,7 @@ class TestCompiler {
6566
final BuildMode buildMode;
6667
final bool trackWidgetCreation;
6768
final String testFilePath;
69+
final List<String> dartExperiments;
6870

6971

7072
ResidentCompiler compiler;
@@ -104,6 +106,7 @@ class TestCompiler {
104106
unsafePackageSerialization: false,
105107
dartDefines: const <String>[],
106108
packagesPath: globalPackagesPath,
109+
experimentalFlags: dartExperiments,
107110
);
108111
if (flutterProject.hasBuilders) {
109112
return CodeGeneratingResidentCompiler.create(

packages/flutter_tools/test/commands.shard/hermetic/test_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ class FakeFlutterTestRunner implements FlutterTestRunner {
184184
Directory coverageDirectory,
185185
bool web = false,
186186
String randomSeed,
187+
@override List<String> dartExperiments,
187188
}) async {
188189
lastEnableObservatoryValue = enableObservatory;
189190
return exitCode;

packages/flutter_tools/test/general.shard/flutter_platform_test.dart

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,29 @@ import '../src/context.dart';
1717

1818
void main() {
1919
group('FlutterPlatform', () {
20-
testUsingContext('ensureConfiguration throws an error if an explicitObservatoryPort is specified and more than one test file', () async {
21-
final FlutterPlatform flutterPlatform = FlutterPlatform(buildMode: BuildMode.debug, shellPath: '/', explicitObservatoryPort: 1234);
20+
testUsingContext('ensureConfiguration throws an error if an '
21+
'explicitObservatoryPort is specified and more than one test file', () async {
22+
final FlutterPlatform flutterPlatform = FlutterPlatform(
23+
buildMode: BuildMode.debug,
24+
shellPath: '/',
25+
explicitObservatoryPort: 1234,
26+
dartExperiments: <String>[],
27+
);
2228
flutterPlatform.loadChannel('test1.dart', MockSuitePlatform());
29+
2330
expect(() => flutterPlatform.loadChannel('test2.dart', MockSuitePlatform()), throwsToolExit());
2431
});
2532

26-
testUsingContext('ensureConfiguration throws an error if a precompiled entrypoint is specified and more that one test file', () {
27-
final FlutterPlatform flutterPlatform = FlutterPlatform(buildMode: BuildMode.debug, shellPath: '/', precompiledDillPath: 'example.dill');
33+
testUsingContext('ensureConfiguration throws an error if a precompiled '
34+
'entrypoint is specified and more that one test file', () {
35+
final FlutterPlatform flutterPlatform = FlutterPlatform(
36+
buildMode: BuildMode.debug,
37+
shellPath: '/',
38+
precompiledDillPath: 'example.dill',
39+
dartExperiments: <String>[],
40+
);
2841
flutterPlatform.loadChannel('test1.dart', MockSuitePlatform());
42+
2943
expect(() => flutterPlatform.loadChannel('test2.dart', MockSuitePlatform()), throwsToolExit());
3044
});
3145

@@ -99,6 +113,7 @@ void main() {
99113
shellPath: 'abc',
100114
enableObservatory: false,
101115
startPaused: true,
116+
dartExperiments: <String>[],
102117
), throwsAssertionError);
103118

104119
expect(() => installHook(
@@ -107,6 +122,7 @@ void main() {
107122
enableObservatory: false,
108123
startPaused: false,
109124
observatoryPort: 123,
125+
dartExperiments: <String>[],
110126
), throwsAssertionError);
111127

112128
FlutterPlatform capturedPlatform;
@@ -127,6 +143,7 @@ void main() {
127143
observatoryPort: 200,
128144
serverType: InternetAddressType.IPv6,
129145
icudtlPath: 'ghi',
146+
dartExperiments: <String>[],
130147
platformPluginRegistration: (FlutterPlatform platform) {
131148
capturedPlatform = platform;
132149
});
@@ -175,6 +192,7 @@ class TestFlutterPlatform extends FlutterPlatform {
175192
startPaused: false,
176193
enableObservatory: false,
177194
buildTestAssets: false,
195+
dartExperiments: <String>[],
178196
);
179197

180198
@override

packages/flutter_tools/test/general.shard/test_compiler_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class FakeTestCompiler extends TestCompiler {
9696
bool trackWidgetCreation,
9797
FlutterProject flutterProject,
9898
this.residentCompiler,
99-
) : super(buildMode, trackWidgetCreation, flutterProject);
99+
) : super(buildMode, trackWidgetCreation, flutterProject, <String>[]);
100100

101101
final MockResidentCompiler residentCompiler;
102102

0 commit comments

Comments
 (0)