diff --git a/Sources/SwiftFormat/Utilities/FileIterator.swift b/Sources/SwiftFormat/Utilities/FileIterator.swift index 9d1a1d2c..1e128006 100644 --- a/Sources/SwiftFormat/Utilities/FileIterator.swift +++ b/Sources/SwiftFormat/Utilities/FileIterator.swift @@ -145,7 +145,7 @@ public struct FileIterator: Sequence, IteratorProtocol { // be displayed as relative paths. Otherwise, they will still be displayed as absolute // paths. let relativePath = - path.hasPrefix(workingDirectory.path) + path.hasPrefix(workingDirectory.path) && FileManager.default.currentDirectoryPath != "/" ? String(path.dropFirst(workingDirectory.path.count + 1)) : path output = diff --git a/Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift b/Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift index ba482063..a4ef883a 100644 --- a/Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift +++ b/Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift @@ -31,8 +31,8 @@ final class FileIteratorTests: XCTestCase { #endif let seen = allFilesSeen(iteratingOver: [tmpdir], followSymlinks: false) XCTAssertEqual(seen.count, 2) - XCTAssertTrue(seen.contains { $0.hasSuffix("project/real1.swift") }) - XCTAssertTrue(seen.contains { $0.hasSuffix("project/real2.swift") }) + XCTAssertTrue(seen.contains { $0.path.hasSuffix("project/real1.swift") }) + XCTAssertTrue(seen.contains { $0.path.hasSuffix("project/real2.swift") }) } func testFollowSymlinks() throws { @@ -41,10 +41,10 @@ final class FileIteratorTests: XCTestCase { #endif let seen = allFilesSeen(iteratingOver: [tmpdir], followSymlinks: true) XCTAssertEqual(seen.count, 3) - XCTAssertTrue(seen.contains { $0.hasSuffix("project/real1.swift") }) - XCTAssertTrue(seen.contains { $0.hasSuffix("project/real2.swift") }) + XCTAssertTrue(seen.contains { $0.path.hasSuffix("project/real1.swift") }) + XCTAssertTrue(seen.contains { $0.path.hasSuffix("project/real2.swift") }) // Hidden but found through the visible symlink project/link.swift - XCTAssertTrue(seen.contains { $0.hasSuffix("project/.hidden.swift") }) + XCTAssertTrue(seen.contains { $0.path.hasSuffix("project/.hidden.swift") }) } func testTraversesHiddenFilesIfExplicitlySpecified() throws { @@ -56,8 +56,8 @@ final class FileIteratorTests: XCTestCase { followSymlinks: false ) XCTAssertEqual(seen.count, 2) - XCTAssertTrue(seen.contains { $0.hasSuffix("project/.build/generated.swift") }) - XCTAssertTrue(seen.contains { $0.hasSuffix("project/.hidden.swift") }) + XCTAssertTrue(seen.contains { $0.path.hasSuffix("project/.build/generated.swift") }) + XCTAssertTrue(seen.contains { $0.path.hasSuffix("project/.hidden.swift") }) } func testDoesNotFollowSymlinksIfFollowSymlinksIsFalseEvenIfExplicitlySpecified() { @@ -71,6 +71,26 @@ final class FileIteratorTests: XCTestCase { ) XCTAssertTrue(seen.isEmpty) } + + func testDoesNotTrimFirstCharacterOfPathIfRunningInRoot() throws { + // Make sure that we don't drop the begining of the path if we are running in root. + // https://github.com/swiftlang/swift-format/issues/862 + FileManager.default.changeCurrentDirectoryPath("/") + let seen = allFilesSeen(iteratingOver: [tmpdir], followSymlinks: false) + XCTAssertEqual(seen.count, 2) + XCTAssertTrue(seen.contains { $0.path.hasPrefix("/private/var") }) + XCTAssertTrue(seen.contains { $0.path.hasPrefix("/private/var") }) + } + + func testShowsRelativePaths() throws { + // Make sure that we still show the relative path if using them. + // https://github.com/swiftlang/swift-format/issues/862 + FileManager.default.changeCurrentDirectoryPath(tmpdir.path) + let seen = allFilesSeen(iteratingOver: [URL(fileURLWithPath: ".")], followSymlinks: false) + XCTAssertEqual(seen.count, 2) + XCTAssertTrue(seen.contains { $0.relativePath == "project/real1.swift" }) + XCTAssertTrue(seen.contains { $0.relativePath == "project/real2.swift" }) + } } extension FileIteratorTests { @@ -111,11 +131,11 @@ extension FileIteratorTests { } /// 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] { + private func allFilesSeen(iteratingOver urls: [URL], followSymlinks: Bool) -> [URL] { let iterator = FileIterator(urls: urls, followSymlinks: followSymlinks) - var seen: [String] = [] + var seen: [URL] = [] for next in iterator { - seen.append(next.path) + seen.append(next) } return seen }