Skip to content

Commit

Permalink
Path(_ url:) -> Path(url:)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Feb 12, 2019
1 parent ed4b773 commit c9d300a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -281,7 +281,7 @@ Therefore, if you are not using this feature you are fine. If you have URLs the
way to get a `Path` is:

```swift
if let path = Path(url) {
if let path = Path(url: url) {
/**/
}
```
Expand Down
15 changes: 12 additions & 3 deletions Sources/Path.swift
Expand Up @@ -104,13 +104,22 @@ public struct Path: Equatable, Hashable, Comparable {
self.string = join_(prefix: "/", pathComponents: pathComponents)
}

public init?(_ url: URL) {
/**
Creates a new absolute, standardized path from the provided file-scheme URL.
- Note: If the URL is not a file URL, returns `nil`.
*/
public init?(url: URL) {
guard url.scheme == "file" else { return nil }
self.init(string: url.path)
self.init(url.path)
//NOTE: URL cannot be a file-reference url, unlike NSURL, so this always works
}

public init?(_ url: NSURL) {
/**
Creates a new absolute, standardized path from the provided file-scheme URL.
- Note: If the URL is not a file URL, returns `nil`.
- Note: If the URL is a file reference URL, converts it to a POSIX path first.
*/
public init?(url: NSURL) {
guard url.scheme == "file", let path = url.path else { return nil }
self.init(string: path)
// ^^ works even if the url is a file-reference url
Expand Down
6 changes: 3 additions & 3 deletions Tests/PathTests/PathTests.swift
Expand Up @@ -577,10 +577,10 @@ class PathTests: XCTestCase {
}

func testURLInitializer() throws {
XCTAssertEqual(Path(Path.home.url), Path.home)
XCTAssertEqual(Path(url: Path.home.url), Path.home)
XCTAssertEqual(Path.home.fileReferenceURL.flatMap(Path.init), Path.home)
XCTAssertNil(Path(URL(string: "https://foo.com")!))
XCTAssertNil(Path(NSURL(string: "https://foo.com")!))
XCTAssertNil(Path(url: URL(string: "https://foo.com")!))
XCTAssertNil(Path(url: NSURL(string: "https://foo.com")!))
}

func testInitializerForRelativePath() throws {
Expand Down

0 comments on commit c9d300a

Please sign in to comment.