[flutter_tools] join flutter specific with home cache (#105343)

This commit is contained in:
Jesús S Guerrero
2022-07-29 19:58:06 +00:00
committed by GitHub
parent 3ce2ab9159
commit 6048e07f47
3 changed files with 306 additions and 11 deletions

View File

@@ -0,0 +1,97 @@
// Copyright 2014 The Flutter 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 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/dart/pub.dart';
import '../../src/common.dart';
void main() {
testWithoutContext('join two folders', () async {
final MemoryFileSystem fileSystem = MemoryFileSystem();
final Directory target = fileSystem.currentDirectory.childDirectory('target');
final Directory extra = fileSystem.currentDirectory.childDirectory('extra');
target.createSync();
target.childFile('first.file').createSync();
target.childDirectory('dir').createSync();
extra.createSync();
extra.childFile('second.file').writeAsBytesSync(<int>[0]);
extra.childDirectory('dir').createSync();
extra.childDirectory('dir').childFile('third.file').writeAsBytesSync(<int>[0]);
extra.childDirectory('dir_2').createSync();
extra.childDirectory('dir_2').childFile('fourth.file').writeAsBytesSync(<int>[0]);
extra.childDirectory('dir_3').createSync();
extra.childDirectory('dir_3').childFile('fifth.file').writeAsBytesSync(<int>[0]);
joinCaches(
fileSystem: fileSystem,
globalCacheDirectory: target,
dependencyDirectory: extra,
);
expect(target.childFile('second.file').existsSync(), true);
expect(target.childDirectory('dir').childFile('third.file').existsSync(), false);
expect(target.childDirectory('dir_2').childFile('fourth.file').existsSync(), true);
expect(target.childDirectory('dir_3').childFile('fifth.file').existsSync(), true);
expect(extra.childDirectory('dir').childFile('third.file').existsSync(), true);
});
group('needsToJoinCache()', (){
testWithoutContext('make join', () async {
final MemoryFileSystem fileSystem = MemoryFileSystem();
final Directory local = fileSystem.currentDirectory.childDirectory('local');
final Directory global = fileSystem.currentDirectory.childDirectory('global');
for (final Directory directory in <Directory>[local, global]) {
directory.createSync();
directory.childDirectory('hosted').createSync();
directory.childDirectory('hosted').childDirectory('pub.dartlang.org').createSync();
}
final bool pass = needsToJoinCache(
fileSystem: fileSystem,
localCachePath: local.path,
globalDirectory: global,
);
expect(pass, true);
});
testWithoutContext('detects when global pub-cache does not have a pub.dartlang.org dir', () async {
final MemoryFileSystem fileSystem = MemoryFileSystem();
final Directory local = fileSystem.currentDirectory.childDirectory('local');
final Directory global = fileSystem.currentDirectory.childDirectory('global');
local.createSync();
global.createSync();
local.childDirectory('hosted').createSync();
local.childDirectory('hosted').childDirectory('pub.dartlang.org').createSync();
expect(
needsToJoinCache(
fileSystem: fileSystem,
localCachePath: local.path,
globalDirectory: global
),
false
);
});
testWithoutContext("don't join global directory null", () async {
final MemoryFileSystem fileSystem = MemoryFileSystem();
final Directory local = fileSystem.currentDirectory.childDirectory('local');
const Directory? global = null;
local.createSync();
local.childDirectory('hosted').createSync();
local.childDirectory('hosted').childDirectory('pub.dartlang.org').createSync();
expect(
needsToJoinCache(
fileSystem: fileSystem,
localCachePath: local.path,
globalDirectory: global
),
false
);
});
});
}

View File

@@ -733,13 +733,12 @@ last line of pub output: "err3"
expect(processManager, hasNoRemainingExpectations);
});
testWithoutContext('pub cache in root is used', () async {
testWithoutContext('pub cache in flutter root is ignored', () async {
String? error;
final FileSystem fileSystem = MemoryFileSystem.test();
final Directory pubCache = fileSystem.directory(Cache.flutterRoot).childDirectory('.pub-cache')..createSync();
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
FakeCommand(
command: const <String>[
const FakeCommand(
command: <String>[
'bin/cache/dart-sdk/bin/dart',
'__deprecated_pub',
'--verbosity=warning',
@@ -750,7 +749,6 @@ last line of pub output: "err3"
environment: <String, String>{
'FLUTTER_ROOT': '',
'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests',
'PUB_CACHE': pubCache.path,
},
),
]);
@@ -776,6 +774,77 @@ last line of pub output: "err3"
});
});
testWithoutContext('pub cache local is merge to global', () async {
String? error;
final FileSystem fileSystem = MemoryFileSystem.test();
final Directory local = fileSystem.currentDirectory.childDirectory('.pub-cache');
final Directory global = fileSystem.currentDirectory.childDirectory('/global');
global.createSync();
for (final Directory dir in <Directory>[global.childDirectory('.pub-cache'), local]) {
dir.createSync();
dir.childDirectory('hosted').createSync();
dir.childDirectory('hosted').childDirectory('pub.dartlang.org').createSync();
}
final Directory globalHosted = global.childDirectory('.pub-cache').childDirectory('hosted').childDirectory('pub.dartlang.org');
globalHosted.childFile('first.file').createSync();
globalHosted.childDirectory('dir').createSync();
final Directory localHosted = local.childDirectory('hosted').childDirectory('pub.dartlang.org');
localHosted.childFile('second.file').writeAsBytesSync(<int>[0]);
localHosted.childDirectory('dir').createSync();
localHosted.childDirectory('dir').childFile('third.file').writeAsBytesSync(<int>[0]);
localHosted.childDirectory('dir_2').createSync();
localHosted.childDirectory('dir_2').childFile('fourth.file').writeAsBytesSync(<int>[0]);
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
const FakeCommand(
command: <String>[
'bin/cache/dart-sdk/bin/dart',
'__deprecated_pub',
'--verbosity=warning',
'get',
'--no-precompile',
],
exitCode: 69,
environment: <String, String>{
'FLUTTER_ROOT': '',
'PUB_CACHE': '/global/.pub-cache',
'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests',
},
),
]);
final Platform platform = FakePlatform(
environment: <String, String>{'HOME': '/global'}
);
final Pub pub = Pub(
platform: platform,
usage: TestUsage(),
fileSystem: fileSystem,
logger: BufferLogger.test(),
processManager: processManager,
botDetector: const BotDetectorAlwaysNo(),
);
FakeAsync().run((FakeAsync time) {
pub.get(context: PubContext.flutterTests).then((void value) {
error = 'test completed unexpectedly';
}, onError: (dynamic thrownError) {
error = thrownError.toString();
});
time.elapse(const Duration(milliseconds: 500));
expect(error, isNull);
expect(processManager, hasNoRemainingExpectations);
expect(local.existsSync(), false);
expect(globalHosted.childFile('second.file').existsSync(), false);
expect(
globalHosted.childDirectory('dir').childFile('third.file').existsSync(), false
); // do not copy dependencies that are already downloaded
expect(globalHosted.childDirectory('dir_2').childFile('fourth.file').existsSync(), true);
});
});
testWithoutContext('pub cache in environment is used', () async {
final FileSystem fileSystem = MemoryFileSystem.test();
fileSystem.directory('custom/pub-cache/path').createSync(recursive: true);