More coverage
Though I can hardly test these functions, at least we can verify they run without crashing etc.
This commit is contained in:
@@ -11,30 +11,12 @@ public extension Bundle {
|
|||||||
|
|
||||||
/// Returns the path for the shared-frameworks directory in this bundle.
|
/// Returns the path for the shared-frameworks directory in this bundle.
|
||||||
var sharedFrameworks: Path {
|
var sharedFrameworks: Path {
|
||||||
var `default`: Path {
|
return sharedFrameworksPath.flatMap(Path.init) ?? defaultSharedFrameworksPath
|
||||||
#if os(macOS)
|
|
||||||
return path.join("Contents/Frameworks")
|
|
||||||
#elseif os(Linux)
|
|
||||||
return path.join("lib")
|
|
||||||
#else
|
|
||||||
return path.join("Frameworks")
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
return sharedFrameworksPath.flatMap(Path.init) ?? `default`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the path for the resources directory in this bundle.
|
/// Returns the path for the resources directory in this bundle.
|
||||||
var resources: Path {
|
var resources: Path {
|
||||||
var `default`: Path {
|
return resourcePath.flatMap(Path.init) ?? defaultResourcesPath
|
||||||
#if os(macOS)
|
|
||||||
return path.join("Contents/Resources")
|
|
||||||
#elseif os(Linux)
|
|
||||||
return path.join("share")
|
|
||||||
#else
|
|
||||||
return path
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
return resourcePath.flatMap(Path.init) ?? `default`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the path for this bundle.
|
/// Returns the path for this bundle.
|
||||||
@@ -107,3 +89,25 @@ public extension FileHandle {
|
|||||||
try self.init(forUpdating: path.url)
|
try self.init(forUpdating: path.url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal extension Bundle {
|
||||||
|
var defaultSharedFrameworksPath: Path {
|
||||||
|
#if os(macOS)
|
||||||
|
return path.join("Contents/Frameworks")
|
||||||
|
#elseif os(Linux)
|
||||||
|
return path.join("lib")
|
||||||
|
#else
|
||||||
|
return path.join("Frameworks")
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
var defaultResourcesPath: Path {
|
||||||
|
#if os(macOS)
|
||||||
|
return path.join("Contents/Resources")
|
||||||
|
#elseif os(Linux)
|
||||||
|
return path.join("share")
|
||||||
|
#else
|
||||||
|
return path
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -47,18 +47,7 @@ extension Path {
|
|||||||
fatalError()
|
fatalError()
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
guard let pathString = FileManager.default.urls(for: searchPath, in: .userDomainMask).first?.path else {
|
guard let pathString = FileManager.default.urls(for: searchPath, in: .userDomainMask).first?.path else { return defaultUrl(for: searchPath) }
|
||||||
switch searchPath {
|
|
||||||
case .documentDirectory:
|
|
||||||
return Path.home/"Documents"
|
|
||||||
case .applicationSupportDirectory:
|
|
||||||
return Path.home/"Library/Application Support"
|
|
||||||
case .cachesDirectory:
|
|
||||||
return Path.home/"Library/Caches"
|
|
||||||
default:
|
|
||||||
fatalError()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Path(string: pathString)
|
return Path(string: pathString)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -90,3 +79,19 @@ extension Path {
|
|||||||
return path(for: .applicationSupportDirectory)
|
return path(for: .applicationSupportDirectory)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !os(Linux)
|
||||||
|
func defaultUrl(for searchPath: FileManager.SearchPathDirectory) -> Path {
|
||||||
|
switch searchPath {
|
||||||
|
case .documentDirectory:
|
||||||
|
return Path.home/"Documents"
|
||||||
|
case .applicationSupportDirectory:
|
||||||
|
return Path.home/"Library/Application Support"
|
||||||
|
case .cachesDirectory:
|
||||||
|
return Path.home/"Library/Caches"
|
||||||
|
default:
|
||||||
|
fatalError()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
|
@testable import Path
|
||||||
import XCTest
|
import XCTest
|
||||||
import Path
|
|
||||||
|
|
||||||
class PathTests: XCTestCase {
|
class PathTests: XCTestCase {
|
||||||
func testConcatenation() {
|
func testConcatenation() {
|
||||||
@@ -253,6 +253,10 @@ class PathTests: XCTestCase {
|
|||||||
XCTAssertEqual(Path.caches.string, NSHomeDirectory() + "/Library/Caches")
|
XCTAssertEqual(Path.caches.string, NSHomeDirectory() + "/Library/Caches")
|
||||||
XCTAssertEqual(Path.cwd.string, FileManager.default.currentDirectoryPath)
|
XCTAssertEqual(Path.cwd.string, FileManager.default.currentDirectoryPath)
|
||||||
XCTAssertEqual(Path.applicationSupport.string, NSHomeDirectory() + "/Library/Application Support")
|
XCTAssertEqual(Path.applicationSupport.string, NSHomeDirectory() + "/Library/Application Support")
|
||||||
|
|
||||||
|
_ = defaultUrl(for: .documentDirectory)
|
||||||
|
_ = defaultUrl(for: .cachesDirectory)
|
||||||
|
_ = defaultUrl(for: .applicationSupportDirectory)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -359,6 +363,17 @@ class PathTests: XCTestCase {
|
|||||||
XCTAssertEqual(bndl.sharedFrameworks, tmpdir.SharedFrameworks)
|
XCTAssertEqual(bndl.sharedFrameworks, tmpdir.SharedFrameworks)
|
||||||
XCTAssertEqual(bndl.resources, tmpdir)
|
XCTAssertEqual(bndl.resources, tmpdir)
|
||||||
XCTAssertNil(bndl.path(forResource: "foo", ofType: "bar"))
|
XCTAssertNil(bndl.path(forResource: "foo", ofType: "bar"))
|
||||||
|
|
||||||
|
#if os(macOS)
|
||||||
|
XCTAssertEqual(bndl.defaultSharedFrameworksPath, tmpdir.Contents.Frameworks)
|
||||||
|
XCTAssertEqual(bndl.defaultResourcesPath, tmpdir.Contents.Resources)
|
||||||
|
#elseif os(tvOS) || os(iOS)
|
||||||
|
XCTAssertEqual(bndl.defaultSharedFrameworksPath, tmpdir.Frameworks)
|
||||||
|
XCTAssertEqual(bndl.defaultResourcesPath, tmpdir)
|
||||||
|
#else
|
||||||
|
XCTAssertEqual(bndl.defaultSharedFrameworksPath, tmpdir.lib)
|
||||||
|
XCTAssertEqual(bndl.defaultResourcesPath, tmpdir.share)
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user