Skip to content

Commit 6b93e27

Browse files
authored
Migrate test/fixtures/context.dart to null-safety (#1698)
1 parent 6586052 commit 6b93e27

File tree

1 file changed

+78
-71
lines changed

1 file changed

+78
-71
lines changed

dwds/test/fixtures/context.dart

Lines changed: 78 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
import 'dart:async';
86
import 'dart:convert';
97
import 'dart:io';
@@ -56,45 +54,53 @@ enum IndexBaseMode { noBase, base }
5654
enum NullSafety { weak, sound }
5755

5856
class TestContext {
59-
String appUrl;
60-
WipConnection tabConnection;
61-
WipConnection extensionConnection;
62-
TestServer testServer;
63-
BuildDaemonClient daemonClient;
64-
ResidentWebRunner webRunner;
65-
WebDriver webDriver;
66-
Process chromeDriver;
67-
AppConnection appConnection;
68-
DebugConnection debugConnection;
69-
WebkitDebugger webkitDebugger;
70-
Client client;
71-
ExpressionCompilerService ddcService;
72-
int port;
73-
Directory _outputDir;
74-
File _entryFile;
75-
Uri _packageConfigFile;
76-
Uri _projectDirectory;
77-
String _entryContents;
78-
79-
/// Null safety mode for the frontend server.
80-
///
81-
/// Note: flutter's frontend server is always launched with
82-
/// the null safety setting inferred from project configurations
83-
/// or the source code. We skip this inference and just set it
84-
/// here to the desired value manually.
85-
///
86-
/// Note: build_runner-based setups ignore this setting and read
87-
/// this value from the ddc debug metadata and pass it to the
88-
/// expression compiler worker initialization API.
89-
///
90-
/// TODO(annagrin): Currently setting sound null safety for frontend
91-
/// server tests fails due to missing sound SDK JavaScript and maps.
92-
/// Issue: https://github.com/dart-lang/webdev/issues/1591
93-
NullSafety nullSafety;
57+
String get appUrl => _appUrl!;
58+
late String? _appUrl;
59+
60+
WipConnection get tabConnection => _tabConnection!;
61+
late WipConnection? _tabConnection;
62+
63+
TestServer get testServer => _testServer!;
64+
TestServer? _testServer;
65+
66+
BuildDaemonClient get daemonClient => _daemonClient!;
67+
BuildDaemonClient? _daemonClient;
68+
69+
ResidentWebRunner get webRunner => _webRunner!;
70+
ResidentWebRunner? _webRunner;
71+
72+
WebDriver get webDriver => _webDriver!;
73+
WebDriver? _webDriver;
74+
75+
Process get chromeDriver => _chromeDriver!;
76+
late Process? _chromeDriver;
77+
78+
WebkitDebugger get webkitDebugger => _webkitDebugger!;
79+
late WebkitDebugger? _webkitDebugger;
80+
81+
Client get client => _client!;
82+
late Client? _client;
83+
84+
ExpressionCompilerService? ddcService;
85+
86+
int get port => _port!;
87+
late int? _port;
88+
89+
Directory get outputDir => _outputDir!;
90+
late Directory? _outputDir;
91+
92+
late WipConnection extensionConnection;
93+
late AppConnection appConnection;
94+
late DebugConnection debugConnection;
95+
late File _entryFile;
96+
late Uri _packageConfigFile;
97+
late Uri _projectDirectory;
98+
late String _entryContents;
99+
94100
final _logger = logging.Logger('Context');
95101

96102
/// Top level directory in which we run the test server..
97-
String workingDirectory;
103+
late String workingDirectory;
98104

99105
/// The path to build and serve.
100106
String pathToServe;
@@ -103,8 +109,8 @@ class TestContext {
103109
String path;
104110

105111
TestContext(
106-
{String directory,
107-
String entry,
112+
{String? directory,
113+
String? entry,
108114
this.path = 'hello_world/index.html',
109115
this.pathToServe = 'example'}) {
110116
final relativeDirectory = p.join('..', 'fixtures', '_test');
@@ -144,19 +150,19 @@ class TestContext {
144150
bool spawnDds = true,
145151
String hostname = 'localhost',
146152
bool waitToDebug = false,
147-
UrlEncoder urlEncoder,
153+
UrlEncoder? urlEncoder,
148154
CompilationMode compilationMode = CompilationMode.buildDaemon,
149155
NullSafety nullSafety = NullSafety.weak,
150156
bool enableExpressionEvaluation = false,
151157
bool verboseCompiler = false,
152-
SdkConfigurationProvider sdkConfigurationProvider,
158+
SdkConfigurationProvider? sdkConfigurationProvider,
153159
}) async {
154160
sdkConfigurationProvider ??= DefaultSdkConfigurationProvider();
155161

156162
try {
157163
configureLogWriter();
158164

159-
client = IOClient(HttpClient()
165+
_client = IOClient(HttpClient()
160166
..maxConnectionsPerHost = 200
161167
..idleTimeout = const Duration(seconds: 30)
162168
..connectionTimeout = const Duration(seconds: 30));
@@ -167,7 +173,7 @@ class TestContext {
167173
final chromeDriverPort = await findUnusedPort();
168174
final chromeDriverUrlBase = 'wd/hub';
169175
try {
170-
chromeDriver = await Process.start('chromedriver$_exeExt',
176+
_chromeDriver = await Process.start('chromedriver$_exeExt',
171177
['--port=$chromeDriverPort', '--url-base=$chromeDriverUrlBase']);
172178
// On windows this takes a while to boot up, wait for the first line
173179
// of stdout as a signal that it is ready.
@@ -195,14 +201,14 @@ class TestContext {
195201
await Process.run(dartPath, ['pub', 'upgrade'],
196202
workingDirectory: workingDirectory);
197203

198-
ExpressionCompiler expressionCompiler;
204+
ExpressionCompiler? expressionCompiler;
199205
AssetReader assetReader;
200206
Handler assetHandler;
201207
Stream<BuildResults> buildResults;
202208
RequireStrategy requireStrategy;
203209
String basePath = '';
204210

205-
port = await findUnusedPort();
211+
_port = await findUnusedPort();
206212
switch (compilationMode) {
207213
case CompilationMode.buildDaemon:
208214
{
@@ -213,7 +219,7 @@ class TestContext {
213219
],
214220
'--verbose',
215221
];
216-
daemonClient =
222+
_daemonClient =
217223
await connectClient(workingDirectory, options, (log) {
218224
final record = log.toLogRecord();
219225
final name =
@@ -263,14 +269,14 @@ class TestContext {
263269
final entry = p.toUri(_entryFile.path
264270
.substring(_projectDirectory.toFilePath().length + 1));
265271

266-
webRunner = ResidentWebRunner(
272+
_webRunner = ResidentWebRunner(
267273
entry,
268274
urlEncoder,
269275
_projectDirectory,
270276
_packageConfigFile,
271277
[_projectDirectory],
272278
'org-dartlang-app',
273-
_outputDir.path,
279+
outputDir.path,
274280
nullSafety == NullSafety.sound,
275281
verboseCompiler,
276282
);
@@ -315,14 +321,14 @@ class TestContext {
315321
]
316322
}
317323
});
318-
webDriver = await createDriver(
324+
_webDriver = await createDriver(
319325
spec: WebDriverSpec.JsonWire,
320326
desired: capabilities,
321327
uri: Uri.parse(
322328
'http://127.0.0.1:$chromeDriverPort/$chromeDriverUrlBase/'));
323329
final connection = ChromeConnection('localhost', debugPort);
324330

325-
testServer = await TestServer.start(
331+
_testServer = await TestServer.start(
326332
hostname,
327333
port,
328334
assetHandler,
@@ -342,13 +348,14 @@ class TestContext {
342348
ddcService,
343349
);
344350

345-
appUrl = basePath.isEmpty
351+
_appUrl = basePath.isEmpty
346352
? 'http://localhost:$port/$path'
347353
: 'http://localhost:$port/$basePath/$path';
348354

349-
await webDriver.get(appUrl);
350-
final tab = await connection.getTab((t) => t.url == appUrl);
351-
tabConnection = await tab.connect();
355+
await _webDriver?.get(appUrl);
356+
final tab = await (connection.getTab((t) => t.url == appUrl)
357+
as FutureOr<ChromeTab>);
358+
_tabConnection = await tab.connect();
352359
await tabConnection.runtime.enable();
353360
await tabConnection.debugger.enable();
354361

@@ -370,30 +377,30 @@ class TestContext {
370377

371378
Future<void> startDebugging() async {
372379
debugConnection = await testServer.dwds.debugConnection(appConnection);
373-
webkitDebugger = WebkitDebugger(WipDebugger(tabConnection));
380+
_webkitDebugger = WebkitDebugger(WipDebugger(tabConnection));
374381
}
375382

376383
Future<void> tearDown() async {
377-
await webDriver?.quit(closeSession: true);
378-
chromeDriver?.kill();
384+
await _webDriver?.quit(closeSession: true);
385+
_chromeDriver?.kill();
379386
DartUri.currentDirectory = p.current;
380387
_entryFile.writeAsStringSync(_entryContents);
381-
await daemonClient?.close();
388+
await _daemonClient?.close();
382389
await ddcService?.stop();
383-
await webRunner?.stop();
384-
await testServer?.stop();
385-
client?.close();
390+
await _webRunner?.stop();
391+
await _testServer?.stop();
392+
_client?.close();
386393
await _outputDir?.delete(recursive: true);
387394
stopLogWriter();
388395

389396
// clear the state for next setup
390-
webDriver = null;
391-
chromeDriver = null;
392-
daemonClient = null;
397+
_webDriver = null;
398+
_chromeDriver = null;
399+
_daemonClient = null;
393400
ddcService = null;
394-
webRunner = null;
395-
testServer = null;
396-
client = null;
401+
_webRunner = null;
402+
_testServer = null;
403+
_client = null;
397404
_outputDir = null;
398405
}
399406

@@ -402,7 +409,7 @@ class TestContext {
402409
_entryContents.replaceAll('Hello World!', 'Gary is awesome!'));
403410

404411
// Wait for the build.
405-
await daemonClient.buildResults.firstWhere((results) => results.results
412+
await _daemonClient?.buildResults.firstWhere((results) => results.results
406413
.any((result) => result.status == BuildStatus.succeeded));
407414

408415
// Allow change to propagate to the browser.
@@ -438,8 +445,8 @@ class TestContext {
438445
Future<int> findBreakpointLine(
439446
String breakpointId, String isolateId, ScriptRef scriptRef) async {
440447
final script = await debugConnection.vmService
441-
.getObject(isolateId, scriptRef.id) as Script;
442-
final lines = LineSplitter.split(script.source).toList();
448+
.getObject(isolateId, scriptRef.id!) as Script;
449+
final lines = LineSplitter.split(script.source!).toList();
443450
final lineNumber =
444451
lines.indexWhere((l) => l.endsWith('// Breakpoint: $breakpointId'));
445452
if (lineNumber == -1) {

0 commit comments

Comments
 (0)