Skip to content

Commit

Permalink
Merge pull request #837 from kkebo/relative-symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoppen authored Oct 18, 2024
2 parents 24fc4d6 + b9065b1 commit 3ca1a18
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
8 changes: 4 additions & 4 deletions Sources/SwiftFormat/Utilities/FileIterator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public struct FileIterator: Sequence, IteratorProtocol {
else {
break
}
next = URL(fileURLWithPath: destination)
next = URL(fileURLWithPath: destination, relativeTo: next)
fallthrough

case .typeDirectory:
Expand All @@ -97,12 +97,12 @@ public struct FileIterator: Sequence, IteratorProtocol {
output = next
}
}
if let out = output, visited.contains(out.absoluteURL.standardized.path) {
if let out = output, visited.contains(out.standardizedFileURL.path) {
output = nil
}
}
if let out = output {
visited.insert(out.absoluteURL.standardized.path)
visited.insert(out.standardizedFileURL.path)
}
return output
}
Expand Down Expand Up @@ -135,7 +135,7 @@ public struct FileIterator: Sequence, IteratorProtocol {
else {
break
}
path = destination
path = URL(fileURLWithPath: destination, relativeTo: item).path
fallthrough

case .typeRegular:
Expand Down
16 changes: 14 additions & 2 deletions Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final class FileIteratorTests: XCTestCase {
try touch("project/.hidden.swift")
try touch("project/.build/generated.swift")
try symlink("project/link.swift", to: "project/.hidden.swift")
try symlink("project/rellink.swift", relativeTo: ".hidden.swift")
}

override func tearDownWithError() throws {
Expand Down Expand Up @@ -64,7 +65,10 @@ final class FileIteratorTests: XCTestCase {
// passed to the iterator. This is meant to avoid situations where a symlink could be hidden by
// shell expansion; for example, if the user writes `swift-format --no-follow-symlinks *`, if
// the current directory contains a symlink, they would probably *not* expect it to be followed.
let seen = allFilesSeen(iteratingOver: [tmpURL("project/link.swift")], followSymlinks: false)
let seen = allFilesSeen(
iteratingOver: [tmpURL("project/link.swift"), tmpURL("project/rellink.swift")],
followSymlinks: false
)
XCTAssertTrue(seen.isEmpty)
}
}
Expand All @@ -90,14 +94,22 @@ extension FileIteratorTests {
}
}

/// Create a symlink between files or directories in the test's temporary space.
/// Create a absolute symlink between files or directories in the test's temporary space.
private func symlink(_ source: String, to target: String) throws {
try FileManager.default.createSymbolicLink(
at: tmpURL(source),
withDestinationURL: tmpURL(target)
)
}

/// Create a relative symlink between files or directories in the test's temporary space.
private func symlink(_ source: String, relativeTo target: String) throws {
try FileManager.default.createSymbolicLink(
atPath: tmpURL(source).path,
withDestinationPath: target
)
}

/// Computes the list of all files seen by using `FileIterator` to iterate over the given URLs.
private func allFilesSeen(iteratingOver urls: [URL], followSymlinks: Bool) -> [String] {
let iterator = FileIterator(urls: urls, followSymlinks: followSymlinks)
Expand Down

0 comments on commit 3ca1a18

Please sign in to comment.