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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will need to add the method to the FileSysteming protocol.

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)
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about creating a private helper method to combine the two createSymbolicLink methods?

    private func createSymbolicLink(fromPathString: String, toPathString: String) async throws {
        logger?.debug("Creating symbolic link from \(fromPathString) to \(toPathString).")
        try await NIOFileSystem.FileSystem.shared.createSymbolicLink(
            at: FilePath(fromPathString),
            withDestination: FilePath(toPathString)
        )
    }

    public func createSymbolicLink(from: AbsolutePath, to: AbsolutePath) async throws {
        try await createSymbolicLink(fromPathString: from.pathString, toPathString: to.pathString)
    }

}

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