103 lines
4.1 KiB
Swift
103 lines
4.1 KiB
Swift
// TemplateInstallerTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable import LyrebirdCLI
|
|
|
|
@Suite("TemplateInstaller")
|
|
struct TemplateInstallerTests {
|
|
|
|
// MARK: - Resource bundle
|
|
|
|
@Test func bundleResolvesXcodeTemplatesDir() throws {
|
|
// The LyrebirdCLI target ships its templates under Resources/XcodeTemplates.
|
|
// This test guards against a Package.swift regression where the .copy(...)
|
|
// entry gets removed or renamed.
|
|
let src: URL = try TemplateInstaller.sourceURL()
|
|
var isDir: ObjCBool = false
|
|
let exists: Bool = FileManager.default.fileExists(atPath: src.path, isDirectory: &isDir)
|
|
#expect(exists)
|
|
#expect(isDir.boolValue)
|
|
}
|
|
|
|
@Test func availableTemplatesIsNonEmpty() throws {
|
|
let names: [String] = try TemplateInstaller.availableTemplates()
|
|
#expect(!names.isEmpty)
|
|
#expect(names.allSatisfy { $0.hasSuffix(".xctemplate") })
|
|
}
|
|
|
|
@Test func availableTemplatesIncludesGenerator() throws {
|
|
let names: [String] = try TemplateInstaller.availableTemplates()
|
|
// LyrebirdGenerator is the canonical Tier 1 template. If it ever vanishes
|
|
// we want this test to fail loudly.
|
|
#expect(names.contains("LyrebirdGenerator.xctemplate"))
|
|
}
|
|
|
|
// MARK: - Destination URL
|
|
|
|
@Test func destinationURLIsInHomeLibrary() {
|
|
let dest: String = TemplateInstaller.destinationURL.path
|
|
let home: String = FileManager.default.homeDirectoryForCurrentUser.path
|
|
#expect(dest.hasPrefix(home + "/Library/Developer/Xcode/Templates/File Templates/"))
|
|
#expect(dest.hasSuffix("/Lyrebird"))
|
|
}
|
|
|
|
// MARK: - Install / uninstall round-trip
|
|
|
|
/// Uses a sandboxed temporary destination instead of the real user template
|
|
/// directory so the test never touches `~/Library`. We monkey-patch by
|
|
/// invoking the lower-level copy logic with explicit URLs.
|
|
@Test func roundTripInstallUninstallInSandbox() throws {
|
|
let fm: FileManager = FileManager.default
|
|
let sandbox: URL = try makeTempDir()
|
|
defer { try? fm.removeItem(at: sandbox) }
|
|
let dest: URL = sandbox.appendingPathComponent("Lyrebird", isDirectory: true)
|
|
|
|
// Replicate install logic in the sandbox.
|
|
let src: URL = try TemplateInstaller.sourceURL()
|
|
try fm.createDirectory(at: dest, withIntermediateDirectories: true)
|
|
let names: [String] = try TemplateInstaller.availableTemplates()
|
|
for name: String in names {
|
|
try fm.copyItem(at: src.appendingPathComponent(name),
|
|
to: dest.appendingPathComponent(name))
|
|
}
|
|
|
|
let copied: [String] = try fm.contentsOfDirectory(atPath: dest.path)
|
|
#expect(copied.sorted() == names.sorted())
|
|
|
|
// Spot-check a TemplateInfo.plist exists inside one of them.
|
|
let firstTemplatePlist: URL = dest
|
|
.appendingPathComponent(names[0])
|
|
.appendingPathComponent("TemplateInfo.plist")
|
|
#expect(fm.fileExists(atPath: firstTemplatePlist.path))
|
|
|
|
// Uninstall = remove dir.
|
|
try fm.removeItem(at: dest)
|
|
#expect(!fm.fileExists(atPath: dest.path))
|
|
}
|
|
|
|
@Test func installThrowsWhenDestinationExistsWithoutForce() throws {
|
|
// We cannot redirect TemplateInstaller's destination, but we can verify the
|
|
// error type the API documents by constructing one manually and round-tripping
|
|
// through Equatable.
|
|
let err1: TemplateInstaller.Error = .destinationExists(path: "/x")
|
|
let err2: TemplateInstaller.Error = .destinationExists(path: "/x")
|
|
let err3: TemplateInstaller.Error = .destinationExists(path: "/y")
|
|
#expect(err1 == err2)
|
|
#expect(err1 != err3)
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private func makeTempDir() throws -> URL {
|
|
let dir: URL = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent("lyrebird-template-installer-\(UUID().uuidString)", isDirectory: true)
|
|
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
|
return dir
|
|
}
|
|
}
|