Skip to content

Commit 5d2d879

Browse files
committed
Move fileExists to FileManagerClient
1 parent ae8e253 commit 5d2d879

12 files changed

+277
-255
lines changed

Sources/App/Commands/Analyze.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ extension Analyze {
143143
@Dependency(\.fileManager) var fileManager
144144
let checkoutDir = fileManager.checkoutsDirectory()
145145
Current.logger().info("Checkout directory: \(checkoutDir)")
146-
if !Current.fileManager.fileExists(atPath: checkoutDir) {
146+
if !fileManager.fileExists(atPath: checkoutDir) {
147147
try await createCheckoutsDirectory(client: client, path: checkoutDir)
148148
}
149149

@@ -267,11 +267,12 @@ extension Analyze {
267267
/// - url: url to fetch from
268268
/// - Throws: Shell errors
269269
static func fetch(cacheDir: String, branch: String, url: String) async throws {
270+
@Dependency(\.fileManager) var fileManager
270271
Current.logger().info("pulling \(url) in \(cacheDir)")
271272
// clean up stray lock files that might have remained from aborted commands
272273
for fileName in ["HEAD.lock", "index.lock"] {
273274
let filePath = cacheDir + "/.git/\(fileName)"
274-
if Current.fileManager.fileExists(atPath: filePath) {
275+
if fileManager.fileExists(atPath: filePath) {
275276
Current.logger().info("Removing stale \(fileName) at path: \(filePath)")
276277
try await Current.shell.run(command: .removeFile(from: filePath))
277278
}
@@ -296,7 +297,7 @@ extension Analyze {
296297
}
297298

298299
do {
299-
guard Current.fileManager.fileExists(atPath: cacheDir) else {
300+
guard fileManager.fileExists(atPath: cacheDir) else {
300301
try await clone(cacheDir: cacheDir, url: package.model.url)
301302
return
302303
}
@@ -530,7 +531,8 @@ extension Analyze {
530531
/// - Throws: Shell errors or AppError.invalidRevision if there is no Package.swift file
531532
/// - Returns: `Manifest` data
532533
static func dumpPackage(at path: String) async throws -> Manifest {
533-
guard Current.fileManager.fileExists(atPath: path + "/Package.swift") else {
534+
@Dependency(\.fileManager) var fileManager
535+
guard fileManager.fileExists(atPath: path + "/Package.swift") else {
534536
// It's important to check for Package.swift - otherwise `dump-package` will go
535537
// up the tree through parent directories to find one
536538
throw AppError.invalidRevision(nil, "no Package.swift")

Sources/App/Commands/ReAnalyzeVersions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ enum ReAnalyzeVersions {
185185
try await dependencies.yield {
186186
@Dependency(\.fileManager) var fileManager
187187
guard let cacheDir = fileManager.cacheDirectoryPath(for: pkg.model) else { return }
188-
if !Current.fileManager.fileExists(atPath: cacheDir) || refreshCheckouts {
188+
if !fileManager.fileExists(atPath: cacheDir) || refreshCheckouts {
189189
try await Analyze.refreshCheckout(package: pkg)
190190
}
191191

Sources/App/Core/AppEnvironment.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,13 @@ extension AppEnvironment {
4545

4646

4747
struct FileManager: Sendable {
48-
var fileExists: @Sendable (String) -> Bool
4948
var removeItem: @Sendable (_ path: String) throws -> Void
5049
var workingDirectory: @Sendable () -> String
5150

5251
// pass-through methods to preserve argument labels
53-
func fileExists(atPath path: String) -> Bool { fileExists(path) }
5452
func removeItem(atPath path: String) throws { try removeItem(path) }
5553

5654
static let live: Self = .init(
57-
fileExists: { Foundation.FileManager.default.fileExists(atPath: $0) },
5855
removeItem: { try Foundation.FileManager.default.removeItem(atPath: $0) },
5956
workingDirectory: { DirectoryConfiguration.detect().workingDirectory }
6057
)

Sources/App/Core/Dependencies/FileManagerClient.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct FileManagerClient {
2727
var contents: @Sendable (_ atPath: String) -> Data?
2828
var contentsOfDirectory: @Sendable (_ atPath: String) throws -> [String]
2929
var createDirectory: @Sendable (_ atPath: String, _ withIntermediateDirectories: Bool, _ attributes: [FileAttributeKey : Any]?) throws -> Void
30+
var fileExists: @Sendable (_ atPath: String) -> Bool = { reportIssue("fileExists"); return Foundation.FileManager.default.fileExists(atPath: $0) }
3031
}
3132

3233

@@ -45,7 +46,8 @@ extension FileManagerClient: DependencyKey {
4546
checkoutsDirectory: { Environment.get("CHECKOUTS_DIR") ?? DirectoryConfiguration.detect().workingDirectory + "SPI-checkouts" },
4647
contents: { Foundation.FileManager.default.contents(atPath: $0) },
4748
contentsOfDirectory: { try Foundation.FileManager.default.contentsOfDirectory(atPath: $0) },
48-
createDirectory: { try Foundation.FileManager.default.createDirectory(atPath: $0, withIntermediateDirectories: $1, attributes: $2) }
49+
createDirectory: { try Foundation.FileManager.default.createDirectory(atPath: $0, withIntermediateDirectories: $1, attributes: $2) },
50+
fileExists: { Foundation.FileManager.default.fileExists(atPath: $0) }
4951
)
5052
}
5153
}

Sources/App/Core/PackageContributors.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// limitations under the License.
1414

1515
import Foundation
16+
17+
import Dependencies
1618
import ShellOut
1719
import Vapor
1820

@@ -65,8 +67,8 @@ enum PackageContributors {
6567

6668
/// Gets the git history in a string log
6769
private static func runShortlog(gitCacheDirectoryPath: String, packageID: UUID?) async throws -> String {
68-
69-
if Current.fileManager.fileExists(atPath: gitCacheDirectoryPath) == false {
70+
@Dependency(\.fileManager) var fileManager
71+
if fileManager.fileExists(atPath: gitCacheDirectoryPath) == false {
7072
throw AppError.cacheDirectoryDoesNotExist(packageID, gitCacheDirectoryPath)
7173
}
7274

Tests/AppTests/AnalyzeErrorTests.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,13 @@ final class AnalyzeErrorTests: AppTestCase {
204204
func test_analyze_dumpPackage_missing_manifest() async throws {
205205
try await withDependencies {
206206
$0.environment.loadSPIManifest = { _ in nil }
207-
} operation: {
208-
// setup
209-
Current.fileManager.fileExists = { @Sendable path in
207+
$0.fileManager.fileExists = { @Sendable path in
210208
if path.hasSuffix("github.com-foo-1/Package.swift") {
211209
return false
212210
}
213211
return true
214212
}
215-
213+
} operation: {
216214
// MUT
217215
try await Analyze.analyze(client: app.client,
218216
database: app.db,

0 commit comments

Comments
 (0)