From 7a791b5dbf953a38f02f2e4bccf4e345b3c33687 Mon Sep 17 00:00:00 2001 From: Reid Baker Date: Fri, 7 Apr 2023 12:45:37 -0400 Subject: [PATCH] Update getGradleVersion to ignore commented out lines (#124260) https://github.com/flutter/flutter/issues/123917 Missed feedback from https://github.com/flutter/flutter/pull/123916 - Handle commented out lines in gradle-wrapper.properties This is 1 of 2 prs to handle missed feedback. Formatting will be done in the second whereas this one captures a weakness missed in the last pr. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] All existing and new tests are passing. --- .../lib/src/android/gradle_utils.dart | 3 ++- .../android/gradle_utils_test.dart | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/android/gradle_utils.dart b/packages/flutter_tools/lib/src/android/gradle_utils.dart index 6ea8c0fc50..f2f92cec88 100644 --- a/packages/flutter_tools/lib/src/android/gradle_utils.dart +++ b/packages/flutter_tools/lib/src/android/gradle_utils.dart @@ -189,8 +189,9 @@ Future getGradleVersion( // Expected content format (with lines above and below). // Version can have 2 or 3 numbers. // 'distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip' + // '^\s*' protects against commented out lines. final RegExp distributionUrlRegex = - RegExp(r'distributionUrl\s?=\s?.*\.zip'); + RegExp(r'^\s*distributionUrl\s?=\s?.*\.zip', multiLine: true); final RegExpMatch? distributionUrl = distributionUrlRegex.firstMatch(wrapperFileContent); diff --git a/packages/flutter_tools/test/general.shard/android/gradle_utils_test.dart b/packages/flutter_tools/test/general.shard/android/gradle_utils_test.dart index 418cf2d2ee..fea5ee3e95 100644 --- a/packages/flutter_tools/test/general.shard/android/gradle_utils_test.dart +++ b/packages/flutter_tools/test/general.shard/android/gradle_utils_test.dart @@ -226,6 +226,33 @@ distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\\://services.gradle.org/distributions/gradle-$expectedVersion-all.zip +'''); + + expect( + await getGradleVersion( + androidDirectory, BufferLogger.test(), FakeProcessManager.empty()), + expectedVersion, + ); + }); + + testWithoutContext('ignores gradle comments', () async { + const String expectedVersion = '7.4.2'; + final Directory androidDirectory = fileSystem.directory('/android') + ..createSync(); + final Directory wrapperDirectory = androidDirectory + .childDirectory('gradle') + .childDirectory('wrapper') + ..createSync(recursive: true); + wrapperDirectory + .childFile('gradle-wrapper.properties') + .writeAsStringSync(''' +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +# distributionUrl=https\\://services.gradle.org/distributions/gradle-8.0.2-all.zip +distributionUrl=https\\://services.gradle.org/distributions/gradle-$expectedVersion-all.zip +# distributionUrl=https\\://services.gradle.org/distributions/gradle-8.0.2-all.zip '''); expect(