From 08205ee2853f6b5b204ff54c7ccbc3d54106715b Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 23 Sep 2021 10:37:45 -0400 Subject: [PATCH 1/3] [flutter_plugin_tools] Improve version check error handling Catches invalid CHANGELOG formats and logs useful error messages for them, rather than throwing FormatExceptions. --- script/tool/CHANGELOG.md | 1 + .../tool/lib/src/version_check_command.dart | 23 +++++-- .../tool/test/version_check_command_test.dart | 67 +++++++++++++++++++ 3 files changed, 84 insertions(+), 7 deletions(-) diff --git a/script/tool/CHANGELOG.md b/script/tool/CHANGELOG.md index 7e9cd3bec938..dadeb79a3498 100644 --- a/script/tool/CHANGELOG.md +++ b/script/tool/CHANGELOG.md @@ -6,6 +6,7 @@ federated packages that have been done in such a way that they will pass in CI, but fail once the change is landed and published. - `publish-check` now validates that there is an `AUTHORS` file. +- Improved error handling and error messages in CHANGELOG version checks. ## 0.7.1 diff --git a/script/tool/lib/src/version_check_command.dart b/script/tool/lib/src/version_check_command.dart index 6b49c40d66bb..5acdb2781ae8 100644 --- a/script/tool/lib/src/version_check_command.dart +++ b/script/tool/lib/src/version_check_command.dart @@ -348,8 +348,9 @@ ${indentation}HTTP response: ${pubVersionFinderResponse.httpResponse.body} print( '${indentation}Found NEXT; validating next version in the CHANGELOG.'); // Ensure that the version in pubspec hasn't changed without updating - // CHANGELOG. That means the next version entry in the CHANGELOG pass the - // normal validation. + // CHANGELOG. That means the next version entry in the CHANGELOG should + // pass the normal validation. + versionString = null; while (iterator.moveNext()) { if (iterator.current.trim().startsWith('## ')) { versionString = iterator.current.trim().split(' ').last; @@ -358,11 +359,19 @@ ${indentation}HTTP response: ${pubVersionFinderResponse.httpResponse.body} } } - final Version? fromChangeLog = - versionString == null ? null : Version.parse(versionString); - if (fromChangeLog == null) { - printError( - '${indentation}Cannot find version on the first line CHANGELOG.md'); + if (versionString == null) { + printError('${indentation}Unable to find a version in CHANGELOG.md'); + print('${indentation}The current version should be on a line starting ' + ' with"## ", either on the first non-empty line or after a "## NEXT" ' + 'section.'); + return false; + } + + final Version fromChangeLog; + try { + fromChangeLog = Version.parse(versionString); + } on FormatException { + printError('"$versionString" could not be parsed as a version.'); return false; } diff --git a/script/tool/test/version_check_command_test.dart b/script/tool/test/version_check_command_test.dart index 9ab7c57089a3..927550bcd59e 100644 --- a/script/tool/test/version_check_command_test.dart +++ b/script/tool/test/version_check_command_test.dart @@ -514,6 +514,73 @@ void main() { ); }); + test( + 'fails gracefully if the version headers are not found due to using the wrong style', + () async { + final Directory pluginDirectory = + createFakePlugin('plugin', packagesDir, version: '1.0.0'); + + const String changelog = ''' +## NEXT +* Some changes for a later release. +# 1.0.0 +* Some other changes. +'''; + createFakeCHANGELOG(pluginDirectory, changelog); + gitShowResponses = { + 'master:packages/plugin/pubspec.yaml': 'version: 1.0.0', + }; + + Error? commandError; + final List output = await runCapturingPrint(runner, [ + 'version-check', + '--base-sha=master', + ], errorHandler: (Error e) { + commandError = e; + }); + + expect(commandError, isA()); + expect( + output, + containsAllInOrder([ + contains('Unable to find a version in CHANGELOG.md'), + contains('The current version should be on a line starting with ' + '"## ", either on the first non-empty line or after a "## NEXT" ' + 'section.'), + ]), + ); + }); + + test('fails gracefully if the version is unparseable', () async { + final Directory pluginDirectory = + createFakePlugin('plugin', packagesDir, version: '1.0.0'); + + const String changelog = ''' +## Alpha +* Some changes. +'''; + createFakeCHANGELOG(pluginDirectory, changelog); + gitShowResponses = { + 'master:packages/plugin/pubspec.yaml': 'version: 1.0.0', + }; + + Error? commandError; + final List output = await runCapturingPrint(runner, [ + 'version-check', + '--base-sha=master', + ], errorHandler: (Error e) { + commandError = e; + }); + + expect(commandError, isA()); + expect( + output, + containsAllInOrder([ + contains('"Alpha" could not be parsed as a version.'), + ]), + ); + }); + test('allows valid against pub', () async { mockHttpResponse = { 'name': 'some_package', From 16767e5d0394d0c430408358a3c0c691f19c4ad8 Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Thu, 23 Sep 2021 13:38:26 -0400 Subject: [PATCH 2/3] Fix string Co-authored-by: David Iglesias --- script/tool/lib/src/version_check_command.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/tool/lib/src/version_check_command.dart b/script/tool/lib/src/version_check_command.dart index 5acdb2781ae8..ab853f4f2c60 100644 --- a/script/tool/lib/src/version_check_command.dart +++ b/script/tool/lib/src/version_check_command.dart @@ -362,7 +362,7 @@ ${indentation}HTTP response: ${pubVersionFinderResponse.httpResponse.body} if (versionString == null) { printError('${indentation}Unable to find a version in CHANGELOG.md'); print('${indentation}The current version should be on a line starting ' - ' with"## ", either on the first non-empty line or after a "## NEXT" ' + ' with "## ", either on the first non-empty line or after a "## NEXT" ' 'section.'); return false; } From d30807a6e9041fb1178c0e1eb82d9e3e583e5c83 Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Thu, 23 Sep 2021 13:51:00 -0400 Subject: [PATCH 3/3] String break fix Co-authored-by: David Iglesias --- script/tool/lib/src/version_check_command.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/tool/lib/src/version_check_command.dart b/script/tool/lib/src/version_check_command.dart index ab853f4f2c60..f7bc908bd1df 100644 --- a/script/tool/lib/src/version_check_command.dart +++ b/script/tool/lib/src/version_check_command.dart @@ -362,7 +362,7 @@ ${indentation}HTTP response: ${pubVersionFinderResponse.httpResponse.body} if (versionString == null) { printError('${indentation}Unable to find a version in CHANGELOG.md'); print('${indentation}The current version should be on a line starting ' - ' with "## ", either on the first non-empty line or after a "## NEXT" ' + 'with "## ", either on the first non-empty line or after a "## NEXT" ' 'section.'); return false; }