From b4c92f86dcd764432806ba08da43f898c7bebccf Mon Sep 17 00:00:00 2001 From: Max Howell Date: Thu, 17 Jan 2019 18:23:22 -0500 Subject: [PATCH] Fix tests on Linux --- README.md | 4 +++- Sources/Extensions.swift | 2 +- Sources/Path+FileManager.swift | 29 +++++++++++++++++++-------- Sources/TemporaryDirectory.swift | 2 +- Tests/PathTests/PathTests.swift | 4 ++-- Tests/PathTests/XCTestManifests.swift | 2 +- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 7720c7b..b516c95 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ A file-system pathing library focused on developer experience and robust end‐results. ```swift +import Path + // convenient static members let home = Path.home @@ -26,7 +28,7 @@ try Path.root.join("foo").copy(into: Path.root.mkdir("bar")) // were meant to be directory destinations ``` -Paths are just string representations, there *may not* be a real file there. +Paths are just string representations, there *might not* be a real file there. # Support mxcl diff --git a/Sources/Extensions.swift b/Sources/Extensions.swift index 1670c49..d31812b 100644 --- a/Sources/Extensions.swift +++ b/Sources/Extensions.swift @@ -47,7 +47,7 @@ public extension Data { func write(to: Path, atomically: Bool = false) throws -> Path { let opts: NSData.WritingOptions if atomically { - #if os(macOS) + #if !os(Linux) opts = .atomicWrite #else opts = .atomic diff --git a/Sources/Path+FileManager.swift b/Sources/Path+FileManager.swift index e7ffa4a..84d7fa9 100644 --- a/Sources/Path+FileManager.swift +++ b/Sources/Path+FileManager.swift @@ -73,24 +73,37 @@ public extension Path { return try "".write(to: self) } - @inlinable - @discardableResult - public func mkdir() throws -> Path { + private func _foo(go: () throws -> Void) throws { + #if !os(Linux) do { - try FileManager.default.createDirectory(at: url, withIntermediateDirectories: false, attributes: nil) + try go() } catch CocoaError.Code.fileWriteFileExists { // noop } + #else + do { + try go() + } catch { + let error = error as NSError + guard error.domain == NSCocoaErrorDomain, error.code == CocoaError.Code.fileWriteFileExists.rawValue else { + throw error + } + } + #endif + } + + @discardableResult + public func mkdir() throws -> Path { + try _foo { + try FileManager.default.createDirectory(at: self.url, withIntermediateDirectories: false, attributes: nil) + } return self } - @inlinable @discardableResult public func mkpath() throws -> Path { - do { + try _foo { try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil) - } catch CocoaError.Code.fileWriteFileExists { - // noop } return self } diff --git a/Sources/TemporaryDirectory.swift b/Sources/TemporaryDirectory.swift index ecacc3e..1be4ea9 100644 --- a/Sources/TemporaryDirectory.swift +++ b/Sources/TemporaryDirectory.swift @@ -5,7 +5,7 @@ public class TemporaryDirectory { public var path: Path { return Path(string: url.path) } public init() throws { - #if os(macOS) + #if !os(Linux) url = try FileManager.default.url(for: .itemReplacementDirectory, in: .userDomainMask, appropriateFor: URL(fileURLWithPath: "/"), create: true) #else let envs = ProcessInfo.processInfo.environment diff --git a/Tests/PathTests/PathTests.swift b/Tests/PathTests/PathTests.swift index 40b6488..114c294 100644 --- a/Tests/PathTests/PathTests.swift +++ b/Tests/PathTests/PathTests.swift @@ -37,12 +37,12 @@ class PathTests: XCTestCase { func testExists() { XCTAssert(Path.root.exists) - XCTAssert((Path.root/"Users").exists) + XCTAssert((Path.root/"bin").exists) } func testIsDirectory() { XCTAssert(Path.root.isDirectory) - XCTAssert((Path.root/"Users").isDirectory) + XCTAssert((Path.root/"bin").isDirectory) } func testMktemp() throws { diff --git a/Tests/PathTests/XCTestManifests.swift b/Tests/PathTests/XCTestManifests.swift index 03eb3ab..8095888 100644 --- a/Tests/PathTests/XCTestManifests.swift +++ b/Tests/PathTests/XCTestManifests.swift @@ -15,7 +15,7 @@ extension PathTests { ] } -#if !os(macOS) +#if os(Linux) public func __allTests() -> [XCTestCaseEntry] { return [ testCase(PathTests.__allTests),