Browse Source

Disable logging in tests for CI only (#549)

Motivation:

It's often useful to have logging output when running tests, however,
Travis will fail the job if we log too much.

Modifications:

Enable logging in tests only of the "CI" environment variable does not
equal "true".

Result:

Logging can be disabled by the environment.
George Barnett 6 years ago
parent
commit
b91c2b107f

+ 14 - 1
Tests/GRPCTests/GRPCTestCase.swift

@@ -20,12 +20,25 @@ import Logging
 ///
 /// This should be used instead of `XCTestCase`.
 class GRPCTestCase: XCTestCase {
+  // Travis will fail the CI if there is too much logging, but it can be useful when running
+  // locally; conditionally enable it based on the environment.
+  //
+  // https://docs.travis-ci.com/user/environment-variables/#default-environment-variables
+  private static var isLoggingEnabled = ProcessInfo.processInfo.environment["CI"] != "true"
+
   // `LoggingSystem.bootstrap` must be called once per process. This is the suggested approach to
   // workaround this for XCTestCase.
   //
   // See: https://github.com/apple/swift-log/issues/77
   private static let isLoggingConfigured: Bool = {
-    LoggingSystem.bootstrap { _ in BlackHole() }
+    LoggingSystem.bootstrap { label in
+      guard isLoggingEnabled else {
+        return BlackHole()
+      }
+      var handler = StreamLogHandler.standardOutput(label: label)
+      handler.logLevel = .debug
+      return handler
+    }
     return true
   }()
 

+ 10 - 5
Tests/GRPCTests/LengthPrefixedMessageReaderTests.swift

@@ -20,11 +20,16 @@ import NIO
 import Logging
 
 class LengthPrefixedMessageReaderTests: GRPCTestCase {
-  var reader = LengthPrefixedMessageReader(
-    mode: .client,
-    compressionMechanism: .none,
-    logger: Logger(label: "io.grpc.testing")
-  )
+  var reader: LengthPrefixedMessageReader!
+
+  override func setUp() {
+    super.setUp()
+    self.reader = LengthPrefixedMessageReader(
+      mode: .client,
+      compressionMechanism: .none,
+      logger: Logger(label: "io.grpc.testing")
+    )
+  }
 
   var allocator = ByteBufferAllocator()