diff --git a/packages/flutter_tools/test/general.shard/cache_test.dart b/packages/flutter_tools/test/general.shard/cache_test.dart index 79274ad366..9b7538063c 100644 --- a/packages/flutter_tools/test/general.shard/cache_test.dart +++ b/packages/flutter_tools/test/general.shard/cache_test.dart @@ -22,6 +22,7 @@ import 'package:mockito/mockito.dart'; import '../src/common.dart'; import '../src/context.dart'; +import '../src/fakes.dart'; const FakeCommand unameCommandForX64 = FakeCommand( command: [ @@ -126,7 +127,7 @@ void main() { throw const FileSystemException('stamp write failed'); }); final FakeSimpleArtifact artifact = FakeSimpleArtifact(mockCache); - await artifact.update(MockArtifactUpdater(), logger, fileSystem, MockOperatingSystemUtils()); + await artifact.update(FakeArtifactUpdater(), logger, fileSystem, FakeOperatingSystemUtils()); expect(logger.errorText, contains('stamp write failed')); }); @@ -142,7 +143,7 @@ void main() { when(mockCache.getArtifactDirectory(any)).thenReturn(artifactDir); when(mockCache.getDownloadDir()).thenReturn(downloadDir); final FakeSimpleArtifact artifact = FakeSimpleArtifact(mockCache); - await artifact.update(MockArtifactUpdater(), logger, fileSystem, MockOperatingSystemUtils()); + await artifact.update(FakeArtifactUpdater(), logger, fileSystem, FakeOperatingSystemUtils()); expect(logger.errorText, contains('No known version for the artifact name "fake"')); }); @@ -276,8 +277,7 @@ void main() { final CachedArtifact artifact2 = MockCachedArtifact(); when(artifact1.isUpToDate(any)).thenAnswer((Invocation _) => Future.value(false)); when(artifact2.isUpToDate(any)).thenAnswer((Invocation _) => Future.value(false)); - final MockInternetAddress address = MockInternetAddress(); - when(address.host).thenReturn('storage.googleapis.com'); + final InternetAddress address = (await InternetAddress.lookup('storage.googleapis.com')).first; when(artifact1.update(any, any, any, any)).thenThrow(SocketException( 'Connection reset by peer', address: address, @@ -320,7 +320,7 @@ void main() { }); testWithoutContext('EngineCachedArtifact makes binary dirs readable and executable by all', () async { - final OperatingSystemUtils operatingSystemUtils = MockOperatingSystemUtils(); + final FakeOperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils(); final MockCache cache = MockCache(); final FileSystem fileSystem = MemoryFileSystem.test(); final Directory artifactDir = fileSystem.systemTempDirectory.createTempSync('flutter_cache_test_artifact.'); @@ -338,7 +338,7 @@ void main() { ], requiredArtifacts: DevelopmentArtifact.universal, ); - await artifact.updateInner(MockArtifactUpdater(), fileSystem, operatingSystemUtils); + await artifact.updateInner(FakeArtifactUpdater(), fileSystem, operatingSystemUtils); final Directory dir = fileSystem.systemTempDirectory .listSync(recursive: true) .whereType() @@ -346,11 +346,11 @@ void main() { expect(dir, isNotNull); expect(dir.path, artifactDir.childDirectory('bin_dir').path); - verify(operatingSystemUtils.chmod(argThat(hasPath(dir.path)), 'a+r,a+x')); + expect(operatingSystemUtils.chmods, >[['/.tmp_rand0/flutter_cache_test_artifact.rand0/bin_dir', 'a+r,a+x']]); }); testWithoutContext('EngineCachedArtifact removes unzipped FlutterMacOS.framework before replacing', () async { - final OperatingSystemUtils operatingSystemUtils = MockOperatingSystemUtils(); + final OperatingSystemUtils operatingSystemUtils = FakeOperatingSystemUtils(); final MockCache cache = MockCache(); final FileSystem fileSystem = MemoryFileSystem.test(); final Directory artifactDir = fileSystem.systemTempDirectory.createTempSync('flutter_cache_test_artifact.'); @@ -371,7 +371,7 @@ void main() { ], requiredArtifacts: DevelopmentArtifact.universal, ); - await artifact.updateInner(MockArtifactUpdater(), fileSystem, operatingSystemUtils); + await artifact.updateInner(FakeArtifactUpdater(), fileSystem, operatingSystemUtils); expect(unzippedFramework, exists); expect(staleFile, isNot(exists)); }); @@ -438,19 +438,18 @@ void main() { fileSystem: fileSystem, processManager: FakeProcessManager.any(), ); - final MockVersionedPackageResolver mockPackageResolver = MockVersionedPackageResolver(); + final FakeVersionedPackageResolver packageResolver = FakeVersionedPackageResolver(); final FlutterRunnerDebugSymbols flutterRunnerDebugSymbols = FlutterRunnerDebugSymbols( cache, - packageResolver: mockPackageResolver, + packageResolver: packageResolver, platform: FakePlatform(operatingSystem: 'linux'), ); - when(mockPackageResolver.resolveUrl(any, any)).thenReturn(''); - await flutterRunnerDebugSymbols.updateInner(MockArtifactUpdater(), fileSystem, MockOperatingSystemUtils()); + await flutterRunnerDebugSymbols.updateInner(FakeArtifactUpdater(), fileSystem, FakeOperatingSystemUtils()); - verifyInOrder([ - mockPackageResolver.resolveUrl('fuchsia-debug-symbols-x64', any), - mockPackageResolver.resolveUrl('fuchsia-debug-symbols-arm64', any), + expect(packageResolver.resolved, >[ + ['fuchsia-debug-symbols-x64', null], + ['fuchsia-debug-symbols-arm64', null], ]); }); @@ -659,7 +658,7 @@ void main() { logger: logger, fileSystem: fileSystem, platform: FakePlatform(), - osUtils: MockOperatingSystemUtils(), + osUtils: FakeOperatingSystemUtils(), rootOverride: fileSystem.currentDirectory, ); final File toolStampFile = fileSystem.file('bin/cache/flutter_tools.stamp'); @@ -687,7 +686,7 @@ void main() { logger: logger, fileSystem: fileSystem, platform: FakePlatform(), - osUtils: MockOperatingSystemUtils(), + osUtils: FakeOperatingSystemUtils(), rootOverride: fileSystem.currentDirectory, ); final File toolStampFile = fileSystem.file('bin/cache/flutter_tools.stamp'); @@ -714,7 +713,7 @@ void main() { logger: logger, fileSystem: fileSystem, platform: FakePlatform(), - osUtils: MockOperatingSystemUtils(), + osUtils: FakeOperatingSystemUtils(), rootOverride: fileSystem.currentDirectory, ); @@ -725,46 +724,40 @@ void main() { testWithoutContext('FlutterWebSdk deletes previous directory contents', () { final MemoryFileSystem fileSystem = MemoryFileSystem.test(); - final Directory webStuff = fileSystem.directory('web-stuff'); - final MockCache cache = MockCache(); - final MockArtifactUpdater artifactUpdater = MockArtifactUpdater(); + final Cache cache = Cache.test(processManager: FakeProcessManager.any(), fileSystem: fileSystem); + final Directory webCacheDirectory = cache.getWebSdkDirectory(); + final FakeArtifactUpdater artifactUpdater = FakeArtifactUpdater(); final FlutterWebSdk webSdk = FlutterWebSdk(cache, platform: FakePlatform(operatingSystem: 'linux')); - when(cache.getWebSdkDirectory()).thenReturn(webStuff); - when(artifactUpdater.downloadZipArchive('Downloading Web SDK...', any, any)) - .thenAnswer((Invocation invocation) async { - final Directory location = invocation.positionalArguments[2] as Directory; - location.createSync(recursive: true); - location.childFile('foo').createSync(); - }); - webStuff.childFile('bar').createSync(recursive: true); + artifactUpdater.onDownloadZipArchive = (String message, Uri uri, Directory location) { + location.createSync(recursive: true); + location.childFile('foo').createSync(); + }; + webCacheDirectory.childFile('bar').createSync(recursive: true); - webSdk.updateInner(artifactUpdater, fileSystem, MockOperatingSystemUtils()); + webSdk.updateInner(artifactUpdater, fileSystem, FakeOperatingSystemUtils()); - expect(webStuff.childFile('foo'), exists); - expect(webStuff.childFile('bar'), isNot(exists)); + expect(webCacheDirectory.childFile('foo'), exists); + expect(webCacheDirectory.childFile('bar'), isNot(exists)); }); testWithoutContext('FlutterWebSdk uses tryToDelete to handle directory edge cases', () async { final FileExceptionHandler handler = FileExceptionHandler(); final MemoryFileSystem fileSystem = MemoryFileSystem.test(opHandle: handler.opHandle); - final Directory webStuff = fileSystem.directory('web-stuff'); - final MockCache cache = MockCache(); - final MockArtifactUpdater artifactUpdater = MockArtifactUpdater(); + final Cache cache = Cache.test(processManager: FakeProcessManager.any(), fileSystem: fileSystem); + final Directory webCacheDirectory = cache.getWebSdkDirectory(); + final FakeArtifactUpdater artifactUpdater = FakeArtifactUpdater(); final FlutterWebSdk webSdk = FlutterWebSdk(cache, platform: FakePlatform(operatingSystem: 'linux')); - when(cache.getWebSdkDirectory()).thenReturn(webStuff); - when(artifactUpdater.downloadZipArchive('Downloading Web SDK...', any, any)) - .thenAnswer((Invocation invocation) async { - final Directory location = invocation.positionalArguments[2] as Directory; - location.createSync(recursive: true); - location.childFile('foo').createSync(); - }); - webStuff.childFile('bar').createSync(recursive: true); - handler.addError(webStuff, FileSystemOp.delete, const FileSystemException('', '', OSError('', 2))); + artifactUpdater.onDownloadZipArchive = (String message, Uri uri, Directory location) { + location.createSync(recursive: true); + location.childFile('foo').createSync(); + }; + webCacheDirectory.childFile('bar').createSync(recursive: true); + handler.addError(webCacheDirectory, FileSystemOp.delete, const FileSystemException('', '', OSError('', 2))); - await expectLater(() => webSdk.updateInner(artifactUpdater, fileSystem, MockOperatingSystemUtils()), throwsToolExit( - message: RegExp('The Flutter tool tried to delete the file or directory web-stuff but was unable to'), + await expectLater(() => webSdk.updateInner(artifactUpdater, fileSystem, FakeOperatingSystemUtils()), throwsToolExit( + message: RegExp('The Flutter tool tried to delete the file or directory cache/bin/cache/flutter_web_sdk but was unable to'), )); }); @@ -776,7 +769,7 @@ void main() { fileSystem: fileSystem, logger: logger, platform: FakePlatform(), - osUtils: MockOperatingSystemUtils() + osUtils: FakeOperatingSystemUtils() ); final File file = fileSystem.file('stamp'); cache.stampFile = file; @@ -797,10 +790,10 @@ void main() { final FileSystem fileSystem = MemoryFileSystem.test(); final Logger logger = BufferLogger.test(); final FakeCache cache = FakeCache( - fileSystem: fileSystem, - logger: logger, - platform: FakePlatform(), - osUtils: MockOperatingSystemUtils() + fileSystem: fileSystem, + logger: logger, + platform: FakePlatform(), + osUtils: FakeOperatingSystemUtils() ); final File file = fileSystem.file('stamp')..writeAsStringSync('ABC '); @@ -816,7 +809,7 @@ void main() { final PubDependencies pubDependencies = PubDependencies( flutterRoot: () => '', logger: logger, - pub: () => MockPub(), + pub: () => FakePub(), ); expect(await pubDependencies.isUpToDate(fileSystem), false); // no package config @@ -854,19 +847,16 @@ void main() { testWithoutContext('PubDependencies updates via pub get', () async { final BufferLogger logger = BufferLogger.test(); final MemoryFileSystem fileSystem = MemoryFileSystem.test(); - final MockPub pub = MockPub(); + final FakePub pub = FakePub(); final PubDependencies pubDependencies = PubDependencies( flutterRoot: () => '', logger: logger, pub: () => pub, ); - await pubDependencies.update(MockArtifactUpdater(), logger, fileSystem, MockOperatingSystemUtils()); + await pubDependencies.update(FakeArtifactUpdater(), logger, fileSystem, FakeOperatingSystemUtils()); - verify(pub.get( - context: PubContext.pubGet, - directory: 'packages/flutter_tools', - )).called(1); + expect(pub.calledGet, 1); }); group('AndroidMavenArtifacts', () { @@ -897,7 +887,7 @@ void main() { gradleWrapperDir.childFile('gradlew').writeAsStringSync('irrelevant'); gradleWrapperDir.childFile('gradlew.bat').writeAsStringSync('irrelevant'); - await mavenArtifacts.update(MockArtifactUpdater(), BufferLogger.test(), memoryFileSystem, MockOperatingSystemUtils()); + await mavenArtifacts.update(FakeArtifactUpdater(), BufferLogger.test(), memoryFileSystem, FakeOperatingSystemUtils()); expect(await mavenArtifacts.isUpToDate(memoryFileSystem), isFalse); expect(fakeAndroidSdk.reinitialized, true); @@ -922,7 +912,7 @@ void main() { final AndroidMavenArtifacts mavenArtifacts = AndroidMavenArtifacts(cache, platform: FakePlatform(operatingSystem: 'linux')); expect(await mavenArtifacts.isUpToDate(memoryFileSystem), isFalse); - await mavenArtifacts.update(MockArtifactUpdater(), BufferLogger.test(), memoryFileSystem, MockOperatingSystemUtils()); + await mavenArtifacts.update(FakeArtifactUpdater(), BufferLogger.test(), memoryFileSystem, FakeOperatingSystemUtils()); expect(await mavenArtifacts.isUpToDate(memoryFileSystem), isFalse); }, overrides: { @@ -982,14 +972,28 @@ class FakeDownloadedArtifact extends CachedArtifact { Future updateInner(ArtifactUpdater artifactUpdater, FileSystem fileSystem, OperatingSystemUtils operatingSystemUtils) async { } } -class MockArtifactUpdater extends Mock implements ArtifactUpdater {} class MockCachedArtifact extends Mock implements CachedArtifact {} class MockIosUsbArtifacts extends Mock implements IosUsbArtifacts {} -class MockInternetAddress extends Mock implements InternetAddress {} class MockCache extends Mock implements Cache {} -class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {} -class MockVersionedPackageResolver extends Mock implements VersionedPackageResolver {} -class MockPub extends Mock implements Pub {} + +class FakeVersionedPackageResolver extends Fake implements VersionedPackageResolver { + final List> resolved = >[]; + + @override + String resolveUrl(String packageName, String version) { + resolved.add([packageName, version]); + return ''; + } +} + +class FakePub extends Fake implements Pub { + int calledGet = 0; + + @override + Future get({PubContext context, String directory, bool skipIfAbsent = false, bool upgrade = false, bool offline = false, bool generateSyntheticPackage = false, String flutterRootOverride, bool checkUpToDate = false}) async { + calledGet += 1; + } +} class FakeCache extends Cache { FakeCache({ @@ -1022,8 +1026,19 @@ class FakeAndroidSdk extends Fake implements AndroidSdk { } class FakeArtifactUpdater extends Fake implements ArtifactUpdater { + void Function(String, Uri, Directory) onDownloadZipArchive; + void Function(String, Uri, Directory) onDownloadZipTarball; + @override Future downloadZippedTarball(String message, Uri url, Directory location) async { - return; + onDownloadZipTarball?.call(message, url, location); } + + @override + Future downloadZipArchive(String message, Uri url, Directory location) async { + onDownloadZipArchive?.call(message, url, location); + } + + @override + void removeDownloadedFiles() { } } diff --git a/packages/flutter_tools/test/src/fakes.dart b/packages/flutter_tools/test/src/fakes.dart index 4026b64f04..e73738a61b 100644 --- a/packages/flutter_tools/test/src/fakes.dart +++ b/packages/flutter_tools/test/src/fakes.dart @@ -498,6 +498,8 @@ class FakeStatusLogger extends DelegatingLogger { class FakeOperatingSystemUtils extends Fake implements OperatingSystemUtils { FakeOperatingSystemUtils({this.hostPlatform = HostPlatform.linux_x64}); + final List> chmods = >[]; + @override void makeExecutable(File file) { } @@ -505,7 +507,9 @@ class FakeOperatingSystemUtils extends Fake implements OperatingSystemUtils { HostPlatform hostPlatform = HostPlatform.linux_x64; @override - void chmod(FileSystemEntity entity, String mode) { } + void chmod(FileSystemEntity entity, String mode) { + chmods.add([entity.path, mode]); + } @override File? which(String execName) => null;