Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parsing of the sparkleShortVersion value into the SUAppcastItem.displayVersionString property #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Sources/Appcast/SUAppcastItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,15 @@ public class SUAppcastItem {
let rolloutIntervalString = dict[SUAppcastElement.PhasedRolloutInterval] as? String
self.phasedRolloutInterval = Int(rolloutIntervalString ?? "")

// Sparkle 1.0.0 supports `<enclosure shortVersionString="">` attribute
var shortVersionString = enclosure?[SUAppcastAttribute.ShortVersionString] as? String
if shortVersionString == nil {
// Sparkle 2.0.0 supports `<sparkle:shortVersionString>` 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)
Expand All @@ -620,7 +629,6 @@ public class SUAppcastItem {
self.fullReleaseNotesURL = nil
}

self.displayVersionString = ""
self.date = nil
self.installationType = ""
self.deltaUpdates = [String: SUAppcastItem]()
Expand Down
79 changes: 79 additions & 0 deletions Tests/AppcastTests/SUAppcastItemTests+displayVersionString.swift
Original file line number Diff line number Diff line change
@@ -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 `<sparkle:version>` 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 `<sparkle:sparkleShortVersion>` 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 `<enclosure sparkle:shortVersionString="1.0.2">` attribute.
/// This value is prefered over the `<sparkle:sparkleShortVersion>` 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
}
}
Loading