Skip to content

Commit f9b6795

Browse files
authored
Update Gradle Version Formatting Validation (#10326)
Updated the Gradle version formatting validation with a stricter regex. Previously passed on invalid versions. ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 3caa48b commit f9b6795

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

script/tool/lib/src/update_dependency_command.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,16 @@ ${response.httpResponse.body}
138138
printError('A version must be provided to update this dependency.');
139139
throw ToolExit(_exitNoTargetVersion);
140140
} else if (_targetAndroidDependency == _AndroidDepdencyType.gradle) {
141-
final RegExp validGradleVersionPattern = RegExp(r'^\d+(?:\.\d+){1,2}$');
141+
final RegExp validGradleVersionPattern =
142+
RegExp(r'^\d{1,2}\.\d{1,2}(?:\.\d)?$');
142143
final bool isValidGradleVersion =
143144
validGradleVersionPattern.stringMatch(version) == version;
144145
if (!isValidGradleVersion) {
145-
printError(
146-
'A version with a valid format (maximum 2-3 numbers separated by period) must be provided.');
146+
printError('''
147+
A version with a valid format (maximum 2-3 numbers separated by 1-2 periods) must be provided.
148+
1. The first number must have one or two digits
149+
2. The second number must have one or two digits
150+
3. If present, the third number must have a single digit''');
147151
throw ToolExit(_exitInvalidTargetVersion);
148152
}
149153
} else if (_targetAndroidDependency == _AndroidDepdencyType.compileSdk ||

script/tool/test/update_dependency_command_test.dart

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -608,27 +608,40 @@ dev_dependencies:
608608

609609
group('Android dependencies', () {
610610
group('gradle', () {
611-
test('throws if version format is invalid', () async {
612-
Error? commandError;
613-
final List<String> output = await runCapturingPrint(runner, <String>[
614-
'update-dependency',
615-
'--android-dependency',
616-
'gradle',
617-
'--version',
618-
'83',
619-
], errorHandler: (Error e) {
620-
commandError = e;
621-
});
611+
final List<String> invalidGradleVersionsFormat = <String>[
612+
'81',
613+
'811.1',
614+
'8.123',
615+
'8.12.12'
616+
];
622617

623-
expect(commandError, isA<ToolExit>());
624-
expect(
625-
output,
626-
containsAllInOrder(<Matcher>[
627-
contains(
628-
'A version with a valid format (maximum 2-3 numbers separated by period) must be provided.'),
629-
]),
630-
);
631-
});
618+
for (final String gradleVersion in invalidGradleVersionsFormat) {
619+
test('throws because gradleVersion: $gradleVersion is invalid',
620+
() async {
621+
Error? commandError;
622+
final List<String> output = await runCapturingPrint(runner, <String>[
623+
'update-dependency',
624+
'--android-dependency',
625+
'gradle',
626+
'--version',
627+
gradleVersion,
628+
], errorHandler: (Error e) {
629+
commandError = e;
630+
});
631+
632+
expect(commandError, isA<ToolExit>());
633+
expect(
634+
output,
635+
containsAllInOrder(<Matcher>[
636+
contains('''
637+
A version with a valid format (maximum 2-3 numbers separated by 1-2 periods) must be provided.
638+
1. The first number must have one or two digits
639+
2. The second number must have one or two digits
640+
3. If present, the third number must have a single digit'''),
641+
]),
642+
);
643+
});
644+
}
632645

633646
test('skips if example app does not run on Android', () async {
634647
final RepositoryPackage package =

0 commit comments

Comments
 (0)