Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle relative symbolic links #98

Merged
merged 10 commits into from
Jan 9, 2025
8 changes: 8 additions & 0 deletions Sources/FileSystem/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,14 @@ public struct FileSystem: FileSysteming, Sendable {
)
}

public func createSymbolicLink(from: AbsolutePath, to: RelativePath) async throws {
fortmarek marked this conversation as resolved.
Show resolved Hide resolved
logger?.debug("Creating symbolic link from \(from.pathString) to \(to.pathString).")
try await NIOFileSystem.FileSystem.shared.createSymbolicLink(
at: FilePath(from.pathString),
withDestination: FilePath(to.pathString)
)
fortmarek marked this conversation as resolved.
Show resolved Hide resolved
}

public func resolveSymbolicLink(_ symlinkPath: AbsolutePath) async throws -> AbsolutePath {
logger?.debug("Resolving symbolink link at path \(symlinkPath.pathString).")
if !(try await exists(symlinkPath)) {
Expand Down
14 changes: 5 additions & 9 deletions Sources/Glob/GlobSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,13 @@ public func search(
}

let path = baseURL.absoluteString.removingPercentEncoding ?? baseURL.absoluteString
let symbolicLinkDestination: URL?
if let symbolicLinkDestinationAbsoluteString = try? FileManager.default
.destinationOfSymbolicLink(atPath: path)
{
symbolicLinkDestination = URL(string: symbolicLinkDestinationAbsoluteString)
} else {
symbolicLinkDestination = nil
}
let symbolicLinkDestination: URL = URL(filePath: path).resolvingSymlinksInPath()
var isDirectory: ObjCBool = false

let symbolicLinkDestinationPath: String = symbolicLinkDestination.path().removingPercentEncoding ?? symbolicLinkDestination.path()

guard FileManager.default.fileExists(
atPath: symbolicLinkDestination?.absoluteString ?? path,
atPath: symbolicLinkDestinationPath,
isDirectory: &isDirectory
),
isDirectory.boolValue
Expand Down
34 changes: 34 additions & 0 deletions Tests/FileSystemTests/FileSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,40 @@ final class FileSystemTests: XCTestCase, @unchecked Sendable {
}
}

func test_glob_with_relative_symlink() async throws {
try await subject.runInTemporaryDirectory(prefix: "FileSystem") { temporaryDirectory in
// Given
let frameworkDir = temporaryDirectory.appending(component: "Framework")
let sourceDir = frameworkDir.appending(component: "Source")
let spmResourcesDir = sourceDir.appending(component: "SwiftPackageResources")
let modelSymLinkPath = spmResourcesDir.appending(component: "MyModel.xcdatamodeld")

let actualResourcesDir = frameworkDir.appending(component: "Resources")
let actualModelPath = actualResourcesDir.appending(component: "MyModel.xcdatamodeld")
let versionPath = actualModelPath.appending(component: "MyModel_0.xcdatamodel")

try await subject.makeDirectory(at: spmResourcesDir)
try await subject.makeDirectory(at: actualResourcesDir)
try await subject.makeDirectory(at: actualModelPath)
try await subject.touch(versionPath)

let relativeActualModelPath = try RelativePath(validating: "../../Resources/MyModel.xcdatamodeld")
try await subject.createSymbolicLink(from: modelSymLinkPath, to: relativeActualModelPath)

// When
let got = try await subject.glob(
directory: modelSymLinkPath,
include: ["*.xcdatamodel"]
)
.collect()
.sorted()

// Then
XCTAssertEqual(got.count, 1)
XCTAssertEqual(got.map(\.basename), [versionPath.basename])
}
}

func test_glob_with_double_directory_wildcard() async throws {
try await subject.runInTemporaryDirectory(prefix: "FileSystem") { temporaryDirectory in
// Given
Expand Down