Skip to content

Commit d2c6c43

Browse files
[flutter_plugin_tools] Improve version check error handling (flutter#4376)
Catches invalid CHANGELOG formats and logs useful error messages for them, rather than throwing FormatExceptions.
1 parent cdabfa9 commit d2c6c43

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

script/tool/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- `publish-check` now validates that there is an `AUTHORS` file.
99
- Added flags to `version-check` to allow overriding the platform interface
1010
major version change restriction.
11+
- Improved error handling and error messages in CHANGELOG version checks.
1112

1213
## 0.7.1
1314

script/tool/lib/src/version_check_command.dart

+16-7
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,9 @@ ${indentation}HTTP response: ${pubVersionFinderResponse.httpResponse.body}
374374
print(
375375
'${indentation}Found NEXT; validating next version in the CHANGELOG.');
376376
// Ensure that the version in pubspec hasn't changed without updating
377-
// CHANGELOG. That means the next version entry in the CHANGELOG pass the
378-
// normal validation.
377+
// CHANGELOG. That means the next version entry in the CHANGELOG should
378+
// pass the normal validation.
379+
versionString = null;
379380
while (iterator.moveNext()) {
380381
if (iterator.current.trim().startsWith('## ')) {
381382
versionString = iterator.current.trim().split(' ').last;
@@ -384,11 +385,19 @@ ${indentation}HTTP response: ${pubVersionFinderResponse.httpResponse.body}
384385
}
385386
}
386387

387-
final Version? fromChangeLog =
388-
versionString == null ? null : Version.parse(versionString);
389-
if (fromChangeLog == null) {
390-
printError(
391-
'${indentation}Cannot find version on the first line CHANGELOG.md');
388+
if (versionString == null) {
389+
printError('${indentation}Unable to find a version in CHANGELOG.md');
390+
print('${indentation}The current version should be on a line starting '
391+
'with "## ", either on the first non-empty line or after a "## NEXT" '
392+
'section.');
393+
return false;
394+
}
395+
396+
final Version fromChangeLog;
397+
try {
398+
fromChangeLog = Version.parse(versionString);
399+
} on FormatException {
400+
printError('"$versionString" could not be parsed as a version.');
392401
return false;
393402
}
394403

script/tool/test/version_check_command_test.dart

+67
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,73 @@ This is necessary because of X, Y, and Z
601601
);
602602
});
603603

604+
test(
605+
'fails gracefully if the version headers are not found due to using the wrong style',
606+
() async {
607+
final Directory pluginDirectory =
608+
createFakePlugin('plugin', packagesDir, version: '1.0.0');
609+
610+
const String changelog = '''
611+
## NEXT
612+
* Some changes for a later release.
613+
# 1.0.0
614+
* Some other changes.
615+
''';
616+
createFakeCHANGELOG(pluginDirectory, changelog);
617+
gitShowResponses = <String, String>{
618+
'master:packages/plugin/pubspec.yaml': 'version: 1.0.0',
619+
};
620+
621+
Error? commandError;
622+
final List<String> output = await runCapturingPrint(runner, <String>[
623+
'version-check',
624+
'--base-sha=master',
625+
], errorHandler: (Error e) {
626+
commandError = e;
627+
});
628+
629+
expect(commandError, isA<ToolExit>());
630+
expect(
631+
output,
632+
containsAllInOrder(<Matcher>[
633+
contains('Unable to find a version in CHANGELOG.md'),
634+
contains('The current version should be on a line starting with '
635+
'"## ", either on the first non-empty line or after a "## NEXT" '
636+
'section.'),
637+
]),
638+
);
639+
});
640+
641+
test('fails gracefully if the version is unparseable', () async {
642+
final Directory pluginDirectory =
643+
createFakePlugin('plugin', packagesDir, version: '1.0.0');
644+
645+
const String changelog = '''
646+
## Alpha
647+
* Some changes.
648+
''';
649+
createFakeCHANGELOG(pluginDirectory, changelog);
650+
gitShowResponses = <String, String>{
651+
'master:packages/plugin/pubspec.yaml': 'version: 1.0.0',
652+
};
653+
654+
Error? commandError;
655+
final List<String> output = await runCapturingPrint(runner, <String>[
656+
'version-check',
657+
'--base-sha=master',
658+
], errorHandler: (Error e) {
659+
commandError = e;
660+
});
661+
662+
expect(commandError, isA<ToolExit>());
663+
expect(
664+
output,
665+
containsAllInOrder(<Matcher>[
666+
contains('"Alpha" could not be parsed as a version.'),
667+
]),
668+
);
669+
});
670+
604671
test('allows valid against pub', () async {
605672
mockHttpResponse = <String, dynamic>{
606673
'name': 'some_package',

0 commit comments

Comments
 (0)