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

Fix windows creation of relative symlinks to directories #931

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ extension _FileManagerImpl {
) throws {
#if os(Windows)
var bIsDirectory = false
_ = fileManager.fileExists(atPath: destPath, isDirectory: &bIsDirectory)
let absoluteDestPath = URL(filePath: destPath, relativeTo: URL(filePath: path, directoryHint: .notDirectory)).path
jmschonfeld marked this conversation as resolved.
Show resolved Hide resolved
_ = fileManager.fileExists(atPath: absoluteDestPath, isDirectory: &bIsDirectory)

try path.withNTPathRepresentation { lpSymlinkFileName in
try destPath.withFileSystemRepresentation {
Expand Down
19 changes: 19 additions & 0 deletions Tests/FoundationEssentialsTests/FileManager/FileManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,13 @@ final class FileManagerTests : XCTestCase {
func testCreateSymbolicLinkAtPath() throws {
try FileManagerPlayground {
"foo"
Directory("dir") {}
}.test {
try $0.createSymbolicLink(atPath: "bar", withDestinationPath: "foo")
XCTAssertEqual(try $0.destinationOfSymbolicLink(atPath: "bar"), "foo")

try $0.createSymbolicLink(atPath: "dir_link", withDestinationPath: "dir")
XCTAssertEqual(try $0.destinationOfSymbolicLink(atPath: "dir_link"), "dir")

XCTAssertThrowsError(try $0.createSymbolicLink(atPath: "bar", withDestinationPath: "foo")) {
XCTAssertEqual(($0 as? CocoaError)?.code, .fileWriteFileExists)
Expand All @@ -426,6 +430,21 @@ final class FileManagerTests : XCTestCase {
XCTAssertEqual(($0 as? CocoaError)?.code, .fileReadUnknown)
}
}

try FileManagerPlayground {
Directory("dir") {
Directory("other_dir") {
"file"
}
}
}.test {
// Create a relative symlink to other_dir from within dir (tests windows special dir symlink handling)
try $0.createSymbolicLink(atPath: "dir/link", withDestinationPath: "other_dir")

// Ensure it is created successfully
XCTAssertEqual(try $0.destinationOfSymbolicLink(atPath: "dir/link"), "other_dir")
XCTAssertEqual(try $0.contentsOfDirectory(atPath: "dir/link"), ["file"])
}
}

func testMoveItemAtPathToPath() throws {
Expand Down