Skip to content

Commit 8a95c62

Browse files
authored
(141294361) URL.appendingPathExtension("") appends a trailing dot (#1082)
1 parent 52ee324 commit 8a95c62

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

Sources/FoundationEssentials/String/String+Path.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ extension String {
207207
}
208208

209209
internal func appendingPathExtension(_ pathExtension: String) -> String {
210-
guard validatePathExtension(pathExtension) else {
210+
guard !pathExtension.isEmpty, validatePathExtension(pathExtension) else {
211211
return self
212212
}
213213
var result = self._droppingTrailingSlashes

Sources/FoundationEssentials/URL/URL.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1754,7 +1754,7 @@ public struct URL: Equatable, Sendable, Hashable {
17541754
return result
17551755
}
17561756
#endif
1757-
guard !relativePath().isEmpty else { return self }
1757+
guard !pathExtension.isEmpty, !relativePath().isEmpty else { return self }
17581758
var components = URLComponents(parseInfo: _parseInfo)
17591759
// pathExtension might need to be percent-encoded, so use .path
17601760
let newPath = components.path.appendingPathExtension(pathExtension)

Tests/FoundationEssentialsTests/URLTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,15 @@ final class URLTests : XCTestCase {
893893
url.deletePathExtension()
894894
// Old behavior only searches the last empty component, so the extension isn't actually removed
895895
checkBehavior(url.path(), new: "/path/", old: "/path.foo///")
896+
897+
url = URL(filePath: "/tmp/x")
898+
url.appendPathExtension("")
899+
XCTAssertEqual(url.path(), "/tmp/x")
900+
XCTAssertEqual(url, url.deletingPathExtension().appendingPathExtension(url.pathExtension))
901+
902+
url = URL(filePath: "/tmp/x.")
903+
url.deletePathExtension()
904+
XCTAssertEqual(url.path(), "/tmp/x.")
896905
}
897906

898907
func testURLAppendingToEmptyPath() throws {

0 commit comments

Comments
 (0)