示例工程 56-ios-signing-distribution

56-ios-signing-distribution/Tests/Ch56KitTests/SigningTests.swift

swift · 4.5 KB

import Foundation
import SwiftUI
import Testing
 
@testable import Ch56Kit
 
@Suite("描述文件")
struct ProfileTests {
    private let now = Date(timeIntervalSince1970: 1_800_000_000)
 
    @Test func validProfileHasNoProblems() {
        let profile = ProvisioningProfile(
            name: "App Store",
            teamID: "TEAM123456",
            bundleID: "dev.xwrt.app",
            expiration: now.addingTimeInterval(86_400),
            deviceCount: 0
        )
        #expect(profile.problems(now: now).isEmpty)
    }
 
    @Test func expiredProfileIsReported() {
        let profile = ProvisioningProfile(
            name: "旧",
            teamID: "TEAM",
            bundleID: "dev.xwrt.app",
            expiration: now.addingTimeInterval(-1),
            deviceCount: 0
        )
        #expect(profile.problems(now: now).count == 1)
    }
 
    @Test func tooManyDevicesIsReported() {
        let profile = ProvisioningProfile(
            name: "Ad Hoc",
            teamID: "TEAM",
            bundleID: "dev.xwrt.app",
            expiration: now.addingTimeInterval(86_400),
            deviceCount: 101
        )
        #expect(profile.problems(now: now).count == 1)
    }
 
    @Test func badBundleIDIsReported() {
        let profile = ProvisioningProfile(
            name: "x",
            teamID: "TEAM",
            bundleID: "nope",
            expiration: now.addingTimeInterval(86_400),
            deviceCount: 0
        )
        #expect(profile.problems(now: now).count == 1)
    }
}
 
@Suite("导出选项")
struct ExportOptionsTests {
    @Test func plistContainsMethodAndProfiles() {
        let options = ExportOptions(
            method: .adHoc,
            teamID: "TEAM",
            provisioningProfiles: ["dev.xwrt.app": "Ad Hoc"]
        )
        #expect(options.plist["method"] as? String == "ad-hoc")
        #expect(options.plist["teamID"] as? String == "TEAM")
        #expect(options.isValid)
    }
 
    @Test func emptyProfilesAreInvalid() {
        let options = ExportOptions(method: .appStore, teamID: "TEAM", provisioningProfiles: [:])
        #expect(!options.isValid)
    }
 
    @Test func plistRoundTrip() throws {
        let options = ExportOptions(
            method: .development,
            teamID: "TEAM",
            provisioningProfiles: ["dev.xwrt.app": "Dev"]
        )
        let data = try options.plistData()
        let parsed = try PropertyListSerialization.propertyList(from: data, format: nil)
        let dictionary = try #require(parsed as? [String: Any])
        #expect(dictionary["signingStyle"] as? String == "manual")
        #expect(dictionary["stripSwiftSymbols"] as? Bool == true)
    }
}
 
@Suite("命令")
struct ArchivePlanTests {
    @Test func archiveEndsWithArchive() {
        let arguments = ArchivePlan.archiveArguments(
            project: "App.xcodeproj",
            scheme: "App",
            configuration: "Release",
            archivePath: "build/App.xcarchive"
        )
        #expect(arguments.last == "archive")
        #expect(arguments.contains("generic/platform=iOS"))
    }
 
    @Test func exportUsesOptionsPlist() {
        let arguments = ArchivePlan.exportArguments(
            archivePath: "a.xcarchive",
            exportPath: "export",
            optionsPlist: "ExportOptions.plist"
        )
        #expect(arguments.first == "-exportArchive")
        #expect(arguments.contains("-exportOptionsPlist"))
    }
}
 
@Suite("视图")
struct SigningViewTests {
    @Test @MainActor func boardRendersAtFixedSize() throws {
        let image = try #require(ImageRenderer(content: SigningBoard()).cgImage)
        #expect(image.width == 360)
        #expect(image.height > 120)
    }
}
 
@Suite("截图")
struct ScreenshotTests {
    @Test @MainActor func shotMatchesExpectedWidth() throws {
        let renderer = ImageRenderer(content: SigningBoard())
        renderer.scale = 2
        let image = try #require(renderer.cgImage)
        #expect(image.width == 720)
 
        let url = URL(fileURLWithPath: #filePath)
            .deletingLastPathComponent()
            .deletingLastPathComponent()
            .deletingLastPathComponent()
            .appendingPathComponent("shots/ios-signing-board.png")
        guard !FileManager.default.fileExists(atPath: url.path) else { return }
        let data = try #require(
            NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:])
        )
        try FileManager.default.createDirectory(
            at: url.deletingLastPathComponent(),
            withIntermediateDirectories: true
        )
        try data.write(to: url)
    }
}