Compare commits

...

3 Commits

Author SHA1 Message Date
Max Howell
c9d300a7b6 Path(_ url:) -> Path(url:) 2019-02-11 20:40:27 -05:00
Max Howell
ed4b773870 Fill in this TODO in README
[skip ci]
2019-02-11 15:13:14 -05:00
Max Howell
097e020735 There are no usernames on iOS etc. 2019-02-11 15:11:22 -05:00
3 changed files with 18 additions and 13 deletions

View File

@@ -181,7 +181,7 @@ with your work without worries.
There is also some magic going on in Foundations filesystem APIs, which we look
for and ensure our API is deterministic, eg. [this test].
[this test]: TODO
[this test]: https://github.com/mxcl/Path.swift/blob/master/Tests/PathTests/PathTests.swift#L539-L554
# `Path.swift` is properly cross-platform
@@ -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) {
/**/
}
```

View File

@@ -78,11 +78,7 @@ public struct Path: Equatable, Hashable, Comparable {
tilded = dir
}
#else
if username != NSUserName() {
return nil
} else {
tilded = NSHomeDirectory()
}
return nil // there are no usernames on iOS, etc.
#endif
}
pathComponents.remove(at: 0)
@@ -108,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

View File

@@ -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 {