Skip to content

Commit

Permalink
feat: add support for getting the current working directory
Browse files Browse the repository at this point in the history
  • Loading branch information
fortmarek committed Oct 8, 2024
1 parent 1f67287 commit 25e1c41
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio",
"state" : {
"revision" : "1b33db2dea6a64d5b619b9e888175133c6d7f410",
"version" : "2.73.0"
"revision" : "665206000b8307cab5ac51203d29b0f232d7e31b",
"version" : "2.74.0"
}
},
{
Expand Down
14 changes: 13 additions & 1 deletion Sources/FileSystem/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,10 @@ public protocol FileSysteming {
skipHiddenFiles: Bool
) throws -> AnyThrowingAsyncSequenceable<Path.AbsolutePath>

/// Returns the path of the current working directory.
func currentWorkingDirectory() async throws -> AbsolutePath

// TODO:
// func changeExtension(path: AbsolutePath, to newExtension: String) throws -> AbsolutePath
// func urlSafeBase64MD5(path: AbsolutePath) throws -> String
// func fileAttributes(at path: AbsolutePath) throws -> [FileAttributeKey: Any]
// func files(in path: AbsolutePath, nameFilter: Set<String>?, extensionFilter: Set<String>?) -> Set<AbsolutePath>
Expand All @@ -285,6 +287,10 @@ public struct FileSystem: FileSysteming, Sendable {
self.logger = logger
}

public func currentWorkingDirectory() async throws -> AbsolutePath {
try await NIOFileSystem.FileSystem.shared.currentWorkingDirectory.path
}

public func exists(_ path: AbsolutePath) async throws -> Bool {
logger?.debug("Checking if a file or directory exists at path \(path.pathString).")
let info = try await NIOFileSystem.FileSystem.shared.info(forFileAt: .init(path.pathString))
Expand Down Expand Up @@ -648,3 +654,9 @@ extension AnyThrowingAsyncSequenceable where Element == Path.AbsolutePath {
try await reduce(into: [Path.AbsolutePath]()) { $0.append($1) }
}
}

extension FilePath {
fileprivate var path: AbsolutePath {
try! AbsolutePath(validating: string) // swiftlint:disable:this force_try
}
}
9 changes: 9 additions & 0 deletions Tests/FileSystemTests/FileSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ final class FileSystemTests: XCTestCase, @unchecked Sendable {
XCTAssertEqual(caughtError as? TestError, TestError())
}

func test_currentWorkingDirectory() async throws {
// When
let got = try await subject.currentWorkingDirectory()

// Then
let isDirectory = try await subject.exists(got, isDirectory: true)
XCTAssertTrue(isDirectory)
}

func test_move_when_fromFileExistsAndToPathsParentDirectoryExists() async throws {
try await subject.runInTemporaryDirectory(prefix: "FileSystem") { temporaryDirectory in
// Given
Expand Down

0 comments on commit 25e1c41

Please sign in to comment.