diff --git a/packages/flutter_tools/test/general.shard/cache_test.dart b/packages/flutter_tools/test/general.shard/cache_test.dart index 84b70e5748a..bc1ed7feb42 100644 --- a/packages/flutter_tools/test/general.shard/cache_test.dart +++ b/packages/flutter_tools/test/general.shard/cache_test.dart @@ -192,12 +192,12 @@ void main() { }); testWithoutContext('should not be up to date, if some cached artifact is not', () async { - final CachedArtifact artifact1 = MockCachedArtifact(); - final CachedArtifact artifact2 = MockCachedArtifact(); + final CachedArtifact artifact1 = FakeSecondayCachedArtifact() + ..upToDate = true; + final CachedArtifact artifact2 = FakeSecondayCachedArtifact() + ..upToDate = false; final FileSystem fileSystem = MemoryFileSystem.test(); - when(artifact1.isUpToDate(fileSystem)).thenAnswer((Invocation _) => Future.value(true)); - when(artifact2.isUpToDate(fileSystem)).thenAnswer((Invocation _) => Future.value(false)); final Cache cache = Cache.test( fileSystem: fileSystem, artifacts: [artifact1, artifact2], @@ -208,12 +208,11 @@ void main() { }); testWithoutContext('should be up to date, if all cached artifacts are', () async { - final CachedArtifact artifact1 = MockCachedArtifact(); - final CachedArtifact artifact2 = MockCachedArtifact(); + final FakeSecondayCachedArtifact artifact1 = FakeSecondayCachedArtifact() + ..upToDate = true; + final FakeSecondayCachedArtifact artifact2 = FakeSecondayCachedArtifact() + ..upToDate = true; final FileSystem fileSystem = MemoryFileSystem.test(); - - when(artifact1.isUpToDate(fileSystem)).thenAnswer((Invocation _) => Future.value(true)); - when(artifact2.isUpToDate(fileSystem)).thenAnswer((Invocation _) => Future.value(true)); final Cache cache = Cache.test( fileSystem: fileSystem, artifacts: [artifact1, artifact2], @@ -224,12 +223,12 @@ void main() { }); testWithoutContext('should update cached artifacts which are not up to date', () async { - final CachedArtifact artifact1 = MockCachedArtifact(); - final CachedArtifact artifact2 = MockCachedArtifact(); + final FakeSecondayCachedArtifact artifact1 = FakeSecondayCachedArtifact() + ..upToDate = true; + final FakeSecondayCachedArtifact artifact2 = FakeSecondayCachedArtifact() + ..upToDate = false; final FileSystem fileSystem = MemoryFileSystem.test(); - when(artifact1.isUpToDate(fileSystem)).thenAnswer((Invocation _) => Future.value(true)); - when(artifact2.isUpToDate(fileSystem)).thenAnswer((Invocation _) => Future.value(false)); final Cache cache = Cache.test( fileSystem: fileSystem, artifacts: [artifact1, artifact2], @@ -237,28 +236,25 @@ void main() { ); await cache.updateAll({ - null, + DevelopmentArtifact.universal, }); - verifyNever(artifact1.update(any, any, any, any)); - verify(artifact2.update(any, any, any, any)); + expect(artifact1.didUpdate, false); + expect(artifact2.didUpdate, true); }); testWithoutContext("getter dyLdLibEntry concatenates the output of each artifact's dyLdLibEntry getter", () async { - final IosUsbArtifacts artifact1 = MockIosUsbArtifacts(); - final IosUsbArtifacts artifact2 = MockIosUsbArtifacts(); - final IosUsbArtifacts artifact3 = MockIosUsbArtifacts(); - when(artifact1.environment) - .thenReturn({ - 'DYLD_LIBRARY_PATH': '/path/to/alpha:/path/to/beta', - }); - when(artifact2.environment) - .thenReturn({ - 'DYLD_LIBRARY_PATH': '/path/to/gamma:/path/to/delta:/path/to/epsilon', - }); - when(artifact3.environment) - .thenReturn({ - 'DYLD_LIBRARY_PATH': '', - }); + final FakeIosUsbArtifacts artifact1 = FakeIosUsbArtifacts(); + final FakeIosUsbArtifacts artifact2 = FakeIosUsbArtifacts(); + final FakeIosUsbArtifacts artifact3 = FakeIosUsbArtifacts(); + artifact1.environment = { + 'DYLD_LIBRARY_PATH': '/path/to/alpha:/path/to/beta', + }; + artifact2.environment = { + 'DYLD_LIBRARY_PATH': '/path/to/gamma:/path/to/delta:/path/to/epsilon', + }; + artifact3.environment = { + 'DYLD_LIBRARY_PATH': '', + }; final Cache cache = Cache.test( artifacts: [artifact1, artifact2, artifact3], processManager: FakeProcessManager.any(), @@ -272,15 +268,16 @@ void main() { }); testWithoutContext('failed storage.googleapis.com download shows China warning', () async { - final CachedArtifact artifact1 = MockCachedArtifact(); - final CachedArtifact artifact2 = MockCachedArtifact(); - when(artifact1.isUpToDate(any)).thenAnswer((Invocation _) => Future.value(false)); - when(artifact2.isUpToDate(any)).thenAnswer((Invocation _) => Future.value(false)); final InternetAddress address = (await InternetAddress.lookup('storage.googleapis.com')).first; - when(artifact1.update(any, any, any, any)).thenThrow(SocketException( + final FakeSecondayCachedArtifact artifact1 = FakeSecondayCachedArtifact() + ..upToDate = false; + final FakeSecondayCachedArtifact artifact2 = FakeSecondayCachedArtifact() + ..upToDate = false + ..updateException = SocketException( 'Connection reset by peer', address: address, - )); + ); + final BufferLogger logger = BufferLogger.test(); final Cache cache = Cache.test( artifacts: [artifact1, artifact2], @@ -288,12 +285,12 @@ void main() { logger: logger, ); await expectLater( - () => cache.updateAll({null}), + () => cache.updateAll({DevelopmentArtifact.universal}), throwsException, ); - verify(artifact1.update(any, any, any, any)); + expect(artifact1.didUpdate, true); // Don't continue when retrieval fails. - verifyNever(artifact2.update(any, any, any, any)); + expect(artifact2.didUpdate, false); expect( logger.errorText, contains('https://flutter.dev/community/china'), @@ -646,10 +643,10 @@ void main() { testWithoutContext('Cache can delete stampfiles of artifacts', () { final FileSystem fileSystem = MemoryFileSystem.test(); - final ArtifactSet artifactSet = MockIosUsbArtifacts(); + final FakeIosUsbArtifacts artifactSet = FakeIosUsbArtifacts(); final BufferLogger logger = BufferLogger.test(); - when(artifactSet.stampName).thenReturn('STAMP'); + artifactSet.stampName = 'STAMP'; final Cache cache = Cache( artifacts: [ artifactSet, @@ -674,10 +671,10 @@ void main() { testWithoutContext('Cache does not attempt to delete already missing stamp files', () { final FileSystem fileSystem = MemoryFileSystem.test(); - final ArtifactSet artifactSet = MockIosUsbArtifacts(); + final FakeIosUsbArtifacts artifactSet = FakeIosUsbArtifacts(); final BufferLogger logger = BufferLogger.test(); - when(artifactSet.stampName).thenReturn('STAMP'); + artifactSet.stampName = 'STAMP'; final Cache cache = Cache( artifacts: [ artifactSet, @@ -701,10 +698,10 @@ void main() { testWithoutContext('Cache catches file system exception from missing tool stamp file', () { final FileSystem fileSystem = MemoryFileSystem.test(); - final ArtifactSet artifactSet = MockIosUsbArtifacts(); + final FakeIosUsbArtifacts artifactSet = FakeIosUsbArtifacts(); final BufferLogger logger = BufferLogger.test(); - when(artifactSet.stampName).thenReturn('STAMP'); + artifactSet.stampName = 'STAMP'; final Cache cache = Cache( artifacts: [ artifactSet, @@ -971,8 +968,33 @@ class FakeDownloadedArtifact extends CachedArtifact { Future updateInner(ArtifactUpdater artifactUpdater, FileSystem fileSystem, OperatingSystemUtils operatingSystemUtils) async { } } -class MockCachedArtifact extends Mock implements CachedArtifact {} -class MockIosUsbArtifacts extends Mock implements IosUsbArtifacts {} +class FakeSecondayCachedArtifact extends Fake implements CachedArtifact { + bool upToDate = false; + bool didUpdate = false; + Exception updateException; + + @override + Future isUpToDate(FileSystem fileSystem) async => upToDate; + + @override + Future update(ArtifactUpdater artifactUpdater, Logger logger, FileSystem fileSystem, OperatingSystemUtils operatingSystemUtils) async { + if (updateException != null) { + throw updateException; + } + didUpdate = true; + } + + @override + DevelopmentArtifact get developmentArtifact => DevelopmentArtifact.universal; +} + +class FakeIosUsbArtifacts extends Fake implements IosUsbArtifacts { + @override + Map environment = {}; + + @override + String stampName = 'ios-usb'; +} class MockCache extends Mock implements Cache {} class FakeVersionedPackageResolver extends Fake implements VersionedPackageResolver { @@ -1015,6 +1037,7 @@ class FakeCache extends Cache { return stampFile; } } + class FakeAndroidSdk extends Fake implements AndroidSdk { bool reinitialized = false;