From cc9e58a33e9f108540c73f6e8d4c4b22fa7ed657 Mon Sep 17 00:00:00 2001 From: George Date: Sat, 13 Nov 2021 10:30:58 -0800 Subject: [PATCH 1/3] Implement FilePath.getCurrentWorkingDirectory() --- Sources/System/FilePath/FilePath.swift | 10 +++++++++ Sources/System/Internals/Syscalls.swift | 21 +++++++++++++++++++ .../FilePathTests/FilePathTest.swift | 6 ++++++ 3 files changed, 37 insertions(+) diff --git a/Sources/System/FilePath/FilePath.swift b/Sources/System/FilePath/FilePath.swift index c2797f5e..e7c83972 100644 --- a/Sources/System/FilePath/FilePath.swift +++ b/Sources/System/FilePath/FilePath.swift @@ -62,6 +62,16 @@ public struct FilePath { // @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) extension FilePath { + + /// Returns the current working directory of this process + public static func getCurrentWorkingDirectory() throws -> FilePath { + guard let cwd = system_getcwd(nil, 0) else { + throw Errno.current + } + defer { system_free(cwd) } + return FilePath(platformString: cwd) + } + /// The length of the file path, excluding the null terminator. public var length: Int { _storage.length } } diff --git a/Sources/System/Internals/Syscalls.swift b/Sources/System/Internals/Syscalls.swift index 453c02fc..6e76f610 100644 --- a/Sources/System/Internals/Syscalls.swift +++ b/Sources/System/Internals/Syscalls.swift @@ -123,3 +123,24 @@ internal func system_pipe(_ fds: UnsafeMutablePointer) -> CInt { return pipe(fds) } #endif + +#if !os(Windows) +internal func system_getcwd(_ buf: UnsafeMutablePointer?, _ size: size_t) -> UnsafeMutablePointer? { +#if ENABLE_MOCKING + if mockingEnabled { + /// I'm not really sure how to mock this since `buf` is passed in uninitialized and it needs to return something + fatalError() + } +#endif + return getcwd(buf, size) +} +#endif + +#if !os(Windows) +internal func system_free(_ ptr: UnsafeMutableRawPointer?) { +#if ENABLE_MOCKING + if mockingEnabled { _ = _mock(ptr) } +#endif + free(ptr) +} +#endif diff --git a/Tests/SystemTests/FilePathTests/FilePathTest.swift b/Tests/SystemTests/FilePathTests/FilePathTest.swift index 93bc3504..96dc5a64 100644 --- a/Tests/SystemTests/FilePathTests/FilePathTest.swift +++ b/Tests/SystemTests/FilePathTests/FilePathTest.swift @@ -72,5 +72,11 @@ final class FilePathTest: XCTestCase { } } } + + func testCurrentWorkingDirectory() { + XCTAssertEqual( + try FilePath.getCurrentWorkingDirectory().string, + FileManager.default.currentDirectoryPath) + } } From 35dfffdd15a0ae1dac88497030594302db77a09f Mon Sep 17 00:00:00 2001 From: George Date: Sat, 13 Nov 2021 14:48:56 -0800 Subject: [PATCH 2/3] Make process-global nature of working directory more apparent --- Sources/System/FilePath/FilePath.swift | 6 ++++-- Tests/SystemTests/FilePathTests/FilePathTest.swift | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Sources/System/FilePath/FilePath.swift b/Sources/System/FilePath/FilePath.swift index e7c83972..cb8ad5ed 100644 --- a/Sources/System/FilePath/FilePath.swift +++ b/Sources/System/FilePath/FilePath.swift @@ -63,8 +63,10 @@ public struct FilePath { // @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) extension FilePath { - /// Returns the current working directory of this process - public static func getCurrentWorkingDirectory() throws -> FilePath { + /// Returns the current working directory of this process. + /// + /// - Warning: This value is global and care should be taken to make sure it does not unexpectedly change. + public static func getCurrentWorkingDirectoryForProcess() throws -> FilePath { guard let cwd = system_getcwd(nil, 0) else { throw Errno.current } diff --git a/Tests/SystemTests/FilePathTests/FilePathTest.swift b/Tests/SystemTests/FilePathTests/FilePathTest.swift index 96dc5a64..77e77e6d 100644 --- a/Tests/SystemTests/FilePathTests/FilePathTest.swift +++ b/Tests/SystemTests/FilePathTests/FilePathTest.swift @@ -75,7 +75,7 @@ final class FilePathTest: XCTestCase { func testCurrentWorkingDirectory() { XCTAssertEqual( - try FilePath.getCurrentWorkingDirectory().string, + try FilePath.getCurrentWorkingDirectoryForProcess().string, FileManager.default.currentDirectoryPath) } } From 3aef3965b210b0ed783f091eacb86ac02e1f1eb3 Mon Sep 17 00:00:00 2001 From: George Date: Mon, 15 Nov 2021 08:56:13 -0800 Subject: [PATCH 3/3] Change name again --- Sources/System/FilePath/FilePath.swift | 4 ++-- Tests/SystemTests/FilePathTests/FilePathTest.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/System/FilePath/FilePath.swift b/Sources/System/FilePath/FilePath.swift index cb8ad5ed..5e93fa8b 100644 --- a/Sources/System/FilePath/FilePath.swift +++ b/Sources/System/FilePath/FilePath.swift @@ -65,8 +65,8 @@ extension FilePath { /// Returns the current working directory of this process. /// - /// - Warning: This value is global and care should be taken to make sure it does not unexpectedly change. - public static func getCurrentWorkingDirectoryForProcess() throws -> FilePath { + /// - Warning: This value is global to the proess and care should be taken to make sure it does not unexpectedly change. + public static func currentWorkingDirectory() throws -> FilePath { guard let cwd = system_getcwd(nil, 0) else { throw Errno.current } diff --git a/Tests/SystemTests/FilePathTests/FilePathTest.swift b/Tests/SystemTests/FilePathTests/FilePathTest.swift index 77e77e6d..6c92c11e 100644 --- a/Tests/SystemTests/FilePathTests/FilePathTest.swift +++ b/Tests/SystemTests/FilePathTests/FilePathTest.swift @@ -75,7 +75,7 @@ final class FilePathTest: XCTestCase { func testCurrentWorkingDirectory() { XCTAssertEqual( - try FilePath.getCurrentWorkingDirectoryForProcess().string, + try FilePath.currentWorkingDirectory().string, FileManager.default.currentDirectoryPath) } }