Skip to content

[6.0.x] Fix windows creation of relative symlinks to directories #940

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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
_ = fileManager.fileExists(atPath: absoluteDestPath, isDirectory: &bIsDirectory)

try path.withNTPathRepresentation { lpSymlinkFileName in
try destPath.withFileSystemRepresentation {
Expand Down
39 changes: 39 additions & 0 deletions Tests/FoundationEssentialsTests/FileManager/FileManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,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 @@ -409,6 +413,41 @@ 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"])

do {
// Second symlink creation with an absolute path
let absolute = URL(filePath: "dir/link2", relativeTo: URL(filePath: $0.currentDirectoryPath, directoryHint: .isDirectory)).path
try $0.createSymbolicLink(atPath: absolute, withDestinationPath: "other_dir")

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

do {
// And lastly a symlink to an absolute path
let absolute = URL(filePath: "dir/other_dir", relativeTo: URL(filePath: $0.currentDirectoryPath, directoryHint: .isDirectory)).path
try $0.createSymbolicLink(atPath: "dir/link3", withDestinationPath: absolute)

// Ensure it is created successfully
XCTAssertEqual(try $0.destinationOfSymbolicLink(atPath: "dir/link3"), absolute.withFileSystemRepresentation { String(cString: $0!) })
XCTAssertEqual(try $0.contentsOfDirectory(atPath: "dir/link3"), ["file"])
}
}
}

func testMoveItemAtPathToPath() throws {
Expand Down