diff --git a/dwds/test/fixtures/context.dart b/dwds/test/fixtures/context.dart index dfc953866..09cfa2ec0 100644 --- a/dwds/test/fixtures/context.dart +++ b/dwds/test/fixtures/context.dart @@ -2,8 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// @dart = 2.9 - import 'dart:async'; import 'dart:convert'; import 'dart:io'; @@ -56,45 +54,53 @@ enum IndexBaseMode { noBase, base } enum NullSafety { weak, sound } class TestContext { - String appUrl; - WipConnection tabConnection; - WipConnection extensionConnection; - TestServer testServer; - BuildDaemonClient daemonClient; - ResidentWebRunner webRunner; - WebDriver webDriver; - Process chromeDriver; - AppConnection appConnection; - DebugConnection debugConnection; - WebkitDebugger webkitDebugger; - Client client; - ExpressionCompilerService ddcService; - int port; - Directory _outputDir; - File _entryFile; - Uri _packageConfigFile; - Uri _projectDirectory; - String _entryContents; - - /// Null safety mode for the frontend server. - /// - /// Note: flutter's frontend server is always launched with - /// the null safety setting inferred from project configurations - /// or the source code. We skip this inference and just set it - /// here to the desired value manually. - /// - /// Note: build_runner-based setups ignore this setting and read - /// this value from the ddc debug metadata and pass it to the - /// expression compiler worker initialization API. - /// - /// TODO(annagrin): Currently setting sound null safety for frontend - /// server tests fails due to missing sound SDK JavaScript and maps. - /// Issue: https://github.com/dart-lang/webdev/issues/1591 - NullSafety nullSafety; + String get appUrl => _appUrl!; + late String? _appUrl; + + WipConnection get tabConnection => _tabConnection!; + late WipConnection? _tabConnection; + + TestServer get testServer => _testServer!; + TestServer? _testServer; + + BuildDaemonClient get daemonClient => _daemonClient!; + BuildDaemonClient? _daemonClient; + + ResidentWebRunner get webRunner => _webRunner!; + ResidentWebRunner? _webRunner; + + WebDriver get webDriver => _webDriver!; + WebDriver? _webDriver; + + Process get chromeDriver => _chromeDriver!; + late Process? _chromeDriver; + + WebkitDebugger get webkitDebugger => _webkitDebugger!; + late WebkitDebugger? _webkitDebugger; + + Client get client => _client!; + late Client? _client; + + ExpressionCompilerService? ddcService; + + int get port => _port!; + late int? _port; + + Directory get outputDir => _outputDir!; + late Directory? _outputDir; + + late WipConnection extensionConnection; + late AppConnection appConnection; + late DebugConnection debugConnection; + late File _entryFile; + late Uri _packageConfigFile; + late Uri _projectDirectory; + late String _entryContents; + final _logger = logging.Logger('Context'); /// Top level directory in which we run the test server.. - String workingDirectory; + late String workingDirectory; /// The path to build and serve. String pathToServe; @@ -103,8 +109,8 @@ class TestContext { String path; TestContext( - {String directory, - String entry, + {String? directory, + String? entry, this.path = 'hello_world/index.html', this.pathToServe = 'example'}) { final relativeDirectory = p.join('..', 'fixtures', '_test'); @@ -144,19 +150,19 @@ class TestContext { bool spawnDds = true, String hostname = 'localhost', bool waitToDebug = false, - UrlEncoder urlEncoder, + UrlEncoder? urlEncoder, CompilationMode compilationMode = CompilationMode.buildDaemon, NullSafety nullSafety = NullSafety.weak, bool enableExpressionEvaluation = false, bool verboseCompiler = false, - SdkConfigurationProvider sdkConfigurationProvider, + SdkConfigurationProvider? sdkConfigurationProvider, }) async { sdkConfigurationProvider ??= DefaultSdkConfigurationProvider(); try { configureLogWriter(); - client = IOClient(HttpClient() + _client = IOClient(HttpClient() ..maxConnectionsPerHost = 200 ..idleTimeout = const Duration(seconds: 30) ..connectionTimeout = const Duration(seconds: 30)); @@ -167,7 +173,7 @@ class TestContext { final chromeDriverPort = await findUnusedPort(); final chromeDriverUrlBase = 'wd/hub'; try { - chromeDriver = await Process.start('chromedriver$_exeExt', + _chromeDriver = await Process.start('chromedriver$_exeExt', ['--port=$chromeDriverPort', '--url-base=$chromeDriverUrlBase']); // On windows this takes a while to boot up, wait for the first line // of stdout as a signal that it is ready. @@ -195,14 +201,14 @@ class TestContext { await Process.run(dartPath, ['pub', 'upgrade'], workingDirectory: workingDirectory); - ExpressionCompiler expressionCompiler; + ExpressionCompiler? expressionCompiler; AssetReader assetReader; Handler assetHandler; Stream buildResults; RequireStrategy requireStrategy; String basePath = ''; - port = await findUnusedPort(); + _port = await findUnusedPort(); switch (compilationMode) { case CompilationMode.buildDaemon: { @@ -213,7 +219,7 @@ class TestContext { ], '--verbose', ]; - daemonClient = + _daemonClient = await connectClient(workingDirectory, options, (log) { final record = log.toLogRecord(); final name = @@ -263,14 +269,14 @@ class TestContext { final entry = p.toUri(_entryFile.path .substring(_projectDirectory.toFilePath().length + 1)); - webRunner = ResidentWebRunner( + _webRunner = ResidentWebRunner( entry, urlEncoder, _projectDirectory, _packageConfigFile, [_projectDirectory], 'org-dartlang-app', - _outputDir.path, + outputDir.path, nullSafety == NullSafety.sound, verboseCompiler, ); @@ -315,14 +321,14 @@ class TestContext { ] } }); - webDriver = await createDriver( + _webDriver = await createDriver( spec: WebDriverSpec.JsonWire, desired: capabilities, uri: Uri.parse( 'http://127.0.0.1:$chromeDriverPort/$chromeDriverUrlBase/')); final connection = ChromeConnection('localhost', debugPort); - testServer = await TestServer.start( + _testServer = await TestServer.start( hostname, port, assetHandler, @@ -342,13 +348,14 @@ class TestContext { ddcService, ); - appUrl = basePath.isEmpty + _appUrl = basePath.isEmpty ? 'http://localhost:$port/$path' : 'http://localhost:$port/$basePath/$path'; - await webDriver.get(appUrl); - final tab = await connection.getTab((t) => t.url == appUrl); - tabConnection = await tab.connect(); + await _webDriver?.get(appUrl); + final tab = await (connection.getTab((t) => t.url == appUrl) + as FutureOr); + _tabConnection = await tab.connect(); await tabConnection.runtime.enable(); await tabConnection.debugger.enable(); @@ -370,30 +377,30 @@ class TestContext { Future startDebugging() async { debugConnection = await testServer.dwds.debugConnection(appConnection); - webkitDebugger = WebkitDebugger(WipDebugger(tabConnection)); + _webkitDebugger = WebkitDebugger(WipDebugger(tabConnection)); } Future tearDown() async { - await webDriver?.quit(closeSession: true); - chromeDriver?.kill(); + await _webDriver?.quit(closeSession: true); + _chromeDriver?.kill(); DartUri.currentDirectory = p.current; _entryFile.writeAsStringSync(_entryContents); - await daemonClient?.close(); + await _daemonClient?.close(); await ddcService?.stop(); - await webRunner?.stop(); - await testServer?.stop(); - client?.close(); + await _webRunner?.stop(); + await _testServer?.stop(); + _client?.close(); await _outputDir?.delete(recursive: true); stopLogWriter(); // clear the state for next setup - webDriver = null; - chromeDriver = null; - daemonClient = null; + _webDriver = null; + _chromeDriver = null; + _daemonClient = null; ddcService = null; - webRunner = null; - testServer = null; - client = null; + _webRunner = null; + _testServer = null; + _client = null; _outputDir = null; } @@ -402,7 +409,7 @@ class TestContext { _entryContents.replaceAll('Hello World!', 'Gary is awesome!')); // Wait for the build. - await daemonClient.buildResults.firstWhere((results) => results.results + await _daemonClient?.buildResults.firstWhere((results) => results.results .any((result) => result.status == BuildStatus.succeeded)); // Allow change to propagate to the browser. @@ -438,8 +445,8 @@ class TestContext { Future findBreakpointLine( String breakpointId, String isolateId, ScriptRef scriptRef) async { final script = await debugConnection.vmService - .getObject(isolateId, scriptRef.id) as Script; - final lines = LineSplitter.split(script.source).toList(); + .getObject(isolateId, scriptRef.id!) as Script; + final lines = LineSplitter.split(script.source!).toList(); final lineNumber = lines.indexWhere((l) => l.endsWith('// Breakpoint: $breakpointId')); if (lineNumber == -1) {