diff --git a/pkg/logs/BUILD.bazel b/pkg/logs/BUILD.bazel index 04c0bd8c0..d7fd24952 100644 --- a/pkg/logs/BUILD.bazel +++ b/pkg/logs/BUILD.bazel @@ -26,7 +26,10 @@ filegroup( filegroup( name = "all-srcs", - srcs = [":package-srcs"], + srcs = [ + ":package-srcs", + "//pkg/logs/testing:all-srcs", + ], tags = ["automanaged"], visibility = ["//visibility:public"], ) diff --git a/pkg/logs/testing/BUILD.bazel b/pkg/logs/testing/BUILD.bazel new file mode 100644 index 000000000..273f6bbe8 --- /dev/null +++ b/pkg/logs/testing/BUILD.bazel @@ -0,0 +1,23 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["log_testing.go"], + importpath = "github.com/jetstack/cert-manager/pkg/logs/testing", + visibility = ["//visibility:public"], + deps = ["@com_github_go_logr_logr//:go_default_library"], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/pkg/logs/testing/log_testing.go b/pkg/logs/testing/log_testing.go new file mode 100644 index 000000000..05ecff996 --- /dev/null +++ b/pkg/logs/testing/log_testing.go @@ -0,0 +1,54 @@ +/* +Copyright 2020 The Jetstack cert-manager contributors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package testing + +import ( + "testing" + + "github.com/go-logr/logr" +) + +// TestLogger is a logr.Logger that prints through a testing.T object. +type TestLogger struct { + T *testing.T +} + +var _ logr.Logger = TestLogger{} + +func (_ TestLogger) Enabled() bool { + return true +} + +func (log TestLogger) Info(msg string, args ...interface{}) { + log.T.Logf("%s: %v", msg, args) +} + +func (log TestLogger) Error(err error, msg string, args ...interface{}) { + log.T.Logf("%s: %v -- %v", msg, err, args) +} + +func (log TestLogger) V(v int) logr.InfoLogger { + return log +} + +func (log TestLogger) WithName(_ string) logr.Logger { + return log +} + +func (log TestLogger) WithValues(_ ...interface{}) logr.Logger { + return log +}