Files
Lyrebird/Tests/LyrebirdTests/Utilities/LyrebirdPathsTests.swift
T

101 lines
4.0 KiB
Swift

// LyrebirdPathsTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
import Testing
@testable import LyrebirdSynth
@Suite("LyrebirdPaths")
struct LyrebirdPathsTests {
@Test("projectRoot finds the package directory containing Package.swift")
func projectRootFindsPackage() throws {
// This test file lives under the Lyrebird package, so the default
// Package.swift marker must resolve to a directory that contains it.
let root: URL = try #require(LyrebirdPaths.projectRoot())
let manifest: URL = root.appendingPathComponent("Package.swift")
#expect(FileManager.default.fileExists(atPath: manifest.path))
}
@Test("projectRoot returns nil when the marker is never found")
func projectRootMissingMarkerReturnsNil() {
let result: URL? = LyrebirdPaths.projectRoot(
marker: "definitely-not-a-real-marker-file.xyz"
)
#expect(result == nil)
}
@Test("projectRoot honours a custom marker located in the source path")
func projectRootCustomMarker() throws {
// A temp tree: <tmp>/proj/<MARKER>, with a nested source file.
let marker: String = "MyProject.xcodeproj"
let fm: FileManager = FileManager.default
let proj: URL = fm.temporaryDirectory
.appendingPathComponent("LyrebirdPathsTest-\(UUID().uuidString)")
let nestedDir: URL = proj.appendingPathComponent("Sources/App")
try fm.createDirectory(at: nestedDir, withIntermediateDirectories: true)
defer { try? fm.removeItem(at: proj) }
// Create the marker directory and a fake source file under the nesting.
try fm.createDirectory(
at: proj.appendingPathComponent(marker),
withIntermediateDirectories: true
)
let fakeSource: URL = nestedDir.appendingPathComponent("main.swift")
try Data().write(to: fakeSource)
let found: URL = try #require(
LyrebirdPaths.projectRoot(from: fakeSource.path, marker: marker)
)
// Resolve symlinks: macOS /tmp → /private/tmp.
#expect(found.resolvingSymlinksInPath().path
== proj.resolvingSymlinksInPath().path)
}
@Test("projectRoot matches a dot-prefixed marker by extension")
func projectRootExtensionMarker() throws {
// <tmp>/proj/MyApp.xcodeproj + a nested source file; marker ".xcodeproj"
// must match the differently-named bundle by extension.
let fm: FileManager = FileManager.default
let proj: URL = fm.temporaryDirectory
.appendingPathComponent("LyrebirdPathsExt-\(UUID().uuidString)")
let nestedDir: URL = proj.appendingPathComponent("Sources/App")
try fm.createDirectory(at: nestedDir, withIntermediateDirectories: true)
defer { try? fm.removeItem(at: proj) }
try fm.createDirectory(
at: proj.appendingPathComponent("MyApp.xcodeproj"),
withIntermediateDirectories: true
)
let fakeSource: URL = nestedDir.appendingPathComponent("main.swift")
try Data().write(to: fakeSource)
let found: URL = try #require(
LyrebirdPaths.projectRoot(from: fakeSource.path, marker: ".xcodeproj")
)
#expect(found.resolvingSymlinksInPath().path
== proj.resolvingSymlinksInPath().path)
}
@Test("standard sandbox directories resolve to non-empty paths")
func sandboxDirectoriesResolve() {
#expect(!LyrebirdPaths.documents.path.isEmpty)
#expect(!LyrebirdPaths.applicationSupport.path.isEmpty)
#expect(!LyrebirdPaths.caches.path.isEmpty)
#expect(!LyrebirdPaths.temporary.path.isEmpty)
#expect(!LyrebirdPaths.home.path.isEmpty)
#expect(!LyrebirdPaths.currentWorkingDirectory.path.isEmpty)
}
#if os(macOS)
@Test("desktop resolves on macOS")
func desktopResolves() throws {
let desktop: URL = try #require(LyrebirdPaths.desktop)
#expect(desktop.lastPathComponent == "Desktop")
}
#endif
}