From 260883589c2ab522d617a8398755ebb395945a05 Mon Sep 17 00:00:00 2001 From: Philip Niedertscheider Date: Mon, 19 Oct 2026 10:32:16 +0200 Subject: [PATCH] fix(logging): Defer documentation logger creation --- Sources/CLI/main/Dependencies.swift | 13 ++++++----- .../AppleDocsCommandIntegrationTests.swift | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Sources/CLI/main/Dependencies.swift b/Sources/CLI/main/Dependencies.swift index 6da0cb1..5f90f29 100644 --- a/Sources/CLI/main/Dependencies.swift +++ b/Sources/CLI/main/Dependencies.swift @@ -18,7 +18,14 @@ struct Dependencies: Sendable { ) let httpCache: URLCache? let httpDataTransport: URLSession - let documentationRepository: DefaultAppleDocumentationRepository> + + // Commands resolve the repository after logging is bootstrapped. + var documentationRepository: DefaultAppleDocumentationRepository> { + DefaultAppleDocumentationRepository( + logger: Logger(label: "com.techprimate.apple-docs.repository"), + dependencies: DefaultAppleDocumentationClient( + logger: Logger(label: "com.techprimate.apple-docs.client"), dependencies: httpDataTransport)) + } init() { if let cachesDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first { @@ -39,10 +46,6 @@ struct Dependencies: Sendable { configuration.urlCache = httpCache } httpDataTransport = URLSession(configuration: configuration) - documentationRepository = DefaultAppleDocumentationRepository( - logger: Logger(label: "com.techprimate.apple-docs.repository"), - dependencies: DefaultAppleDocumentationClient( - logger: Logger(label: "com.techprimate.apple-docs.client"), dependencies: httpDataTransport)) } var documentationCache: URLCache? { diff --git a/Tests/CLIIntegrationTests/AppleDocsCommandIntegrationTests.swift b/Tests/CLIIntegrationTests/AppleDocsCommandIntegrationTests.swift index 2ea9510..e2581eb 100644 --- a/Tests/CLIIntegrationTests/AppleDocsCommandIntegrationTests.swift +++ b/Tests/CLIIntegrationTests/AppleDocsCommandIntegrationTests.swift @@ -46,6 +46,28 @@ struct AppleDocsCommandIntegrationTests { #expect(!output.contains("\u{1B}")) } + @Test("startup routes documentation logs only to verbose stderr", arguments: [false, true]) + func routesDocumentationLogs(verbose: Bool) throws { + // -- Arrange -- + var arguments = ["types", "view", "environmentvalues/linelimit", "--technology", "SwiftUI", "--agent"] + if verbose { arguments.append("--verbose") } + var diagnostics = "" + + // -- Act -- + let output = try runAppleDocs(arguments, captureStandardError: { diagnostics = $0 }) + + // -- Assert -- + #expect(output.hasPrefix("# lineLimit\n")) + #expect(!output.contains("Fetched type documentation")) + #expect(!output.contains("Requesting documentation data")) + if verbose { + #expect(diagnostics.contains("Fetched type documentation")) + #expect(diagnostics.contains("Requesting documentation data")) + } else { + #expect(diagnostics.isEmpty) + } + } + @Test("agent JSON includes navigation without verbose diagnostics on stdout") func rendersAgentJSON() throws { // -- Arrange --