Unverified Commit 35f8724b authored by Christopher Fujino's avatar Christopher Fujino Committed by GitHub

[flutter_tools] fix AndroidSdk.reinitialize bad state error (#111527)

parent 619cbc9e
...@@ -354,12 +354,16 @@ class AndroidSdk { ...@@ -354,12 +354,16 @@ class AndroidSdk {
platformVersion = int.parse(numberedVersion.group(1)!); platformVersion = int.parse(numberedVersion.group(1)!);
} else { } else {
final String buildProps = platformDir.childFile('build.prop').readAsStringSync(); final String buildProps = platformDir.childFile('build.prop').readAsStringSync();
final String? versionString = const LineSplitter() final Iterable<Match> versionMatches = const LineSplitter()
.convert(buildProps) .convert(buildProps)
.map<RegExpMatch?>(_sdkVersionRe.firstMatch) .map<RegExpMatch?>(_sdkVersionRe.firstMatch)
.whereType<Match>() .whereType<Match>();
.first
.group(1); if (versionMatches.isEmpty) {
return null;
}
final String? versionString = versionMatches.first.group(1);
if (versionString == null) { if (versionString == null) {
return null; return null;
} }
......
...@@ -24,18 +24,29 @@ void main() { ...@@ -24,18 +24,29 @@ void main() {
}); });
group('AndroidSdk', () { group('AndroidSdk', () {
Directory? sdkDir; testUsingContext('constructing an AndroidSdk handles no matching lines in build.prop', () {
final Directory sdkDir = createSdkDirectory(
fileSystem: fileSystem,
withAndroidN: true,
// Does not have valid version string
buildProp: '\n\n\n',
);
config.setValue('android-sdk', sdkDir.path);
tearDown(() { try {
if (sdkDir != null) { AndroidSdk.locateAndroidSdk()!;
tryToDelete(sdkDir!); } on StateError catch (err) {
sdkDir = null; fail('sdk.reinitialize() threw a StateError:\n$err');
} }
}, overrides: <Type, Generator>{
FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(),
Config: () => config,
}); });
testUsingContext('parse sdk', () { testUsingContext('parse sdk', () {
sdkDir = createSdkDirectory(fileSystem: fileSystem); final Directory sdkDir = createSdkDirectory(fileSystem: fileSystem);
config.setValue('android-sdk', sdkDir!.path); config.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!; final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!;
expect(sdk.latestVersion, isNotNull); expect(sdk.latestVersion, isNotNull);
...@@ -47,8 +58,11 @@ void main() { ...@@ -47,8 +58,11 @@ void main() {
}); });
testUsingContext('parse sdk N', () { testUsingContext('parse sdk N', () {
sdkDir = createSdkDirectory(withAndroidN: true, fileSystem: fileSystem); final Directory sdkDir = createSdkDirectory(
config.setValue('android-sdk', sdkDir!.path); withAndroidN: true,
fileSystem: fileSystem,
);
config.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!; final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!;
expect(sdk.latestVersion, isNotNull); expect(sdk.latestVersion, isNotNull);
...@@ -60,8 +74,8 @@ void main() { ...@@ -60,8 +74,8 @@ void main() {
}); });
testUsingContext('returns sdkmanager path under cmdline tools on Linux/macOS', () { testUsingContext('returns sdkmanager path under cmdline tools on Linux/macOS', () {
sdkDir = createSdkDirectory(fileSystem: fileSystem); final Directory sdkDir = createSdkDirectory(fileSystem: fileSystem);
config.setValue('android-sdk', sdkDir!.path); config.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!; final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!;
fileSystem.file( fileSystem.file(
...@@ -77,8 +91,11 @@ void main() { ...@@ -77,8 +91,11 @@ void main() {
}); });
testUsingContext('returns sdkmanager path under cmdline tools (highest version) on Linux/macOS', () { testUsingContext('returns sdkmanager path under cmdline tools (highest version) on Linux/macOS', () {
sdkDir = createSdkDirectory(fileSystem: fileSystem, withSdkManager: false); final Directory sdkDir = createSdkDirectory(
config.setValue('android-sdk', sdkDir!.path); fileSystem: fileSystem,
withSdkManager: false,
);
config.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!; final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!;
final List<String> versions = <String>['3.0', '2.1', '1.0']; final List<String> versions = <String>['3.0', '2.1', '1.0'];
...@@ -97,8 +114,11 @@ void main() { ...@@ -97,8 +114,11 @@ void main() {
}); });
testUsingContext('Does not return sdkmanager under deprecated tools component', () { testUsingContext('Does not return sdkmanager under deprecated tools component', () {
sdkDir = createSdkDirectory(fileSystem: fileSystem, withSdkManager: false); final Directory sdkDir = createSdkDirectory(
config.setValue('android-sdk', sdkDir!.path); fileSystem: fileSystem,
withSdkManager: false,
);
config.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!; final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!;
fileSystem.file( fileSystem.file(
...@@ -114,8 +134,11 @@ void main() { ...@@ -114,8 +134,11 @@ void main() {
}); });
testUsingContext('Can look up cmdline tool from deprecated tools path', () { testUsingContext('Can look up cmdline tool from deprecated tools path', () {
sdkDir = createSdkDirectory(fileSystem: fileSystem, withSdkManager: false); final Directory sdkDir = createSdkDirectory(
config.setValue('android-sdk', sdkDir!.path); fileSystem: fileSystem,
withSdkManager: false,
);
config.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!; final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!;
fileSystem.file( fileSystem.file(
...@@ -131,8 +154,8 @@ void main() { ...@@ -131,8 +154,8 @@ void main() {
}); });
testUsingContext('Caches adb location after first access', () { testUsingContext('Caches adb location after first access', () {
sdkDir = createSdkDirectory(fileSystem: fileSystem); final Directory sdkDir = createSdkDirectory(fileSystem: fileSystem);
config.setValue('android-sdk', sdkDir!.path); config.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!; final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!;
final File adbFile = fileSystem.file( final File adbFile = fileSystem.file(
...@@ -152,8 +175,8 @@ void main() { ...@@ -152,8 +175,8 @@ void main() {
}); });
testUsingContext('returns sdkmanager.bat path under cmdline tools for windows', () { testUsingContext('returns sdkmanager.bat path under cmdline tools for windows', () {
sdkDir = createSdkDirectory(fileSystem: fileSystem); final Directory sdkDir = createSdkDirectory(fileSystem: fileSystem);
config.setValue('android-sdk', sdkDir!.path); config.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!; final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!;
fileSystem.file( fileSystem.file(
...@@ -170,8 +193,8 @@ void main() { ...@@ -170,8 +193,8 @@ void main() {
}); });
testUsingContext('returns sdkmanager version', () { testUsingContext('returns sdkmanager version', () {
sdkDir = createSdkDirectory(fileSystem: fileSystem); final Directory sdkDir = createSdkDirectory(fileSystem: fileSystem);
config.setValue('android-sdk', sdkDir!.path); config.setValue('android-sdk', sdkDir.path);
processManager.addCommand( processManager.addCommand(
const FakeCommand( const FakeCommand(
command: <String>[ command: <String>[
...@@ -192,14 +215,12 @@ void main() { ...@@ -192,14 +215,12 @@ void main() {
}); });
testUsingContext('returns validate sdk is well formed', () { testUsingContext('returns validate sdk is well formed', () {
sdkDir = createBrokenSdkDirectory( final Directory sdkDir = createBrokenSdkDirectory(fileSystem: fileSystem);
fileSystem: fileSystem,
);
processManager.addCommand(const FakeCommand(command: <String>[ processManager.addCommand(const FakeCommand(command: <String>[
'/.tmp_rand0/flutter_mock_android_sdk.rand0/cmdline-tools/latest/bin/sdkmanager', '/.tmp_rand0/flutter_mock_android_sdk.rand0/cmdline-tools/latest/bin/sdkmanager',
'--version', '--version',
])); ]));
config.setValue('android-sdk', sdkDir!.path); config.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!; final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!;
final List<String> validationIssues = sdk.validateSdkWellFormed(); final List<String> validationIssues = sdk.validateSdkWellFormed();
...@@ -215,8 +236,8 @@ void main() { ...@@ -215,8 +236,8 @@ void main() {
}); });
testUsingContext('does not throw on sdkmanager version check failure', () { testUsingContext('does not throw on sdkmanager version check failure', () {
sdkDir = createSdkDirectory(fileSystem: fileSystem); final Directory sdkDir = createSdkDirectory(fileSystem: fileSystem);
config.setValue('android-sdk', sdkDir!.path); config.setValue('android-sdk', sdkDir.path);
processManager.addCommand( processManager.addCommand(
const FakeCommand( const FakeCommand(
command: <String>[ command: <String>[
...@@ -240,8 +261,8 @@ void main() { ...@@ -240,8 +261,8 @@ void main() {
}); });
testUsingContext('throws on sdkmanager version check if sdkmanager not found', () { testUsingContext('throws on sdkmanager version check if sdkmanager not found', () {
sdkDir = createSdkDirectory(withSdkManager: false, fileSystem: fileSystem); final Directory sdkDir = createSdkDirectory(withSdkManager: false, fileSystem: fileSystem);
config.setValue('android-sdk', sdkDir!.path); config.setValue('android-sdk', sdkDir.path);
processManager.excludedExecutables.add('/.tmp_rand0/flutter_mock_android_sdk.rand0/cmdline-tools/latest/bin/sdkmanager'); processManager.excludedExecutables.add('/.tmp_rand0/flutter_mock_android_sdk.rand0/cmdline-tools/latest/bin/sdkmanager');
final AndroidSdk? sdk = AndroidSdk.locateAndroidSdk(); final AndroidSdk? sdk = AndroidSdk.locateAndroidSdk();
...@@ -254,8 +275,8 @@ void main() { ...@@ -254,8 +275,8 @@ void main() {
}); });
testUsingContext('returns avdmanager path under cmdline tools', () { testUsingContext('returns avdmanager path under cmdline tools', () {
sdkDir = createSdkDirectory(fileSystem: fileSystem); final Directory sdkDir = createSdkDirectory(fileSystem: fileSystem);
config.setValue('android-sdk', sdkDir!.path); config.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!; final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!;
fileSystem.file( fileSystem.file(
...@@ -271,8 +292,8 @@ void main() { ...@@ -271,8 +292,8 @@ void main() {
}); });
testUsingContext('returns avdmanager path under cmdline tools on windows', () { testUsingContext('returns avdmanager path under cmdline tools on windows', () {
sdkDir = createSdkDirectory(fileSystem: fileSystem); final Directory sdkDir = createSdkDirectory(fileSystem: fileSystem);
config.setValue('android-sdk', sdkDir!.path); config.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!; final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!;
fileSystem.file( fileSystem.file(
...@@ -288,8 +309,8 @@ void main() { ...@@ -288,8 +309,8 @@ void main() {
}); });
testUsingContext("returns avdmanager path under tools if cmdline doesn't exist", () { testUsingContext("returns avdmanager path under tools if cmdline doesn't exist", () {
sdkDir = createSdkDirectory(fileSystem: fileSystem); final Directory sdkDir = createSdkDirectory(fileSystem: fileSystem);
config.setValue('android-sdk', sdkDir!.path); config.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!; final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!;
fileSystem.file( fileSystem.file(
...@@ -305,8 +326,8 @@ void main() { ...@@ -305,8 +326,8 @@ void main() {
}); });
testUsingContext("returns avdmanager path under tools if cmdline doesn't exist on windows", () { testUsingContext("returns avdmanager path under tools if cmdline doesn't exist on windows", () {
sdkDir = createSdkDirectory(fileSystem: fileSystem); final Directory sdkDir = createSdkDirectory(fileSystem: fileSystem);
config.setValue('android-sdk', sdkDir!.path); config.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!; final AndroidSdk sdk = AndroidSdk.locateAndroidSdk()!;
fileSystem.file( fileSystem.file(
...@@ -357,6 +378,7 @@ Directory createSdkDirectory({ ...@@ -357,6 +378,7 @@ Directory createSdkDirectory({
bool withPlatformTools = true, bool withPlatformTools = true,
bool withBuildTools = true, bool withBuildTools = true,
required FileSystem fileSystem, required FileSystem fileSystem,
String buildProp = _buildProp,
}) { }) {
final Directory dir = fileSystem.systemTempDirectory.createTempSync('flutter_mock_android_sdk.'); final Directory dir = fileSystem.systemTempDirectory.createTempSync('flutter_mock_android_sdk.');
final String exe = globals.platform.isWindows ? '.exe' : ''; final String exe = globals.platform.isWindows ? '.exe' : '';
...@@ -386,7 +408,7 @@ Directory createSdkDirectory({ ...@@ -386,7 +408,7 @@ Directory createSdkDirectory({
_createSdkFile(dir, 'platforms/android-23/android.jar'); _createSdkFile(dir, 'platforms/android-23/android.jar');
if (withAndroidN) { if (withAndroidN) {
_createSdkFile(dir, 'platforms/android-N/android.jar'); _createSdkFile(dir, 'platforms/android-N/android.jar');
_createSdkFile(dir, 'platforms/android-N/build.prop', contents: _buildProp); _createSdkFile(dir, 'platforms/android-N/build.prop', contents: buildProp);
} }
if (withSdkManager) { if (withSdkManager) {
......
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