[flutter_tools] remove package HTTP and add pub.dev head check tests (#75871)

This commit is contained in:
Jonah Williams
2021-02-12 10:44:19 -08:00
committed by GitHub
parent af0ea8c3db
commit 980880e2b6
6 changed files with 125 additions and 31 deletions

View File

@@ -6,7 +6,6 @@
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';
import 'package:process/process.dart';
@@ -19,8 +18,8 @@ import 'resident_runner.dart';
/// An implementation of the devtools launcher that uses the server package.
///
/// This is implemented in isolated to prevent the flutter_tool from needing
/// a devtools dep in google3.
/// This is implemented in `isolated/` to prevent the flutter_tool from needing
/// a devtools dependency in google3.
class DevtoolsServerLauncher extends DevtoolsLauncher {
DevtoolsServerLauncher({
@required Platform platform,
@@ -28,22 +27,26 @@ class DevtoolsServerLauncher extends DevtoolsLauncher {
@required String pubExecutable,
@required Logger logger,
@required PersistentToolState persistentToolState,
@visibleForTesting io.HttpClient httpClient,
}) : _processManager = processManager,
_pubExecutable = pubExecutable,
_logger = logger,
_platform = platform,
_persistentToolState = persistentToolState;
_persistentToolState = persistentToolState,
_httpClient = httpClient ?? io.HttpClient();
final ProcessManager _processManager;
final String _pubExecutable;
final Logger _logger;
final Platform _platform;
final PersistentToolState _persistentToolState;
final io.HttpClient _httpClient;
io.Process _devToolsProcess;
static final RegExp _serveDevToolsPattern =
RegExp(r'Serving DevTools at ((http|//)[a-zA-Z0-9:/=_\-\.\[\]]+)');
static const String _pubHostedUrlKey = 'PUB_HOSTED_URL';
@override
Future<void> launch(Uri vmServiceUri) async {
@@ -51,15 +54,33 @@ class DevtoolsServerLauncher extends DevtoolsLauncher {
// this method is guaranteed not to return a Future that throws.
try {
bool offline = false;
bool useOverrideUrl = false;
try {
const String pubHostedUrlKey = 'PUB_HOSTED_URL';
if (_platform.environment.containsKey(pubHostedUrlKey)) {
await http.head(Uri.parse(_platform.environment[pubHostedUrlKey]));
Uri uri;
if (_platform.environment.containsKey(_pubHostedUrlKey)) {
useOverrideUrl = true;
uri = Uri.parse(_platform.environment[_pubHostedUrlKey]);
} else {
await http.head(Uri.https('pub.dev', ''));
uri = Uri.https('pub.dev', '');
}
final io.HttpClientRequest request = await _httpClient.headUrl(uri);
final io.HttpClientResponse response = await request.close();
await response.drain<void>();
if (response.statusCode != io.HttpStatus.ok) {
offline = true;
}
} on Exception {
offline = true;
} on ArgumentError {
if (!useOverrideUrl) {
rethrow;
}
// The user supplied a custom pub URL that was invalid, pretend to be offline
// and inform them that the URL was invalid.
offline = true;
_logger.printError(
'PUB_HOSTED_URL was set to an invalid URL: "${_platform.environment[_pubHostedUrlKey]}".'
);
}
if (offline) {