|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#if canImport(FoundationInternationalization) |
| 14 | +@testable import FoundationEssentials |
| 15 | +@testable import FoundationInternationalization |
| 16 | +#else |
| 17 | +@testable import Foundation |
| 18 | +#endif |
| 19 | + |
| 20 | +// This actor is private and not exposed to tests to prevent accidentally writing tests annotated with @CurrentInternationalizationPreferencesActor which may have suspension points |
| 21 | +// Using the global helper function below ensures that only synchronous work with no suspension points is queued |
| 22 | +@globalActor |
| 23 | +private actor CurrentInternationalizationPreferencesActor: GlobalActor { |
| 24 | + static let shared = CurrentInternationalizationPreferencesActor() |
| 25 | + |
| 26 | + private init() {} |
| 27 | + |
| 28 | + @CurrentInternationalizationPreferencesActor |
| 29 | + static func usingCurrentInternationalizationPreferences( |
| 30 | + body: () throws -> Void // Must be synchronous to prevent suspension points within body which could introduce a change in the preferences |
| 31 | + ) rethrows { |
| 32 | + try body() |
| 33 | + |
| 34 | + // Reset everything after the test runs to ensure custom values don't persist |
| 35 | + LocaleCache.cache.reset() |
| 36 | + CalendarCache.cache.reset() |
| 37 | + _ = TimeZoneCache.cache.reset() |
| 38 | + _ = TimeZone.resetSystemTimeZone() |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +internal func usingCurrentInternationalizationPreferences(_ body: sending () throws -> Void) async rethrows { |
| 43 | + try await CurrentInternationalizationPreferencesActor.usingCurrentInternationalizationPreferences(body: body) |
| 44 | +} |
0 commit comments