From a2dac18e19a8587c7918a2a7d54d115a2872fc9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20M=C3=B6ltgen?= <39344769+M123-dev@users.noreply.github.com> Date: Tue, 13 Jun 2023 17:34:53 +0200 Subject: [PATCH] fix: More robust version check (#104) * fix: More combust version check * Switch to regex extraction Reverted logic in firehose.dart Updated latestVersion Added a new latestHeading method Added lots of test cases * comment * Version bump + remove prints --- pkgs/firehose/CHANGELOG.md | 4 ++ pkgs/firehose/lib/src/changelog.dart | 21 +++++++- pkgs/firehose/pubspec.yaml | 2 +- pkgs/firehose/test/changelog_test.dart | 70 +++++++++++++++++++++++++- 4 files changed, 94 insertions(+), 3 deletions(-) diff --git a/pkgs/firehose/CHANGELOG.md b/pkgs/firehose/CHANGELOG.md index 65500579..40a47c0e 100644 --- a/pkgs/firehose/CHANGELOG.md +++ b/pkgs/firehose/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.3.16 + +- More robust version checking, now more diverse changelog formats are accepted. + ## 0.3.15 - Make publish tags link to the new release page for that tag, with diff --git a/pkgs/firehose/lib/src/changelog.dart b/pkgs/firehose/lib/src/changelog.dart index 0e61df4d..76b65ef9 100644 --- a/pkgs/firehose/lib/src/changelog.dart +++ b/pkgs/firehose/lib/src/changelog.dart @@ -14,8 +14,27 @@ class Changelog { bool get exists => file.existsSync(); String? get latestVersion { + var input = latestHeading; + + if (input == null) { + return null; + } + + final versionRegex = RegExp(r'[0-9]+\.[0-9]+\.[0-9]+(\+[0-9]+)?'); + + var match = versionRegex.firstMatch(input); + + if (match != null) { + var version = match.group(0); + return version; + } + return null; + } + + String? get latestHeading { var sections = _parseSections(); - return sections.firstOrNull?.title.substring(3).trim(); + // Remove all leading "#" + return sections.firstOrNull?.title.replaceAll(RegExp(r'^#*'), '').trim(); } List get latestChangeEntries { diff --git a/pkgs/firehose/pubspec.yaml b/pkgs/firehose/pubspec.yaml index 91516093..42d6581d 100644 --- a/pkgs/firehose/pubspec.yaml +++ b/pkgs/firehose/pubspec.yaml @@ -1,6 +1,6 @@ name: firehose description: A tool to automate publishing of Pub packages from GitHub actions. -version: 0.3.15 +version: 0.3.16 repository: https://github.com/dart-lang/ecosystem/tree/main/pkgs/firehose environment: diff --git a/pkgs/firehose/test/changelog_test.dart b/pkgs/firehose/test/changelog_test.dart index 6ec0e8fe..3b1cc00f 100644 --- a/pkgs/firehose/test/changelog_test.dart +++ b/pkgs/firehose/test/changelog_test.dart @@ -17,11 +17,79 @@ void main() { }); }); + test('latestHeading', () { + withChangelog(_defaultContents, (file) { + var changelog = Changelog(file); + var heading = changelog.latestHeading; + expect(heading, '0.3.7+1'); + }); + }); + + test('Custom heading extraction', () { + withChangelog(''' +## 1.2.3+4 is the new version ## :) +''', (file) { + var changelog = Changelog(file); + var heading = changelog.latestHeading; + expect(heading, '1.2.3+4 is the new version ## :)'); + }); + }); + test('latestVersion', () { withChangelog(_defaultContents, (file) { var changelog = Changelog(file); var version = changelog.latestVersion; - expect(version, isNotNull); + expect(version, '0.3.7+1'); + }); + }); + + test('on digit version + x', () { + withChangelog(''' +## 1.2.3+4 +''', (file) { + var changelog = Changelog(file); + var version = changelog.latestVersion; + expect(version, '1.2.3+4'); + }); + }); + + test('multi digit version + x', () { + withChangelog(''' +## 123.456.789+123456789 +''', (file) { + var changelog = Changelog(file); + var version = changelog.latestVersion; + expect(version, '123.456.789+123456789'); + }); + }); + + test('no "+ x" at the end', () { + withChangelog(''' +## 123.456.789 +''', (file) { + var changelog = Changelog(file); + var version = changelog.latestVersion; + expect(version, '123.456.789'); + }); + }); + + test('custom heading version', () { + withChangelog(''' +## [4.7.0](https://github.com/...) (2023-05-06) +''', (file) { + var changelog = Changelog(file); + var version = changelog.latestVersion; + expect(version, '4.7.0'); + }); + }); + + test('multiple versions mentioned', () { + withChangelog(''' +## [4.7.0](https://github.com/.../.../compare/v4.6.0...v4.7.0) (25.05.23) +''', (file) { + var changelog = Changelog(file); + var version = changelog.latestVersion; + expect(version, '4.7.0'); }); });