Cotton code
64 lines · 2.6 KB · SwiftRaw
import Foundationimport XCTest@testable import CottonKit/// The store root is excluded from iCloud backup, and the call really sets the/// flag.////// `isExcludedFromBackup` is an extended attribute on a real directory, so this/// runs against one: a temp directory is created, excluded, and the value is/// read back from the file system rather than from the `URL` that wrote it./// `StoreBackupSweepTests` (scripts/__tests__/swift-sweep-media-sweeps.test.ts) is the other half — that the app/// actually makes this call, at every open.final class StoreBackupTests: XCTestCase {    private func temporaryRoot() throws -> URL {        let root = URL(fileURLWithPath: NSTemporaryDirectory())            .appendingPathComponent("cotton-store-backup-\(UUID().uuidString)")        try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)        addTeardownBlock { try? FileManager.default.removeItem(at: root) }        return root    }    func testADirectoryIsExcludedAndReadsBackAsExcluded() throws {        let root = try temporaryRoot()        var before = root        before.removeAllCachedResourceValues()        XCTAssertNotEqual(            try before.resourceValues(forKeys: [.isExcludedFromBackupKey]).isExcludedFromBackup,            true,            "a fresh directory already reads as excluded — the assertion below would prove nothing"        )        XCTAssertTrue(StoreBackup.exclude(root))        var after = root        after.removeAllCachedResourceValues()        XCTAssertEqual(            try after.resourceValues(forKeys: [.isExcludedFromBackupKey]).isExcludedFromBackup,            true        )    }    /// RE-ASSERTED, not "set once": calling it again on a directory that is    /// already excluded is fine and still answers true.    func testAssertingTwiceIsTheSameAsOnce() throws {        let root = try temporaryRoot()        XCTAssertTrue(StoreBackup.exclude(root))        XCTAssertTrue(StoreBackup.exclude(root))    }    /// A LOUD failure, not a silent one. A path that does not exist cannot    /// carry the attribute; the call answers false and writes a line rather    /// than returning as if it had worked.    func testAMissingRootAnswersFalseAndSaysSo() throws {        let missing = URL(fileURLWithPath: NSTemporaryDirectory())            .appendingPathComponent("cotton-store-backup-absent-\(UUID().uuidString)")        let before = Log.ring.lines().count        XCTAssertFalse(StoreBackup.exclude(missing))        XCTAssertGreaterThan(            Log.ring.lines().count, before,            "the failure was swallowed — nothing reached the log"        )    }}