Unverified Commit 34fa90b0 authored by jslavitz's avatar jslavitz Committed by GitHub

Improves SDK Search Error Messages (#23884)

* Improves error message and adds test.
parent 99374edc
......@@ -317,14 +317,36 @@ class AndroidSdk {
String get avdManagerPath => getAvdManagerPath();
Directory get _platformsDir => fs.directory(fs.path.join(directory, 'platforms'));
Iterable<Directory> get _platforms {
Iterable<Directory> platforms = <Directory>[];
if (_platformsDir.existsSync()) {
platforms = _platformsDir
.listSync()
.whereType<Directory>();
}
return platforms;
}
/// Validate the Android SDK. This returns an empty list if there are no
/// issues; otherwise, it returns a list of issues found.
List<String> validateSdkWellFormed() {
if (!processManager.canRun(adbPath))
return <String>['Android SDK file not found: $adbPath.'];
if (sdkVersions.isEmpty || latestVersion == null)
return <String>['Android SDK is missing command line tools; download from https://goo.gl/XxQghQ'];
if (sdkVersions.isEmpty || latestVersion == null) {
final StringBuffer msg = StringBuffer('No valid Android SDK platforms found in ${_platformsDir.path}.');
if (_platforms.isEmpty) {
msg.write(' Directory was empty.');
} else {
msg.write(' Candidates were:\n');
msg.write(_platforms
.map((Directory dir) => ' - ${dir.basename}')
.join('\n'));
}
return <String>[msg.toString()];
}
return latestVersion.validateSdkWellFormed();
}
......@@ -355,15 +377,6 @@ class AndroidSdk {
}
void _init() {
Iterable<Directory> platforms = <Directory>[]; // android-22, ...
final Directory platformsDir = fs.directory(fs.path.join(directory, 'platforms'));
if (platformsDir.existsSync()) {
platforms = platformsDir
.listSync()
.whereType<Directory>();
}
List<Version> buildTools = <Version>[]; // 19.1.0, 22.0.1, ...
final Directory buildToolsDir = fs.directory(fs.path.join(directory, 'build-tools'));
......@@ -382,7 +395,7 @@ class AndroidSdk {
}
// Match up platforms with the best corresponding build-tools.
_sdkVersions = platforms.map<AndroidSdkVersion>((Directory platformDir) {
_sdkVersions = _platforms.map<AndroidSdkVersion>((Directory platformDir) {
final String platformName = platformDir.basename;
int platformVersion;
......
......@@ -83,6 +83,23 @@ void main() {
ProcessManager: () => processManager,
});
testUsingContext('returns validate sdk is well formed', () {
sdkDir = MockBrokenAndroidSdk.createSdkDirectory();
Config.instance.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
when(processManager.canRun(sdk.adbPath)).thenReturn(true);
final List<String> validationIssues = sdk.validateSdkWellFormed();
expect(validationIssues.first, 'No valid Android SDK platforms found in'
' /.tmp_rand0/flutter_mock_android_sdk.rand0/platforms. Candidates were:\n'
' - android-22\n'
' - android-23');
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => processManager,
});
testUsingContext('does not throw on sdkmanager version check failure', () {
sdkDir = MockAndroidSdk.createSdkDirectory();
Config.instance.setValue('android-sdk', sdkDir.path);
......@@ -164,3 +181,34 @@ void main() {
});
});
}
/// A broken SDK installation.
class MockBrokenAndroidSdk extends Mock implements AndroidSdk {
static Directory createSdkDirectory({
bool withAndroidN = false,
String withNdkDir,
bool withNdkSysroot = false,
bool withSdkManager = true,
}) {
final Directory dir = fs.systemTempDirectory.createTempSync('flutter_mock_android_sdk.');
_createSdkFile(dir, 'platform-tools/adb');
_createSdkFile(dir, 'build-tools/sda/aapt');
_createSdkFile(dir, 'build-tools/af/aapt');
_createSdkFile(dir, 'build-tools/ljkasd/aapt');
_createSdkFile(dir, 'platforms/android-22/android.jar');
_createSdkFile(dir, 'platforms/android-23/android.jar');
return dir;
}
static void _createSdkFile(Directory dir, String filePath, { String contents }) {
final File file = dir.childFile(filePath);
file.createSync(recursive: true);
if (contents != null) {
file.writeAsStringSync(contents, flush: true);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment