Skip to content

Commit ce098f7

Browse files
committed
(133882014) URL(filePath: path, directoryHint: .notDirectory) should strip trailing slashes
1 parent 7242610 commit ce098f7

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

Sources/FoundationEssentials/URL/URL.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,7 @@ extension URL {
21402140
case .isDirectory:
21412141
isDirectory = true
21422142
case .notDirectory:
2143+
filePath = filePath._droppingTrailingSlashes
21432144
isDirectory = false
21442145
case .checkFileSystem:
21452146
#if !NO_FILESYSTEM
@@ -2156,7 +2157,7 @@ extension URL {
21562157
filePath = filePath.replacing(UInt8(ascii: "\\"), with: UInt8(ascii: "/"))
21572158
#endif
21582159

2159-
if !filePath.isEmpty && filePath.utf8.last != UInt8(ascii: "/") && isDirectory {
2160+
if isDirectory && !filePath.isEmpty && filePath.utf8.last != UInt8(ascii: "/") {
21602161
filePath += "/"
21612162
}
21622163
var components = URLComponents()

Tests/FoundationEssentialsTests/URLTests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,42 @@ final class URLTests : XCTestCase {
586586
XCTAssertEqual(url.fileSystemPath, "/path/slashes")
587587
}
588588

589+
func testURLNotDirectoryHintStripsTrailingSlash() throws {
590+
// Supply a path with a trailing slash but say it's not a direcotry
591+
var url = URL(filePath: "/path/", directoryHint: .notDirectory)
592+
XCTAssertFalse(url.hasDirectoryPath)
593+
XCTAssertEqual(url.path(), "/path")
594+
595+
url = URL(fileURLWithPath: "/path/", isDirectory: false)
596+
XCTAssertFalse(url.hasDirectoryPath)
597+
XCTAssertEqual(url.path(), "/path")
598+
599+
url = URL(filePath: "/path///", directoryHint: .notDirectory)
600+
XCTAssertFalse(url.hasDirectoryPath)
601+
XCTAssertEqual(url.path(), "/path")
602+
603+
url = URL(fileURLWithPath: "/path///", isDirectory: false)
604+
XCTAssertFalse(url.hasDirectoryPath)
605+
XCTAssertEqual(url.path(), "/path")
606+
607+
// With .checkFileSystem, don't modify the path for a non-existent file
608+
url = URL(filePath: "/my/non/existent/path/", directoryHint: .checkFileSystem)
609+
XCTAssertTrue(url.hasDirectoryPath)
610+
XCTAssertEqual(url.path(), "/my/non/existent/path/")
611+
612+
url = URL(fileURLWithPath: "/my/non/existent/path/")
613+
XCTAssertTrue(url.hasDirectoryPath)
614+
XCTAssertEqual(url.path(), "/my/non/existent/path/")
615+
616+
url = URL(filePath: "/my/non/existent/path", directoryHint: .checkFileSystem)
617+
XCTAssertFalse(url.hasDirectoryPath)
618+
XCTAssertEqual(url.path(), "/my/non/existent/path")
619+
620+
url = URL(fileURLWithPath: "/my/non/existent/path")
621+
XCTAssertFalse(url.hasDirectoryPath)
622+
XCTAssertEqual(url.path(), "/my/non/existent/path")
623+
}
624+
589625
func testURLComponentsPercentEncodedUnencodedProperties() throws {
590626
var comp = URLComponents()
591627

0 commit comments

Comments
 (0)