Skip to content

Commit

Permalink
SystemInfo.init no longer throws (#2803)
Browse files Browse the repository at this point in the history
Not sure when we stopped throwing errors, but we no longer do, which
simplifies code a lot.
  • Loading branch information
NachoSoto authored Jul 13, 2023
1 parent f7e36fb commit 79aa55f
Show file tree
Hide file tree
Showing 29 changed files with 161 additions and 179 deletions.
2 changes: 1 addition & 1 deletion Sources/Misc/SystemInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class SystemInfo {
sandboxEnvironmentDetector: SandboxEnvironmentDetector = BundleSandboxEnvironmentDetector.default,
storeKit2Setting: StoreKit2Setting = .default,
responseVerificationMode: Signing.ResponseVerificationMode = .default,
dangerousSettings: DangerousSettings? = nil) throws {
dangerousSettings: DangerousSettings? = nil) {
self.platformFlavor = platformInfo?.flavor ?? "native"
self.platformFlavorVersion = platformInfo?.version
self._bundle = .init(bundle)
Expand Down
17 changes: 6 additions & 11 deletions Sources/Purchasing/Purchases/Purchases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,12 @@ public typealias StartPurchaseBlock = (@escaping PurchaseCompletedBlock) -> Void
let receiptRefreshRequestFactory = ReceiptRefreshRequestFactory()
let fetcher = StoreKitRequestFetcher(requestFactory: receiptRefreshRequestFactory,
operationDispatcher: operationDispatcher)
let systemInfo: SystemInfo
do {
systemInfo = try SystemInfo(platformInfo: platformInfo,
finishTransactions: !observerMode,
operationDispatcher: operationDispatcher,
storeKit2Setting: storeKit2Setting,
responseVerificationMode: responseVerificationMode,
dangerousSettings: dangerousSettings)
} catch {
fatalError(error.localizedDescription)
}
let systemInfo = SystemInfo(platformInfo: platformInfo,
finishTransactions: !observerMode,
operationDispatcher: operationDispatcher,
storeKit2Setting: storeKit2Setting,
responseVerificationMode: responseVerificationMode,
dangerousSettings: dangerousSettings)

let receiptFetcher = ReceiptFetcher(requestFetcher: fetcher, systemInfo: systemInfo)
let eTagManager = ETagManager()
Expand Down
8 changes: 4 additions & 4 deletions Tests/StoreKitUnitTests/LocalReceiptParserStoreKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class LocalReceiptParserStoreKitTests: StoreKitConfigTestCase {
self.requestFetcher = StoreKitRequestFetcher(requestFactory: receiptRefreshRequestFactory,
operationDispatcher: operationDispatcher)

self.systemInfo = try SystemInfo(platformInfo: Purchases.platformInfo,
finishTransactions: true,
operationDispatcher: operationDispatcher,
storeKit2Setting: .disabled)
self.systemInfo = SystemInfo(platformInfo: Purchases.platformInfo,
finishTransactions: true,
operationDispatcher: operationDispatcher,
storeKit2Setting: .disabled)
self.receiptFetcher = ReceiptFetcher(requestFetcher: self.requestFetcher, systemInfo: systemInfo)
self.parser = .default
}
Expand Down
7 changes: 3 additions & 4 deletions Tests/StoreKitUnitTests/OfferingsManagerStoreKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ class OfferingsManagerStoreKitTests: StoreKitConfigTestCase {

var mockDeviceCache: MockDeviceCache!
let mockOperationDispatcher = MockOperationDispatcher()
// swiftlint:disable:next force_try
let mockSystemInfo = try! MockSystemInfo(platformInfo: .init(flavor: "iOS", version: "3.2.1"),
finishTransactions: true,
storeKit2Setting: .enabledForCompatibleDevices)
let mockSystemInfo = MockSystemInfo(platformInfo: .init(flavor: "iOS", version: "3.2.1"),
finishTransactions: true,
storeKit2Setting: .enabledForCompatibleDevices)
let mockBackend = MockBackend()
var mockOfferings: MockOfferingsAPI!
let mockOfferingsFactory = OfferingsFactory()
Expand Down
12 changes: 6 additions & 6 deletions Tests/StoreKitUnitTests/ProductsManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import XCTest
class ProductsManagerTests: StoreKitConfigTestCase {

func testFetchProductsWithIdentifiersSK1() throws {
let manager = try createManager(storeKit2Setting: .disabled)
let manager = self.createManager(storeKit2Setting: .disabled)

let identifier = "com.revenuecat.monthly_4.99.1_week_intro"
let receivedProducts = waitUntilValue(timeout: Self.requestDispatchTimeout) { completed in
Expand All @@ -40,7 +40,7 @@ class ProductsManagerTests: StoreKitConfigTestCase {
throw XCTSkip("Required API is not available for this test.")
}

let manager = try createManager(storeKit2Setting: .enabledForCompatibleDevices)
let manager = self.createManager(storeKit2Setting: .enabledForCompatibleDevices)

let identifier = "com.revenuecat.monthly_4.99.1_week_intro"
let receivedProducts = waitUntilValue(timeout: Self.requestDispatchTimeout) { completed in
Expand All @@ -56,7 +56,7 @@ class ProductsManagerTests: StoreKitConfigTestCase {
}

func testClearCacheAfterStorefrontChangesSK1() async throws {
let manager = try createManager(storeKit2Setting: .disabled)
let manager = self.createManager(storeKit2Setting: .disabled)

let identifier = "com.revenuecat.monthly_4.99.1_week_intro"
var receivedProducts: Set<StoreProduct>?
Expand Down Expand Up @@ -86,7 +86,7 @@ class ProductsManagerTests: StoreKitConfigTestCase {
func testInvalidateAndReFetchCachedProductsAfterStorefrontChangesSK2() async throws {
try AvailabilityChecks.iOS15APIAvailableOrSkipTest()

let manager = try createManager(storeKit2Setting: .enabledForCompatibleDevices)
let manager = self.createManager(storeKit2Setting: .enabledForCompatibleDevices)

let identifier = "com.revenuecat.monthly_4.99.1_week_intro"
var receivedProducts: Set<StoreProduct>?
Expand All @@ -112,10 +112,10 @@ class ProductsManagerTests: StoreKitConfigTestCase {
expect(unwrappedFirstProduct.currencyCode) == "EUR"
}

private func createManager(storeKit2Setting: StoreKit2Setting) throws -> ProductsManager {
private func createManager(storeKit2Setting: StoreKit2Setting) -> ProductsManager {
let platformInfo = Purchases.PlatformInfo(flavor: "xyz", version: "123")
return ProductsManager(
systemInfo: try MockSystemInfo(
systemInfo: MockSystemInfo(
platformInfo: platformInfo,
finishTransactions: true,
storeKit2Setting: storeKit2Setting
Expand Down
20 changes: 10 additions & 10 deletions Tests/StoreKitUnitTests/PurchasesOrchestratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PurchasesOrchestratorTests: StoreKitConfigTestCase {
override func setUpWithError() throws {
try super.setUpWithError()

try self.setUpSystemInfo()
self.setUpSystemInfo()

self.productsManager = MockProductsManager(systemInfo: self.systemInfo,
requestTimeout: Configuration.storeKitRequestTimeoutDefault)
Expand Down Expand Up @@ -111,12 +111,12 @@ class PurchasesOrchestratorTests: StoreKitConfigTestCase {
fileprivate func setUpSystemInfo(
finishTransactions: Bool = true,
storeKit2Setting: StoreKit2Setting = .default
) throws {
) {
let platformInfo = Purchases.PlatformInfo(flavor: "xyz", version: "1.2.3")

self.systemInfo = try MockSystemInfo(platformInfo: platformInfo,
finishTransactions: finishTransactions,
storeKit2Setting: storeKit2Setting)
self.systemInfo = MockSystemInfo(platformInfo: platformInfo,
finishTransactions: finishTransactions,
storeKit2Setting: storeKit2Setting)
self.systemInfo.stubbedIsSandbox = true
}

Expand Down Expand Up @@ -742,7 +742,7 @@ class PurchasesOrchestratorTests: StoreKitConfigTestCase {
func testPurchaseSK2PackageRetriesReceiptFetchIfEnabled() async throws {
try AvailabilityChecks.iOS15APIAvailableOrSkipTest()

self.systemInfo = try .init(
self.systemInfo = .init(
platformInfo: nil,
finishTransactions: false,
storeKit2Setting: .enabledForCompatibleDevices,
Expand Down Expand Up @@ -959,7 +959,7 @@ class PurchasesOrchestratorTests: StoreKitConfigTestCase {
func testStoreKit2TransactionListenerDelegateWithObserverMode() async throws {
try AvailabilityChecks.iOS15APIAvailableOrSkipTest()

try self.setUpSystemInfo(finishTransactions: false, storeKit2Setting: .enabledForCompatibleDevices)
self.setUpSystemInfo(finishTransactions: false, storeKit2Setting: .enabledForCompatibleDevices)

self.setUpOrchestrator()
self.setUpStoreKit2Listener()
Expand Down Expand Up @@ -1030,7 +1030,7 @@ class PurchasesOrchestratorTests: StoreKitConfigTestCase {

let transactionListener = MockStoreKit2TransactionListener()

try self.setUpSystemInfo(storeKit2Setting: .disabled)
self.setUpSystemInfo(storeKit2Setting: .disabled)

self.setUpOrchestrator(storeKit2TransactionListener: transactionListener,
storeKit2StorefrontListener: StoreKit2StorefrontListener(delegate: nil))
Expand All @@ -1044,7 +1044,7 @@ class PurchasesOrchestratorTests: StoreKitConfigTestCase {

let transactionListener = MockStoreKit2TransactionListener()

try self.setUpSystemInfo(storeKit2Setting: .enabledOnlyForOptimizations)
self.setUpSystemInfo(storeKit2Setting: .enabledOnlyForOptimizations)

self.setUpOrchestrator(storeKit2TransactionListener: transactionListener,
storeKit2StorefrontListener: StoreKit2StorefrontListener(delegate: nil))
Expand All @@ -1058,7 +1058,7 @@ class PurchasesOrchestratorTests: StoreKitConfigTestCase {

let transactionListener = MockStoreKit2TransactionListener()

try self.setUpSystemInfo(storeKit2Setting: .enabledForCompatibleDevices)
self.setUpSystemInfo(storeKit2Setting: .enabledForCompatibleDevices)

self.setUpOrchestrator(storeKit2TransactionListener: transactionListener,
storeKit2StorefrontListener: StoreKit2StorefrontListener(delegate: nil))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class TrialOrIntroPriceEligibilityCheckerSK1Tests: StoreKitConfigTestCase {
override func setUpWithError() throws {
try super.setUpWithError()
let platformInfo = Purchases.PlatformInfo(flavor: "xyz", version: "123")
mockSystemInfo = try MockSystemInfo(platformInfo: platformInfo,
finishTransactions: true,
storeKit2Setting: .disabled)
self.mockSystemInfo = MockSystemInfo(platformInfo: platformInfo,
finishTransactions: true,
storeKit2Setting: .disabled)
receiptFetcher = MockReceiptFetcher(requestFetcher: MockRequestFetcher(), systemInfo: mockSystemInfo)
self.mockProductsManager = MockProductsManager(systemInfo: mockSystemInfo,
requestTimeout: Configuration.storeKitRequestTimeoutDefault)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class TrialOrIntroPriceEligibilityCheckerSK2Tests: StoreKitConfigTestCase {
override func setUpWithError() throws {
try super.setUpWithError()
let platformInfo = Purchases.PlatformInfo(flavor: "xyz", version: "123")
mockSystemInfo = try MockSystemInfo(platformInfo: platformInfo,
finishTransactions: true,
storeKit2Setting: .enabledForCompatibleDevices)
mockSystemInfo = MockSystemInfo(platformInfo: platformInfo,
finishTransactions: true,
storeKit2Setting: .enabledForCompatibleDevices)

receiptFetcher = MockReceiptFetcher(requestFetcher: MockRequestFetcher(), systemInfo: mockSystemInfo)
mockProductsManager = MockProductsManager(
Expand Down
6 changes: 3 additions & 3 deletions Tests/UnitTests/Attribution/AttributionPosterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class BaseAttributionPosterTests: TestCase {
var backend: MockBackend!
var subscriberAttributesManager: MockSubscriberAttributesManager!
var attributionFactory: AttributionTypeFactory! = MockAttributionTypeFactory()
// swiftlint:disable:next force_try
var systemInfo: MockSystemInfo! = try! MockSystemInfo(
var systemInfo: MockSystemInfo! = MockSystemInfo(
platformInfo: .init(flavor: "iOS", version: "3.2.1"),
finishTransactions: true)
finishTransactions: true
)

let userDefaultsSuiteName = "testUserDefaults"

Expand Down
28 changes: 14 additions & 14 deletions Tests/UnitTests/Misc/SandboxEnvironmentDetectorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@ import XCTest

class SandboxEnvironmentDetectorTests: TestCase {

func testIsSandbox() throws {
expect(try SystemInfo.with(receiptResult: .sandboxReceipt, inSimulator: false).isSandbox) == true
func testIsSandbox() {
expect(SystemInfo.with(receiptResult: .sandboxReceipt, inSimulator: false).isSandbox) == true
}

func testIsNotSandbox() throws {
expect(try SystemInfo.with(receiptResult: .receiptWithData, inSimulator: false).isSandbox) == false
func testIsNotSandbox() {
expect(SystemInfo.with(receiptResult: .receiptWithData, inSimulator: false).isSandbox) == false
}

func testIsNotSandboxIfNoReceiptURL() throws {
expect(try SystemInfo.with(receiptResult: .nilURL, inSimulator: false).isSandbox) == false
func testIsNotSandboxIfNoReceiptURL() {
expect(SystemInfo.with(receiptResult: .nilURL, inSimulator: false).isSandbox) == false
}

func testMacSandboxReceiptIsSandbox() throws {
expect(try SystemInfo.with(receiptResult: .macOSSandboxReceipt, inSimulator: false).isSandbox) == true
func testMacSandboxReceiptIsSandbox() {
expect(SystemInfo.with(receiptResult: .macOSSandboxReceipt, inSimulator: false).isSandbox) == true
}

func testMacAppStoreReceiptIsNotSandbox() throws {
expect(try SystemInfo.with(receiptResult: .macOSAppStoreReceipt, inSimulator: false).isSandbox) == false
func testMacAppStoreReceiptIsNotSandbox() {
expect(SystemInfo.with(receiptResult: .macOSAppStoreReceipt, inSimulator: false).isSandbox) == false
}

func testIsAlwaysSandboxIfRunningInSimulator() {
expect(try SystemInfo.with(receiptResult: .sandboxReceipt, inSimulator: true).isSandbox) == true
expect(try SystemInfo.with(receiptResult: .receiptWithData, inSimulator: true).isSandbox) == true
expect(try SystemInfo.with(receiptResult: .nilURL, inSimulator: true).isSandbox) == true
expect(SystemInfo.with(receiptResult: .sandboxReceipt, inSimulator: true).isSandbox) == true
expect(SystemInfo.with(receiptResult: .receiptWithData, inSimulator: true).isSandbox) == true
expect(SystemInfo.with(receiptResult: .nilURL, inSimulator: true).isSandbox) == true
}

}
Expand All @@ -51,7 +51,7 @@ private extension SandboxEnvironmentDetector {
static func with(
receiptResult result: MockBundle.ReceiptURLResult,
inSimulator: Bool
) throws -> SandboxEnvironmentDetector {
) -> SandboxEnvironmentDetector {
let bundle = MockBundle()
bundle.receiptURLResult = result

Expand Down
43 changes: 19 additions & 24 deletions Tests/UnitTests/Misc/SystemInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,45 @@ class SystemInfoTests: TestCase {
expect(SystemInfo.systemVersion) == ProcessInfo().operatingSystemVersionString
}

func testPlatformFlavor() throws {
func testPlatformFlavor() {
let flavor = "flavor"
let platformInfo = Purchases.PlatformInfo(flavor: flavor, version: "foo")
let systemInfo = try SystemInfo(platformInfo: platformInfo,
finishTransactions: false)
let systemInfo = SystemInfo(platformInfo: platformInfo, finishTransactions: false)
expect(systemInfo.platformFlavor) == flavor
}

func testPlatformFlavorVersion() throws {
func testPlatformFlavorVersion() {
let flavorVersion = "flavorVersion"
let platformInfo = Purchases.PlatformInfo(flavor: "foo", version: flavorVersion)
let systemInfo = try SystemInfo(platformInfo: platformInfo,
finishTransactions: false)
let systemInfo = SystemInfo(platformInfo: platformInfo, finishTransactions: false)
expect(systemInfo.platformFlavorVersion) == flavorVersion
}

func testFinishTransactions() throws {
func testFinishTransactions() {
var finishTransactions = false
var systemInfo = try SystemInfo(platformInfo: nil,
finishTransactions: finishTransactions)
var systemInfo = SystemInfo(platformInfo: nil, finishTransactions: finishTransactions)
expect(systemInfo.finishTransactions) == finishTransactions
expect(systemInfo.observerMode) == !finishTransactions

finishTransactions = true

systemInfo = try SystemInfo(platformInfo: nil,
finishTransactions: finishTransactions)
systemInfo = SystemInfo(platformInfo: nil, finishTransactions: finishTransactions)
expect(systemInfo.finishTransactions) == finishTransactions
expect(systemInfo.observerMode) == !finishTransactions
}

func testIsSandbox() throws {
func testIsSandbox() {
let sandboxDetector = MockSandboxEnvironmentDetector(isSandbox: true)

expect(try SystemInfo.withReceiptResult(.sandboxReceipt, sandboxDetector).isSandbox) == true
expect(try SystemInfo.withReceiptResult(.receiptWithData, sandboxDetector).isSandbox) == true
expect(SystemInfo.withReceiptResult(.sandboxReceipt, sandboxDetector).isSandbox) == true
expect(SystemInfo.withReceiptResult(.receiptWithData, sandboxDetector).isSandbox) == true
}

func testIsNotSandbox() throws {
func testIsNotSandbox() {
let sandboxDetector = MockSandboxEnvironmentDetector(isSandbox: false)

expect(try SystemInfo.withReceiptResult(.sandboxReceipt, sandboxDetector).isSandbox) == false
expect(try SystemInfo.withReceiptResult(.receiptWithData, sandboxDetector).isSandbox) == false
expect(SystemInfo.withReceiptResult(.sandboxReceipt, sandboxDetector).isSandbox) == false
expect(SystemInfo.withReceiptResult(.receiptWithData, sandboxDetector).isSandbox) == false
}

func testIsAppleSubscriptionURLWithAnotherURL() {
Expand Down Expand Up @@ -144,21 +140,20 @@ private extension SystemInfo {
static func withReceiptResult(
_ result: MockBundle.ReceiptURLResult,
_ sandboxEnvironmentDetector: SandboxEnvironmentDetector? = nil
) throws -> SystemInfo {
) -> SystemInfo {
let bundle = MockBundle()
bundle.receiptURLResult = result

let sandboxDetector = sandboxEnvironmentDetector ?? BundleSandboxEnvironmentDetector(bundle: bundle)

return try SystemInfo(platformInfo: nil,
finishTransactions: false,
bundle: bundle,
sandboxEnvironmentDetector: sandboxDetector)
return SystemInfo(platformInfo: nil,
finishTransactions: false,
bundle: bundle,
sandboxEnvironmentDetector: sandboxDetector)
}

static var `default`: SystemInfo {
// swiftlint:disable:next force_try
return try! .init(platformInfo: nil, finishTransactions: true)
return .init(platformInfo: nil, finishTransactions: true)
}

}
Loading

0 comments on commit 79aa55f

Please sign in to comment.