Skip to content

Commit

Permalink
Merge pull request mas-cli#692 from rgoldberg/679-cleanup
Browse files Browse the repository at this point in the history
Minor Swift cleanup
  • Loading branch information
rgoldberg authored Dec 31, 2024
2 parents 49003bc + 00a180b commit af55727
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Sources/mas/Commands/Outdated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension MAS {
appLibrary.installedApps.map { installedApp in
searcher.lookup(appID: installedApp.itemIdentifier.appIDValue)
.done { storeApp in
if installedApp.isOutdatedWhenComparedTo(storeApp) {
if installedApp.isOutdated(comparedTo: storeApp) {
print(
"""
\(installedApp.itemIdentifier) \(installedApp.displayName) \
Expand Down
2 changes: 1 addition & 1 deletion Sources/mas/Commands/Upgrade.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ extension MAS {
// only upgrade apps whose local version differs from the store version
searcher.lookup(appID: installedApp.itemIdentifier.appIDValue)
.map { storeApp -> (SoftwareProduct, SearchResult)? in
guard installedApp.isOutdatedWhenComparedTo(storeApp) else {
guard installedApp.isOutdated(comparedTo: storeApp) else {
return nil
}
return (installedApp, storeApp)
Expand Down
4 changes: 2 additions & 2 deletions Sources/mas/Controllers/ITunesSearchAppStoreSearcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Version
/// Manages searching the MAS catalog. Uses the iTunes Search and Lookup APIs:
/// https://performance-partners.apple.com/search-api
struct ITunesSearchAppStoreSearcher: AppStoreSearcher {
private static let appVersionExpression = Regex(#"\"versionDisplay\"\:\"([^\"]+)\""#)
private static let appVersionRegex = Regex(#""versionDisplay":"([^"]+)""#)

private let networkManager: NetworkManager

Expand Down Expand Up @@ -119,7 +119,7 @@ struct ITunesSearchAppStoreSearcher: AppStoreSearcher {
.map { data in
guard
let html = String(data: data, encoding: .utf8),
let capture = Self.appVersionExpression.firstMatch(in: html)?.captures[0],
let capture = Self.appVersionRegex.firstMatch(in: html)?.captures[0],
let version = Version(tolerant: capture)
else {
return nil
Expand Down
11 changes: 4 additions & 7 deletions Sources/mas/Errors/MASError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum MASError: Error, Equatable {
case macOSUserMustBeRoot

case noData
case jsonParsing(data: Data?)
case jsonParsing(data: Data)
}

// MARK: - CustomStringConvertible
Expand Down Expand Up @@ -102,13 +102,10 @@ extension MASError: CustomStringConvertible {
case .noData:
return "Service did not return data"
case .jsonParsing(let data):
if let data {
if let unparsable = String(data: data, encoding: .utf8) {
return "Unable to parse response as JSON: \n\(unparsable)"
}
return "Received defective response"
if let unparsable = String(data: data, encoding: .utf8) {
return "Unable to parse response as JSON:\n\(unparsable)"
}
return "Received empty response"
return "Received defective response"
}
}
}
2 changes: 1 addition & 1 deletion Sources/mas/Models/SearchResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ struct SearchResult: Decodable {

extension SearchResult {
var displayPrice: String {
formattedPrice ?? "Unknown"
formattedPrice ?? "?"
}
}
2 changes: 1 addition & 1 deletion Sources/mas/Models/SoftwareProduct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension SoftwareProduct {
///
/// - Parameter storeApp: App from search result.
/// - Returns: true if the app is outdated; false otherwise.
func isOutdatedWhenComparedTo(_ storeApp: SearchResult) -> Bool {
func isOutdated(comparedTo storeApp: SearchResult) -> Bool {
// If storeApp requires a version of macOS newer than the running version, do not consider self outdated.
if let osVersion = Version(tolerant: storeApp.minimumOsVersion) {
let requiredVersion = OperatingSystemVersion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,10 @@ public class ITunesSearchAppStoreSearcherSpec: QuickSpec {
let searcher = ITunesSearchAppStoreSearcher(networkManager: NetworkManager(session: networkSession))

var result: SearchResult?
do {
expect {
result = try searcher.lookup(appID: appID).wait()
} catch {
let maserror = error as! MASError
if case .jsonParsing(let nserror) = maserror {
fail("\(maserror) \(nserror!)")
}
}
.toNot(throwError())

guard let result else {
fatalError("lookup result was nil")
Expand Down
2 changes: 1 addition & 1 deletion Tests/masTests/Errors/MASErrorTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ class MASErrorTestCase: XCTestCase {
}

func testJsonParsing() {
XCTAssertEqual(MASError.jsonParsing(data: nil).description, "Received empty response")
XCTAssertEqual(MASError.jsonParsing(data: Data()).description, "Unable to parse response as JSON:\n")
}
}
8 changes: 4 additions & 4 deletions Tests/masTests/Models/SoftwareProductSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ public class SoftwareProductSpec: QuickSpec {
let updateIos = SearchResult(minimumOsVersion: "99.0.0", version: "3.0.0")

it("is not outdated when there is no new version available") {
expect(app.isOutdatedWhenComparedTo(currentApp)) == false
expect(app.isOutdated(comparedTo: currentApp)) == false
}
it("is outdated when there is a new version available") {
expect(app.isOutdatedWhenComparedTo(appUpdate)) == true
expect(app.isOutdated(comparedTo: appUpdate)) == true
}
it("is not outdated when the new version of mac-software requires a higher OS version") {
expect(app.isOutdatedWhenComparedTo(higherOs)) == false
expect(app.isOutdated(comparedTo: higherOs)) == false
}
it("is not outdated when the new version of software requires a higher OS version") {
expect(app.isOutdatedWhenComparedTo(updateIos)) == false
expect(app.isOutdated(comparedTo: updateIos)) == false
}
}
}
Expand Down

0 comments on commit af55727

Please sign in to comment.