1+ import Foundation
2+ #if canImport(System)
3+ import System
4+ #else
5+ import SystemPackage
6+ #endif
17import XCTest
2- @testable import JSONLogger
38
49import Logging
510
11+ @testable import JSONLogger
12+
613
714
815final class JSONLoggerTests : XCTestCase {
@@ -27,11 +34,25 @@ final class JSONLoggerTests: XCTestCase {
2734 XCTAssertEqual ( " second " , logger2 [ metadataKey: " only-on " ] )
2835 }
2936
30- func testVisual1( ) {
31- XCTAssertTrue ( true , " We only want to see how the log look, so please see the logs. " )
32-
33- let logger = Logger ( label: " my logger " )
34- logger. info ( " First log message using JSONLogger " )
37+ /* Must be the first test. */
38+ func test0NoSeparatorForFirstLog( ) throws {
39+ /* We do not init the JSONLogger using Logger because we want to test multiple configurations
40+ * which is not possible using LoggingSystem as the bootstrap can only be done once. */
41+ let pipe = Pipe ( )
42+ let jsonLogger = JSONLogger ( label: " best-logger " , fd: FileDescriptor ( rawValue: pipe. fileHandleForWriting. fileDescriptor) )
43+ jsonLogger. log ( level: . info, message: " First log message " , metadata: nil , source: " dummy-source " , file: " dummy-file " , function: " dummy-function " , line: 42 )
44+ try pipe. fileHandleForWriting. close ( )
45+ let data = try pipe. fileHandleForReading. readToEnd ( ) ?? Data ( )
46+ XCTAssertEqual ( data. first, 0x7b )
47+ }
48+
49+ func testSeparatorForNotFirstLog( ) throws {
50+ let pipe = Pipe ( )
51+ let jsonLogger = JSONLogger ( label: " best-logger " , fd: FileDescriptor ( rawValue: pipe. fileHandleForWriting. fileDescriptor) )
52+ jsonLogger. log ( level: . info, message: " Not first log message " , metadata: nil , source: " dummy-source " , file: " dummy-file " , function: " dummy-function " , line: 42 )
53+ try pipe. fileHandleForWriting. close ( )
54+ let data = try pipe. fileHandleForReading. readToEnd ( ) ?? Data ( )
55+ XCTAssertEqual ( data. first, 0x0a )
3556 }
3657
3758}
0 commit comments