Add Path.source()

This commit is contained in:
Max Howell
2020-08-19 11:41:26 -04:00
parent 8b90260517
commit 6461a550c6
5 changed files with 72 additions and 23 deletions

3
.github/codecov.yml vendored
View File

@@ -1,2 +1,3 @@
ignore:
- Tests
- Tests/PathTests/etc.swift
- Tests/PathTests/TemporaryDirectory.swift

View File

@@ -17,7 +17,6 @@ jobs:
# - 10.3 # Swift 5.0 (doesnt work on GHA macOS image :-/)
- 11.3 # Swift 5.1
- ^11.4 # Swift 5.2
- latest # Swift 5.3
steps:
- uses: actions/checkout@v2
- name: setup-xcode
@@ -34,9 +33,13 @@ jobs:
destination:
- platform=iOS Simulator,OS=latest,name=iPhone 11
- platform=tvOS Simulator,OS=latest,name=Apple TV
- platform=macOS # for code-coverage
- platform=macOS
steps:
- uses: actions/checkout@v2
- uses: maxim-lobanov/setup-xcode@1.0
with:
xcode-version: 12 # Swift 5.3
- run: swift --version
- run: swift package generate-xcodeproj --enable-code-coverage
- uses: sersoft-gmbh/xcodebuild-action@v1
with:
@@ -50,36 +53,39 @@ jobs:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- run: swift package generate-xcodeproj --enable-code-coverage
- run: swift package generate-xcodeproj
- uses: sersoft-gmbh/xcodebuild-action@v1
with:
project: Path.swift.xcodeproj
scheme: Path.swift-Package
destination: platform=watchOS Simulator,OS=latest,name=Apple Watch Series 5 - 40mm
action: build
linux-4-2:
needs: smoke
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: fwal/setup-swift@v1
with:
swift-version: 4.2
- run: swift test --parallel # doesnt support code coverage
linux:
needs: smoke
runs-on: ubuntu-latest
strategy:
matrix:
swift:
# - 4.0.3 fails for some reason
- 4.2
- 5.0.3
- 5.1
- 5.2
# - 5.3 not available yet sigh
# - 5.3 not yet available sigh
steps:
- uses: actions/checkout@v2
- uses: fwal/setup-swift@v1
with:
swift-version: ${{ matrix.swift }}
- run: swift test --parallel --enable-code-coverage
if: ${{ matrix.swift != '4.2' }}
- run: swift test --parallel
if: ${{ matrix.swift == '4.2' }}
- name: Generate Coverage Report
if: ${{ matrix.swift != '4.2' }}
run: |
sudo apt-get -qq update && sudo apt-get -qq install llvm-10
export b=$(swift build --show-bin-path) && llvm-cov-10 \
@@ -89,6 +95,5 @@ jobs:
$b/Path.swiftPackageTests.xctest \
> info.lcov
- uses: codecov/codecov-action@v1
if: ${{ matrix.swift != '4.2' }}
with:
file: ./info.lcov

View File

@@ -14,6 +14,18 @@ extension Path {
return .init(string: "/")
}
#if swift(>=5.3)
public static func source(for filePath: String = #filePath) -> (file: DynamicPath, directory: DynamicPath) {
let file = DynamicPath(string: filePath)
return (file: file, directory: .init(file.parent))
}
#else
public static func source(for filePath: String = #file) -> (file: DynamicPath, directory: DynamicPath) {
let file = DynamicPath(string: filePath)
return (file: file, directory: .init(file.parent))
}
#endif
/// Returns a `Path` representing the users home directory
public static var home: DynamicPath {
let string: String

View File

@@ -305,6 +305,13 @@ class PathTests: XCTestCase {
XCTAssertEqual(Path.root.string, "/")
XCTAssertEqual(Path.home.string, NSHomeDirectory())
XCTAssertEqual(Path.documents.string, NSHomeDirectory() + "/Documents")
#if swift(>=5.3)
let filePath = Path(#filePath)!
#else
let filePath = Path(#file)!
#endif
XCTAssertEqual(Path.source().file, filePath)
XCTAssertEqual(Path.source().directory, filePath.parent)
#if !os(Linux)
XCTAssertEqual(Path.caches.string, NSHomeDirectory() + "/Library/Caches")
XCTAssertEqual(Path.cwd.string, FileManager.default.currentDirectoryPath)
@@ -660,11 +667,3 @@ class PathTests: XCTestCase {
XCTAssertEqual(Path("/foo"), Path.root.foo)
}
}
private func XCTAssertEqual<P: Pathish, Q: Pathish>(_ p: P, _ q: Q, file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(p.string, q.string, file: file, line: line)
}
private func XCTAssertEqual<P: Pathish, Q: Pathish>(_ p: P?, _ q: Q?, file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(p?.string, q?.string, file: file, line: line)
}

View File

@@ -1,11 +1,43 @@
import XCTest
import Path
#if swift(>=5.3)
func XCTAssertEqual<P: Pathish>(_ set1: Set<Path>, _ set2: Set<Path>, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line, relativeTo: P) {
logic(set1, set2, relativeTo: relativeTo) {
XCTFail($0, file: file, line: line)
}
}
#else
func XCTAssertEqual<P: Pathish>(_ set1: Set<Path>, _ set2: Set<Path>, _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line, relativeTo: P) {
logic(set1, set2, relativeTo: relativeTo) {
XCTFail($0, file: file, line: line)
}
}
#endif
private func logic<P: Pathish>(_ set1: Set<Path>, _ set2: Set<Path>, relativeTo: P, fail: (String) -> Void) {
if set1 != set2 {
let cvt: (Path) -> String = { $0.relative(to: relativeTo) }
let out1 = set1.map(cvt).sorted()
let out2 = set1.map(cvt).sorted()
XCTFail("Set(\(out1)) is not equal to Set(\(out2))", file: file, line: line)
fail("Set(\(out1)) is not equal to Set(\(out2))")
}
}
#if swift(>=5.3)
func XCTAssertEqual<P: Pathish, Q: Pathish>(_ p: P, _ q: Q, file: StaticString = #filePath, line: UInt = #line) {
XCTAssertEqual(p.string, q.string, file: file, line: line)
}
func XCTAssertEqual<P: Pathish, Q: Pathish>(_ p: P?, _ q: Q?, file: StaticString = #filePath, line: UInt = #line) {
XCTAssertEqual(p?.string, q?.string, file: file, line: line)
}
#else
func XCTAssertEqual<P: Pathish, Q: Pathish>(_ p: P, _ q: Q, file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(p.string, q.string, file: file, line: line)
}
func XCTAssertEqual<P: Pathish, Q: Pathish>(_ p: P?, _ q: Q?, file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(p?.string, q?.string, file: file, line: line)
}
#endif