blob: 19f914ed0553c612795532e40613095df79df3de [file] [log] [blame] [edit]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package testing_test
import (
"context"
gotesting "testing"
"github.com/google/go-cmp/cmp"
"go.chromium.org/infra/cros/servo/logging"
"go.chromium.org/infra/cros/servo/logging/loggingtest"
"go.chromium.org/infra/cros/servo/testing"
)
func TestContextLogger(t *gotesting.T) {
ctx := context.Background()
if _, ok := testing.ContextLogger(ctx); ok {
t.Errorf("Expected logger to not be available from background context")
}
logger := loggingtest.NewLogger(t, logging.LevelInfo)
ctx = logging.AttachLogger(ctx, logger)
logger2, ok := testing.ContextLogger(ctx)
if !ok {
t.Errorf("Expected logger to be available")
}
const testLog = "foo"
logger2.Print(testLog)
logger2.VPrint("ignore me, I'm verbose")
if diff := cmp.Diff(logger.Logs(), []string{testLog}); diff != "" {
t.Errorf("Log mismatch (-got +want):\n%s", diff)
}
}
func TestContextLoggerVerbose(t *gotesting.T) {
ctx := context.Background()
if _, ok := testing.ContextLogger(ctx); ok {
t.Errorf("Expected logger to not be available from background context")
}
logger := loggingtest.NewLogger(t, logging.LevelDebug)
ctx = logging.AttachLogger(ctx, logger)
logger2, ok := testing.ContextLogger(ctx)
if !ok {
t.Errorf("Expected logger to be available")
}
logger2.Print("foo")
logger2.VPrint("bar")
if diff := cmp.Diff(logger.Logs(), []string{"foo", "bar"}); diff != "" {
t.Errorf("Log mismatch (-got +want):\n%s", diff)
}
}