From 1e7278025f2083fd2c2bca3341540e090951633f Mon Sep 17 00:00:00 2001 From: Kenta Kubo <601636+kkebo@users.noreply.github.com> Date: Sun, 6 Oct 2024 00:30:59 +0900 Subject: [PATCH 1/5] Add a relative symlink to FileIteratorTests --- .../Utilities/FileIteratorTests.swift | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift b/Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift index 965c90fe3..691e568c6 100644 --- a/Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift +++ b/Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift @@ -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 { @@ -55,7 +56,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) } } @@ -81,7 +85,7 @@ 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), @@ -89,6 +93,14 @@ extension FileIteratorTests { ) } + /// 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) From f824817fc015c15e6e3762391bf9d82e5cd1e572 Mon Sep 17 00:00:00 2001 From: Kenta Kubo <601636+kkebo@users.noreply.github.com> Date: Sun, 6 Oct 2024 00:32:57 +0900 Subject: [PATCH 2/5] Fix relative symlinks when using `--follow-symlinks` --- Sources/SwiftFormat/Utilities/FileIterator.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftFormat/Utilities/FileIterator.swift b/Sources/SwiftFormat/Utilities/FileIterator.swift index bd3a2c0f0..b833bc6f4 100644 --- a/Sources/SwiftFormat/Utilities/FileIterator.swift +++ b/Sources/SwiftFormat/Utilities/FileIterator.swift @@ -79,7 +79,7 @@ public struct FileIterator: Sequence, IteratorProtocol { else { break } - next = URL(fileURLWithPath: destination) + next = URL(fileURLWithPath: destination, relativeTo: next) fallthrough case .typeDirectory: @@ -135,7 +135,7 @@ public struct FileIterator: Sequence, IteratorProtocol { else { break } - path = destination + path = URL(fileURLWithPath: destination, isDirectory: false, relativeTo: item).path fallthrough case .typeRegular: From ef73806f30f24736156221665543e73bff8afff8 Mon Sep 17 00:00:00 2001 From: Kenta Kubo <601636+kkebo@users.noreply.github.com> Date: Thu, 17 Oct 2024 02:29:28 +0900 Subject: [PATCH 3/5] Fix test by deduplicating files in `FileIterator.next()` --- Sources/SwiftFormat/Utilities/FileIterator.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftFormat/Utilities/FileIterator.swift b/Sources/SwiftFormat/Utilities/FileIterator.swift index b833bc6f4..0ce11354d 100644 --- a/Sources/SwiftFormat/Utilities/FileIterator.swift +++ b/Sources/SwiftFormat/Utilities/FileIterator.swift @@ -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.absoluteURL.standardized.standardizedFileURL.path) { output = nil } } if let out = output { - visited.insert(out.absoluteURL.standardized.path) + visited.insert(out.absoluteURL.standardized.standardizedFileURL.path) } return output } From 5f90fb3d8646b74946bd9917c72d49d8acdeba16 Mon Sep 17 00:00:00 2001 From: Kenta Kubo <601636+kkebo@users.noreply.github.com> Date: Thu, 17 Oct 2024 04:25:38 +0900 Subject: [PATCH 4/5] Remove `isDirectory: false` so that `URL` can infer it --- Sources/SwiftFormat/Utilities/FileIterator.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftFormat/Utilities/FileIterator.swift b/Sources/SwiftFormat/Utilities/FileIterator.swift index 0ce11354d..8189f18cf 100644 --- a/Sources/SwiftFormat/Utilities/FileIterator.swift +++ b/Sources/SwiftFormat/Utilities/FileIterator.swift @@ -135,7 +135,7 @@ public struct FileIterator: Sequence, IteratorProtocol { else { break } - path = URL(fileURLWithPath: destination, isDirectory: false, relativeTo: item).path + path = URL(fileURLWithPath: destination, relativeTo: item).path fallthrough case .typeRegular: From b9065b18d8435618635e0f619e62a97e7dee55fb Mon Sep 17 00:00:00 2001 From: Kenta Kubo <601636+kkebo@users.noreply.github.com> Date: Fri, 18 Oct 2024 03:18:39 +0900 Subject: [PATCH 5/5] Simplify the code by replacing `.abosoluteURL.standardized` with `.standardizedFileURL` --- Sources/SwiftFormat/Utilities/FileIterator.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftFormat/Utilities/FileIterator.swift b/Sources/SwiftFormat/Utilities/FileIterator.swift index 8189f18cf..9d1a1d2cf 100644 --- a/Sources/SwiftFormat/Utilities/FileIterator.swift +++ b/Sources/SwiftFormat/Utilities/FileIterator.swift @@ -97,12 +97,12 @@ public struct FileIterator: Sequence, IteratorProtocol { output = next } } - if let out = output, visited.contains(out.absoluteURL.standardized.standardizedFileURL.path) { + if let out = output, visited.contains(out.standardizedFileURL.path) { output = nil } } if let out = output { - visited.insert(out.absoluteURL.standardized.standardizedFileURL.path) + visited.insert(out.standardizedFileURL.path) } return output }