Skip to content

[6.1] URL.appendingPathExtension("") appends a trailing dot #1091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/String/String+Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ extension String {
}

internal func appendingPathExtension(_ pathExtension: String) -> String {
guard validatePathExtension(pathExtension) else {
guard !pathExtension.isEmpty, validatePathExtension(pathExtension) else {
return self
}
var result = self._droppingTrailingSlashes
Expand Down
2 changes: 1 addition & 1 deletion Sources/FoundationEssentials/URL/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,7 @@ public struct URL: Equatable, Sendable, Hashable {
return result
}
#endif
guard !relativePath().isEmpty else { return self }
guard !pathExtension.isEmpty, !relativePath().isEmpty else { return self }
var components = URLComponents(parseInfo: _parseInfo)
// pathExtension might need to be percent-encoded, so use .path
let newPath = components.path.appendingPathExtension(pathExtension)
Expand Down
9 changes: 9 additions & 0 deletions Tests/FoundationEssentialsTests/URLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,15 @@ final class URLTests : XCTestCase {
url.deletePathExtension()
// Old behavior only searches the last empty component, so the extension isn't actually removed
checkBehavior(url.path(), new: "/path/", old: "/path.foo///")

url = URL(filePath: "/tmp/x")
url.appendPathExtension("")
XCTAssertEqual(url.path(), "/tmp/x")
XCTAssertEqual(url, url.deletingPathExtension().appendingPathExtension(url.pathExtension))

url = URL(filePath: "/tmp/x.")
url.deletePathExtension()
XCTAssertEqual(url.path(), "/tmp/x.")
}

func testURLAppendingToEmptyPath() throws {
Expand Down