Skip to content

Commit

Permalink
Merge pull request #849 from ahoppen/6.0/infinite-searching
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoppen authored Oct 12, 2024
2 parents 58f07e3 + 5a1348b commit 55024a4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
16 changes: 15 additions & 1 deletion Sources/SwiftFormat/API/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public struct Configuration: Codable, Equatable {
if FileManager.default.isReadableFile(atPath: candidateFile.path) {
return candidateFile
}
} while candidateDirectory.path != "/"
} while !candidateDirectory.isRoot

return nil
}
Expand Down Expand Up @@ -370,3 +370,17 @@ public struct NoAssignmentInExpressionsConfiguration: Codable, Equatable {

public init() {}
}

fileprivate extension URL {
var isRoot: Bool {
#if os(Windows)
// FIXME: We should call into Windows' native check to check if this path is a root once https://github.com/swiftlang/swift-foundation/issues/976 is fixed.
// https://github.com/swiftlang/swift-format/issues/844
return self.pathComponents.count == 1
#else
// On Linux, we may end up with an string for the path due to https://github.com/swiftlang/swift-foundation/issues/980
// TODO: Remove the check for "" once https://github.com/swiftlang/swift-foundation/issues/980 is fixed.
return self.path == "/" || self.path == ""
#endif
}
}
26 changes: 26 additions & 0 deletions Tests/SwiftFormatTests/API/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,30 @@ final class ConfigurationTests: XCTestCase {

XCTAssertEqual(defaultInitConfig, emptyJSONConfig)
}

func testMissingConfigurationFile() {
#if os(Windows)
let path = #"C:\test.swift"#
#else
let path = "/test.swift"
#endif
XCTAssertNil(Configuration.url(forConfigurationFileApplyingTo: URL(fileURLWithPath: path)))
}

func testMissingConfigurationFileInSubdirectory() {
#if os(Windows)
let path = #"C:\whatever\test.swift"#
#else
let path = "/whatever/test.swift"
#endif
XCTAssertNil(Configuration.url(forConfigurationFileApplyingTo: URL(fileURLWithPath: path)))
}

func testMissingConfigurationFileMountedDirectory() throws {
#if !os(Windows)
try XCTSkipIf(true, #"\\ file mounts are only a concept on Windows"#)
#endif
let path = #"\\mount\test.swift"#
XCTAssertNil(Configuration.url(forConfigurationFileApplyingTo: URL(fileURLWithPath: path)))
}
}

0 comments on commit 55024a4

Please sign in to comment.