Skip to content

Commit 8d3cca4

Browse files
authored
use root directory as the default for rootOverride in Cache.test constructor (#158201)
While doing some hacking on `Cache` in flutter/flutter#158081, I noticed that [`Cache.test`](https://github.com/flutter/flutter/blob/de9318275391a6d2f10ae33c576f4113b25fd156/packages/flutter_tools/lib/src/cache.dart#L139) allows the caller to tell Cache to use some given directory as the flutter root (instead of depending on the static global [`Cache.flutterRoot`](https://github.com/flutter/flutter/blob/4f3976a4f2b722d26c3353158dcd26590859dde0/packages/flutter_tools/lib/src/cache.dart#L206)). This has a default value, `/cache`. However, `/cache` is an unintuitive name for the root directory of a Flutter installation. This led to confusion when updating some tests. I wanted to create `/bin/cache/engine-dart-sdk.stamp` for tests, but in reality I needed to create `/cache/bin/cache/engine-dart-sdk.stamp`. This PR changes this default to the current directory of the file system (which I'm guessing is `/` for all intents and purposes). <details> <summary> Pre-launch checklist </summary> </details>
1 parent b6fef5c commit 8d3cca4

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

packages/flutter_tools/lib/src/cache.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,20 @@ class Cache {
144144
Platform? platform,
145145
required ProcessManager processManager,
146146
}) {
147+
if (rootOverride?.fileSystem != null &&
148+
fileSystem != null &&
149+
rootOverride!.fileSystem != fileSystem) {
150+
throw ArgumentError(
151+
'If rootOverride and fileSystem are both non-null, '
152+
'rootOverride.fileSystem must be the same as fileSystem.',
153+
'fileSystem'
154+
);
155+
}
147156
fileSystem ??= rootOverride?.fileSystem ?? MemoryFileSystem.test();
148157
platform ??= FakePlatform(environment: <String, String>{});
149158
logger ??= BufferLogger.test();
150159
return Cache(
151-
rootOverride: rootOverride ?? fileSystem.directory('cache'),
160+
rootOverride: rootOverride ?? fileSystem.currentDirectory,
152161
artifacts: artifacts ?? <ArtifactSet>[],
153162
logger: logger,
154163
fileSystem: fileSystem,

packages/flutter_tools/test/general.shard/android/gradle_utils_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void main() {
2424
setUp(() {
2525
fileSystem = MemoryFileSystem.test();
2626
gradleWrapperDirectory =
27-
fileSystem.directory('cache/bin/cache/artifacts/gradle_wrapper');
27+
fileSystem.directory('bin/cache/artifacts/gradle_wrapper');
2828
gradleWrapperDirectory.createSync(recursive: true);
2929
gradleWrapperDirectory
3030
.childFile('gradlew')

packages/flutter_tools/test/general.shard/cache_test.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ void main() {
338338
testWithoutContext('a non-empty realm is included in the storage url', () async {
339339
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
340340
final Directory internalDir = fileSystem.currentDirectory
341-
.childDirectory('cache')
342341
.childDirectory('bin')
343342
.childDirectory('internal');
344343
final File engineVersionFile = internalDir.childFile('engine.version');
@@ -803,7 +802,6 @@ void main() {
803802
testWithoutContext('FlutterWebSdk fetches web artifacts and deletes previous directory contents', () async {
804803
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
805804
final Directory internalDir = fileSystem.currentDirectory
806-
.childDirectory('cache')
807805
.childDirectory('bin')
808806
.childDirectory('internal');
809807
final File canvasKitVersionFile = internalDir.childFile('canvaskit.version');
@@ -842,7 +840,7 @@ void main() {
842840
]);
843841

844842
expect(locations, <String>[
845-
'cache/bin/cache/flutter_web_sdk',
843+
'/bin/cache/flutter_web_sdk',
846844
]);
847845

848846
expect(webCacheDirectory.childFile('foo'), exists);
@@ -852,7 +850,6 @@ void main() {
852850
testWithoutContext('FlutterWebSdk CanvasKit URL can be overridden via FLUTTER_STORAGE_BASE_URL', () async {
853851
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
854852
final Directory internalDir = fileSystem.currentDirectory
855-
.childDirectory('cache')
856853
.childDirectory('bin')
857854
.childDirectory('internal');
858855
final File canvasKitVersionFile = internalDir.childFile('canvaskit.version');
@@ -909,7 +906,7 @@ void main() {
909906
handler.addError(webCacheDirectory, FileSystemOp.delete, const FileSystemException('', '', OSError('', 2)));
910907

911908
await expectLater(() => webSdk.updateInner(artifactUpdater, fileSystem, FakeOperatingSystemUtils()), throwsToolExit(
912-
message: RegExp('Unable to delete file or directory at "cache/bin/cache/flutter_web_sdk"'),
909+
message: RegExp('Unable to delete file or directory at "/bin/cache/flutter_web_sdk"'),
913910
));
914911
});
915912

@@ -1108,11 +1105,11 @@ void main() {
11081105
Platform: () => FakePlatform(),
11091106
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
11101107
const FakeCommand(command: <String>[
1111-
'/cache/bin/cache/flutter_gradle_wrapper.rand0/gradlew',
1108+
'/bin/cache/flutter_gradle_wrapper.rand0/gradlew',
11121109
'-b',
11131110
'packages/flutter_tools/gradle/resolve_dependencies.gradle',
11141111
'--project-cache-dir',
1115-
'cache/bin/cache/flutter_gradle_wrapper.rand0',
1112+
'/bin/cache/flutter_gradle_wrapper.rand0',
11161113
'resolveDependencies',
11171114
]),
11181115
]),

0 commit comments

Comments
 (0)