Skip to content

Commit 3ca1a18

Browse files
authored
Merge pull request #837 from kkebo/relative-symlinks
2 parents 24fc4d6 + b9065b1 commit 3ca1a18

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

Sources/SwiftFormat/Utilities/FileIterator.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public struct FileIterator: Sequence, IteratorProtocol {
7979
else {
8080
break
8181
}
82-
next = URL(fileURLWithPath: destination)
82+
next = URL(fileURLWithPath: destination, relativeTo: next)
8383
fallthrough
8484

8585
case .typeDirectory:
@@ -97,12 +97,12 @@ public struct FileIterator: Sequence, IteratorProtocol {
9797
output = next
9898
}
9999
}
100-
if let out = output, visited.contains(out.absoluteURL.standardized.path) {
100+
if let out = output, visited.contains(out.standardizedFileURL.path) {
101101
output = nil
102102
}
103103
}
104104
if let out = output {
105-
visited.insert(out.absoluteURL.standardized.path)
105+
visited.insert(out.standardizedFileURL.path)
106106
}
107107
return output
108108
}
@@ -135,7 +135,7 @@ public struct FileIterator: Sequence, IteratorProtocol {
135135
else {
136136
break
137137
}
138-
path = destination
138+
path = URL(fileURLWithPath: destination, relativeTo: item).path
139139
fallthrough
140140

141141
case .typeRegular:

Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ final class FileIteratorTests: XCTestCase {
1818
try touch("project/.hidden.swift")
1919
try touch("project/.build/generated.swift")
2020
try symlink("project/link.swift", to: "project/.hidden.swift")
21+
try symlink("project/rellink.swift", relativeTo: ".hidden.swift")
2122
}
2223

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

93-
/// Create a symlink between files or directories in the test's temporary space.
97+
/// Create a absolute symlink between files or directories in the test's temporary space.
9498
private func symlink(_ source: String, to target: String) throws {
9599
try FileManager.default.createSymbolicLink(
96100
at: tmpURL(source),
97101
withDestinationURL: tmpURL(target)
98102
)
99103
}
100104

105+
/// Create a relative symlink between files or directories in the test's temporary space.
106+
private func symlink(_ source: String, relativeTo target: String) throws {
107+
try FileManager.default.createSymbolicLink(
108+
atPath: tmpURL(source).path,
109+
withDestinationPath: target
110+
)
111+
}
112+
101113
/// Computes the list of all files seen by using `FileIterator` to iterate over the given URLs.
102114
private func allFilesSeen(iteratingOver urls: [URL], followSymlinks: Bool) -> [String] {
103115
let iterator = FileIterator(urls: urls, followSymlinks: followSymlinks)

0 commit comments

Comments
 (0)