From 175c1c6d3306d2dd3015ccf11737eaefd9f34517 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Mon, 18 Dec 2023 18:19:04 +0100 Subject: [PATCH] refactor(nextcloud_test): Remove version handling mess Signed-off-by: jld3103 --- packages/nextcloud/test/dashboard_test.dart | 2 +- .../nextcloud_test/bin/generate_presets.dart | 37 +++---- packages/nextcloud_test/lib/src/app.dart | 19 ++-- .../lib/src/docker_container.dart | 2 +- .../lib/src/extended_version.dart | 103 ------------------ packages/nextcloud_test/lib/src/fixtures.dart | 2 +- packages/nextcloud_test/lib/src/presets.dart | 12 +- 7 files changed, 36 insertions(+), 141 deletions(-) delete mode 100644 packages/nextcloud_test/lib/src/extended_version.dart diff --git a/packages/nextcloud/test/dashboard_test.dart b/packages/nextcloud/test/dashboard_test.dart index 020fc043dae..22e0188d864 100644 --- a/packages/nextcloud/test/dashboard_test.dart +++ b/packages/nextcloud/test/dashboard_test.dart @@ -48,7 +48,7 @@ void main() { final items = response.body.ocs.data['recommendations']!.items; expect(items, hasLength(7)); }, - skip: preset.version.getRaw() < Version(27, 1, 0), + skip: preset.version < Version(27, 1, 0), ); }); }, diff --git a/packages/nextcloud_test/bin/generate_presets.dart b/packages/nextcloud_test/bin/generate_presets.dart index 77be1b41f89..540e03060fb 100644 --- a/packages/nextcloud_test/bin/generate_presets.dart +++ b/packages/nextcloud_test/bin/generate_presets.dart @@ -1,8 +1,8 @@ import 'package:nextcloud/core.dart' as core; import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud_test/src/app.dart'; -import 'package:nextcloud_test/src/extended_version.dart'; import 'package:universal_io/io.dart'; +import 'package:version/version.dart'; Future main() async { const appIDs = [ @@ -43,7 +43,8 @@ Future main() async { } } - File('${appPresetsDir.path}/${release.version.withoutPatch()}').writeAsStringSync(buffer.toString()); + File('${appPresetsDir.path}/${release.version.major}.${release.version.minor}') + .writeAsStringSync(buffer.toString()); } } @@ -63,21 +64,21 @@ Future main() async { buffer.writeln('${app.id.toUpperCase()}_URL=${release.url}'); } - File('${serverPresetsDir.path}/${serverVersion.withoutPatch()}').writeAsStringSync(buffer.toString()); + File('${serverPresetsDir.path}/${serverVersion.major}.${serverVersion.minor}').writeAsStringSync(buffer.toString()); } final latestPresetLink = Link('docker/presets/latest'); if (latestPresetLink.existsSync()) { - latestPresetLink.updateSync('server/${serverVersions.first.withoutPatch()}'); + latestPresetLink.updateSync('server/${serverVersions.first.major}.${serverVersions.first.minor}'); } else { - latestPresetLink.createSync('server/${serverVersions.first.withoutPatch()}'); + latestPresetLink.createSync('server/${serverVersions.first.major}.${serverVersions.first.minor}'); } httpClient.close(force: true); } -Future> _getServerVersions(final HttpClient httpClient) async { - final versions = {}; +Future> _getServerVersions(final HttpClient httpClient) async { + final versions = {}; String? next = 'https://hub.docker.com/v2/repositories/library/nextcloud/tags?page_size=1000'; while (next != null) { @@ -96,14 +97,10 @@ Future> _getServerVersions(final HttpClient httpClient) as try { final tag = result as Map; - final version = ExtendedVersion.parse(tag['name'] as String); - if (version.minor == null || version.patch == null) { - continue; - } - - final normalizedVersion = version.withoutPatch(); + final version = Version.parse(tag['name'] as String); + final normalizedVersion = Version(version.major, version.minor, 0); - if (version < coreMinVersion || version.major > core.maxMajor) { + if (version < core.minVersion || version.major > core.maxMajor) { continue; } @@ -138,22 +135,22 @@ Future> _getApps(final List appIDs, final HttpClient httpClien continue; } - final releases = {}; + final releases = {}; final releasesItems = app['releases'] as List; for (final releaseItem in releasesItems) { final release = releaseItem as Map; - final version = ExtendedVersion.parse(release['version'] as String); - final normalizedVersion = version.withoutPatch(); + final version = Version.parse(release['version'] as String); + final normalizedVersion = Version(version.major, version.minor, 0); final rawPlatformVersionSpec = release['rawPlatformVersionSpec'] as String; final minimumServerVersionRequirement = - ExtendedVersion.parse(rawPlatformVersionSpec.split(' ')[0].replaceFirst('>=', '')); + Version.parse(rawPlatformVersionSpec.split(' ')[0].replaceFirst('>=', '')); final maximumServerVersionRequirement = - ExtendedVersion.parse(rawPlatformVersionSpec.split(' ')[1].replaceFirst('<=', '')); + Version.parse(rawPlatformVersionSpec.split(' ')[1].replaceFirst('<=', '')); - if (maximumServerVersionRequirement < coreMinVersion) { + if (maximumServerVersionRequirement < core.minVersion) { continue; } if (minimumServerVersionRequirement.major > core.maxMajor) { diff --git a/packages/nextcloud_test/lib/src/app.dart b/packages/nextcloud_test/lib/src/app.dart index cd7d41f172b..633ffcc1f6f 100644 --- a/packages/nextcloud_test/lib/src/app.dart +++ b/packages/nextcloud_test/lib/src/app.dart @@ -1,13 +1,13 @@ import 'package:meta/meta.dart'; -import 'package:nextcloud_test/src/extended_version.dart'; +import 'package:version/version.dart'; /// Describes a release of an [App] from https://apps.nextcloud.com @internal typedef AppRelease = ({ - ExtendedVersion version, + Version version, String url, - ExtendedVersion minimumServerVersion, - ExtendedVersion maximumServerVersion, + Version minimumServerVersion, + Version maximumServerVersion, }); /// Describes an app from https://apps.nextcloud.com @@ -19,13 +19,13 @@ typedef App = ({ @internal extension AppFindLatestRelease on App { - AppRelease? findLatestCompatibleRelease(final ExtendedVersion serverVersion, {final bool allowUnstable = false}) { + AppRelease? findLatestCompatibleRelease(final Version serverVersion, {final bool allowUnstable = false}) { final compatibleReleases = releases .where( (final release) => serverVersion >= release.minimumServerVersion && - serverVersion <= release.maximumServerVersion && - (allowUnstable || !release.version.getRaw().isPreRelease), + serverVersion < release.maximumServerVersion.incrementMajor() && + (allowUnstable || !release.version.isPreRelease), ) .toList() ..sort((final a, final b) => b.version.compareTo(a.version)); @@ -40,10 +40,11 @@ extension AppFindLatestRelease on App { @internal extension AppReleaseFindLatestServerVersion on AppRelease { - ExtendedVersion findLatestServerVersion(final List serverVersions) { + Version findLatestServerVersion(final List serverVersions) { final compatibleReleases = serverVersions .where( - (final serverVersion) => serverVersion >= minimumServerVersion && serverVersion <= maximumServerVersion, + (final serverVersion) => + serverVersion >= minimumServerVersion && serverVersion < maximumServerVersion.incrementMajor(), ) .toList() ..sort((final a, final b) => b.compareTo(a)); diff --git a/packages/nextcloud_test/lib/src/docker_container.dart b/packages/nextcloud_test/lib/src/docker_container.dart index 527542a91a0..27d6f839c8e 100644 --- a/packages/nextcloud_test/lib/src/docker_container.dart +++ b/packages/nextcloud_test/lib/src/docker_container.dart @@ -16,7 +16,7 @@ class DockerContainer { /// Creates a new docker container and returns its representation. static Future create(final Preset preset) async { - final dockerImageName = 'ghcr.io/nextcloud/neon/dev:${preset.name}-${preset.version}'; + final dockerImageName = 'ghcr.io/nextcloud/neon/dev:${preset.name}-${preset.version.major}.${preset.version.minor}'; var result = await runExecutableArguments( 'docker', diff --git a/packages/nextcloud_test/lib/src/extended_version.dart b/packages/nextcloud_test/lib/src/extended_version.dart deleted file mode 100644 index 1a749dfacea..00000000000 --- a/packages/nextcloud_test/lib/src/extended_version.dart +++ /dev/null @@ -1,103 +0,0 @@ -import 'package:meta/meta.dart'; -import 'package:nextcloud/core.dart' as core; -import 'package:version/version.dart'; - -/// An [ExtendedVersion] for [core.minVersion]. -@internal -final coreMinVersion = ExtendedVersion.fromVersion(core.minVersion); - -/// A replacement for [Version] that correctly compares [minor] and [patch] versions. -@immutable -@internal -class ExtendedVersion implements Comparable { - const ExtendedVersion( - final Version raw, - this.major, - this.minor, - this.patch, - ) : _raw = raw; - - factory ExtendedVersion.parse(final String versionString) { - final version = Version.parse(versionString); - final dotCount = versionString.split('').where((final char) => char == '.').length; - - return ExtendedVersion( - version, - version.major, - dotCount >= 1 ? version.minor : null, - dotCount >= 2 ? version.patch : null, - ); - } - - factory ExtendedVersion.fromVersion(final Version version) => ExtendedVersion( - version, - version.major, - version.minor, - version.patch, - ); - - final Version _raw; - - final int major; - - final int? minor; - - final int? patch; - - ExtendedVersion withoutPatch() => ExtendedVersion( - _raw, - major, - minor, - null, - ); - - Version getRaw() => _raw; - - @override - String toString() { - final buffer = StringBuffer('$major'); - - if (minor != null) { - buffer.write('.$minor'); - } - if (patch != null) { - buffer.write('.$patch'); - } - - return buffer.toString(); - } - - @override - int compareTo(final ExtendedVersion other) => _compare(this, other); - - bool operator <(final dynamic other) => other is ExtendedVersion && _compare(this, other) < 0; - - bool operator <=(final dynamic other) => other is ExtendedVersion && _compare(this, other) <= 0; - - @override - bool operator ==(final dynamic other) => other is ExtendedVersion && _compare(this, other) == 0; - - bool operator >=(final dynamic other) => other is ExtendedVersion && _compare(this, other) >= 0; - - bool operator >(final dynamic other) => other is ExtendedVersion && _compare(this, other) > 0; - - static int _compare(final ExtendedVersion a, final ExtendedVersion b) { - final compareMinors = a.minor != null && b.minor != null; - final comparePatches = a.patch != null && b.patch != null; - - return Version( - a.major, - compareMinors ? a.minor! : 0, - comparePatches ? a.patch! : 0, - ).compareTo( - Version( - b.major, - compareMinors ? b.minor! : 0, - comparePatches ? b.patch! : 0, - ), - ); - } - - @override - int get hashCode => Object.hashAll([major, minor, patch]); -} diff --git a/packages/nextcloud_test/lib/src/fixtures.dart b/packages/nextcloud_test/lib/src/fixtures.dart index 4a8b41696c6..4d7e619ce1d 100644 --- a/packages/nextcloud_test/lib/src/fixtures.dart +++ b/packages/nextcloud_test/lib/src/fixtures.dart @@ -33,7 +33,7 @@ void validateFixture(final Preset preset) { // Remove the groups that are the preset name and the preset version and the app is kept. for (var i = 0; i <= 2; i++) { - if (groups[i] == preset.version.toString()) { + if (groups[i] == '${preset.version.major}.${preset.version.minor}') { if (i == 1) { // Remove preset version groups.removeAt(1); diff --git a/packages/nextcloud_test/lib/src/presets.dart b/packages/nextcloud_test/lib/src/presets.dart index f32a6ba8dea..4606dab2f38 100644 --- a/packages/nextcloud_test/lib/src/presets.dart +++ b/packages/nextcloud_test/lib/src/presets.dart @@ -1,14 +1,14 @@ import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud_test/nextcloud_test.dart'; -import 'package:nextcloud_test/src/extended_version.dart'; import 'package:test/test.dart'; import 'package:universal_io/io.dart'; +import 'package:version/version.dart'; /// Combination of preset `name` and preset `version`. -typedef Preset = ({String name, ExtendedVersion version}); +typedef Preset = ({String name, Version version}); -final Map> _presets = () { - final presets = >{}; +final Map> _presets = () { + final presets = >{}; final presetGroups = Directory('../nextcloud_test/docker/presets') .listSync(followLinks: false) @@ -19,7 +19,7 @@ final Map> _presets = () { final presetVersions = Directory('../nextcloud_test/docker/presets/$presetGroup') .listSync(followLinks: false) .whereType() - .map((final f) => ExtendedVersion.parse(PathUri.parse(f.path).name)); + .map((final f) => Version.parse(PathUri.parse(f.path).name)); presets[presetGroup] = presetVersions.toList(); } @@ -41,7 +41,7 @@ void presets( void innerBody() { for (final presetVersion in _presets[presetGroup]!) { - group(presetVersion, () { + group('${presetVersion.major}.${presetVersion.minor}', () { final preset = (name: presetGroup, version: presetVersion); tearDown(() {