Skip to content

Commit ae8e253

Browse files
committed
Move createDirectory to FileManagerClient
1 parent e6419d3 commit ae8e253

File tree

5 files changed

+11
-16
lines changed

5 files changed

+11
-16
lines changed

Sources/App/Commands/Analyze.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,10 @@ extension Analyze {
236236
path: String) async throws {
237237
Current.logger().info("Creating checkouts directory at path: \(path)")
238238
do {
239-
try Current.fileManager.createDirectory(atPath: path,
240-
withIntermediateDirectories: false,
241-
attributes: nil)
239+
@Dependency(\.fileManager) var fileManager
240+
try fileManager.createDirectory(atPath: path,
241+
withIntermediateDirectories: false,
242+
attributes: nil)
242243
} catch {
243244
let error = AppError.genericError(nil, "Failed to create checkouts directory: \(error.localizedDescription)")
244245
Current.logger().report(error: error)

Sources/App/Core/AppEnvironment.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,15 @@ extension AppEnvironment {
4545

4646

4747
struct FileManager: Sendable {
48-
var createDirectory: @Sendable (String, Bool, [FileAttributeKey : Any]?) throws -> Void
4948
var fileExists: @Sendable (String) -> Bool
5049
var removeItem: @Sendable (_ path: String) throws -> Void
5150
var workingDirectory: @Sendable () -> String
5251

5352
// pass-through methods to preserve argument labels
54-
func createDirectory(atPath path: String,
55-
withIntermediateDirectories createIntermediates: Bool,
56-
attributes: [FileAttributeKey : Any]?) throws {
57-
try createDirectory(path, createIntermediates, attributes)
58-
}
5953
func fileExists(atPath path: String) -> Bool { fileExists(path) }
6054
func removeItem(atPath path: String) throws { try removeItem(path) }
6155

6256
static let live: Self = .init(
63-
createDirectory: { try Foundation.FileManager.default.createDirectory(atPath: $0, withIntermediateDirectories: $1, attributes: $2) },
6457
fileExists: { Foundation.FileManager.default.fileExists(atPath: $0) },
6558
removeItem: { try Foundation.FileManager.default.removeItem(atPath: $0) },
6659
workingDirectory: { DirectoryConfiguration.detect().workingDirectory }

Sources/App/Core/Dependencies/FileManagerClient.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct FileManagerClient {
2626
var checkoutsDirectory: @Sendable () -> String = { reportIssue("checkoutsDirectory"); return "SPI-checkouts" }
2727
var contents: @Sendable (_ atPath: String) -> Data?
2828
var contentsOfDirectory: @Sendable (_ atPath: String) throws -> [String]
29+
var createDirectory: @Sendable (_ atPath: String, _ withIntermediateDirectories: Bool, _ attributes: [FileAttributeKey : Any]?) throws -> Void
2930
}
3031

3132

@@ -43,7 +44,8 @@ extension FileManagerClient: DependencyKey {
4344
attributesOfItem: { try Foundation.FileManager.default.attributesOfItem(atPath: $0) },
4445
checkoutsDirectory: { Environment.get("CHECKOUTS_DIR") ?? DirectoryConfiguration.detect().workingDirectory + "SPI-checkouts" },
4546
contents: { Foundation.FileManager.default.contents(atPath: $0) },
46-
contentsOfDirectory: { try Foundation.FileManager.default.contentsOfDirectory(atPath: $0) }
47+
contentsOfDirectory: { try Foundation.FileManager.default.contentsOfDirectory(atPath: $0) },
48+
createDirectory: { try Foundation.FileManager.default.createDirectory(atPath: $0, withIntermediateDirectories: $1, attributes: $2) }
4749
)
4850
}
4951
}

Tests/AppTests/AnalyzerTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class AnalyzerTests: AppTestCase {
3333
// End-to-end test, where we mock at the shell command level (i.e. we
3434
// don't mock the git commands themselves to ensure we're running the
3535
// expected shell commands for the happy path.)
36+
let checkoutDir = QueueIsolated<String?>(nil)
3637
try await withDependencies {
3738
$0.date.now = .now
3839
$0.environment.allowSocialPosts = { true }
@@ -43,6 +44,7 @@ class AnalyzerTests: AppTestCase {
4344
return nil
4445
}
4546
}
47+
$0.fileManager.createDirectory = { @Sendable path, _, _ in checkoutDir.setValue(path) }
4648
$0.httpClient.mastodonPost = { @Sendable _ in }
4749
} operation: {
4850
// setup
@@ -62,7 +64,6 @@ class AnalyzerTests: AppTestCase {
6264
owner: "foo",
6365
stars: 100).save(on: app.db)
6466

65-
let checkoutDir = QueueIsolated<String?>(nil)
6667
let commands = QueueIsolated<[Command]>([])
6768
let firstDirCloned = QueueIsolated(false)
6869
Current.fileManager.fileExists = { @Sendable path in
@@ -75,7 +76,6 @@ class AnalyzerTests: AppTestCase {
7576
if path.hasSuffix("Package.resolved") { return true }
7677
return false
7778
}
78-
Current.fileManager.createDirectory = { @Sendable path, _, _ in checkoutDir.setValue(path) }
7979
Current.git = .live
8080
Current.shell.run = { @Sendable cmd, path in
8181
let trimmedPath = path.replacingOccurrences(of: checkoutDir.value!, with: ".")
@@ -409,26 +409,26 @@ class AnalyzerTests: AppTestCase {
409409

410410
func test_continue_on_exception() async throws {
411411
// Test to ensure exceptions don't interrupt processing
412+
let checkoutDir: NIOLockedValueBox<String?> = .init(nil)
412413
try await withDependencies {
413414
$0.date.now = .now
414415
$0.environment.allowSocialPosts = { true }
415416
$0.environment.loadSPIManifest = { _ in nil }
417+
$0.fileManager.createDirectory = { @Sendable path, _, _ in checkoutDir.withLockedValue { $0 = path } }
416418
} operation: {
417419
// setup
418420
let urls = ["https://github.com/foo/1", "https://github.com/foo/2"]
419421
let pkgs = try await savePackages(on: app.db, urls.asURLs, processingStage: .ingestion)
420422
for p in pkgs {
421423
try await Repository(package: p, defaultBranch: "main").save(on: app.db)
422424
}
423-
let checkoutDir: NIOLockedValueBox<String?> = .init(nil)
424425

425426
Current.fileManager.fileExists = { @Sendable path in
426427
if let outDir = checkoutDir.withLockedValue({ $0 }), path == "\(outDir)/github.com-foo-1" { return true }
427428
if let outDir = checkoutDir.withLockedValue({ $0 }), path == "\(outDir)/github.com-foo-2" { return true }
428429
if path.hasSuffix("Package.swift") { return true }
429430
return false
430431
}
431-
Current.fileManager.createDirectory = { @Sendable path, _, _ in checkoutDir.withLockedValue { $0 = path } }
432432

433433
Current.git = .live
434434

Tests/AppTests/Mocks/AppFileManager+mock.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ extension App.FileManager {
2121
static let mock = Self.mock(fileExists: true)
2222
static func mock(fileExists: Bool) -> Self {
2323
.init(
24-
createDirectory: { _, _, _ in },
2524
fileExists: { path in fileExists },
2625
removeItem: { _ in },
2726
workingDirectory: { DirectoryConfiguration.detect().workingDirectory }

0 commit comments

Comments
 (0)