Skip to content

Commit

Permalink
Fix logging tests so that the logged line number is deterministic des…
Browse files Browse the repository at this point in the history
…pite changes to the file.
  • Loading branch information
gwynne committed May 29, 2024
1 parent 4ef8237 commit 33544ad
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions Tests/ConsoleKitTests/LoggingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ final class ConsoleLoggerTests: XCTestCase {
ConsoleLogger(label: label, console: console, level: .debug)
}

logger.debug("debug")
XCTAssertLog(console, .debug, "debug (ConsoleKitTests/LoggingTests.swift:74)")
logger.debug("debug", line: 1)
XCTAssertLog(console, .debug, "debug (ConsoleKitTests/LoggingTests.swift:1)")
}

func testMetadataProviders() {
Expand All @@ -92,9 +92,9 @@ final class ConsoleLoggerTests: XCTestCase {
}

TraceNamespace.$simpleTraceID.withValue("1234-5678") {
logger.debug("debug")
logger.debug("debug", line: 1)
}
XCTAssertLog(console, .debug, "debug [simple-trace-id: 1234-5678] (ConsoleKitTests/LoggingTests.swift:92)")
XCTAssertLog(console, .debug, "debug [simple-trace-id: 1234-5678] (ConsoleKitTests/LoggingTests.swift:1)")
}

func testTimestampFragment() {
Expand Down Expand Up @@ -124,7 +124,7 @@ final class ConsoleLoggerTests: XCTestCase {
)
}

logger.info("logged")
logger.info("logged", line: 1)

var logged = console.testOutputQueue.first!
let expect = "2000-06-04T03:02:01"
Expand All @@ -134,7 +134,7 @@ final class ConsoleLoggerTests: XCTestCase {
// Remove the timezone, since there doesn't appear to be a good way to mock it with strftime.
while logged.removeFirst() != " " { }

XCTAssertEqual(logged, "[ \(Logger.Level.info.name) ] logged (ConsoleKitTests/LoggingTests.swift:124)\n")
XCTAssertEqual(logged, "[ \(Logger.Level.info.name) ] logged (ConsoleKitTests/LoggingTests.swift:1)\n")
}

func testSourceFragment() {
Expand All @@ -148,14 +148,14 @@ final class ConsoleLoggerTests: XCTestCase {
)
}

logger.info("logged")
logger.info("logged", line: 1)

XCTAssertEqual(console.testOutputQueue.first, "ConsoleKitTests [ \(Logger.Level.info.name) ] logged (ConsoleKitTests/LoggingTests.swift:148)\n")
XCTAssertEqual(console.testOutputQueue.first, "ConsoleKitTests [ \(Logger.Level.info.name) ] logged (ConsoleKitTests/LoggingTests.swift:1)\n")
}
}

private func XCTAssertLog(_ console: TestConsole, _ level: Logger.Level, _ message: String, file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(console.testOutputQueue.first ?? "", "[ \(level.name) ] \(message)\n", file: (file), line: line)
private func XCTAssertLog(_ console: TestConsole, _ level: Logger.Level, _ message: String, file: StaticString = #filePath, line: UInt = #line) {
XCTAssertEqual(console.testOutputQueue.first ?? "", "[ \(level.name) ] \(message)\n", file: file, line: line)
}

enum TraceNamespace {
Expand Down

0 comments on commit 33544ad

Please sign in to comment.