Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 5 | import 'package:file/file.dart'; |
| 6 | import 'package:file/memory.dart'; |
Todd Volkert | adb2aee | 2019-07-18 15:29:06 -0700 | [diff] [blame] | 7 | import 'package:file_testing/file_testing.dart'; |
| 8 | import 'package:meta/meta.dart'; |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 9 | import 'package:mockito/mockito.dart'; |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 10 | import 'package:platform/platform.dart'; |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 11 | import 'package:process/process.dart'; |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 12 | |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 13 | import 'package:flutter_tools/src/android/gradle.dart'; |
Zachary Anderson | 99d66f2 | 2019-07-30 12:16:32 -0700 | [diff] [blame] | 14 | import 'package:flutter_tools/src/base/common.dart'; |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 15 | import 'package:flutter_tools/src/base/io.dart'; |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 16 | import 'package:flutter_tools/src/cache.dart'; |
Todd Volkert | adb2aee | 2019-07-18 15:29:06 -0700 | [diff] [blame] | 17 | import 'package:flutter_tools/src/base/file_system.dart'; |
xster | bdc0619 | 2018-04-06 18:44:05 -0700 | [diff] [blame] | 18 | import 'package:flutter_tools/src/base/io.dart' show InternetAddress, SocketException; |
Todd Volkert | adb2aee | 2019-07-18 15:29:06 -0700 | [diff] [blame] | 19 | import 'package:flutter_tools/src/base/net.dart'; |
| 20 | import 'package:flutter_tools/src/base/os.dart'; |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 21 | |
Ian Hickson | d919e69 | 2019-07-13 11:51:44 -0700 | [diff] [blame] | 22 | import '../src/common.dart'; |
| 23 | import '../src/context.dart'; |
Todd Volkert | adb2aee | 2019-07-18 15:29:06 -0700 | [diff] [blame] | 24 | import '../src/testbed.dart'; |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 25 | |
| 26 | void main() { |
| 27 | group('$Cache.checkLockAcquired', () { |
Zachary Anderson | 99d66f2 | 2019-07-30 12:16:32 -0700 | [diff] [blame] | 28 | MockFileSystem mockFileSystem; |
| 29 | MemoryFileSystem memoryFileSystem; |
| 30 | MockFile mockFile; |
| 31 | MockRandomAccessFile mockRandomAccessFile; |
| 32 | |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 33 | setUp(() { |
Zachary Anderson | 99d66f2 | 2019-07-30 12:16:32 -0700 | [diff] [blame] | 34 | mockFileSystem = MockFileSystem(); |
| 35 | memoryFileSystem = MemoryFileSystem(); |
| 36 | mockFile = MockFile(); |
| 37 | mockRandomAccessFile = MockRandomAccessFile(); |
| 38 | when(mockFileSystem.path).thenReturn(memoryFileSystem.path); |
| 39 | |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 40 | Cache.enableLocking(); |
| 41 | }); |
| 42 | |
| 43 | tearDown(() { |
| 44 | // Restore locking to prevent potential side-effects in |
| 45 | // tests outside this group (this option is globally shared). |
| 46 | Cache.enableLocking(); |
Zachary Anderson | 99d66f2 | 2019-07-30 12:16:32 -0700 | [diff] [blame] | 47 | Cache.releaseLockEarly(); |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 48 | }); |
| 49 | |
| 50 | test('should throw when locking is not acquired', () { |
| 51 | expect(() => Cache.checkLockAcquired(), throwsStateError); |
| 52 | }); |
| 53 | |
| 54 | test('should not throw when locking is disabled', () { |
| 55 | Cache.disableLocking(); |
| 56 | Cache.checkLockAcquired(); |
| 57 | }); |
| 58 | |
| 59 | testUsingContext('should not throw when lock is acquired', () async { |
Zachary Anderson | 99d66f2 | 2019-07-30 12:16:32 -0700 | [diff] [blame] | 60 | when(mockFileSystem.file(argThat(endsWith('lockfile')))).thenReturn(mockFile); |
| 61 | when(mockFile.openSync(mode: anyNamed('mode'))).thenReturn(mockRandomAccessFile); |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 62 | await Cache.lock(); |
| 63 | Cache.checkLockAcquired(); |
Zachary Anderson | 99d66f2 | 2019-07-30 12:16:32 -0700 | [diff] [blame] | 64 | Cache.releaseLockEarly(); |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 65 | }, overrides: <Type, Generator>{ |
Zachary Anderson | 99d66f2 | 2019-07-30 12:16:32 -0700 | [diff] [blame] | 66 | FileSystem: () => mockFileSystem, |
Ian Hickson | 3597bae | 2019-10-21 20:15:20 -0700 | [diff] [blame^] | 67 | ProcessManager: () => FakeProcessManager.any(), |
Zachary Anderson | 99d66f2 | 2019-07-30 12:16:32 -0700 | [diff] [blame] | 68 | }); |
| 69 | |
| 70 | testUsingContext('throws tool exit when lockfile open fails', () async { |
| 71 | when(mockFileSystem.file(argThat(endsWith('lockfile')))).thenReturn(mockFile); |
| 72 | when(mockFile.openSync(mode: anyNamed('mode'))).thenThrow(const FileSystemException()); |
| 73 | expect(() async => await Cache.lock(), throwsA(isA<ToolExit>())); |
| 74 | }, overrides: <Type, Generator>{ |
| 75 | FileSystem: () => mockFileSystem, |
Ian Hickson | 3597bae | 2019-10-21 20:15:20 -0700 | [diff] [blame^] | 76 | ProcessManager: () => FakeProcessManager.any(), |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 77 | }); |
| 78 | |
| 79 | testUsingContext('should not throw when FLUTTER_ALREADY_LOCKED is set', () async { |
| 80 | Cache.checkLockAcquired(); |
| 81 | }, overrides: <Type, Generator>{ |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 82 | Platform: () => FakePlatform()..environment = <String, String>{'FLUTTER_ALREADY_LOCKED': 'true'}, |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 83 | }); |
| 84 | }); |
Ian Hickson | e1fa035 | 2017-09-28 17:37:34 -0700 | [diff] [blame] | 85 | |
Mikkel Nygaard Ravn | c5999c7 | 2017-06-26 12:47:43 +0200 | [diff] [blame] | 86 | group('Cache', () { |
Todd Volkert | adb2aee | 2019-07-18 15:29:06 -0700 | [diff] [blame] | 87 | MockCache mockCache; |
| 88 | MemoryFileSystem memoryFileSystem; |
| 89 | |
| 90 | setUp(() { |
| 91 | mockCache = MockCache(); |
| 92 | memoryFileSystem = MemoryFileSystem(); |
| 93 | }); |
KyleWong | 87d6e93 | 2019-01-09 10:34:58 +0800 | [diff] [blame] | 94 | |
| 95 | testUsingContext('Gradle wrapper should not be up to date, if some cached artifact is not available', () { |
| 96 | final GradleWrapper gradleWrapper = GradleWrapper(mockCache); |
| 97 | final Directory directory = fs.directory('/Applications/flutter/bin/cache'); |
| 98 | directory.createSync(recursive: true); |
| 99 | fs.file(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper', 'gradle', 'wrapper', 'gradle-wrapper.jar')).createSync(recursive: true); |
| 100 | when(mockCache.getCacheDir(fs.path.join('artifacts', 'gradle_wrapper'))).thenReturn(fs.directory(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper'))); |
Jonah Williams | 8c0cf1d | 2019-03-08 13:31:51 -0800 | [diff] [blame] | 101 | expect(gradleWrapper.isUpToDateInner(), false); |
KyleWong | 87d6e93 | 2019-01-09 10:34:58 +0800 | [diff] [blame] | 102 | }, overrides: <Type, Generator>{ |
| 103 | Cache: ()=> mockCache, |
Todd Volkert | adb2aee | 2019-07-18 15:29:06 -0700 | [diff] [blame] | 104 | FileSystem: () => memoryFileSystem, |
Ian Hickson | 3597bae | 2019-10-21 20:15:20 -0700 | [diff] [blame^] | 105 | ProcessManager: () => FakeProcessManager.any(), |
KyleWong | 87d6e93 | 2019-01-09 10:34:58 +0800 | [diff] [blame] | 106 | }); |
| 107 | |
| 108 | testUsingContext('Gradle wrapper should be up to date, only if all cached artifact are available', () { |
| 109 | final GradleWrapper gradleWrapper = GradleWrapper(mockCache); |
| 110 | final Directory directory = fs.directory('/Applications/flutter/bin/cache'); |
| 111 | directory.createSync(recursive: true); |
| 112 | fs.file(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper', 'gradle', 'wrapper', 'gradle-wrapper.jar')).createSync(recursive: true); |
| 113 | fs.file(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper', 'gradlew')).createSync(recursive: true); |
| 114 | fs.file(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper', 'gradlew.bat')).createSync(recursive: true); |
| 115 | |
| 116 | when(mockCache.getCacheDir(fs.path.join('artifacts', 'gradle_wrapper'))).thenReturn(fs.directory(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper'))); |
Jonah Williams | 8c0cf1d | 2019-03-08 13:31:51 -0800 | [diff] [blame] | 117 | expect(gradleWrapper.isUpToDateInner(), true); |
KyleWong | 87d6e93 | 2019-01-09 10:34:58 +0800 | [diff] [blame] | 118 | }, overrides: <Type, Generator>{ |
| 119 | Cache: ()=> mockCache, |
Todd Volkert | adb2aee | 2019-07-18 15:29:06 -0700 | [diff] [blame] | 120 | FileSystem: () => memoryFileSystem, |
Ian Hickson | 3597bae | 2019-10-21 20:15:20 -0700 | [diff] [blame^] | 121 | ProcessManager: () => FakeProcessManager.any(), |
KyleWong | 87d6e93 | 2019-01-09 10:34:58 +0800 | [diff] [blame] | 122 | }); |
| 123 | |
Mikkel Nygaard Ravn | c5999c7 | 2017-06-26 12:47:43 +0200 | [diff] [blame] | 124 | test('should not be up to date, if some cached artifact is not', () { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 125 | final CachedArtifact artifact1 = MockCachedArtifact(); |
| 126 | final CachedArtifact artifact2 = MockCachedArtifact(); |
Jonah Williams | 164dae3 | 2019-03-26 10:01:22 -0700 | [diff] [blame] | 127 | when(artifact1.isUpToDate()).thenReturn(true); |
| 128 | when(artifact2.isUpToDate()).thenReturn(false); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 129 | final Cache cache = Cache(artifacts: <CachedArtifact>[artifact1, artifact2]); |
Jonah Williams | 164dae3 | 2019-03-26 10:01:22 -0700 | [diff] [blame] | 130 | expect(cache.isUpToDate(), isFalse); |
Mikkel Nygaard Ravn | c5999c7 | 2017-06-26 12:47:43 +0200 | [diff] [blame] | 131 | }); |
| 132 | test('should be up to date, if all cached artifacts are', () { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 133 | final CachedArtifact artifact1 = MockCachedArtifact(); |
| 134 | final CachedArtifact artifact2 = MockCachedArtifact(); |
Jonah Williams | 164dae3 | 2019-03-26 10:01:22 -0700 | [diff] [blame] | 135 | when(artifact1.isUpToDate()).thenReturn(true); |
| 136 | when(artifact2.isUpToDate()).thenReturn(true); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 137 | final Cache cache = Cache(artifacts: <CachedArtifact>[artifact1, artifact2]); |
Jonah Williams | 164dae3 | 2019-03-26 10:01:22 -0700 | [diff] [blame] | 138 | expect(cache.isUpToDate(), isTrue); |
Mikkel Nygaard Ravn | c5999c7 | 2017-06-26 12:47:43 +0200 | [diff] [blame] | 139 | }); |
| 140 | test('should update cached artifacts which are not up to date', () async { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 141 | final CachedArtifact artifact1 = MockCachedArtifact(); |
| 142 | final CachedArtifact artifact2 = MockCachedArtifact(); |
Jonah Williams | 164dae3 | 2019-03-26 10:01:22 -0700 | [diff] [blame] | 143 | when(artifact1.isUpToDate()).thenReturn(true); |
| 144 | when(artifact2.isUpToDate()).thenReturn(false); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 145 | final Cache cache = Cache(artifacts: <CachedArtifact>[artifact1, artifact2]); |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 146 | await cache.updateAll(<DevelopmentArtifact>{ |
| 147 | null, |
| 148 | }); |
| 149 | verifyNever(artifact1.update()); |
| 150 | verify(artifact2.update()); |
Mikkel Nygaard Ravn | c5999c7 | 2017-06-26 12:47:43 +0200 | [diff] [blame] | 151 | }); |
Christopher Fujino | 102ab1e | 2019-07-15 09:22:29 -0700 | [diff] [blame] | 152 | testUsingContext('getter dyLdLibEntry concatenates the output of each artifact\'s dyLdLibEntry getter', () async { |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 153 | final IosUsbArtifacts artifact1 = MockIosUsbArtifacts(); |
| 154 | final IosUsbArtifacts artifact2 = MockIosUsbArtifacts(); |
| 155 | final IosUsbArtifacts artifact3 = MockIosUsbArtifacts(); |
| 156 | when(artifact1.environment) |
| 157 | .thenReturn(<String, String>{ |
| 158 | 'DYLD_LIBRARY_PATH': '/path/to/alpha:/path/to/beta', |
| 159 | }); |
| 160 | when(artifact2.environment) |
| 161 | .thenReturn(<String, String>{ |
| 162 | 'DYLD_LIBRARY_PATH': '/path/to/gamma:/path/to/delta:/path/to/epsilon', |
| 163 | }); |
| 164 | when(artifact3.environment) |
| 165 | .thenReturn(<String, String>{ |
| 166 | 'DYLD_LIBRARY_PATH': '', |
| 167 | }); |
Christopher Fujino | 102ab1e | 2019-07-15 09:22:29 -0700 | [diff] [blame] | 168 | final Cache cache = Cache(artifacts: <CachedArtifact>[artifact1, artifact2, artifact3]); |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 169 | |
Christopher Fujino | 102ab1e | 2019-07-15 09:22:29 -0700 | [diff] [blame] | 170 | expect(cache.dyLdLibEntry.key, 'DYLD_LIBRARY_PATH'); |
| 171 | expect( |
| 172 | cache.dyLdLibEntry.value, |
| 173 | '/path/to/alpha:/path/to/beta:/path/to/gamma:/path/to/delta:/path/to/epsilon', |
| 174 | ); |
| 175 | }, overrides: <Type, Generator>{ |
| 176 | Cache: ()=> mockCache, |
| 177 | }); |
xster | bdc0619 | 2018-04-06 18:44:05 -0700 | [diff] [blame] | 178 | testUsingContext('failed storage.googleapis.com download shows China warning', () async { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 179 | final CachedArtifact artifact1 = MockCachedArtifact(); |
| 180 | final CachedArtifact artifact2 = MockCachedArtifact(); |
Jonah Williams | 164dae3 | 2019-03-26 10:01:22 -0700 | [diff] [blame] | 181 | when(artifact1.isUpToDate()).thenReturn(false); |
| 182 | when(artifact2.isUpToDate()).thenReturn(false); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 183 | final MockInternetAddress address = MockInternetAddress(); |
xster | bdc0619 | 2018-04-06 18:44:05 -0700 | [diff] [blame] | 184 | when(address.host).thenReturn('storage.googleapis.com'); |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 185 | when(artifact1.update()).thenThrow(SocketException( |
xster | bdc0619 | 2018-04-06 18:44:05 -0700 | [diff] [blame] | 186 | 'Connection reset by peer', |
| 187 | address: address, |
| 188 | )); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 189 | final Cache cache = Cache(artifacts: <CachedArtifact>[artifact1, artifact2]); |
xster | bdc0619 | 2018-04-06 18:44:05 -0700 | [diff] [blame] | 190 | try { |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 191 | await cache.updateAll(<DevelopmentArtifact>{ |
| 192 | null, |
| 193 | }); |
xster | bdc0619 | 2018-04-06 18:44:05 -0700 | [diff] [blame] | 194 | fail('Mock thrown exception expected'); |
| 195 | } catch (e) { |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 196 | verify(artifact1.update()); |
xster | bdc0619 | 2018-04-06 18:44:05 -0700 | [diff] [blame] | 197 | // Don't continue when retrieval fails. |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 198 | verifyNever(artifact2.update()); |
xster | bdc0619 | 2018-04-06 18:44:05 -0700 | [diff] [blame] | 199 | expect( |
| 200 | testLogger.errorText, |
Tim Sneath | 5291897 | 2019-04-05 11:39:30 -0700 | [diff] [blame] | 201 | contains('https://flutter.dev/community/china'), |
xster | bdc0619 | 2018-04-06 18:44:05 -0700 | [diff] [blame] | 202 | ); |
| 203 | } |
| 204 | }); |
Mikkel Nygaard Ravn | c5999c7 | 2017-06-26 12:47:43 +0200 | [diff] [blame] | 205 | }); |
Michael Goderbauer | a67df9e | 2018-01-17 16:08:19 -0800 | [diff] [blame] | 206 | |
| 207 | testUsingContext('flattenNameSubdirs', () { |
Tim Sneath | 5291897 | 2019-04-05 11:39:30 -0700 | [diff] [blame] | 208 | expect(flattenNameSubdirs(Uri.parse('http://flutter.dev/foo/bar')), 'flutter.dev/foo/bar'); |
Michael Goderbauer | a67df9e | 2018-01-17 16:08:19 -0800 | [diff] [blame] | 209 | expect(flattenNameSubdirs(Uri.parse('http://docs.flutter.io/foo/bar')), 'docs.flutter.io/foo/bar'); |
Tim Sneath | 5291897 | 2019-04-05 11:39:30 -0700 | [diff] [blame] | 210 | expect(flattenNameSubdirs(Uri.parse('https://www.flutter.dev')), 'www.flutter.dev'); |
Alexandre Ardhuin | c02b6a8 | 2018-02-02 23:27:29 +0100 | [diff] [blame] | 211 | }, overrides: <Type, Generator>{ |
Zachary Anderson | 99d66f2 | 2019-07-30 12:16:32 -0700 | [diff] [blame] | 212 | FileSystem: () => MemoryFileSystem(), |
Ian Hickson | 3597bae | 2019-10-21 20:15:20 -0700 | [diff] [blame^] | 213 | ProcessManager: () => FakeProcessManager.any(), |
Michael Goderbauer | a67df9e | 2018-01-17 16:08:19 -0800 | [diff] [blame] | 214 | }); |
Todd Volkert | adb2aee | 2019-07-18 15:29:06 -0700 | [diff] [blame] | 215 | |
Jonah Williams | c9a5f94 | 2019-07-29 16:13:47 -0700 | [diff] [blame] | 216 | test('Unstable artifacts', () { |
Jonah Williams | 861fe0a | 2019-10-09 16:27:39 -0700 | [diff] [blame] | 217 | expect(DevelopmentArtifact.web.unstable, false); |
Jonah Williams | c9a5f94 | 2019-07-29 16:13:47 -0700 | [diff] [blame] | 218 | expect(DevelopmentArtifact.linux.unstable, true); |
| 219 | expect(DevelopmentArtifact.macOS.unstable, true); |
| 220 | expect(DevelopmentArtifact.windows.unstable, true); |
| 221 | expect(DevelopmentArtifact.fuchsia.unstable, true); |
| 222 | expect(DevelopmentArtifact.flutterRunner.unstable, true); |
| 223 | }); |
| 224 | |
Todd Volkert | adb2aee | 2019-07-18 15:29:06 -0700 | [diff] [blame] | 225 | group('EngineCachedArtifact', () { |
| 226 | FakeHttpClient fakeHttpClient; |
| 227 | FakePlatform fakePlatform; |
| 228 | MemoryFileSystem memoryFileSystem; |
| 229 | MockCache mockCache; |
| 230 | MockOperatingSystemUtils mockOperatingSystemUtils; |
| 231 | |
| 232 | setUp(() { |
| 233 | fakeHttpClient = FakeHttpClient(); |
| 234 | fakePlatform = FakePlatform()..environment = const <String, String>{}; |
| 235 | memoryFileSystem = MemoryFileSystem(); |
| 236 | mockCache = MockCache(); |
| 237 | mockOperatingSystemUtils = MockOperatingSystemUtils(); |
| 238 | when(mockOperatingSystemUtils.verifyZip(any)).thenReturn(true); |
| 239 | }); |
| 240 | |
| 241 | testUsingContext('makes binary dirs readable and executable by all', () async { |
Ian Hickson | cdc2d99 | 2019-10-07 16:43:04 -0700 | [diff] [blame] | 242 | final Directory artifactDir = fs.systemTempDirectory.createTempSync('flutter_cache_test_artifact.'); |
| 243 | final Directory downloadDir = fs.systemTempDirectory.createTempSync('flutter_cache_test_download.'); |
Todd Volkert | adb2aee | 2019-07-18 15:29:06 -0700 | [diff] [blame] | 244 | when(mockCache.getArtifactDirectory(any)).thenReturn(artifactDir); |
| 245 | when(mockCache.getDownloadDir()).thenReturn(downloadDir); |
| 246 | final FakeCachedArtifact artifact = FakeCachedArtifact( |
| 247 | cache: mockCache, |
| 248 | binaryDirs: <List<String>>[ |
| 249 | <String>['bin_dir', 'unused_url_path'], |
| 250 | ], |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 251 | requiredArtifacts: DevelopmentArtifact.universal, |
Todd Volkert | adb2aee | 2019-07-18 15:29:06 -0700 | [diff] [blame] | 252 | ); |
| 253 | await artifact.updateInner(); |
| 254 | final Directory dir = memoryFileSystem.systemTempDirectory |
| 255 | .listSync(recursive: true) |
| 256 | .whereType<Directory>() |
| 257 | .singleWhere((Directory directory) => directory.basename == 'bin_dir', orElse: () => null); |
| 258 | expect(dir, isNotNull); |
| 259 | expect(dir.path, artifactDir.childDirectory('bin_dir').path); |
| 260 | verify(mockOperatingSystemUtils.chmod(argThat(hasPath(dir.path)), 'a+r,a+x')); |
| 261 | }, overrides: <Type, Generator>{ |
| 262 | Cache: ()=> mockCache, |
| 263 | FileSystem: () => memoryFileSystem, |
Ian Hickson | 3597bae | 2019-10-21 20:15:20 -0700 | [diff] [blame^] | 264 | ProcessManager: () => FakeProcessManager.any(), |
Todd Volkert | adb2aee | 2019-07-18 15:29:06 -0700 | [diff] [blame] | 265 | HttpClientFactory: () => () => fakeHttpClient, |
| 266 | OperatingSystemUtils: () => mockOperatingSystemUtils, |
| 267 | Platform: () => fakePlatform, |
| 268 | }); |
| 269 | }); |
Jonah Williams | 77e1510 | 2019-08-09 16:21:32 -0700 | [diff] [blame] | 270 | |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 271 | group('AndroidMavenArtifacts', () { |
| 272 | MemoryFileSystem memoryFileSystem; |
| 273 | MockProcessManager processManager; |
| 274 | MockCache mockCache; |
Jonah Williams | 77e1510 | 2019-08-09 16:21:32 -0700 | [diff] [blame] | 275 | |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 276 | setUp(() { |
| 277 | memoryFileSystem = MemoryFileSystem(); |
| 278 | processManager = MockProcessManager(); |
| 279 | mockCache = MockCache(); |
| 280 | }); |
| 281 | |
| 282 | test('development artifact', () async { |
| 283 | final AndroidMavenArtifacts mavenArtifacts = AndroidMavenArtifacts(); |
| 284 | expect(mavenArtifacts.developmentArtifact, DevelopmentArtifact.androidMaven); |
| 285 | }); |
| 286 | |
| 287 | testUsingContext('update', () async { |
| 288 | final AndroidMavenArtifacts mavenArtifacts = AndroidMavenArtifacts(); |
| 289 | expect(mavenArtifacts.isUpToDate(), isFalse); |
| 290 | |
Ian Hickson | cdc2d99 | 2019-10-07 16:43:04 -0700 | [diff] [blame] | 291 | final Directory gradleWrapperDir = fs.systemTempDirectory.createTempSync('flutter_cache_test_gradle_wrapper.'); |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 292 | when(mockCache.getArtifactDirectory('gradle_wrapper')).thenReturn(gradleWrapperDir); |
| 293 | |
| 294 | fs.directory(gradleWrapperDir.childDirectory('gradle').childDirectory('wrapper')) |
| 295 | .createSync(recursive: true); |
| 296 | fs.file(fs.path.join(gradleWrapperDir.path, 'gradlew')).writeAsStringSync('irrelevant'); |
| 297 | fs.file(fs.path.join(gradleWrapperDir.path, 'gradlew.bat')).writeAsStringSync('irrelevant'); |
| 298 | |
| 299 | when(processManager.run(any, environment: captureAnyNamed('environment'))) |
| 300 | .thenAnswer((Invocation invocation) { |
| 301 | final List<String> args = invocation.positionalArguments[0]; |
| 302 | expect(args.length, 6); |
| 303 | expect(args[1], '-b'); |
| 304 | expect(args[2].endsWith('resolve_dependencies.gradle'), isTrue); |
| 305 | expect(args[5], 'resolveDependencies'); |
| 306 | expect(invocation.namedArguments[#environment], gradleEnv); |
| 307 | return Future<ProcessResult>.value(ProcessResult(0, 0, '', '')); |
| 308 | }); |
| 309 | |
| 310 | await mavenArtifacts.update(); |
| 311 | |
| 312 | expect(mavenArtifacts.isUpToDate(), isFalse); |
| 313 | }, overrides: <Type, Generator>{ |
| 314 | Cache: ()=> mockCache, |
| 315 | FileSystem: () => memoryFileSystem, |
| 316 | ProcessManager: () => processManager, |
| 317 | }); |
Jonah Williams | 77e1510 | 2019-08-09 16:21:32 -0700 | [diff] [blame] | 318 | }); |
Lau Ching Jun | 5a41374 | 2019-10-09 22:24:15 -0700 | [diff] [blame] | 319 | |
| 320 | group('Unsigned mac artifacts', () { |
| 321 | MockCache mockCache; |
| 322 | |
| 323 | setUp(() { |
| 324 | mockCache = MockCache(); |
| 325 | }); |
| 326 | |
| 327 | testUsingContext('use unsigned when specified', () async { |
| 328 | when(mockCache.useUnsignedMacBinaries).thenReturn(true); |
| 329 | |
| 330 | final IosUsbArtifacts iosUsbArtifacts = IosUsbArtifacts('name', mockCache); |
| 331 | expect(iosUsbArtifacts.archiveUri.toString(), contains('/unsigned/')); |
| 332 | }, overrides: <Type, Generator>{ |
| 333 | Cache: () => mockCache, |
| 334 | }); |
| 335 | |
| 336 | testUsingContext('not use unsigned when not specified', () async { |
| 337 | when(mockCache.useUnsignedMacBinaries).thenReturn(false); |
| 338 | |
| 339 | final IosUsbArtifacts iosUsbArtifacts = IosUsbArtifacts('name', mockCache); |
| 340 | expect(iosUsbArtifacts.archiveUri.toString(), isNot(contains('/unsigned/'))); |
| 341 | }, overrides: <Type, Generator>{ |
| 342 | Cache: () => mockCache, |
| 343 | }); |
| 344 | }); |
Todd Volkert | adb2aee | 2019-07-18 15:29:06 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | class FakeCachedArtifact extends EngineCachedArtifact { |
| 348 | FakeCachedArtifact({ |
| 349 | String stampName = 'STAMP', |
| 350 | @required Cache cache, |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 351 | DevelopmentArtifact requiredArtifacts, |
Todd Volkert | adb2aee | 2019-07-18 15:29:06 -0700 | [diff] [blame] | 352 | this.binaryDirs = const <List<String>>[], |
| 353 | this.licenseDirs = const <String>[], |
| 354 | this.packageDirs = const <String>[], |
| 355 | }) : super(stampName, cache, requiredArtifacts); |
| 356 | |
| 357 | final List<List<String>> binaryDirs; |
| 358 | final List<String> licenseDirs; |
| 359 | final List<String> packageDirs; |
| 360 | |
| 361 | @override |
| 362 | List<List<String>> getBinaryDirs() => binaryDirs; |
| 363 | |
| 364 | @override |
| 365 | List<String> getLicenseDirs() => licenseDirs; |
| 366 | |
| 367 | @override |
| 368 | List<String> getPackageDirs() => packageDirs; |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 371 | class MockProcessManager extends Mock implements ProcessManager {} |
Zachary Anderson | 99d66f2 | 2019-07-30 12:16:32 -0700 | [diff] [blame] | 372 | class MockFileSystem extends Mock implements FileSystem {} |
| 373 | class MockFile extends Mock implements File {} |
Jonah Williams | 77e1510 | 2019-08-09 16:21:32 -0700 | [diff] [blame] | 374 | class MockDirectory extends Mock implements Directory {} |
Yegor | 361afef | 2017-04-07 21:08:53 -0700 | [diff] [blame] | 375 | |
| 376 | class MockRandomAccessFile extends Mock implements RandomAccessFile {} |
Mikkel Nygaard Ravn | c5999c7 | 2017-06-26 12:47:43 +0200 | [diff] [blame] | 377 | class MockCachedArtifact extends Mock implements CachedArtifact {} |
Emmanuel Garcia | 8a1bf5b | 2019-09-17 08:19:33 -0700 | [diff] [blame] | 378 | class MockIosUsbArtifacts extends Mock implements IosUsbArtifacts {} |
xster | bdc0619 | 2018-04-06 18:44:05 -0700 | [diff] [blame] | 379 | class MockInternetAddress extends Mock implements InternetAddress {} |
Alexandre Ardhuin | 4c1f4d1 | 2019-03-06 09:37:32 +0100 | [diff] [blame] | 380 | class MockCache extends Mock implements Cache {} |
Todd Volkert | adb2aee | 2019-07-18 15:29:06 -0700 | [diff] [blame] | 381 | class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {} |