Skip to content

Commit

Permalink
Merge pull request #64 from mxcl/Path.source()
Browse files Browse the repository at this point in the history
Add `Path.source()`
  • Loading branch information
mxcl authored Aug 19, 2020
2 parents 8b90260 + 6461a55 commit 142d4bc
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ignore:
- Tests
- Tests/PathTests/etc.swift
- Tests/PathTests/TemporaryDirectory.swift
31 changes: 18 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ jobs:
strategy:
matrix:
xcode:
#- 10.3 # Swift 5.0 (doesn’t work on GHA macOS image :-/)
# - 10.3 # Swift 5.0 (doesn’t 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
Expand All @@ -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/[email protected]
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:
Expand All @@ -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 # doesn’t 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 \
Expand All @@ -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
file: ./info.lcov
12 changes: 12 additions & 0 deletions Sources/Path+CommonDirectories.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 user’s home directory
public static var home: DynamicPath {
let string: String
Expand Down
15 changes: 7 additions & 8 deletions Tests/PathTests/PathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
34 changes: 33 additions & 1 deletion Tests/PathTests/etc.swift
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 142d4bc

Please sign in to comment.