Skip to content

Commit 87d6e93

Browse files
kangwang1988xster
authored andcommitted
Improve the intergrity checking for "gradle wrapper". (flutter#26069)
1 parent 6412f35 commit 87d6e93

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

packages/flutter_tools/lib/src/cache.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,10 @@ class FlutterEngine extends CachedArtifact {
576576
class GradleWrapper extends CachedArtifact {
577577
GradleWrapper(Cache cache): super('gradle_wrapper', cache);
578578

579+
List<String> get _gradleScripts => <String>['gradlew', 'gradlew.bat'];
580+
581+
String get _gradleWrapper => fs.path.join('gradle', 'wrapper', 'gradle-wrapper.jar');
582+
579583
@override
580584
Future<void> updateInner() {
581585
final Uri archiveUri = _toStorageUri(version);
@@ -586,6 +590,22 @@ class GradleWrapper extends CachedArtifact {
586590
fs.file(fs.path.join(location.path, 'NOTICE')).deleteSync();
587591
});
588592
}
593+
594+
@override
595+
bool isUpToDateInner() {
596+
final Directory wrapperDir = cache.getCacheDir(fs.path.join('artifacts', 'gradle_wrapper'));
597+
if (!fs.directory(wrapperDir).existsSync())
598+
return false;
599+
for (String scriptName in _gradleScripts) {
600+
final File scriptFile = fs.file(fs.path.join(wrapperDir.path, scriptName));
601+
if (!scriptFile.existsSync())
602+
return false;
603+
}
604+
final File gradleWrapperJar = fs.file(fs.path.join(wrapperDir.path, _gradleWrapper));
605+
if (!gradleWrapperJar.existsSync())
606+
return false;
607+
return true;
608+
}
589609
}
590610

591611
// Many characters are problematic in filenames, especially on Windows.

packages/flutter_tools/test/cache_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,36 @@ void main() {
5151
});
5252

5353
group('Cache', () {
54+
final MockCache mockCache = MockCache();
55+
final MemoryFileSystem fs = MemoryFileSystem();
56+
57+
testUsingContext('Gradle wrapper should not be up to date, if some cached artifact is not available', () {
58+
final GradleWrapper gradleWrapper = GradleWrapper(mockCache);
59+
final Directory directory = fs.directory('/Applications/flutter/bin/cache');
60+
directory.createSync(recursive: true);
61+
fs.file(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper', 'gradle', 'wrapper', 'gradle-wrapper.jar')).createSync(recursive: true);
62+
when(mockCache.getCacheDir(fs.path.join('artifacts', 'gradle_wrapper'))).thenReturn(fs.directory(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper')));
63+
expect(gradleWrapper.isUpToDateInner(), false);
64+
}, overrides: <Type, Generator>{
65+
Cache: ()=> mockCache,
66+
FileSystem: () => fs
67+
});
68+
69+
testUsingContext('Gradle wrapper should be up to date, only if all cached artifact are available', () {
70+
final GradleWrapper gradleWrapper = GradleWrapper(mockCache);
71+
final Directory directory = fs.directory('/Applications/flutter/bin/cache');
72+
directory.createSync(recursive: true);
73+
fs.file(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper', 'gradle', 'wrapper', 'gradle-wrapper.jar')).createSync(recursive: true);
74+
fs.file(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper', 'gradlew')).createSync(recursive: true);
75+
fs.file(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper', 'gradlew.bat')).createSync(recursive: true);
76+
77+
when(mockCache.getCacheDir(fs.path.join('artifacts', 'gradle_wrapper'))).thenReturn(fs.directory(fs.path.join(directory.path, 'artifacts', 'gradle_wrapper')));
78+
expect(gradleWrapper.isUpToDateInner(), true);
79+
}, overrides: <Type, Generator>{
80+
Cache: ()=> mockCache,
81+
FileSystem: () => fs
82+
});
83+
5484
test('should not be up to date, if some cached artifact is not', () {
5585
final CachedArtifact artifact1 = MockCachedArtifact();
5686
final CachedArtifact artifact2 = MockCachedArtifact();
@@ -132,3 +162,4 @@ class MockFile extends Mock implements File {
132162
class MockRandomAccessFile extends Mock implements RandomAccessFile {}
133163
class MockCachedArtifact extends Mock implements CachedArtifact {}
134164
class MockInternetAddress extends Mock implements InternetAddress {}
165+
class MockCache extends Mock implements Cache {}

0 commit comments

Comments
 (0)