Cotton code
58 lines · 2.7 KB · SwiftRaw
import XCTest@testable import CottonKit/// Which container each bundle in the family writes to.////// It is worth a table rather than a sentence because there are several/// bundles across two systems and the share extension's whole contract is/// "the extension writes the mailbox where the app reads it". A derivation that/// answered a slightly different string for the extension than for its app/// would not fail anything — `UserDefaults(suiteName:)` happily hands back a/// suite for a container nobody else uses — it would just drain nothing for/// ever.////// Both families are driven on whichever system runs the test: the platform is/// an ARGUMENT to the derivation, not a `#if` inside it, exactly so this table/// can exist.final class AppGroupTests: XCTestCase {    /// **Does a team-prefixed group suite actually persist?**    ///    /// `FileManager.containerURL(forSecurityApplicationGroupIdentifier:)` hands    /// back a URL on macOS even for a group the process may not use, so the only    /// honest proof is a write that a SECOND suite object reads back. That is    /// what this does.    ///    /// It SKIPS rather than fails when the answer does not come back, and says    /// why: a `swift test` binary carries no entitlements at all, so it is not    /// evidence about the shipping app either way — the shipping evidence is    /// `codesign -d --entitlements :-` on the packaged bundle. A skip that    /// explains itself is the difference between "we checked and this process    /// cannot" and a green tick over nothing.    func testATeamPrefixedSuiteIsReadableBackFromThisProcessOrSaysWhyNot() throws {        #if !os(macOS)        throw XCTSkip("the team-prefixed group is macOS-only")        #else        let group = CottonDefaults.macAppGroup        guard let suite = UserDefaults(suiteName: group) else {            throw XCTSkip("this process cannot open the \(group) suite at all")        }        let key = "cotton.appgroup.probe.\(UUID().uuidString)"        suite.set("written", forKey: key)        defer { suite.removeObject(forKey: key) }        guard let second = UserDefaults(suiteName: group) else {            throw XCTSkip("a second suite object for \(group) could not be made")        }        guard let read = second.string(forKey: key) else {            throw XCTSkip(                "\(group) did not read back from this process — a `swift test` binary carries no "                    + "application-groups entitlement, so this says nothing about the packaged app; "                    + "verify that with `codesign -d --entitlements :- CottonDB.app`"            )        }        XCTAssertEqual(read, "written")        #endif    }}