Unverified Commit 2fb53d83 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] remove all globals from cache and cache_test (#69505)

parent 9b201dd0
This diff is collapsed.
...@@ -130,7 +130,7 @@ class PrecacheCommand extends FlutterCommand { ...@@ -130,7 +130,7 @@ class PrecacheCommand extends FlutterCommand {
Future<FlutterCommandResult> runCommand() async { Future<FlutterCommandResult> runCommand() async {
// Re-lock the cache. // Re-lock the cache.
if (_platform.environment['FLUTTER_ALREADY_LOCKED'] != 'true') { if (_platform.environment['FLUTTER_ALREADY_LOCKED'] != 'true') {
await Cache.lock(); await _cache.lock();
} }
if (boolArg('force')) { if (boolArg('force')) {
_cache.clearStampFiles(); _cache.clearStampFiles();
......
...@@ -120,6 +120,7 @@ Future<T> runInContext<T>( ...@@ -120,6 +120,7 @@ Future<T> runInContext<T>(
fileSystem: globals.fs, fileSystem: globals.fs,
logger: globals.logger, logger: globals.logger,
platform: globals.platform, platform: globals.platform,
osUtils: globals.os,
), ),
CocoaPods: () => CocoaPods( CocoaPods: () => CocoaPods(
fileSystem: globals.fs, fileSystem: globals.fs,
......
...@@ -962,7 +962,7 @@ abstract class FlutterCommand extends Command<void> { ...@@ -962,7 +962,7 @@ abstract class FlutterCommand extends Command<void> {
void _registerSignalHandlers(String commandPath, DateTime startTime) { void _registerSignalHandlers(String commandPath, DateTime startTime) {
final SignalHandler handler = (io.ProcessSignal s) { final SignalHandler handler = (io.ProcessSignal s) {
Cache.releaseLock(); globals.cache.releaseLock();
_sendPostUsage( _sendPostUsage(
commandPath, commandPath,
const FlutterCommandResult(ExitStatus.killed), const FlutterCommandResult(ExitStatus.killed),
...@@ -1035,7 +1035,7 @@ abstract class FlutterCommand extends Command<void> { ...@@ -1035,7 +1035,7 @@ abstract class FlutterCommand extends Command<void> {
await globals.cache.updateAll(<DevelopmentArtifact>{DevelopmentArtifact.universal}); await globals.cache.updateAll(<DevelopmentArtifact>{DevelopmentArtifact.universal});
await globals.cache.updateAll(await requiredArtifacts); await globals.cache.updateAll(await requiredArtifacts);
} }
Cache.releaseLock(); globals.cache.releaseLock();
await validateCommand(); await validateCommand();
......
...@@ -235,7 +235,7 @@ class FlutterCommandRunner extends CommandRunner<void> { ...@@ -235,7 +235,7 @@ class FlutterCommandRunner extends CommandRunner<void> {
globals.logger.quiet = topLevelResults['quiet'] as bool; globals.logger.quiet = topLevelResults['quiet'] as bool;
if (globals.platform.environment['FLUTTER_ALREADY_LOCKED'] != 'true') { if (globals.platform.environment['FLUTTER_ALREADY_LOCKED'] != 'true') {
await Cache.lock(); await globals.cache.lock();
} }
if (topLevelResults['suppress-analytics'] as bool) { if (topLevelResults['suppress-analytics'] as bool) {
......
...@@ -484,7 +484,7 @@ class FlutterVersion { ...@@ -484,7 +484,7 @@ class FlutterVersion {
/// Returns null if the cached version is out-of-date or missing, and we are /// Returns null if the cached version is out-of-date or missing, and we are
/// unable to reach the server to get the latest version. /// unable to reach the server to get the latest version.
Future<DateTime> _getLatestAvailableFlutterDate() async { Future<DateTime> _getLatestAvailableFlutterDate() async {
Cache.checkLockAcquired(); globals.cache.checkLockAcquired();
final VersionCheckStamp versionCheckStamp = await VersionCheckStamp.load(); final VersionCheckStamp versionCheckStamp = await VersionCheckStamp.load();
if (versionCheckStamp.lastTimeVersionWasChecked != null) { if (versionCheckStamp.lastTimeVersionWasChecked != null) {
......
...@@ -20,7 +20,7 @@ void main() { ...@@ -20,7 +20,7 @@ void main() {
setUp(() { setUp(() {
cache = MockCache(); cache = MockCache();
// Release lock between test cases. // Release lock between test cases.
Cache.releaseLock(); cache.releaseLock();
when(cache.isUpToDate()).thenAnswer((Invocation _) => Future<bool>.value(false)); when(cache.isUpToDate()).thenAnswer((Invocation _) => Future<bool>.value(false));
when(cache.updateAll(any)).thenAnswer((Invocation invocation) { when(cache.updateAll(any)).thenAnswer((Invocation invocation) {
...@@ -39,9 +39,8 @@ void main() { ...@@ -39,9 +39,8 @@ void main() {
); );
await createTestCommandRunner(command).run(const <String>['precache']); await createTestCommandRunner(command).run(const <String>['precache']);
expect(Cache.isLocked(), isTrue);
// Do not throw StateError, lock is acquired. // Do not throw StateError, lock is acquired.
expect(() => Cache.checkLockAcquired(platform), returnsNormally); expect(() => cache.checkLockAcquired(), returnsNormally);
}); });
testUsingContext('precache should not re-entrantly acquire lock', () async { testUsingContext('precache should not re-entrantly acquire lock', () async {
...@@ -62,7 +61,7 @@ void main() { ...@@ -62,7 +61,7 @@ void main() {
expect(Cache.isLocked(), isFalse); expect(Cache.isLocked(), isFalse);
// Do not throw StateError, acquired reentrantly with FLUTTER_ALREADY_LOCKED. // Do not throw StateError, acquired reentrantly with FLUTTER_ALREADY_LOCKED.
expect(() => Cache.checkLockAcquired(platform), returnsNormally); expect(() => cache.checkLockAcquired(), returnsNormally);
}); });
testUsingContext('precache downloads web artifacts on dev branch when feature is enabled.', () async { testUsingContext('precache downloads web artifacts on dev branch when feature is enabled.', () async {
......
...@@ -1024,7 +1024,7 @@ plugin1=${plugin1.path} ...@@ -1024,7 +1024,7 @@ plugin1=${plugin1.path}
when(mockAndroidSdk.directory).thenReturn('irrelevant'); when(mockAndroidSdk.directory).thenReturn('irrelevant');
final Directory rootDirectory = fileSystem.currentDirectory; final Directory rootDirectory = fileSystem.currentDirectory;
cache = Cache( cache = Cache.test(
rootOverride: rootDirectory, rootOverride: rootDirectory,
fileSystem: fileSystem, fileSystem: fileSystem,
); );
......
...@@ -72,7 +72,7 @@ void main() { ...@@ -72,7 +72,7 @@ void main() {
'zipStorePath=wrapper/dists\n' 'zipStorePath=wrapper/dists\n'
'distributionUrl=https\\://services.gradle.org/distributions/gradle-5.6.2-all.zip\n'); 'distributionUrl=https\\://services.gradle.org/distributions/gradle-5.6.2-all.zip\n');
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
Cache: () => Cache(rootOverride: tempDir), Cache: () => Cache.test(rootOverride: tempDir, fileSystem: memoryFileSystem),
FileSystem: () => memoryFileSystem, FileSystem: () => memoryFileSystem,
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
}); });
...@@ -113,7 +113,7 @@ void main() { ...@@ -113,7 +113,7 @@ void main() {
'zipStorePath=wrapper/dists\n' 'zipStorePath=wrapper/dists\n'
'distributionUrl=https\\://services.gradle.org/distributions/gradle-5.6.2-all.zip\n'); 'distributionUrl=https\\://services.gradle.org/distributions/gradle-5.6.2-all.zip\n');
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
Cache: () => Cache(rootOverride: tempDir), Cache: () => Cache.test(rootOverride: tempDir, fileSystem: memoryFileSystem),
FileSystem: () => memoryFileSystem, FileSystem: () => memoryFileSystem,
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
}); });
......
...@@ -771,7 +771,12 @@ void _testInMemory(String description, Future<void> testMethod()) { ...@@ -771,7 +771,12 @@ void _testInMemory(String description, Future<void> testMethod()) {
testFileSystem.file('.packages').writeAsStringSync('\n'); testFileSystem.file('.packages').writeAsStringSync('\n');
// Transfer needed parts of the Flutter installation folder // Transfer needed parts of the Flutter installation folder
// to the in-memory file system used during testing. // to the in-memory file system used during testing.
transfer(Cache().getArtifactDirectory('gradle_wrapper'), testFileSystem); transfer(Cache(
fileSystem: globals.fs,
logger: globals.logger,
osUtils: globals.os,
platform: globals.platform,
).getArtifactDirectory('gradle_wrapper'), testFileSystem);
transfer(globals.fs.directory(Cache.flutterRoot) transfer(globals.fs.directory(Cache.flutterRoot)
.childDirectory('packages') .childDirectory('packages')
.childDirectory('flutter_tools') .childDirectory('flutter_tools')
...@@ -801,7 +806,12 @@ void _testInMemory(String description, Future<void> testMethod()) { ...@@ -801,7 +806,12 @@ void _testInMemory(String description, Future<void> testMethod()) {
overrides: <Type, Generator>{ overrides: <Type, Generator>{
FileSystem: () => testFileSystem, FileSystem: () => testFileSystem,
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
Cache: () => Cache(), Cache: () => Cache(
logger: globals.logger,
fileSystem: globals.fs,
osUtils: globals.os,
platform: globals.platform,
),
FlutterProjectFactory: () => flutterProjectFactory, FlutterProjectFactory: () => flutterProjectFactory,
}, },
); );
......
...@@ -57,7 +57,7 @@ void main() { ...@@ -57,7 +57,7 @@ void main() {
testUsingContext('honors shouldUpdateCache false', () async { testUsingContext('honors shouldUpdateCache false', () async {
final DummyFlutterCommand flutterCommand = DummyFlutterCommand(shouldUpdateCache: false); final DummyFlutterCommand flutterCommand = DummyFlutterCommand(shouldUpdateCache: false);
await flutterCommand.run(); await flutterCommand.run();
verifyZeroInteractions(cache); verifyNever(cache.updateAll(any));
expect(flutterCommand.deprecated, isFalse); expect(flutterCommand.deprecated, isFalse);
expect(flutterCommand.hidden, isFalse); expect(flutterCommand.hidden, isFalse);
}, },
...@@ -352,7 +352,7 @@ void main() { ...@@ -352,7 +352,7 @@ void main() {
final Completer<void> checkLockCompleter = Completer<void>(); final Completer<void> checkLockCompleter = Completer<void>();
final DummyFlutterCommand flutterCommand = final DummyFlutterCommand flutterCommand =
DummyFlutterCommand(commandFunction: () async { DummyFlutterCommand(commandFunction: () async {
await Cache.lock(); await globals.cache.lock();
checkLockCompleter.complete(); checkLockCompleter.complete();
final Completer<void> c = Completer<void>(); final Completer<void> c = Completer<void>();
await c.future; await c.future;
...@@ -362,13 +362,13 @@ void main() { ...@@ -362,13 +362,13 @@ void main() {
unawaited(flutterCommand.run()); unawaited(flutterCommand.run());
await checkLockCompleter.future; await checkLockCompleter.future;
Cache.checkLockAcquired(); globals.cache.checkLockAcquired();
signalController.add(mockSignal); signalController.add(mockSignal);
await completer.future; await completer.future;
await Cache.lock(); await globals.cache.lock();
Cache.releaseLock(); globals.cache.releaseLock();
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
ProcessInfo: () => mockProcessInfo, ProcessInfo: () => mockProcessInfo,
Signals: () => FakeSignals( Signals: () => FakeSignals(
......
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
import 'dart:async'; import 'dart:async';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/os.dart';
import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/device.dart'; import 'package:flutter_tools/src/device.dart';
...@@ -51,12 +54,12 @@ class FakeDyldEnvironmentArtifact extends ArtifactSet { ...@@ -51,12 +54,12 @@ class FakeDyldEnvironmentArtifact extends ArtifactSet {
}; };
@override @override
Future<bool> isUpToDate() => Future<bool>.value(true); Future<bool> isUpToDate(FileSystem fileSystem) => Future<bool>.value(true);
@override @override
String get name => 'fake'; String get name => 'fake';
@override @override
Future<void> update(ArtifactUpdater artifactUpdater) async { Future<void> update(ArtifactUpdater artifactUpdater, Logger logger, FileSystem fileSystem, OperatingSystemUtils operatingSystemUtils) async {
} }
} }
...@@ -897,5 +897,14 @@ class FakeCache implements Cache { ...@@ -897,5 +897,14 @@ class FakeCache implements Cache {
} }
@override @override
void clearStampFiles() {} void clearStampFiles() { }
@override
void checkLockAcquired() { }
@override
Future<void> lock() async { }
@override
void releaseLock() { }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment