From 09f1764d14b167cbc7562928c4ff08bc24bc032b Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Mon, 22 Jun 2020 17:04:39 -0700 Subject: [PATCH] [flutter_tools] android test cleanups (#59996) Fixes several tests that were failing under tester. Minor style cleanups otherwise --- .../android/android_studio_test.dart | 67 ++++++++-------- .../android_studio_validator_test.dart | 79 +++++++++---------- .../android/gradle_errors_test.dart | 34 ++++---- 3 files changed, 90 insertions(+), 90 deletions(-) diff --git a/packages/flutter_tools/test/general.shard/android/android_studio_test.dart b/packages/flutter_tools/test/general.shard/android/android_studio_test.dart index ebb44300853..0041995c571 100644 --- a/packages/flutter_tools/test/general.shard/android/android_studio_test.dart +++ b/packages/flutter_tools/test/general.shard/android/android_studio_test.dart @@ -28,14 +28,12 @@ const Map macStudioInfoPlist = { }, }; -class MockPlistUtils extends Mock implements PlistParser {} +final Platform linuxPlatform = FakePlatform( + operatingSystem: 'linux', + environment: {'HOME': homeLinux}, +); -Platform linuxPlatform() { - return FakePlatform( - operatingSystem: 'linux', - environment: {'HOME': homeLinux}, - ); -} +class MockPlistUtils extends Mock implements PlistParser {} Platform macPlatform() { return FakePlatform( @@ -45,45 +43,47 @@ Platform macPlatform() { } void main() { - MemoryFileSystem fs; - MockPlistUtils plistUtils; + FileSystem fileSystem; setUp(() { - fs = MemoryFileSystem(); - plistUtils = MockPlistUtils(); + fileSystem = MemoryFileSystem.test(); }); - group('pluginsPath on Linux', () { - testUsingContext('extracts custom paths from home dir', () { - const String installPath = '/opt/android-studio-with-cheese-5.0'; - const String studioHome = '$homeLinux/.AndroidStudioWithCheese5.0'; - const String homeFile = '$studioHome/system/.home'; - globals.fs.directory(installPath).createSync(recursive: true); - globals.fs.file(homeFile).createSync(recursive: true); - globals.fs.file(homeFile).writeAsStringSync(installPath); + testUsingContext('pluginsPath on Linux extracts custom paths from home dir', () { + const String installPath = '/opt/android-studio-with-cheese-5.0'; + const String studioHome = '$homeLinux/.AndroidStudioWithCheese5.0'; + const String homeFile = '$studioHome/system/.home'; + globals.fs.directory(installPath).createSync(recursive: true); + globals.fs.file(homeFile).createSync(recursive: true); + globals.fs.file(homeFile).writeAsStringSync(installPath); - final AndroidStudio studio = + final AndroidStudio studio = AndroidStudio.fromHomeDot(globals.fs.directory(studioHome)); - expect(studio, isNotNull); - expect(studio.pluginsPath, - equals('/home/me/.AndroidStudioWithCheese5.0/config/plugins')); - }, overrides: { - FileSystem: () => fs, - ProcessManager: () => FakeProcessManager.any(), - // Custom home paths are not supported on macOS nor Windows yet, - // so we force the platform to fake Linux here. - Platform: () => linuxPlatform(), - }); + expect(studio, isNotNull); + expect(studio.pluginsPath, + equals('/home/me/.AndroidStudioWithCheese5.0/config/plugins')); + }, overrides: { + FileSystem: () => fileSystem, + ProcessManager: () => FakeProcessManager.any(), + // Custom home paths are not supported on macOS nor Windows yet, + // so we force the platform to fake Linux here. + Platform: () => linuxPlatform, + FileSystemUtils: () => FileSystemUtils( + fileSystem: fileSystem, + platform: linuxPlatform, + ), }); group('pluginsPath on Mac', () { FileSystemUtils fsUtils; Platform platform; + MockPlistUtils plistUtils; setUp(() { + plistUtils = MockPlistUtils(); platform = macPlatform(); fsUtils = FileSystemUtils( - fileSystem: fs, + fileSystem: fileSystem, platform: platform, ); }); @@ -110,7 +110,7 @@ void main() { 'AndroidStudio3.3', ))); }, overrides: { - FileSystem: () => fs, + FileSystem: () => fileSystem, FileSystemUtils: () => fsUtils, ProcessManager: () => FakeProcessManager.any(), // Custom home paths are not supported on macOS nor Windows yet, @@ -168,7 +168,7 @@ void main() { 'AndroidStudio3.3', ))); }, overrides: { - FileSystem: () => fs, + FileSystem: () => fileSystem, FileSystemUtils: () => fsUtils, ProcessManager: () => FakeProcessManager.any(), // Custom home paths are not supported on macOS nor Windows yet, @@ -176,6 +176,5 @@ void main() { Platform: () => platform, PlistParser: () => plistUtils, }); - }); } diff --git a/packages/flutter_tools/test/general.shard/android/android_studio_validator_test.dart b/packages/flutter_tools/test/general.shard/android/android_studio_validator_test.dart index 792c7ae23fa..2e218e0a973 100644 --- a/packages/flutter_tools/test/general.shard/android/android_studio_validator_test.dart +++ b/packages/flutter_tools/test/general.shard/android/android_studio_validator_test.dart @@ -17,59 +17,56 @@ import '../../src/context.dart'; const String home = '/home/me'; -Platform linuxPlatform() { - return FakePlatform( - operatingSystem: 'linux', - environment: {'HOME': home} - ); -} +final Platform linuxPlatform = FakePlatform( + operatingSystem: 'linux', + environment: {'HOME': home} +); void main() { + FileSystem fileSystem; + + setUp(() { + fileSystem = MemoryFileSystem.test(); + }); + group('NoAndroidStudioValidator', () { testUsingContext('shows Android Studio as "not available" when not available.', () async { final NoAndroidStudioValidator validator = NoAndroidStudioValidator(); expect((await validator.validate()).type, equals(ValidationType.notAvailable)); }, overrides: { - // Custom home paths are not supported on macOS nor Windows yet, - // so we force the platform to fake Linux here. - Platform: () => linuxPlatform(), + Platform: () => linuxPlatform, }); }); - group('AndroidStudioValidator', () { - MemoryFileSystem fs; - MockProcessManager mockProcessManager; - setUp(() { - fs = MemoryFileSystem(); - mockProcessManager = MockProcessManager(); + testUsingContext('AndroidStudioValidator gives doctor error on java crash', () async { + when(globals.processManager.canRun(any)).thenReturn(true); + when(globals.processManager.runSync(any)).thenAnswer((Invocation _) { + throw const ProcessException('java', ['--version']); }); + const String installPath = '/opt/android-studio-with-cheese-5.0'; + const String studioHome = '$home/.AndroidStudioWithCheese5.0'; + const String homeFile = '$studioHome/system/.home'; + globals.fs.directory(installPath).createSync(recursive: true); + globals.fs.file(homeFile).createSync(recursive: true); + globals.fs.file(homeFile).writeAsStringSync(installPath); - testUsingContext('gives doctor error on java crash', () async { - when(mockProcessManager.canRun(any)).thenReturn(true); - when(mockProcessManager.runSync(any)).thenAnswer((Invocation _) { - throw const ProcessException('java', ['--version']); - }); - const String installPath = '/opt/android-studio-with-cheese-5.0'; - const String studioHome = '$home/.AndroidStudioWithCheese5.0'; - const String homeFile = '$studioHome/system/.home'; - globals.fs.directory(installPath).createSync(recursive: true); - globals.fs.file(homeFile).createSync(recursive: true); - globals.fs.file(homeFile).writeAsStringSync(installPath); - - // This checks that running the validator doesn't throw an unhandled - // exception and that the ProcessException makes it into the error - // message list. - for (final DoctorValidator validator in AndroidStudioValidator.allValidators) { - final ValidationResult result = await validator.validate(); - expect(result.messages.where((ValidationMessage message) { - return message.isError && message.message.contains('ProcessException'); - }).isNotEmpty, true); - } - }, overrides: { - FileSystem: () => fs, - ProcessManager: () => mockProcessManager, - Platform: () => linuxPlatform(), - }); + // This checks that running the validator doesn't throw an unhandled + // exception and that the ProcessException makes it into the error + // message list. + for (final DoctorValidator validator in AndroidStudioValidator.allValidators) { + final ValidationResult result = await validator.validate(); + expect(result.messages.where((ValidationMessage message) { + return message.isError && message.message.contains('ProcessException'); + }).isNotEmpty, true); + } + }, overrides: { + FileSystem: () => fileSystem, + ProcessManager: () => MockProcessManager(), + Platform: () => linuxPlatform, + FileSystemUtils: () => FileSystemUtils( + fileSystem: fileSystem, + platform: linuxPlatform, + ), }); } diff --git a/packages/flutter_tools/test/general.shard/android/gradle_errors_test.dart b/packages/flutter_tools/test/general.shard/android/gradle_errors_test.dart index 32228642c3f..c6298da7154 100644 --- a/packages/flutter_tools/test/general.shard/android/gradle_errors_test.dart +++ b/packages/flutter_tools/test/general.shard/android/gradle_errors_test.dart @@ -52,7 +52,7 @@ at org.gradle.wrapper.Install.createDist(Install.java:48) at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:128) at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)'''; - expect(testErrorMessage(errorMessage, networkErrorHandler), isTrue); + expect(formatTestErrorMessage(errorMessage, networkErrorHandler), isTrue); expect(await networkErrorHandler.handler(), equals(GradleBuildStatus.retry)); expect(testLogger.errorText, @@ -80,7 +80,7 @@ at org.gradle.wrapper.Install.createDist(Install.java:48) at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:128) at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)'''; - expect(testErrorMessage(errorMessage, networkErrorHandler), isTrue); + expect(formatTestErrorMessage(errorMessage, networkErrorHandler), isTrue); expect(await networkErrorHandler.handler(), equals(GradleBuildStatus.retry)); expect(testLogger.errorText, @@ -99,7 +99,7 @@ Exception in thread "main" java.lang.RuntimeException: Timeout of 120000 reached at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:128) at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)'''; - expect(testErrorMessage(errorMessage, networkErrorHandler), isTrue); + expect(formatTestErrorMessage(errorMessage, networkErrorHandler), isTrue); expect(await networkErrorHandler.handler(), equals(GradleBuildStatus.retry)); expect(testLogger.errorText, @@ -134,7 +134,7 @@ Exception in thread "main" javax.net.ssl.SSLHandshakeException: Remote host clos at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:128) at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)'''; - expect(testErrorMessage(errorMessage, networkErrorHandler), isTrue); + expect(formatTestErrorMessage(errorMessage, networkErrorHandler), isTrue); expect(await networkErrorHandler.handler(), equals(GradleBuildStatus.retry)); expect(testLogger.errorText, @@ -161,7 +161,7 @@ Exception in thread "main" java.io.FileNotFoundException: https://downloads.grad at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:128) at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)'''; - expect(testErrorMessage(errorMessage, networkErrorHandler), isTrue); + expect(formatTestErrorMessage(errorMessage, networkErrorHandler), isTrue); expect(await networkErrorHandler.handler(), equals(GradleBuildStatus.retry)); expect(testLogger.errorText, @@ -199,7 +199,7 @@ Exception in thread "main" java.net.SocketException: Connection reset at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:128) at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)'''; - expect(testErrorMessage(errorMessage, networkErrorHandler), isTrue); + expect(formatTestErrorMessage(errorMessage, networkErrorHandler), isTrue); expect(await networkErrorHandler.handler(), equals(GradleBuildStatus.retry)); expect(testLogger.errorText, @@ -224,7 +224,7 @@ A problem occurred configuring root project 'android'. > Could not get resource 'https://jcenter.bintray.com/net/sf/proguard/proguard-parent/6.0.3/proguard-parent-6.0.3.pom'. > Could not GET 'https://jcenter.bintray.com/net/sf/proguard/proguard-parent/6.0.3/proguard-parent-6.0.3.pom'. Received status code 504 from server: Gateway Time-out'''; - expect(testErrorMessage(errorMessage, networkErrorHandler), isTrue); + expect(formatTestErrorMessage(errorMessage, networkErrorHandler), isTrue); expect(await networkErrorHandler.handler(), equals(GradleBuildStatus.retry)); expect(testLogger.errorText, @@ -242,7 +242,7 @@ A problem occurred configuring root project 'android'. Permission denied Command: /home/android/gradlew assembleRelease '''; - expect(testErrorMessage(errorMessage, permissionDeniedErrorHandler), isTrue); + expect(formatTestErrorMessage(errorMessage, permissionDeniedErrorHandler), isTrue); expect(await permissionDeniedErrorHandler.handler(), equals(GradleBuildStatus.exit)); expect( @@ -303,7 +303,7 @@ Command: /home/android/gradlew assembleRelease expect(status, equals(GradleBuildStatus.exit)); }, overrides: { - FileSystem: () => MemoryFileSystem(), + FileSystem: () => MemoryFileSystem.test(), ProcessManager: () => MockProcessManager(), Usage: () => mockUsage, }); @@ -335,7 +335,7 @@ Command: /home/android/gradlew assembleRelease expect(status, equals(GradleBuildStatus.exit)); }, overrides: { - FileSystem: () => MemoryFileSystem(), + FileSystem: () => MemoryFileSystem.test(), ProcessManager: () => MockProcessManager(), Usage: () => mockUsage, }); @@ -361,7 +361,7 @@ Command: /home/android/gradlew assembleRelease expect(status, equals(GradleBuildStatus.exit)); }, overrides: { - FileSystem: () => MemoryFileSystem(), + FileSystem: () => MemoryFileSystem.test(), ProcessManager: () => MockProcessManager(), Usage: () => mockUsage, }); @@ -392,7 +392,7 @@ Command: /home/android/gradlew assembleRelease )).called(1); expect(status, equals(GradleBuildStatus.retryWithAarPlugins)); }, overrides: { - FileSystem: () => MemoryFileSystem(), + FileSystem: () => MemoryFileSystem.test(), ProcessManager: () => MockProcessManager(), Usage: () => mockUsage, }); @@ -404,7 +404,7 @@ Command: /home/android/gradlew assembleRelease Permission denied Command: /home/android/gradlew assembleRelease '''; - expect(testErrorMessage(errorMessage, permissionDeniedErrorHandler), isTrue); + expect(formatTestErrorMessage(errorMessage, permissionDeniedErrorHandler), isTrue); }); testUsingContext('handler', () async { @@ -539,6 +539,7 @@ assembleFooTest GradleUtils: () => FakeGradleUtils(), Platform: () => fakePlatform('android'), ProcessManager: () => mockProcessManager, + FileSystem: () => MemoryFileSystem.test(), }); testUsingContext('handler - without flavor', () async { @@ -586,13 +587,14 @@ assembleProfile GradleUtils: () => FakeGradleUtils(), Platform: () => fakePlatform('android'), ProcessManager: () => mockProcessManager, + FileSystem: () => MemoryFileSystem.test(), }); }); } class MockUsage extends Mock implements Usage {} -bool testErrorMessage(String errorMessage, GradleHandledError error) { +bool formatTestErrorMessage(String errorMessage, GradleHandledError error) { return errorMessage .split('\n') .any((String line) => error.test(line)); @@ -600,7 +602,9 @@ bool testErrorMessage(String errorMessage, GradleHandledError error) { Platform fakePlatform(String name) { return FakePlatform( - environment: {}, + environment: { + 'HOME': '/', + }, operatingSystem: name, ); }