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.
|
||||
var sharedFrameworks: Path {
|
||||
var `default`: Path {
|
||||
#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`
|
||||
return sharedFrameworksPath.flatMap(Path.init) ?? defaultSharedFrameworksPath
|
||||
}
|
||||
|
||||
/// Returns the path for the resources directory in this bundle.
|
||||
var resources: Path {
|
||||
var `default`: Path {
|
||||
#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`
|
||||
return resourcePath.flatMap(Path.init) ?? defaultResourcesPath
|
||||
}
|
||||
|
||||
/// Returns the path for this bundle.
|
||||
@@ -107,3 +89,25 @@ public extension FileHandle {
|
||||
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()
|
||||
}
|
||||
#else
|
||||
guard let pathString = FileManager.default.urls(for: searchPath, in: .userDomainMask).first?.path else {
|
||||
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()
|
||||
}
|
||||
}
|
||||
guard let pathString = FileManager.default.urls(for: searchPath, in: .userDomainMask).first?.path else { return defaultUrl(for: searchPath) }
|
||||
return Path(string: pathString)
|
||||
#endif
|
||||
}
|
||||
@@ -90,3 +79,19 @@ extension Path {
|
||||
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 Path
|
||||
|
||||
class PathTests: XCTestCase {
|
||||
func testConcatenation() {
|
||||
@@ -253,6 +253,10 @@ class PathTests: XCTestCase {
|
||||
XCTAssertEqual(Path.caches.string, NSHomeDirectory() + "/Library/Caches")
|
||||
XCTAssertEqual(Path.cwd.string, FileManager.default.currentDirectoryPath)
|
||||
XCTAssertEqual(Path.applicationSupport.string, NSHomeDirectory() + "/Library/Application Support")
|
||||
|
||||
_ = defaultUrl(for: .documentDirectory)
|
||||
_ = defaultUrl(for: .cachesDirectory)
|
||||
_ = defaultUrl(for: .applicationSupportDirectory)
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -359,6 +363,17 @@ class PathTests: XCTestCase {
|
||||
XCTAssertEqual(bndl.sharedFrameworks, tmpdir.SharedFrameworks)
|
||||
XCTAssertEqual(bndl.resources, tmpdir)
|
||||
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