From dadebfa8a3936994d9bd5a2b27c84a5443188862 Mon Sep 17 00:00:00 2001 From: Mark Watts Date: Fri, 21 Aug 2026 15:34:21 +0100 Subject: [PATCH] Route test-run diagnostics to a temp log, not the user's real one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test suites exercise the same code paths as the app (store inits, integrity verdicts, rule supersession), and their DiagnosticsLog lines landed in the real ~/Library/Logs/Argus/argus.log — indistinguishable from app activity, since log lines carry only basenames like rules-state.json. Under XCTest (env var or loaded XCTestCase class) the log now goes to a per-process file under the temp directory; the write path still runs for real, and the user's log stays an honest record of the app alone. Verified: a full test run adds zero lines to the real log and the temp file receives them. Co-Authored-By: Claude Fable 5 --- Sources/Argus/DiagnosticsLog.swift | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Sources/Argus/DiagnosticsLog.swift b/Sources/Argus/DiagnosticsLog.swift index 2495f40..a285e02 100644 --- a/Sources/Argus/DiagnosticsLog.swift +++ b/Sources/Argus/DiagnosticsLog.swift @@ -3,7 +3,28 @@ import Foundation /// Minimal on-disk activity log, independent of the UI, so posture can be /// inspected (`tail -f`) without the window ever being on screen. enum DiagnosticsLog { + /// True when this process is an XCTest run rather than the real app. + /// Both signals are checked: the XCTest environment variable covers + /// `swift test`/xcodebuild runs, and the class lookup covers any harness + /// that loads XCTest without setting it. + private static let isRunningUnderXCTest = + ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil + || NSClassFromString("XCTestCase") != nil + static let url: URL = { + // Test runs exercise the same code paths as the app (store inits, + // integrity verdicts, rule supersession) and used to append their + // noise to the real log — and since log lines only carry basenames + // like "rules-state.json", test lines were indistinguishable from + // real app activity when tailing it. Route tests to a per-process + // temp file instead: the write path still runs for real, but the + // user's log stays an honest record of the app alone. + if isRunningUnderXCTest { + let dir = FileManager.default.temporaryDirectory + .appendingPathComponent("argus-test-logs", isDirectory: true) + try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true) + return dir.appendingPathComponent("argus-\(ProcessInfo.processInfo.processIdentifier).log") + } let dir = FileManager.default.homeDirectoryForCurrentUser .appendingPathComponent("Library/Logs/Argus", isDirectory: true) try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)