2017-04-08 04:08:53 +00:00
|
|
|
// Copyright 2016 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:file/file.dart';
|
|
|
|
import 'package:file/memory.dart';
|
|
|
|
import 'package:mockito/mockito.dart';
|
|
|
|
import 'package:platform/platform.dart';
|
|
|
|
|
|
|
|
import 'package:flutter_tools/src/cache.dart';
|
2018-04-07 01:44:05 +00:00
|
|
|
import 'package:flutter_tools/src/base/io.dart' show InternetAddress, SocketException;
|
2017-04-08 04:08:53 +00:00
|
|
|
|
2018-08-15 03:33:58 +00:00
|
|
|
import 'src/common.dart';
|
2017-05-12 17:44:52 +00:00
|
|
|
import 'src/context.dart';
|
2017-04-08 04:08:53 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
group('$Cache.checkLockAcquired', () {
|
|
|
|
setUp(() {
|
|
|
|
Cache.enableLocking();
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() {
|
|
|
|
// Restore locking to prevent potential side-effects in
|
|
|
|
// tests outside this group (this option is globally shared).
|
|
|
|
Cache.enableLocking();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should throw when locking is not acquired', () {
|
|
|
|
expect(() => Cache.checkLockAcquired(), throwsStateError);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not throw when locking is disabled', () {
|
|
|
|
Cache.disableLocking();
|
|
|
|
Cache.checkLockAcquired();
|
|
|
|
});
|
|
|
|
|
|
|
|
testUsingContext('should not throw when lock is acquired', () async {
|
|
|
|
await Cache.lock();
|
|
|
|
Cache.checkLockAcquired();
|
|
|
|
}, overrides: <Type, Generator>{
|
2018-09-12 06:29:29 +00:00
|
|
|
FileSystem: () => MockFileSystem(),
|
2017-04-08 04:08:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
testUsingContext('should not throw when FLUTTER_ALREADY_LOCKED is set', () async {
|
|
|
|
Cache.checkLockAcquired();
|
|
|
|
}, overrides: <Type, Generator>{
|
2018-09-12 06:29:29 +00:00
|
|
|
Platform: () => FakePlatform()..environment = <String, String>{'FLUTTER_ALREADY_LOCKED': 'true'},
|
2017-04-08 04:08:53 +00:00
|
|
|
});
|
|
|
|
});
|
2017-09-29 00:37:34 +00:00
|
|
|
|
2017-06-26 10:47:43 +00:00
|
|
|
group('Cache', () {
|
2019-01-09 02:34:58 +00:00
|
|
|
final MockCache mockCache = MockCache();
|
|
|
|
final MemoryFileSystem fs = MemoryFileSystem();
|
|
|
|
|
|
|
|
testUsingContext('Gradle wrapper should not be up to date, if some cached artifact is not available', () {
|
|
|
|
final GradleWrapper gradleWrapper = GradleWrapper(mockCache);
|
|
|
|
final Directory directory = fs.directory('/Applications/flutter/bin/cache');
|
|
|
|
directory.createSync(recursive: true);
|
|
|
|
fs.file(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper', 'gradle', 'wrapper', 'gradle-wrapper.jar')).createSync(recursive: true);
|
|
|
|
when(mockCache.getCacheDir(fs.path.join('artifacts', 'gradle_wrapper'))).thenReturn(fs.directory(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper')));
|
2019-03-08 21:31:51 +00:00
|
|
|
expect(gradleWrapper.isUpToDateInner(), false);
|
2019-01-09 02:34:58 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
Cache: ()=> mockCache,
|
2019-03-01 07:17:55 +00:00
|
|
|
FileSystem: () => fs,
|
2019-01-09 02:34:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
testUsingContext('Gradle wrapper should be up to date, only if all cached artifact are available', () {
|
|
|
|
final GradleWrapper gradleWrapper = GradleWrapper(mockCache);
|
|
|
|
final Directory directory = fs.directory('/Applications/flutter/bin/cache');
|
|
|
|
directory.createSync(recursive: true);
|
|
|
|
fs.file(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper', 'gradle', 'wrapper', 'gradle-wrapper.jar')).createSync(recursive: true);
|
|
|
|
fs.file(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper', 'gradlew')).createSync(recursive: true);
|
|
|
|
fs.file(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper', 'gradlew.bat')).createSync(recursive: true);
|
|
|
|
|
|
|
|
when(mockCache.getCacheDir(fs.path.join('artifacts', 'gradle_wrapper'))).thenReturn(fs.directory(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper')));
|
2019-03-08 21:31:51 +00:00
|
|
|
expect(gradleWrapper.isUpToDateInner(), true);
|
2019-01-09 02:34:58 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
Cache: ()=> mockCache,
|
2019-03-01 07:17:55 +00:00
|
|
|
FileSystem: () => fs,
|
2019-01-09 02:34:58 +00:00
|
|
|
});
|
|
|
|
|
2017-06-26 10:47:43 +00:00
|
|
|
test('should not be up to date, if some cached artifact is not', () {
|
2018-09-12 06:29:29 +00:00
|
|
|
final CachedArtifact artifact1 = MockCachedArtifact();
|
|
|
|
final CachedArtifact artifact2 = MockCachedArtifact();
|
2019-03-26 17:01:22 +00:00
|
|
|
when(artifact1.isUpToDate()).thenReturn(true);
|
|
|
|
when(artifact2.isUpToDate()).thenReturn(false);
|
2018-09-12 06:29:29 +00:00
|
|
|
final Cache cache = Cache(artifacts: <CachedArtifact>[artifact1, artifact2]);
|
2019-03-26 17:01:22 +00:00
|
|
|
expect(cache.isUpToDate(), isFalse);
|
2017-06-26 10:47:43 +00:00
|
|
|
});
|
|
|
|
test('should be up to date, if all cached artifacts are', () {
|
2018-09-12 06:29:29 +00:00
|
|
|
final CachedArtifact artifact1 = MockCachedArtifact();
|
|
|
|
final CachedArtifact artifact2 = MockCachedArtifact();
|
2019-03-26 17:01:22 +00:00
|
|
|
when(artifact1.isUpToDate()).thenReturn(true);
|
|
|
|
when(artifact2.isUpToDate()).thenReturn(true);
|
2018-09-12 06:29:29 +00:00
|
|
|
final Cache cache = Cache(artifacts: <CachedArtifact>[artifact1, artifact2]);
|
2019-03-26 17:01:22 +00:00
|
|
|
expect(cache.isUpToDate(), isTrue);
|
2017-06-26 10:47:43 +00:00
|
|
|
});
|
|
|
|
test('should update cached artifacts which are not up to date', () async {
|
2018-09-12 06:29:29 +00:00
|
|
|
final CachedArtifact artifact1 = MockCachedArtifact();
|
|
|
|
final CachedArtifact artifact2 = MockCachedArtifact();
|
2019-03-26 17:01:22 +00:00
|
|
|
when(artifact1.isUpToDate()).thenReturn(true);
|
|
|
|
when(artifact2.isUpToDate()).thenReturn(false);
|
2018-09-12 06:29:29 +00:00
|
|
|
final Cache cache = Cache(artifacts: <CachedArtifact>[artifact1, artifact2]);
|
2019-03-27 20:54:09 +00:00
|
|
|
await cache.updateAll(<DevelopmentArtifact>{});
|
|
|
|
verifyNever(artifact1.update(<DevelopmentArtifact>{}));
|
|
|
|
verify(artifact2.update(<DevelopmentArtifact>{}));
|
2017-06-26 10:47:43 +00:00
|
|
|
});
|
2018-04-07 01:44:05 +00:00
|
|
|
testUsingContext('failed storage.googleapis.com download shows China warning', () async {
|
2018-09-12 06:29:29 +00:00
|
|
|
final CachedArtifact artifact1 = MockCachedArtifact();
|
|
|
|
final CachedArtifact artifact2 = MockCachedArtifact();
|
2019-03-26 17:01:22 +00:00
|
|
|
when(artifact1.isUpToDate()).thenReturn(false);
|
|
|
|
when(artifact2.isUpToDate()).thenReturn(false);
|
2018-09-12 06:29:29 +00:00
|
|
|
final MockInternetAddress address = MockInternetAddress();
|
2018-04-07 01:44:05 +00:00
|
|
|
when(address.host).thenReturn('storage.googleapis.com');
|
2019-03-27 20:54:09 +00:00
|
|
|
when(artifact1.update(<DevelopmentArtifact>{})).thenThrow(SocketException(
|
2018-04-07 01:44:05 +00:00
|
|
|
'Connection reset by peer',
|
|
|
|
address: address,
|
|
|
|
));
|
2018-09-12 06:29:29 +00:00
|
|
|
final Cache cache = Cache(artifacts: <CachedArtifact>[artifact1, artifact2]);
|
2018-04-07 01:44:05 +00:00
|
|
|
try {
|
2019-03-27 20:54:09 +00:00
|
|
|
await cache.updateAll(<DevelopmentArtifact>{});
|
2018-04-07 01:44:05 +00:00
|
|
|
fail('Mock thrown exception expected');
|
|
|
|
} catch (e) {
|
2019-03-27 20:54:09 +00:00
|
|
|
verify(artifact1.update(<DevelopmentArtifact>{}));
|
2018-04-07 01:44:05 +00:00
|
|
|
// Don't continue when retrieval fails.
|
2019-03-27 20:54:09 +00:00
|
|
|
verifyNever(artifact2.update(<DevelopmentArtifact>{}));
|
2018-04-07 01:44:05 +00:00
|
|
|
expect(
|
|
|
|
testLogger.errorText,
|
2019-04-05 18:39:30 +00:00
|
|
|
contains('https://flutter.dev/community/china'),
|
2018-04-07 01:44:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2017-06-26 10:47:43 +00:00
|
|
|
});
|
2018-01-18 00:08:19 +00:00
|
|
|
|
|
|
|
testUsingContext('flattenNameSubdirs', () {
|
2019-04-05 18:39:30 +00:00
|
|
|
expect(flattenNameSubdirs(Uri.parse('http://flutter.dev/foo/bar')), 'flutter.dev/foo/bar');
|
2018-01-18 00:08:19 +00:00
|
|
|
expect(flattenNameSubdirs(Uri.parse('http://docs.flutter.io/foo/bar')), 'docs.flutter.io/foo/bar');
|
2019-04-05 18:39:30 +00:00
|
|
|
expect(flattenNameSubdirs(Uri.parse('https://www.flutter.dev')), 'www.flutter.dev');
|
2018-02-02 22:27:29 +00:00
|
|
|
}, overrides: <Type, Generator>{
|
2018-09-12 06:29:29 +00:00
|
|
|
FileSystem: () => MockFileSystem(),
|
2018-01-18 00:08:19 +00:00
|
|
|
});
|
2017-04-08 04:08:53 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 21:27:53 +00:00
|
|
|
class MockFileSystem extends ForwardingFileSystem {
|
2018-09-12 06:29:29 +00:00
|
|
|
MockFileSystem() : super(MemoryFileSystem());
|
2018-07-20 06:03:58 +00:00
|
|
|
|
2017-04-08 04:08:53 +00:00
|
|
|
@override
|
|
|
|
File file(dynamic path) {
|
2018-09-12 06:29:29 +00:00
|
|
|
return MockFile();
|
2017-04-08 04:08:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockFile extends Mock implements File {
|
|
|
|
@override
|
2019-02-21 08:27:07 +00:00
|
|
|
Future<RandomAccessFile> open({ FileMode mode = FileMode.read }) async {
|
2018-09-12 06:29:29 +00:00
|
|
|
return MockRandomAccessFile();
|
2017-04-08 04:08:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockRandomAccessFile extends Mock implements RandomAccessFile {}
|
2017-06-26 10:47:43 +00:00
|
|
|
class MockCachedArtifact extends Mock implements CachedArtifact {}
|
2018-04-07 01:44:05 +00:00
|
|
|
class MockInternetAddress extends Mock implements InternetAddress {}
|
2019-03-06 08:37:32 +00:00
|
|
|
class MockCache extends Mock implements Cache {}
|