From 6461a550c639aa264747b6054e878eaa7887e06e Mon Sep 17 00:00:00 2001 From: Max Howell Date: Wed, 19 Aug 2020 11:41:26 -0400 Subject: [PATCH] Add `Path.source()` --- .github/codecov.yml | 3 ++- .github/workflows/ci.yml | 31 ++++++++++++++----------- Sources/Path+CommonDirectories.swift | 12 ++++++++++ Tests/PathTests/PathTests.swift | 15 ++++++------ Tests/PathTests/etc.swift | 34 +++++++++++++++++++++++++++- 5 files changed, 72 insertions(+), 23 deletions(-) diff --git a/.github/codecov.yml b/.github/codecov.yml index 3922952..2546f55 100644 --- a/.github/codecov.yml +++ b/.github/codecov.yml @@ -1,2 +1,3 @@ ignore: - - Tests + - Tests/PathTests/etc.swift + - Tests/PathTests/TemporaryDirectory.swift diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 47559b7..5ce68de 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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 # 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 \ @@ -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 \ No newline at end of file diff --git a/Sources/Path+CommonDirectories.swift b/Sources/Path+CommonDirectories.swift index 6d12fd6..c0edb20 100644 --- a/Sources/Path+CommonDirectories.swift +++ b/Sources/Path+CommonDirectories.swift @@ -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 diff --git a/Tests/PathTests/PathTests.swift b/Tests/PathTests/PathTests.swift index 48ba0d0..e037515 100644 --- a/Tests/PathTests/PathTests.swift +++ b/Tests/PathTests/PathTests.swift @@ -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: P, _ q: Q, file: StaticString = #file, line: UInt = #line) { - XCTAssertEqual(p.string, q.string, file: file, line: line) -} - -private func XCTAssertEqual(_ p: P?, _ q: Q?, file: StaticString = #file, line: UInt = #line) { - XCTAssertEqual(p?.string, q?.string, file: file, line: line) -} diff --git a/Tests/PathTests/etc.swift b/Tests/PathTests/etc.swift index 544f197..177fd7e 100644 --- a/Tests/PathTests/etc.swift +++ b/Tests/PathTests/etc.swift @@ -1,11 +1,43 @@ import XCTest import Path +#if swift(>=5.3) +func XCTAssertEqual(_ set1: Set, _ set2: Set, _ 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(_ set1: Set, _ set2: Set, _ 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(_ set1: Set, _ set2: Set, 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: P, _ q: Q, file: StaticString = #filePath, line: UInt = #line) { + XCTAssertEqual(p.string, q.string, file: file, line: line) +} + +func XCTAssertEqual(_ p: P?, _ q: Q?, file: StaticString = #filePath, line: UInt = #line) { + XCTAssertEqual(p?.string, q?.string, file: file, line: line) +} +#else +func XCTAssertEqual(_ p: P, _ q: Q, file: StaticString = #file, line: UInt = #line) { + XCTAssertEqual(p.string, q.string, file: file, line: line) +} + +func XCTAssertEqual(_ p: P?, _ q: Q?, file: StaticString = #file, line: UInt = #line) { + XCTAssertEqual(p?.string, q?.string, file: file, line: line) +} +#endif