From 9c4dee426a70977e9bfcc3d139a6a05e14954298 Mon Sep 17 00:00:00 2001 From: Jozef Izso Date: Fri, 29 Nov 2024 17:39:31 +0100 Subject: [PATCH] Add parsing of the `sparkleShortVersion` value into the `SUAppcastItem.displayVersionString` property Issue #4 --- Sources/Appcast/SUAppcastItem.swift | 10 ++- ...ppcastItemTests+displayVersionString.swift | 79 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 Tests/AppcastTests/SUAppcastItemTests+displayVersionString.swift diff --git a/Sources/Appcast/SUAppcastItem.swift b/Sources/Appcast/SUAppcastItem.swift index e607b0a..2678544 100644 --- a/Sources/Appcast/SUAppcastItem.swift +++ b/Sources/Appcast/SUAppcastItem.swift @@ -602,6 +602,15 @@ public class SUAppcastItem { let rolloutIntervalString = dict[SUAppcastElement.PhasedRolloutInterval] as? String self.phasedRolloutInterval = Int(rolloutIntervalString ?? "") + // Sparkle 1.0.0 supports `` attribute + var shortVersionString = enclosure?[SUAppcastAttribute.ShortVersionString] as? String + if shortVersionString == nil { + // Sparkle 2.0.0 supports `` element + shortVersionString = dict[SUAppcastElement.ShortVersionString] as? String + } + + self.displayVersionString = shortVersionString ?? self.versionString + // Find the appropriate release notes URL. if let releaseNotesLinkString = dict[SUAppcastElement.ReleaseNotesLink] as? String { self.releaseNotesURL = URL(string: releaseNotesLinkString, relativeTo: appcastURL) @@ -620,7 +629,6 @@ public class SUAppcastItem { self.fullReleaseNotesURL = nil } - self.displayVersionString = "" self.date = nil self.installationType = "" self.deltaUpdates = [String: SUAppcastItem]() diff --git a/Tests/AppcastTests/SUAppcastItemTests+displayVersionString.swift b/Tests/AppcastTests/SUAppcastItemTests+displayVersionString.swift new file mode 100644 index 0000000..9360636 --- /dev/null +++ b/Tests/AppcastTests/SUAppcastItemTests+displayVersionString.swift @@ -0,0 +1,79 @@ +// +// Copyright 2024 Cisco Systems, Inc. All rights reserved. +// Licensed under MIT-style license (see LICENSE.txt file). +// + +import XCTest +@testable import Appcast + +class SUAppcastItemTests_displayVersionString: SUAppcastItemBaseTests { + // MARK: displayVersionString() tests + /// When update has only the `` element the ``SUAppcastItem.displayVersionString`` value is equal to the ``SUAppcastItem.versionString`` value. + func test_displayVersionString_appcastWithoutSparkleShortVersionStringValue() throws { + // Arrange + let item = try self.createAppcastItemWithShortVersionString(sparkleVersion: "1.4.8", sparkleShortVersion: nil) + + // Act + let version = item.versionString + let displayVersion = item.displayVersionString + + // Assert + XCTAssertEqual("1.4.8", version) + XCTAssertEqual(version, displayVersion) + } + + /// When update has the `` element, use it for the ``SUAppcastItem.displayVersionString`` value. + func test_displayVersionString_appcastWithSparkleShortVersionStringElement() throws { + // Arrange + let item = try self.createAppcastItemWithShortVersionString(sparkleVersion: "1799", sparkleShortVersion: "2.3.0") + + // Act + let version = item.versionString + let displayVersion = item.displayVersionString + + // Assert + XCTAssertEqual("1799", version) + XCTAssertEqual("2.3.0", displayVersion) + } + + /// Sparkle 1.0 supports the `` attribute. + /// This value is prefered over the `` element based on the original implementation. + func test_displayVersionString_enclosureElementWithShortVersionStringAttribute() throws { + // Arrange + let item = try self.createLegacyAppcastItemWithEnclosureShortVersionString(sparkleVersion: "1430", sparkleShortVersion: "1.0.2") + + // Act + let version = item.versionString + let displayVersion = item.displayVersionString + + // Assert + XCTAssertEqual("1430", version) + XCTAssertEqual("1.0.2", displayVersion) + } + + // MARK: helper methods for creating test data + func createAppcastItemWithShortVersionString(sparkleVersion: String, sparkleShortVersion: String?) throws -> SUAppcastItem { + var dict = self.createBasicAppcastItemDictionary() + + dict[SUAppcastElement.Version] = sparkleVersion + if let sparkleShortVersion { + dict[SUAppcastElement.ShortVersionString] = sparkleShortVersion + } + + let item = try SUAppcastItem(dictionary: dict, relativeTo: nil, stateResolver: nil, resolvedState: nil) + return item + } + + func createLegacyAppcastItemWithEnclosureShortVersionString(sparkleVersion: String, sparkleShortVersion: String) throws -> SUAppcastItem { + var enclosure = SUAppcastItem.AppcastItemDictionary() + enclosure[SUAppcastAttribute.ShortVersionString] = sparkleShortVersion + + var dict = self.createBasicAppcastItemDictionary() + + dict[SUAppcastElement.Version] = sparkleVersion + dict[SURSSElement.Enclosure] = enclosure + + let item = try SUAppcastItem(dictionary: dict, relativeTo: nil, stateResolver: nil, resolvedState: nil) + return item + } +}